
package com.tmerclub.cloud.multishop.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.multishop.constant.ShopStatus;
import com.tmerclub.cloud.api.multishop.vo.ShopBankCardVO;
import com.tmerclub.cloud.api.multishop.vo.ShopDetailVO;
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.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.multishop.constant.AuditStatus;
import com.tmerclub.cloud.multishop.constant.IsDefault;
import com.tmerclub.cloud.multishop.constant.ShopWithdrawCashStatus;
import com.tmerclub.cloud.multishop.dto.AllinpayShopBankCardDTO;
import com.tmerclub.cloud.multishop.dto.ShopAuditingDTO;
import com.tmerclub.cloud.multishop.dto.ShopBankCardDTO;
import com.tmerclub.cloud.multishop.dto.ShopCompanyAuditingDTO;
import com.tmerclub.cloud.multishop.mapper.ShopBankCardMapper;
import com.tmerclub.cloud.multishop.mapper.ShopCompanyMapper;
import com.tmerclub.cloud.multishop.mapper.ShopDetailMapper;
import com.tmerclub.cloud.multishop.mapper.ShopWithdrawCashMapper;
import com.tmerclub.cloud.multishop.model.ShopAuditing;
import com.tmerclub.cloud.multishop.model.ShopBankCard;
import com.tmerclub.cloud.multishop.service.*;
import com.tmerclub.cloud.multishop.vo.ShopAuditingVO;
import com.tmerclub.cloud.multishop.vo.ShopCompanyAuditingVO;
import com.tmerclub.cloud.multishop.vo.ShopCompanyVO;
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 lth
 * @Date 2021/4/25 17:02
 */
@Service
public class ShopBankCardServiceImpl implements ShopBankCardService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ShopBankCardServiceImpl.class);

    @Autowired
    private ShopBankCardMapper shopBankCardMapper;
    @Autowired
    private ShopWithdrawCashMapper shopWithdrawCashMapper;
    @Autowired
    private ShopApplyService shopApplyService;
    @DubboReference
    private AllinpayFeignClient allinpayFeignClient;
    @Autowired
    private ShopDetailMapper shopDetailMapper;
    @Autowired
    private ShopUserService shopUserService;
    @DubboReference
    private AccountFeignClient accountFeignClient;
    @Autowired
    private ShopCompanyMapper shopCompanyMapper;
    @Autowired
    @Lazy
    private CompanyAuditingService companyAuditingService;
    @Autowired
    @Lazy
    private ShopDetailService shopDetailService;
    @Autowired
    private ShopAuditingService shopAuditingService;


    @Override
    public void save(ShopBankCard shopBankCard) {
        shopBankCardMapper.save(shopBankCard);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<ShopBankCardDTO> shopBankCardDTOList, Long shopId) {
        if (CollUtil.isEmpty(shopBankCardDTOList)) {
            return;
        }
        List<ShopBankCardVO> shopBankCardVos = shopBankCardMapper.listByShopId(shopId);
        if (CollUtil.isEmpty(shopBankCardVos)) {
            shopBankCardVos = new ArrayList<>(0);
        }
        // 检查银行卡信息
        this.checkSaveBankCardList(shopBankCardVos, shopBankCardDTOList, shopId);
        shopBankCardMapper.insertBatch(shopBankCardDTOList, shopId);
    }

    @Override
    public List<ShopBankCardVO> listByShopId(Long shopId) {
        return shopBankCardMapper.listByShopId(shopId);
    }

    @Override
    public void deleteByShopBankCardId(Long shopBankCardId, Long shopId) {
        ShopBankCardVO shopBankCard = shopBankCardMapper.getByShopBankCardId(shopBankCardId);
        if (Objects.equals(shopBankCard.getIsDefault(), 1)) {
            // 不能删除默认银行卡
            throw new LuckException("不能删除默认银行卡");
        }
        // 检查是否为最后一张银行卡
        if (shopBankCardMapper.countByShopId(shopId) == 1) {
            throw new LuckException("至少保留一张银行卡");
        }
        // 检查是否属于当前店铺
        this.checkIsCurrentShop(shopId, shopBankCardId);
        // 检查是否处于提现状态
        this.checkIsWithdraw(shopBankCardId);
        // 进行删除
        shopBankCardMapper.deleteById(shopBankCardId);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertAndSetCompanyInfo(AllinpayShopBankCardDTO allinpayShopBankCardDTO, Long shopId, ShopCompanyVO shopCompany) {
        // 保存数据到数据库
        shopBankCardMapper.deleteByShopId(shopId);
        ShopBankCard shopBankCard = new ShopBankCard();
        shopBankCard.setShopId(shopId);
        shopBankCard.setBankName(allinpayShopBankCardDTO.getParentBankName());
        shopBankCard.setBranchName(allinpayShopBankCardDTO.getBankName());
        shopBankCard.setOpeningBank(allinpayShopBankCardDTO.getParentBankName());
        shopBankCard.setRecipientName(shopCompany.getFirmName());
        shopBankCard.setCardNo(allinpayShopBankCardDTO.getAccountNo());
        shopBankCard.setStatus(StatusEnum.ENABLE.value());
        shopBankCard.setUnionBank(allinpayShopBankCardDTO.getUnionBank());
        shopBankCard.setBankCardPro(1);
        shopBankCard.setBankCardNo(allinpayShopBankCardDTO.getAccountNo());
        shopBankCardMapper.save(shopBankCard);
        ShopDetailVO shopDetailVO = shopDetailMapper.getByShopId(shopId);
        if (Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.APPLYING.value())) {
            LOGGER.info("店铺{}提交开店申请", shopId + "shopName：" + shopDetailVO.getShopName());
            // 提交开店申请
            shopApplyService.apply(shopId);
        }
        // 通联设置企业信息及后续处理
        allinpaySetCompanyInfo(allinpayShopBankCardDTO, shopId, shopCompany, shopDetailVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void allinpaySetCompanyInfo(AllinpayShopBankCardDTO allinpayShopBankCardDTO, Long shopId, ShopCompanyVO shopCompany, ShopDetailVO shopDetailVO) {
        SetCompanyInfoDTO setCompanyInfoDTO = BeanUtil.map(allinpayShopBankCardDTO, SetCompanyInfoDTO.class);
        setCompanyInfoDTO.setCompanyName(shopCompany.getFirmName());
        setCompanyInfoDTO.setCompanyAddress(shopCompany.getResidence());
        setCompanyInfoDTO.setUniCredit(shopCompany.getCreditCode());
        setCompanyInfoDTO.setLegalName(shopCompany.getRepresentative());
        setCompanyInfoDTO.setLegalPhone(shopCompany.getLegalPhone());
        setCompanyInfoDTO.setLegalIds(shopCompany.getLegalIds());
        setCompanyInfoDTO.setBizUserId(AllinpayConstant.SHOP + shopId);
        // 设置完企业信息——店铺变为上线待审核(已开店)/开店待审核(未开店)——回调假如成功更改为营业，失败则为审核失败，重新填写，平台开店也一样
        ServerResponseEntity<SetCompanyInfoResp> response = allinpayFeignClient.setCompanyInfo(setCompanyInfoDTO);
        LOGGER.info("店铺{}设置企业信息结果：{}", shopId + "shopName：" + shopDetailVO.getShopName(), response);
        ShopCompanyAuditingVO shopCompanyAuditingVO = companyAuditingService.getLatestAuditingByShopId(shopId);
        ServerResponseEntity<AuthAccountVO> accountInfoByTenantId = accountFeignClient.getAccountInfoByTenantId(shopId, SysTypeEnum.MULTISHOP.value());
        String failReason = response.getMsg();
        if (!response.isSuccess()) {
            if (Objects.equals(failReason, AllinpayConstant.SET_COMPANY_INFO_RESULT)) {
                LOGGER.info("当前商家在通联已审核通过，无法设置企业信息。更新店铺状态，上传影印件，等待影印件通过审核。");
                // 审核成功 更新店铺企业信息审核状态为审核通过，待影印件通过后即可将通联店铺状态更改为营业中,并且把工商信息修改为成功
                updateShopStatusToWaitAudit(shopId, shopDetailVO, shopCompanyAuditingVO);
                companyAuditingService.updateAllinpayShopStatus(AllinpayConstant.SHOP + shopId, com.tmerclub.cloud.api.payment.constant.AuditStatus.SUCCESS.getCode(),
                        "", accountInfoByTenantId.getData().getIsPassShop());
                return;
            }
            // 更新审核失败
            if (Objects.nonNull(shopCompanyAuditingVO)) {
                // 如果是已经开店来审核的话要把审核拒绝通过
                if (Objects.equals(shopCompanyAuditingVO.getStatus(), AuditStatus.FAILAUDIT.value())) {
                    LOGGER.info("已开店商家：{}审核失败，更新失败原因：{}", shopId + "shopName：" + shopDetailVO.getShopName(), failReason);
                    companyAuditingService.updateRemark(new ShopCompanyAuditingDTO(shopCompanyAuditingVO.getCompanyAuditingId(), AuditStatus.FAILAUDIT.value(), failReason));
                } else {
                    LOGGER.info("已开店商家：{}审核失败，开店拒绝通过，原因为：{}", shopId + "shopName：" + shopDetailVO.getShopName(), failReason);
                    companyAuditingService.audit(new ShopCompanyAuditingDTO(shopCompanyAuditingVO.getCompanyAuditingId(), AuditStatus.FAILAUDIT.value(), failReason));
                }
            }
            if (Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.APPLYING.value()) || Objects.equals(accountInfoByTenantId.getData().getIsPassShop(), 0)) {
                // 申请拒绝
                ShopAuditingVO shopAuditing = shopAuditingService.getByshopId(shopId);
                ShopAuditingDTO shopAuditingDTO = BeanUtil.map(shopAuditing, ShopAuditingDTO.class);
                shopAuditingDTO.setStatus(AuditStatus.FAILAUDIT.value());
                shopAuditingDTO.setRemarks(failReason);
                if (Objects.equals(shopAuditing.getStatus(), AuditStatus.FAILAUDIT.value())) {
                    LOGGER.info("申请开店商家：{}审核失败，更新失败原因：{}", shopId + "shopName：" + shopDetailVO.getShopName(), failReason);
                    shopAuditingService.update(BeanUtil.map(shopAuditingDTO, ShopAuditing.class));
                } else {
                    LOGGER.info("申请开店商家：{}审核失败，开店拒绝通过，原因为：{}", shopId + "shopName：" + shopDetailVO.getShopName(), failReason);
                    shopDetailService.audit(shopAuditingDTO);
                }
            }
            shopDetailMapper.updateShopAuditFail(shopId);
            return;
        }
        SetCompanyInfoResp setCompanyInfoResp = response.getData();
        updateShopStatusToWaitAudit(shopId, shopDetailVO, shopCompanyAuditingVO);
        if (Objects.nonNull(setCompanyInfoResp.getResult())) {
            LOGGER.info("通联回调不为空，进行店铺审核后续处理");
            companyAuditingService.updateAllinpayShopStatus(setCompanyInfoResp.getBizUserId(), setCompanyInfoResp.getResult(), "", accountInfoByTenantId.getData().getIsPassShop());
        }
    }

    private void updateShopStatusToWaitAudit(Long shopId, ShopDetailVO shopDetailVO, ShopCompanyAuditingVO shopCompanyAuditingVO) {
        if (Objects.nonNull(shopCompanyAuditingVO) && Objects.equals(shopCompanyAuditingVO.getStatus(), AuditStatus.WAITAUDIT.value())) {
            LOGGER.info("已开店商家：{}审核失败，更新店铺状态为上线待审核", shopId + "shopName：" + shopDetailVO.getShopName());
            // 已经开店来审核更改为上线待审核
            shopDetailMapper.updateAllinpayShopStatus(shopId, AllinpayShopStatus.WAIT_AUDIT.value());
        } else {
            LOGGER.info("申请开店商家：{}审核失败，更新店铺状态为开店待审核", shopId + "shopName：" + shopDetailVO.getShopName());
            // 未开店更改为开店待审核
            shopDetailMapper.updateAllinpayShopStatus(shopId, AllinpayShopStatus.OPEN_WAIT_AUDIT.value());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateShopAllinpayStatusToSuccess(Long shopId) {
        ShopDetailVO shopDetailVO = shopDetailMapper.getByShopId(shopId);
        if (Objects.isNull(shopDetailVO)) {
            return;
        }
        if (Objects.equals(shopDetailVO.getCompanyInfoProcessStatus(), CompanyInfoProcessStatus.SUCCESS.value())) {
            return;
        }
        shopDetailMapper.updateShopAuditSuccess(shopId);
        boolean shouldUpdate = Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.APPLYING.value()) || Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.OPEN_AWAIT_AUDIT.value());
        ServerResponseEntity<AuthAccountVO> accountInfoByTenantId = accountFeignClient.getAccountInfoByTenantId(shopId, SysTypeEnum.MULTISHOP.value());
        if (shouldUpdate && Objects.equals(accountInfoByTenantId.getData().getIsPassShop(), 0)) {
            Long userId = shopUserService.getUserIdByShopId(shopId);
            UidInfoBO uidInfoBO = new UidInfoBO();
            uidInfoBO.setUserId(userId);
            uidInfoBO.setSysType(SysTypeEnum.MULTISHOP.value());
            uidInfoBO.setTenantId(shopId);
            uidInfoBO.setIsAdmin(UserAdminType.ADMIN.value());
            uidInfoBO.setIsPassShop(IsPassShopEnum.YES.value());
            ServerResponseEntity<Void> updateTenantIdRes = accountFeignClient.updateUserInfoByUserIdAndSysType(uidInfoBO, userId, SysTypeEnum.MULTISHOP.value());
            if (!updateTenantIdRes.isSuccess()) {
                throw new LuckException("审核失败");
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByShopId(ShopBankCardDTO shopBankCardDTO, Long shopId) {
        if (Objects.isNull(shopBankCardDTO.getShopBankCardId())) {
            throw new LuckException("银行卡信息错误");
        }
        List<ShopBankCardVO> shopBankCardVOList = this.listByShopId(shopId);
        // 检查银行卡是否属于当前店铺
        this.checkIsCurrentShop(shopId, shopBankCardDTO.getShopBankCardId());
        // 检查是否处于提现状态
        this.checkIsWithdraw(shopBankCardDTO.getShopBankCardId());
        // 检查银行卡信息
        this.checkSaveBankCardList(shopBankCardVOList, Collections.singletonList(shopBankCardDTO), shopId);
        shopBankCardMapper.update(BeanUtil.map(shopBankCardDTO, ShopBankCard.class));
        if (Objects.equals(shopBankCardDTO.getIsDefault(), IsDefault.YES.value())) {
            shopBankCardMapper.setPrimary(shopBankCardDTO.getShopBankCardId(), shopId);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setPrimaryByShopId(Long shopBankCardId, Long shopId) {
        this.checkIsCurrentShop(shopId, shopBankCardId);
        shopBankCardMapper.setPrimary(shopBankCardId, shopId);
    }

    @Override
    public void setNotPrimaryByShopId(Long shopBankCardId, Long shopId) {
        this.checkIsCurrentShop(shopId, shopBankCardId);
        shopBankCardMapper.setNotPrimary(shopBankCardId, shopId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertByShopId(ShopBankCardDTO shopBankCardDTO, Long shopId) {
        if (Objects.isNull(shopBankCardDTO)) {
            return;
        }
        List<ShopBankCardVO> shopBankCardVOList = this.listByShopId(shopId);
        if (CollUtil.isEmpty(shopBankCardVOList)) {
            shopBankCardVOList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        }
        // 检查银行卡信息
        this.checkSaveBankCardList(shopBankCardVOList, Collections.singletonList(shopBankCardDTO), shopId);
        ShopBankCard shopBankCard = BeanUtil.map(shopBankCardDTO, ShopBankCard.class);
        shopBankCard.setShopId(shopId);
        shopBankCard.setStatus(StatusEnum.ENABLE.value());
        shopBankCardMapper.save(shopBankCard);
        if (Objects.equals(shopBankCardDTO.getIsDefault(), 1)) {
            shopBankCardMapper.setPrimary(shopBankCard.getShopBankCardId(), shopId);
        }
    }

    @Override
    public ShopBankCardVO getByShopBankCardId(Long shopBankCardId) {
        return shopBankCardMapper.getByShopBankCardId(shopBankCardId);
    }

    @Override
    public ShopBankCardVO getByShopBankCardIdAndShopId(Long shopBankCardId, Long shopId) {
        ShopBankCardVO shopBankCardVO = shopBankCardMapper.getByShopBankCardId(shopBankCardId);
        if (!Objects.equals(shopBankCardVO.getShopId(), shopId)) {
            throw new LuckException("当前银行卡不属于该店铺");
        }
        return shopBankCardVO;
    }

    @Override
    public void deleteByShopId(Long shopId) {
        shopBankCardMapper.deleteByShopId(shopId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void refresh(Long shopId) {
        ServerResponseEntity<List<BindCard>> response = allinpayFeignClient.listBindCard(AllinpayConstant.SHOP + shopId, null);
        if (!response.isSuccess()) {
            throw new LuckException(response.getMsg());
        }
        LOGGER.info("店铺：{}在通联的银行卡列表信息查询结果：{}", shopId, response);
        List<BindCard> bindCardList = response.getData().stream().filter(card -> Objects.equals(card.getBindState(), 1)).collect(Collectors.toList());
        List<ShopBankCardVO> dbCardList = shopBankCardMapper.listByShopId(shopId);
        LOGGER.info("店铺：{}在数据库的银行卡列表信息查询结果：{}", shopId, dbCardList);
        if (Objects.equals(bindCardList.size(), dbCardList.size())) {
            return;
        }
        // 获取主账号卡号
        List<ShopBankCardVO> 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();
        }
        // 删除原有的
        shopBankCardMapper.deleteByShopId(shopId);
        // 更新
        List<ShopBankCardDTO> saveList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        ShopCompanyVO shopCompanyVO = shopCompanyMapper.getShopCompanyByShopIdAndStatus(shopId, AuditStatus.SUCCESSAUDIT.value());
        for (BindCard bindCard : bindCardList) {
            ShopBankCardDTO shopBankCardDTO = new ShopBankCardDTO();
            shopBankCardDTO.setBankName(bindCard.getBankName());
            shopBankCardDTO.setShopId(shopId);
            shopBankCardDTO.setBranchName(bindCard.getBranchBankName());
            shopBankCardDTO.setOpeningBank(bindCard.getBankName());
            shopBankCardDTO.setUnionBank(bindCard.getUnionBank());
            shopBankCardDTO.setBankCardPro(bindCard.getBankCardPro());
            shopBankCardDTO.setBankCardNo(bindCard.getBankCardNo());
            if (Objects.equals(bindCard.getBankCardPro(), 0)) {
                shopBankCardDTO.setCardNo(bindCard.getBankCardNo());
                shopBankCardDTO.setRecipientName(shopCompanyVO.getRepresentative());
            } else if (Objects.equals(bindCard.getBankCardPro(), 1)) {
                shopBankCardDTO.setCardNo(bindCard.getAccountNo());
                shopBankCardDTO.setRecipientName(shopCompanyVO.getFirmName());
            }
            boolean isDefault = Objects.nonNull(cardNo) && (Objects.equals(cardNo, bindCard.getBankCardNo()) || Objects.equals(cardNo, bindCard.getUnionBank()));
            if (isDefault) {
                shopBankCardDTO.setIsDefault(1);
            }
            saveList.add(shopBankCardDTO);
        }
        shopBankCardMapper.insertBatch(saveList, shopId);
    }

    /**
     * 检查银行卡列表信息
     * @param savedBankCardList 已保存的银行卡列表
     * @param newBankCardList 新增的银行卡列表
     * @param shopId 用于保存银行卡的店铺id
     */
    private void checkSaveBankCardList(List<ShopBankCardVO> savedBankCardList, List<ShopBankCardDTO> newBankCardList, Long shopId) {
        List<ShopBankCardDTO> insertList = newBankCardList.stream().filter(item -> Objects.isNull(item.getShopBankCardId())).collect(Collectors.toList());
        if (savedBankCardList.size() + insertList.size() > Constant.SHOP_BANK_CARD_LIMIT_NUM) {
            throw new LuckException("银行卡数量不能超过" + Constant.SHOP_BANK_CARD_LIMIT_NUM);
        }
        Map<String, ShopBankCardVO> savedBankCardMap = savedBankCardList.stream().collect(Collectors.toMap(ShopBankCardVO::getCardNo, item -> item));
        newBankCardList.forEach(item -> {
            //新增的需要校验
            if (savedBankCardMap.containsKey(item.getCardNo()) && !Objects.equals(savedBankCardMap.get(item.getCardNo()).getShopBankCardId(), item.getShopBankCardId())) {
                throw new LuckException("银行卡账号" + item.getCardNo() + "重复");
            }
        });
    }

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

    /**
     * 检查银行卡是否属于当前店铺
     * @param shopId
     * @param shopBankCardId
     */
    private void checkIsCurrentShop(Long shopId, Long shopBankCardId) {
        if (Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            // 平台端操作
            return;
        }
        ShopBankCardVO shopBankCard = this.shopBankCardMapper.getByShopBankCardId(shopBankCardId);
        if (!Objects.equals(shopBankCard.getShopId(), shopId)) {
            throw new LuckException("当前银行卡不属于你的店铺");
        }
    }
}
