
package com.tmerclub.cloud.supplier.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.auth.feign.AccountFeignClient;
import com.tmerclub.cloud.api.auth.vo.AuthAccountVO;
import com.tmerclub.cloud.api.payment.allinpay.member.resp.BindCard;
import com.tmerclub.cloud.api.payment.allinpay.member.resp.SetCompanyInfoResp;
import com.tmerclub.cloud.api.payment.dto.SetCompanyInfoDTO;
import com.tmerclub.cloud.api.payment.feign.AllinpayFeignClient;
import com.tmerclub.cloud.api.supplier.constant.SupplierStatus;
import com.tmerclub.cloud.api.supplier.vo.SupplierApiDetailVO;
import com.tmerclub.cloud.api.supplier.vo.SupplierBankCardVO;
import com.tmerclub.cloud.common.constant.*;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.supplier.constant.AuditStatus;
import com.tmerclub.cloud.supplier.constant.IsDefault;
import com.tmerclub.cloud.supplier.constant.SupplierWithdrawCashStatus;
import com.tmerclub.cloud.supplier.dto.AllinpaySupplierBankCardDTO;
import com.tmerclub.cloud.supplier.dto.SupplierAuditingDTO;
import com.tmerclub.cloud.supplier.dto.SupplierBankCardDTO;
import com.tmerclub.cloud.supplier.dto.SupplierCompanyAuditingDTO;
import com.tmerclub.cloud.supplier.mapper.SupplierBankCardMapper;
import com.tmerclub.cloud.supplier.mapper.SupplierCompanyMapper;
import com.tmerclub.cloud.supplier.mapper.SupplierDetailMapper;
import com.tmerclub.cloud.supplier.mapper.SupplierWithdrawCashMapper;
import com.tmerclub.cloud.supplier.model.SupplierAuditing;
import com.tmerclub.cloud.supplier.model.SupplierBankCard;
import com.tmerclub.cloud.supplier.service.*;
import com.tmerclub.cloud.supplier.vo.SupplierAuditingVO;
import com.tmerclub.cloud.supplier.vo.SupplierCompanyAuditingVO;
import com.tmerclub.cloud.supplier.vo.SupplierCompanyVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 银行卡信息
 *
 * @author lhd
 * @date 2022-03-14 17:04:32
 */
@Service
public class SupplierBankCardServiceImpl implements SupplierBankCardService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SupplierBankCardServiceImpl.class);

    @Autowired
    private SupplierBankCardMapper supplierBankCardMapper;

    @Autowired
    private SupplierWithdrawCashMapper supplierWithdrawCashMapper;

    @Autowired
    private SupplierApplyService supplierApplyService;


    @DubboReference
    private AccountFeignClient accountFeignClient;

    @DubboReference
    private AllinpayFeignClient allinpayFeignClient;

    @Autowired
    private SupplierDetailMapper supplierDetailMapper;

    @Autowired
    private SupplierUserService supplierUserService;

    @Autowired
    private SupplierCompanyMapper supplierCompanyMapper;

    @Autowired
    @Lazy
    private SupplierCompanyAuditingService supplierCompanyAuditingService;
    @Autowired
    @Lazy
    private SupplierDetailServiceImpl supplierDetailService;
    @Autowired
    private SupplierAuditingService supplierAuditingService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<SupplierBankCardDTO> supplierBankCardDTOList, Long supplierId) {
        if (CollUtil.isEmpty(supplierBankCardDTOList)) {
            return;
        }
        List<SupplierBankCardVO> supplierBankCardVos = supplierBankCardMapper.listBySupplierId(supplierId);
        if (CollUtil.isEmpty(supplierBankCardVos)) {
            supplierBankCardVos = new ArrayList<>(0);
        }
        // 检查银行卡信息
        this.checkSaveBankCardList(supplierBankCardVos, supplierBankCardDTOList, supplierId, false);
        supplierBankCardMapper.insertBatch(supplierBankCardDTOList, supplierId);
    }

    @Override
    public List<SupplierBankCardVO> listBySupplierId(Long supplierId) {
        return supplierBankCardMapper.listBySupplierId(supplierId);
    }

    @Override
    public void deleteBySupplierBankCardId(Long supplierBankCardId, Long supplierId) {
        SupplierBankCardVO supplierBankCard = supplierBankCardMapper.getBySupplierBankCardId(supplierBankCardId);
        if (Objects.equals(supplierBankCard.getIsDefault(), 1)) {
            // 不能删除默认银行卡
            throw new LuckException("不能删除默认银行卡");
        }
        // 检查是否为最后一张银行卡
        if (supplierBankCardMapper.countBySupplierId(supplierId) == 1) {
            throw new LuckException("至少保留一张银行卡");
        }
        // 检查是否属于当前供应商
        this.checkIsCurrentSupplier(supplierId, supplierBankCardId);
        // 检查是否处于提现状态
        this.checkIsWithdraw(supplierBankCardId);
        // 进行删除
        supplierBankCardMapper.deleteById(supplierBankCardId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBatchAndSubmitApply(List<SupplierBankCardDTO> supplierBankCardDTOList, Long supplierId) {
        if (Objects.equals(AuthUserContext.get().getIsPassShop(), IsPassShopEnum.YES.value())) {
            // 供应商已入驻不能调用该方法
            throw new LuckException("接口调用错误");
        }
        if (supplierBankCardDTOList == null || supplierBankCardDTOList.size() == 0) {
            throw new LuckException("至少要添加一个银行卡");
        }
        // 先删除已保存的银行卡信息
        supplierBankCardMapper.deleteBySupplierId(AuthUserContext.get().getTenantId());
        // 检查银行卡信息
        this.checkSaveBankCardList(new ArrayList<>(Constant.INITIAL_CAPACITY), supplierBankCardDTOList, supplierId, false);
        // 保存银行卡信息
        supplierBankCardMapper.insertBatch(supplierBankCardDTOList, supplierId);
        // 提交开店申请
        supplierApplyService.apply(supplierId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertAndSetCompanyInfo(AllinpaySupplierBankCardDTO allinpaySupplierBankCardDTO, Long supplierId, SupplierCompanyVO supplierCompanyVO) {
        // 保存数据到数据库
        supplierBankCardMapper.deleteBySupplierId(supplierId);
        SupplierBankCard supplierBankCard = new SupplierBankCard();
        supplierBankCard.setSupplierId(supplierId);
        supplierBankCard.setBankName(allinpaySupplierBankCardDTO.getParentBankName());
        supplierBankCard.setOpeningBank(allinpaySupplierBankCardDTO.getParentBankName());
        supplierBankCard.setBranchName(allinpaySupplierBankCardDTO.getBankName());
        supplierBankCard.setRecipientName(supplierCompanyVO.getFirmName());
        supplierBankCard.setCardNo(allinpaySupplierBankCardDTO.getAccountNo());
        supplierBankCard.setStatus(StatusEnum.ENABLE.value());
        supplierBankCard.setUnionBank(allinpaySupplierBankCardDTO.getUnionBank());
        supplierBankCard.setBankCardNo(allinpaySupplierBankCardDTO.getAccountNo());
        supplierBankCard.setBankCardPro(1);
        supplierBankCardMapper.save(supplierBankCard);
        SupplierApiDetailVO supplierDetail = supplierDetailMapper.getBySupplierId(supplierId);
        if (Objects.equals(supplierDetail.getSupplierStatus(), SupplierStatus.APPLYING.value())) {
            LOGGER.info("供应商{}提交开店申请", supplierId + "supplierName：" + supplierDetail.getSupplierName());
            // 提交开店申请
            supplierApplyService.apply(supplierId);
        }

        allinpaySetCompanyInfo(allinpaySupplierBankCardDTO, supplierId, supplierCompanyVO, supplierDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void allinpaySetCompanyInfo(AllinpaySupplierBankCardDTO allinpaySupplierBankCardDTO, Long supplierId, SupplierCompanyVO supplierCompanyVO, SupplierApiDetailVO supplierDetail) {
        SetCompanyInfoDTO setCompanyInfoDTO = BeanUtil.map(allinpaySupplierBankCardDTO, SetCompanyInfoDTO.class);
        setCompanyInfoDTO.setCompanyName(supplierCompanyVO.getFirmName());
        setCompanyInfoDTO.setCompanyAddress(supplierCompanyVO.getResidence());
        setCompanyInfoDTO.setUniCredit(supplierCompanyVO.getCreditCode());
        setCompanyInfoDTO.setLegalName(supplierCompanyVO.getRepresentative());
        setCompanyInfoDTO.setLegalIds(supplierCompanyVO.getLegalIds());
        setCompanyInfoDTO.setLegalPhone(supplierCompanyVO.getLegalPhone());
        setCompanyInfoDTO.setBizUserId(AllinpayConstant.SUPPLIER + supplierId);
        // 设置完企业信息——供应商变为上线待审核(已开店)/开店待审核(未开店)——回调假如成功更改为营业，失败则为审核失败，重新填写，平台开店也一样
        ServerResponseEntity<SetCompanyInfoResp> response = allinpayFeignClient.setCompanyInfo(setCompanyInfoDTO);
        LOGGER.info("供应商{}设置企业信息结果：{}", supplierId + "supplierName：" + supplierDetail.getSupplierName(), response);
        SupplierCompanyAuditingVO supplierCompanyAuditingVO = supplierCompanyAuditingService.getLatestAuditingBySupplierId(supplierId);
        ServerResponseEntity<AuthAccountVO> accountInfoByTenantId = accountFeignClient.getAccountInfoByTenantId(supplierId, SysTypeEnum.SUPPLIER.value());
        String failReason = response.getMsg();
        if (!response.isSuccess()) {
            if (Objects.equals(failReason, AllinpayConstant.SET_COMPANY_INFO_RESULT)) {
                LOGGER.info("当前供应商在通联已审核通过，无法设置企业信息。更新供应商状态，上传影印件，等待影印件通过审核。");
                // 审核成功 更新供应商企业信息审核状态为审核通过，待影印件通过后即可将通联供应商状态更改为营业中,并且把工商信息修改为成功
                updateSupplierStatusToWaitAudit(supplierId, supplierDetail, supplierCompanyAuditingVO);
                supplierCompanyAuditingService.updateAllinpaySupplierStatus(AllinpayConstant.SUPPLIER + supplierId, com.tmerclub.cloud.api.payment.constant.AuditStatus.SUCCESS.getCode(),
                        "", accountInfoByTenantId.getData().getIsPassShop());
                return;
            }
            // 更新审核失败
            if (Objects.nonNull(supplierCompanyAuditingVO)) {
                // 如果是已经开店来审核的话要把审核拒绝通过
                if (Objects.equals(supplierCompanyAuditingVO.getStatus(), AuditStatus.FAILAUDIT.value())) {
                    LOGGER.info("已开店供应商：{}审核失败，更新失败原因：{}", supplierId + "supplierName：" + supplierDetail.getSupplierName(), failReason);
                    supplierCompanyAuditingService.updateRemark(new SupplierCompanyAuditingDTO(supplierCompanyAuditingVO.getCompanyAuditingId(), AuditStatus.FAILAUDIT.value(), failReason));
                } else {
                    LOGGER.info("已开店供应商：{}审核失败，开店拒绝通过，原因为：{}", supplierId + "supplierName：" + supplierDetail.getSupplierName(), failReason);
                    supplierCompanyAuditingService.audit(new SupplierCompanyAuditingDTO(supplierCompanyAuditingVO.getCompanyAuditingId(), AuditStatus.FAILAUDIT.value(), failReason));
                }
            }
            if (Objects.equals(supplierDetail.getSupplierStatus(), SupplierStatus.APPLYING.value()) || Objects.equals(accountInfoByTenantId.getData().getIsPassShop(), 0)) {
                // 申请拒绝
                SupplierAuditingVO supplierAuditingVO = supplierAuditingService.getBySupplierId(supplierId);
                SupplierAuditingDTO supplierAuditingDTO = BeanUtil.map(supplierAuditingVO, SupplierAuditingDTO.class);
                supplierAuditingDTO.setStatus(AuditStatus.FAILAUDIT.value());
                supplierAuditingDTO.setRemarks(failReason);
                if (Objects.equals(supplierAuditingVO.getStatus(), AuditStatus.FAILAUDIT.value())) {
                    LOGGER.info("申请开店供应商：{}审核失败，更新失败原因：{}", supplierId + "supplierName：" + supplierDetail.getSupplierName(), failReason);
                    supplierAuditingService.update(BeanUtil.map(supplierAuditingDTO, SupplierAuditing.class));
                } else {
                    LOGGER.info("申请开店供应商：{}审核失败，开店拒绝通过，原因为：{}", supplierId + "supplierName：" + supplierDetail.getSupplierName(), failReason);
                    supplierDetailService.audit(supplierAuditingDTO);
                }
            }
            supplierDetailMapper.updateSupplierAuditFail(supplierId);
            return;
        }
        SetCompanyInfoResp setCompanyInfoResp = response.getData();
        updateSupplierStatusToWaitAudit(supplierId, supplierDetail, supplierCompanyAuditingVO);
        if (Objects.nonNull(setCompanyInfoResp.getResult())) {
            supplierCompanyAuditingService.updateAllinpaySupplierStatus(setCompanyInfoResp.getBizUserId(), setCompanyInfoResp.getResult(), "", accountInfoByTenantId.getData().getIsPassShop());
        }
    }

    private void updateSupplierStatusToWaitAudit(Long supplierId, SupplierApiDetailVO supplierDetail, SupplierCompanyAuditingVO supplierCompanyAuditingVO) {
        if (Objects.nonNull(supplierCompanyAuditingVO) && Objects.equals(supplierCompanyAuditingVO.getStatus(), AuditStatus.WAITAUDIT.value())) {
            LOGGER.info("已开店供应商：{}审核失败，更新供应商状态为上线待审核", supplierId + "supplierName：" + supplierDetail.getSupplierName());
            // 已经开店来审核更改为上线待审核
            supplierDetailMapper.updateAllinpaySupplierStatus(supplierId, AllinpayShopStatus.WAIT_AUDIT.value());
        } else {
            LOGGER.info("申请开店供应商：{}审核失败，更新供应商状态为开店待审核", supplierId + "supplierName：" + supplierDetail.getSupplierName());
            // 未开店更改为开店待审核
            supplierDetailMapper.updateAllinpaySupplierStatus(supplierId, AllinpayShopStatus.OPEN_WAIT_AUDIT.value());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSupplierAllinpayStatusToSuccess(Long supplierId) {
        // 影印件回调只会在企业审核通过后，所以这里再次更新一下，可能之前设置企业信息回调失败了
        SupplierApiDetailVO supplierApiDetailVO = supplierDetailMapper.getBySupplierId(supplierId);
        if (Objects.isNull(supplierApiDetailVO)) {
            return;
        }
        if (Objects.equals(supplierApiDetailVO.getCompanyInfoProcessStatus(), CompanyInfoProcessStatus.SUCCESS.value())) {
            return;
        }
        supplierDetailMapper.updateSupplierAuditSuccess(supplierId);
        boolean shouldUpdate = Objects.equals(supplierApiDetailVO.getSupplierStatus(), SupplierStatus.APPLYING.value()) || Objects.equals(supplierApiDetailVO.getSupplierStatus(), SupplierStatus.OPEN_AWAIT_AUDIT.value());
        ServerResponseEntity<AuthAccountVO> accountInfoByTenantId = accountFeignClient.getAccountInfoByTenantId(supplierId, SysTypeEnum.SUPPLIER.value());
        if (shouldUpdate && Objects.equals(accountInfoByTenantId.getData().getIsPassShop(), 0)) {
            Long userId = supplierUserService.getUserIdBySupplierId(supplierId);
            UidInfoBO uidInfoBO = new UidInfoBO();
            uidInfoBO.setUserId(userId);
            uidInfoBO.setSysType(SysTypeEnum.SUPPLIER.value());
            uidInfoBO.setTenantId(supplierId);
            uidInfoBO.setIsAdmin(UserAdminType.ADMIN.value());
            uidInfoBO.setIsPassShop(IsPassShopEnum.YES.value());
            ServerResponseEntity<Void> updateTenantIdRes = accountFeignClient.updateUserInfoByUserIdAndSysType(uidInfoBO, userId, SysTypeEnum.SUPPLIER.value());
            if (!updateTenantIdRes.isSuccess()) {
                throw new LuckException("审核失败");
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBySupplierId(SupplierBankCardDTO supplierBankCardDTO, Long supplierId) {
        if (Objects.isNull(supplierBankCardDTO.getSupplierBankCardId())) {
            throw new LuckException("银行卡信息错误");
        }
        List<SupplierBankCardVO> supplierBankCardVOList = this.listBySupplierId(supplierId);
        // 检查银行卡是否属于当前供应商
        this.checkIsCurrentSupplier(supplierId, supplierBankCardDTO.getSupplierBankCardId());
        // 检查是否处于提现状态
        this.checkIsWithdraw(supplierBankCardDTO.getSupplierBankCardId());
        // 检查银行卡信息
        this.checkSaveBankCardList(supplierBankCardVOList, Collections.singletonList(supplierBankCardDTO), supplierId, true);
        supplierBankCardMapper.update(BeanUtil.map(supplierBankCardDTO, SupplierBankCard.class));
        if (Objects.equals(supplierBankCardDTO.getIsDefault(), IsDefault.YES.value())) {
            supplierBankCardMapper.setPrimary(supplierBankCardDTO.getSupplierBankCardId(), supplierId);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setPrimaryBySupplierId(Long supplierBankCardId, Long supplierId) {
        this.checkIsCurrentSupplier(supplierId, supplierBankCardId);
        supplierBankCardMapper.setPrimary(supplierBankCardId, supplierId);
    }

    @Override
    public void setNotPrimaryBySupplierId(Long supplierBankCardId, Long supplierId) {
        this.checkIsCurrentSupplier(supplierId, supplierBankCardId);
        supplierBankCardMapper.setNotPrimary(supplierBankCardId, supplierId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBySupplierId(SupplierBankCardDTO supplierBankCardDTO, Long supplierId) {
        if (Objects.isNull(supplierBankCardDTO)) {
            return;
        }
        List<SupplierBankCardVO> supplierBankCardVOList = this.listBySupplierId(supplierId);
        if (CollUtil.isEmpty(supplierBankCardVOList)) {
            supplierBankCardVOList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        }
        // 检查银行卡信息
        this.checkSaveBankCardList(supplierBankCardVOList, Collections.singletonList(supplierBankCardDTO), supplierId, false);
        SupplierBankCard supplierBankCard = BeanUtil.map(supplierBankCardDTO, SupplierBankCard.class);
        supplierBankCard.setSupplierId(supplierId);
        supplierBankCard.setStatus(StatusEnum.ENABLE.value());
        supplierBankCardMapper.save(supplierBankCard);
        if (Objects.equals(supplierBankCardDTO.getIsDefault(), 1)) {
            supplierBankCardMapper.setPrimary(supplierBankCard.getSupplierBankCardId(), supplierId);
        }
    }

    @Override
    public SupplierBankCardVO getBySupplierBankCardId(Long supplierBankCardId) {
        return supplierBankCardMapper.getBySupplierBankCardId(supplierBankCardId);
    }

    @Override
    public SupplierBankCardVO getBySupplierBankCardIdAndSupplierId(Long supplierBankCardId, Long supplierId) {
        SupplierBankCardVO supplierBankCardVO = supplierBankCardMapper.getBySupplierBankCardId(supplierBankCardId);
        if (!Objects.equals(supplierBankCardVO.getSupplierId(), supplierId)) {
            throw new LuckException("当前银行卡不属于该供应商");
        }
        return supplierBankCardVO;
    }

    @Override
    public void deleteBySupplierId(Long supplierId) {
        supplierBankCardMapper.deleteBySupplierId(supplierId);
    }

    @Override
    public void refresh(Long supplierId) {
        ServerResponseEntity<List<BindCard>> response = allinpayFeignClient.listBindCard(AllinpayConstant.SUPPLIER + supplierId, null);
        if (!response.isSuccess()) {
            throw new LuckException(response.getMsg());
        }
        LOGGER.info("供应商：{}在通联的银行卡列表信息查询结果：{}", supplierId, response);
        List<BindCard> bindCardList = response.getData().stream().filter(card -> Objects.equals(card.getBindState(), 1)).collect(Collectors.toList());
        List<SupplierBankCardVO> dbCardList = supplierBankCardMapper.listBySupplierId(supplierId);
        LOGGER.info("供应商：{}在数据库的银行卡列表信息查询结果：{}", supplierId, dbCardList);
        if (Objects.equals(bindCardList.size(), dbCardList.size())) {
            return;
        }
        // 获取主账号卡号
        List<SupplierBankCardVO> collect = dbCardList.stream().filter(card -> Objects.equals(card.getIsDefault(), 1)).collect(Collectors.toList());
        String cardNo = null;
        if (CollUtil.isNotEmpty(collect)) {
            cardNo = collect.get(0).getCardNo();
        }
        // 删除原有的
        supplierBankCardMapper.deleteBySupplierId(supplierId);
        // 更新
        List<SupplierBankCardDTO> saveList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        SupplierCompanyVO supplierCompanyVO = supplierCompanyMapper.getSupplierCompanyBySupplierIdAndStatus(supplierId, AuditStatus.SUCCESSAUDIT.value());
        for (BindCard bindCard : bindCardList) {
            SupplierBankCardDTO supplierBankCardDTO = new SupplierBankCardDTO();
            supplierBankCardDTO.setBankName(bindCard.getBankName());
            supplierBankCardDTO.setSupplierId(supplierId);
            supplierBankCardDTO.setBranchName(bindCard.getBranchBankName());
            supplierBankCardDTO.setOpeningBank(bindCard.getBankName());
            supplierBankCardDTO.setUnionBank(bindCard.getUnionBank());
            supplierBankCardDTO.setBankCardNo(bindCard.getBankCardNo());
            supplierBankCardDTO.setBankCardPro(bindCard.getBankCardPro());
            if (Objects.equals(bindCard.getBankCardPro(), 0)) {
                supplierBankCardDTO.setRecipientName(supplierCompanyVO.getRepresentative());
                supplierBankCardDTO.setCardNo(bindCard.getBankCardNo());
            } else if (Objects.equals(bindCard.getBankCardPro(), 1)) {
                supplierBankCardDTO.setRecipientName(supplierCompanyVO.getFirmName());
                supplierBankCardDTO.setCardNo(bindCard.getAccountNo());
            }
            boolean isDefault = Objects.nonNull(cardNo) && (Objects.equals(cardNo, bindCard.getBankCardNo()) || Objects.equals(cardNo, bindCard.getUnionBank()));
            if (isDefault) {
                supplierBankCardDTO.setIsDefault(1);
            }
            saveList.add(supplierBankCardDTO);
        }
        supplierBankCardMapper.insertBatch(saveList, supplierId);
    }

    /**
     * 检查银行卡列表信息
     * @param savedBankCardList 已保存的银行卡列表
     * @param newBankCardList 新增的银行卡列表
     * @param supplierId 用于保存银行卡的供应商id
     */
    private void checkSaveBankCardList(List<SupplierBankCardVO> savedBankCardList, List<SupplierBankCardDTO> newBankCardList,
                                       Long supplierId, Boolean isUpdate) {
        if (!isUpdate && (savedBankCardList.size() + newBankCardList.size() > Constant.SHOP_BANK_CARD_LIMIT_NUM)) {
            throw new LuckException("银行卡数量不能超过" + Constant.SHOP_BANK_CARD_LIMIT_NUM);
        }
        Map<String, Integer> bankCardMap = new HashMap<>(5);
        savedBankCardList.forEach(item -> {
            if (bankCardMap.containsKey(item.getCardNo())) {
                throw new LuckException("已添加的银行卡中存在重复账号" + item.getCardNo() + "，请先删除重复的银行卡账号");
            }
            bankCardMap.put(item.getCardNo(), 1);
        });
        newBankCardList.forEach(item -> {
            //新增的需要校验
            if (Objects.isNull(item.getSupplierBankCardId()) && bankCardMap.containsKey(item.getCardNo())) {
                throw new LuckException("银行卡账号" + item.getCardNo() + "重复");
            }
            bankCardMap.put(item.getCardNo(), 1);
        });
    }

    /**
     * 检查是否处于提现状态
     * @param supplierBankCardId 银行卡id
     */
    private void checkIsWithdraw(Long supplierBankCardId) {
        int count = supplierWithdrawCashMapper.countBySupplierBandCardIdAndStatus(supplierBankCardId, SupplierWithdrawCashStatus.WAITAUDIT.value());
        if (count > 0) {
            // 正在用于提现的银行卡不能删除
            throw new LuckException("正在用于提现的银行卡账号不能修改或删除");
        }
    }

    /**
     * 检查银行卡是否属于当前供应商
     * @param supplierId
     * @param supplierBankCardId
     */
    private void checkIsCurrentSupplier(Long supplierId, Long supplierBankCardId) {
        if (Objects.equals(supplierId, Constant.PLATFORM_SHOP_ID)) {
            // 平台端操作
            return;
        }
        SupplierBankCardVO supplierBankCard = this.supplierBankCardMapper.getBySupplierBankCardId(supplierBankCardId);
        if (!Objects.equals(supplierBankCard.getSupplierId(), supplierId)) {
            throw new LuckException("当前银行卡不属于你的供应商");
        }
    }
}
