package com.chaozhanggui.admin.system.service;

import com.alipay.api.domain.AccountDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.chaozhanggui.common.system.config.RespBody;
import com.chaozhanggui.dao.system.dao.*;
import com.chaozhanggui.dao.system.entity.*;
import com.chaozhanggui.dao.system.entity.VO.AccountVO;
import com.chaozhanggui.dao.system.entity.VO.MerchantBaseVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

import static com.chaozhanggui.dao.system.entity.TbPlussAccount.CHANNEL_TYPE_D0;
import static com.chaozhanggui.dao.system.entity.TbPlussAccount.CHANNEL_TYPE_D1;
import static com.chaozhanggui.dao.system.entity.TbPlussBankCard.ACCOUNT_TYPE_CERT;
import static com.chaozhanggui.dao.system.entity.TbPlussIdCard.TYPE_CERT;

/**
 * @author lyf
 */
@Service
public class MerchantInfoService {
    @Resource
    private TbPlussIdCardMapper idCardMapper;
    @Resource
    private TbPlussBankCardMapper bankCardMapper;
    @Resource
    private TbPlussMerchantBaseInfoMapper merchantBaseInfoMapper;
    @Resource
    private TbPlussMerchantImageMapper merchantImageMapper;
    @Resource
    private TbPlussAccountMapper accountMapper;
    @Resource
    private TbPlussMerchantChannelMapper merchantChannelMapper;
    @Resource
    private TbPlussUserInfoMapper userInfoMapper;
    @Resource
    private TbPlussUserAppMapper userAppMapper;


    public RespBody merchantReal(Integer userId){
        HashMap<String, Object> map = new HashMap<>();
        //身份证
        TbPlussIdCard idCard = idCardMapper.selectCertByUserId(userId,TYPE_CERT);
        //银行卡
        TbPlussBankCard bankCard = bankCardMapper.selectByUserIdCert(userId, ACCOUNT_TYPE_CERT);
        map.put("idCard",idCard);
        map.put("bankCard",bankCard);
        return new RespBody("000000",map);
    }

    public RespBody merchantBaseInfo(Integer userId){
        MerchantBaseVO merchantBaseVO = new MerchantBaseVO();
        //商户基础信息相关
        TbPlussMerchantBaseInfo merchantBaseInfo = merchantBaseInfoMapper.selectByUserId(userId.toString());
        if (merchantBaseInfo == null){
            return new RespBody("000000",merchantBaseVO);
        }
        merchantBaseVO.setMerchantBaseInfo(merchantBaseInfo);
        //图片
        List<TbPlussMerchantImage> merchantImages = merchantImageMapper.selectByMerchantCode(merchantBaseInfo.getMerchantcode());
        if (merchantImages == null){
            return new RespBody("000000",merchantBaseVO);
        }
        merchantBaseVO.setMerchantImagesList(merchantImages);

        return new RespBody("000000",merchantBaseVO);
    }

    public RespBody merchantAccount(Integer userId){
        HashMap<String, Object> realAccount = getRealAccount(userId);
        return new RespBody("000000",realAccount);
    }

    public HashMap<String,Object> getRealAccount(Integer userId) {
        HashMap<String, Object> map = new HashMap<>();
        //D1
        TbPlussAccount accountD1 = accountMapper.selectByUser(userId, CHANNEL_TYPE_D1);
        if (accountD1 == null){
            map.put("D1",new AccountVO());
        }else {
            AccountVO accountVO = new AccountVO();
            // 获取身份证信息
            TbPlussIdCard idCard = idCardMapper.selectByPrimaryKey(Integer.valueOf(accountD1.getIdcardid()));
            accountVO.setIdCard(idCard);
            // 获取银行卡信息
            TbPlussBankCard bankCard = bankCardMapper.selectByPrimaryKey(Integer.valueOf(accountD1.getBankcardid()));
            accountVO.setBankCard(bankCard);
            map.put("D1",accountVO);
        }
        //D0
        TbPlussAccount accountD0 = accountMapper.selectByUser(userId, CHANNEL_TYPE_D0);
        if (accountD0 == null){
            map.put("D0",new AccountVO());
        }else {
            AccountVO accountVO = new AccountVO();
            // 获取身份证信息
            TbPlussIdCard idCard = idCardMapper.selectByPrimaryKey(Integer.valueOf(accountD0.getIdcardid()));
            accountVO.setIdCard(idCard);
            // 获取银行卡信息
            TbPlussBankCard bankCard = bankCardMapper.selectByPrimaryKey(Integer.valueOf(accountD0.getBankcardid()));
            accountVO.setBankCard(bankCard);
            map.put("D0",accountVO);
        }
        return map;
    }

//    public RespBody updatePromoterInformation(TbPlussUserApp userApp){
//        //修改推广员名称和登录账号
//        TbPlussUserApp appUser = new TbPlussUserApp();
//        TbPlussUserInfo userInfo = new TbPlussUserInfo();
//        userInfo.setLoginname(userApp.getLoginname());
//        userInfo.setPhone(userApp.getLoginname());
//        userInfo.setTruename(userApp.getUsername());
//        appUser.setLoginname(userApp.getLoginname());
//        appUser.setUsername(userApp.getUsername());
//        userAppMapper.updateByPrimaryKeySelective(appUser);
//        userInfoMapper.updateByPrimaryKeySelective(userInfo);
//
//        if (Objects.equals(userApp.getStatus(), 3)) {
//            //判断实名人与结算人是否为同一人
//            AccountDTO accountDTO = this.getRealAccount(userApp.getBankCard().getUserid());
//            if (accountDTO == null || accountDTO.getIdcard() == null || !userApp.getIdCard().getCertNo().equals(accountDTO.getIdcard().getCertNo())) {
//                bankCardUpdateWrapper.eq("accountType", BankCard.ACCOUNT_TYPE_CERT);
//            }
//            if ("".equals(userApp.getBankCard().getImgUrl())) {
//                userApp.getBankCard().setImgUrl(null);
//            }
//            QueryWrapper<AreaCity> areaCityQueryWrapper = new QueryWrapper<AreaCity>().
//                    in("areaCode", userApp.getBankCard().getBranchProvince(),
//                            userApp.getBankCard().getBranchCity(), userApp.getBankCard().getBranchArea());
//            List<AreaCity> areaCityList = areaCityMapper.selectList(areaCityQueryWrapper);
//            for (AreaCity areaCity : areaCityList) {
//                if ("1".equals(areaCity.getType())) {
//                    userApp.getBankCard().setBranchProvince(areaCity.getAreaName());
//                } else if ("2".equals(areaCity.getType())) {
//                    userApp.getBankCard().setBranchCity(areaCity.getAreaName());
//                } else if ("3".equals(areaCity.getType()) || "4".equals(areaCity.getType())) {
//                    userApp.getBankCard().setBranchArea(areaCity.getAreaName());
//                }
//            }
//            LambdaQueryWrapper<BankCodeSxf> sxfQueryWrapper = new LambdaQueryWrapper<BankCodeSxf>()
//                    .eq(StringUtils.isNotEmpty(userApp.getBankCard().getContactLine()), BankCodeSxf::getCnapsCode, userApp.getBankCard().getContactLine())
//                    .eq(StringUtils.isNotEmpty(userApp.getBankCard().getBranchName()), BankCodeSxf::getCnapsName, userApp.getBankCard().getBranchName())
//                    .last("limit 1");
//            BankCodeSxf bankCodeSxf = bankCodeSxfMapper.selectOne(sxfQueryWrapper);
//            if (bankCodeSxf == null) {
//                throw new MsgException("未找到支行信息");
//            }
//            userApp.getBankCard().setContactLine(bankCodeSxf.getCnapsCode());
//            bankCardService.update(userApp.getBankCard(), bankCardUpdateWrapper);
//            UpdateWrapper<IdCard> idCardUpdateWrapper = new UpdateWrapper<IdCard>().eq("userId", userApp.getUserId()).eq("userType", IdCard.TYPE_CERT);
//            if ("".equals(userApp.getIdCard().getImgPositive())) {
//                userApp.getIdCard().setImgPositive(null);
//            }
//            if ("".equals(userApp.getIdCard().getImgNegative())) {
//                userApp.getIdCard().setImgNegative(null);
//            }
//            idCardService.update(userApp.getIdCard(), idCardUpdateWrapper);
//        }
//
//    }
}
