package com.chaozhanggui.admin.system.service;

import cn.hutool.core.util.ObjectUtil;
import com.chaozhanggui.common.system.config.MsgException;
import com.chaozhanggui.common.system.config.RespBody;
import com.chaozhanggui.dao.system.dao.TbPlussMerchantChannelStatusMapper;
import com.chaozhanggui.dao.system.entity.TbPlussMerchantChannelStatus;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author lyf
 */
@Service
@Slf4j
public class MerchantChannelStatusService {
//    public Page<UserInfoVO> queryUserInfoVOPage(Map<String, Object> map) {
//        List<UserInfoVO> result = baseMapper.queryUserInfoVOPage(map);
//
//        long page = ((Integer) map.get("offset")).longValue() + 1L;
//        long pageSize = ((Integer) map.get("pageSize")).longValue();
//        Page<UserInfoVO> pageResult = new Page<>(page, pageSize);
//        pageResult.setRecords(result);
//        Long count = baseMapper.queryUserPageCount(map).longValue();
//        pageResult.setTotal(count);
//
//        if (result.isEmpty()) {
//            return pageResult;
//        }
//        List<String> merchantCodeList = new ArrayList<>();
//        List<Integer> userIdList = new ArrayList<>();
//        for (UserInfoVO userInfoVO : result) {
//            merchantCodeList.add(userInfoVO.getMerchantCode() + "");
//            userIdList.add(Integer.valueOf(userInfoVO.getUserId() + ""));
//        }
//
//        List<MerchantChannelStatusVO> channelStatusList = mcsService.listByMerchantCodeList(merchantCodeList);
//        Map<String, Integer> mapMerchantId = new HashMap<>();
//        for (UserInfoVO userInfoVO : result) {
//            List<MerchantChannelStatus> statusList = userInfoVO.getChannelStatusList();
//            if (statusList == null) {
//                statusList = new ArrayList<>();
//                userInfoVO.setChannelStatusList(statusList);
//            }
//
//            Iterator<MerchantChannelStatusVO> iterator = channelStatusList.iterator();
//            while (iterator.hasNext()) {
//                MerchantChannelStatusVO item = iterator.next();
//                if (item.getMerchantCode().equals(userInfoVO.getMerchantCode())) {
//                    if (!mapMerchantId.containsKey(item.getChannel() + item.getMerchantId() + item.getMerchantCode())) {
//                        statusList.add(item);
//                        mapMerchantId.put(item.getChannel() + item.getMerchantId() + item.getMerchantCode(), 1);
//                    }
//                    if (Objects.equals(4, item.getChannel())) {
//                        if (userInfoVO.getWxMercNoD0() == null) {
//                            userInfoVO.setWxMercNoD0(item.getWxMercNo());
//                        } else {
//                            userInfoVO.setWxMercNoD0(userInfoVO.getWxMercNoD0() + " / " + item.getWxMercNo());
//                        }
//
//                        if (userInfoVO.getZfbMercNoD0() == null) {
//                            userInfoVO.setZfbMercNoD0(item.getZfbMercNo());
//                        } else {
//                            userInfoVO.setZfbMercNoD0(userInfoVO.getZfbMercNoD0() + " / " + item.getWxMercNo());
//                        }
//                    } else {
//                        userInfoVO.setWxMercNoD1(item.getWxMercNo());
//                        userInfoVO.setZfbMercNoD1(item.getZfbMercNo());
//                    }
//
//                    iterator.remove();
//                }
//            }
//        }
//
//        return pageResult;
//    }
//    public TbPlussMerchantChannelStatus getByMerchantCodeAndChannelType(String merchantCode, String channelType) {
//        LambdaQueryWrapper<TbPlussMerchantChannelStatus> qWrapper = Wrappers.lambdaQuery();
//        qWrapper.eq(TbPlussMerchantChannelStatus::getVirchannelflag, channelType);
//        qWrapper.eq(TbPlussMerchantChannelStatus::getMerchantcode, merchantCode);
//        qWrapper.eq(TbPlussMerchantChannelStatus::getAuthorizationstatus,1);
//
//        return getOne(qWrapper);
//    }
    @Resource
    private TbPlussMerchantChannelStatusMapper merchantChannelStatusMapper;
    @Resource
    private SxfMerchantAuditService sxfMerchantAuditService;

    /**
     * 后台进件
     * @param userId
     * @param channelId
     * @param merchantCode
     * @return
     */
    public RespBody manualMerchantIncome(Integer userId, String channelId, String merchantCode){
        //检查进件通道
        if (ObjectUtil.isEmpty(channelId)){
            return new RespBody("000080");
        }
        TbPlussMerchantChannelStatus merchantChannelStatus = merchantChannelStatusMapper.selectByPrimaryKey(Integer.valueOf(channelId));
        if (ObjectUtil.isEmpty(merchantChannelStatus)){
            return new RespBody("000081");
        }
        //通道互不干扰
        if ("1".equals(channelId)) {
            TbPlussMerchantChannelStatus sxfChannel = merchantChannelStatusMapper.selectByMerchantCode(merchantCode, 3);
            if (sxfChannel != null || Objects.equals(sxfChannel.getChannel(),3)){
                return new RespBody("000082");
            }
        }
        if ("3".equals(channelId)) {
            TbPlussMerchantChannelStatus ryxChannel = merchantChannelStatusMapper.selectByMerchantCode(merchantCode, 1);
            if (ryxChannel != null && Objects.equals(ryxChannel.getChannel(), 1)) {
                return new RespBody("000083");
            }
        }
        //先查询该通道下是否有记录
        TbPlussMerchantChannelStatus mcs = merchantChannelStatusMapper.selectByMerchantCode(merchantCode, Integer.valueOf(channelId));
        if (mcs == null) {
            return new RespBody("000084");
        } else {
            if (mcs.getChannel() != null) {
                if (Integer.parseInt(channelId) == mcs.getChannel()
                        && TbPlussMerchantChannelStatus.isAudited(mcs)) {
                    return new RespBody("000085");
                }
            }
        }
        TbPlussMerchantChannelStatus.checkCanAudit(mcs);
        //选择通道
        switch (channelId) {
            case "1":
                // 随行付
                sxfMerchantAuditService.merchantAudit(String.valueOf(userId), false);
                break;
//            case "2":
//                // 乐刷，暂不支持
//                leshuaMerchantAuditService.merchantAudit(userId, false);
//                break;
//            case "3":
//                // 瑞银信
//                ryxMerchantAuditService.merchantAudit(userId, false);
//                break;
//            case "4":
//                // 银盛
//                ysAuditServiceV3.merchantAuditV3(userId, false, 4);
//                break;
//            case "5":
//                laKalaInterface.createTuoKeInfo(userId);
//                break;
//            case "6":
//                ysAuditServiceV3.merchantAuditV3(userId, false, 6);
//                break;
//            default:
//                MsgException.throwException("未知的进件通道");
        }
        return new RespBody("000000");
    }

    public TbPlussMerchantChannelStatus getValidData(String merchantCode){
        if (StringUtils.isEmpty(merchantCode)) {
            return null;
        }
        TbPlussMerchantChannelStatus mcs = merchantChannelStatusMapper.getIncomingInfo(TbPlussMerchantChannelStatus.AUDIT_STATUS_SUCCESS,
                                                    TbPlussMerchantChannelStatus.AUDIT_STATUS_DATA_EDIT, merchantCode);
        if (mcs != null) {
            return mcs;
        }

        return merchantChannelStatusMapper.getIncomingInfoAnother(merchantCode);
    }
}
