package com.chaozhanggui.admin.system.service;

import com.chaozhanggui.admin.system.model.CommonError;
import com.chaozhanggui.common.system.config.MsgException;
import com.chaozhanggui.dao.system.dao.TbPlussIdCardMapper;
import com.chaozhanggui.dao.system.dao.TbPlussMerchantBaseInfoMapper;
import com.chaozhanggui.dao.system.dao.TbPlussMerchantChannelStatusMapper;
import com.chaozhanggui.dao.system.dao.TbPlussMerchantImageMapper;
import com.chaozhanggui.dao.system.entity.*;
import com.chaozhanggui.dao.system.entity.DTO.AccountDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * @author lyf
 */
@Service
@Slf4j
public class MerchantImageService {
    @Resource
    private TbPlussMerchantBaseInfoMapper merchantBaseInfoMapper;
    @Resource
    private TbPlussMerchantImageMapper merchantImageMapper;
    @Resource
    private AccountService accountService;
    @Resource
    private TbPlussIdCardMapper idCardMapper;

    /**
     * 根据用户id获取进件图片信息, 包含结算信息等.
     * 鉴于某些原因，结算信息相关的图片不再实时保存到当前表中.
     * 此方法就是用于获取进件需要的所有图片信息
     *
     * @param userId      用户id
     * @param channelType 通道类型
     * @return 进件图片资料
     */
    List<TbPlussMerchantImage> getListOfMerch(String userId, String channelType){
        TbPlussMerchantBaseInfo merchantBaseInfo = merchantBaseInfoMapper.selectByUserId(userId);
        //查询图片
        List<TbPlussMerchantImage> miList = merchantImageMapper.getListOfMerch(merchantBaseInfo.getMerchantcode());

        AccountDTO accountDTO = accountService.getRealAccount(userId, channelType);


        MsgException.checkNull(accountDTO.getIdcard(), CommonError.NO_ACCOUNT_ID_CARD);
        MsgException.checkBlank(accountDTO.getIdcard().getImgpositive(), CommonError.NO_PART_ACCOUNT_ID_CARD_HEAD);
        MsgException.checkBlank(accountDTO.getIdcard().getImgnegative(), CommonError.NO_PART_ACCOUNT_ID_CARD_EMBLEM);

        MsgException.checkNull(accountDTO.getBankCard(), CommonError.NO_ACCOUNT_BANK_CARD);
        if (accountDTO.getBankCard().getAccounttype().equals(TbPlussBankCard.ACCOUNT_TYPE_CORPORATE)) {
            // 对公
            MsgException.checkBlank(accountDTO.getBankCard().getLicenseurl(), CommonError.NO_ACCOUNT_BANK_CARD_LICENSE);
        } else {
            // 对私
            MsgException.checkBlank(accountDTO.getBankCard().getImgurl(), CommonError.NO_ACCOUNT_BANK_CARD_IMG);
        }

        if (accountDTO.getSettletype().startsWith(TbPlussAccount.SETTLE_TYPE_PRIVATE_ILLEGAL)) {
            // 非法人结算需要非法人授权函
            MsgException.checkBlank(accountDTO.getCertificateurl(), CommonError.NO_ILLEGAL_ACCOUNT_CERTIFICATE_URL);

            TbPlussMerchantImage mi = new TbPlussMerchantImage();
            mi.setPicurl(accountDTO.getCertificateurl());
            mi.setPhotoType(TbPlussMerchantImage.NON_LEG_SETTLE_AUTH);
            miList.add(mi);
        }

        TbPlussMerchantImage mi = new TbPlussMerchantImage();
        mi.setPicurl(accountDTO.getIdcard().getImgpositive());
        mi.setPhotoType(TbPlussMerchantImage.SETTLE_IDCARD_FRONT);
        miList.add(mi);

        mi = new TbPlussMerchantImage();
        mi.setPicurl(accountDTO.getIdcard().getImgnegative());
        mi.setPhotoType(TbPlussMerchantImage.SETTLE_IDCARD_BACK);
        miList.add(mi);

        mi = new TbPlussMerchantImage();
        if (accountDTO.getBankCard().getAccounttype().equals(TbPlussBankCard.ACCOUNT_TYPE_CORPORATE)) {
            mi.setPicurl(accountDTO.getBankCard().getLicenseurl());
            mi.setPhotoType(TbPlussMerchantImage.ACCOUNT_PERMITS);
        } else {
            mi.setPicurl(accountDTO.getBankCard().getImgurl());
            mi.setPhotoType(TbPlussMerchantImage.BANK_CARD_FRONT);
        }
        miList.add(mi);

        TbPlussIdCard legalIdCard = idCardMapper.getLegalIdCard(userId);
        if (legalIdCard == null) {
            legalIdCard = accountDTO.getIdcard();
        }
        mi = new TbPlussMerchantImage();
        mi.setPicurl(legalIdCard.getImgpositive());
        mi.setPhotoType(TbPlussMerchantImage.IDCARD_FRONT);
        miList.add(mi);

        mi = new TbPlussMerchantImage();
        mi.setPicurl(legalIdCard.getImgnegative());
        mi.setPhotoType(TbPlussMerchantImage.IDCARD_BACK);
        miList.add(mi);

        return miList;
    }
}
