

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

import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.api.payment.allinpay.member.resp.UpdateCompanyInfoResp;
import com.tmerclub.cloud.api.payment.dto.UpdateCompanyInfoDTO;
import com.tmerclub.cloud.api.payment.feign.AllinpayFeignClient;
import com.tmerclub.cloud.api.supplier.vo.SupplierApiDetailVO;
import com.tmerclub.cloud.common.constant.*;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
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.common.util.PrincipalUtil;
import com.tmerclub.cloud.supplier.constant.AuditStatus;
import com.tmerclub.cloud.supplier.dto.SupplierAuditingDTO;
import com.tmerclub.cloud.supplier.dto.SupplierCompanyAuditingDTO;
import com.tmerclub.cloud.supplier.dto.SupplierCompanyDTO;
import com.tmerclub.cloud.supplier.mapper.SupplierCompanyAuditingMapper;
import com.tmerclub.cloud.supplier.mapper.SupplierDetailMapper;
import com.tmerclub.cloud.supplier.model.SupplierCompany;
import com.tmerclub.cloud.supplier.model.SupplierCompanyAuditing;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author chiley
 * @date 2022/9/13 14:40
 */

@Service
public class SupplierCompanyAuditingServiceImpl implements SupplierCompanyAuditingService {
    private static final Logger logger = LoggerFactory.getLogger(SupplierCompanyAuditingServiceImpl.class);

    @Autowired
    private SupplierCompanyAuditingMapper companyAuditingMapper;
    @Autowired
    private SupplierCompanyService supplierCompanyService;
    @Autowired
    private SupplierDetailMapper supplierDetailMapper;
    @Autowired
    private SupplierAllinpayService supplierAllinpayService;
    @DubboReference
    private AllinpayFeignClient allinpayFeignClient;
    @Autowired
    private SupplierDetailService supplierDetailService;
    @Autowired
    private SupplierBankCardService supplierBankCardService;
    @Autowired
    private SupplierAuditingService supplierAuditingService;

    @Override
    public void updateRemark(SupplierCompanyAuditingDTO supplierCompanyAuditingDTO) {
        companyAuditingMapper.update(BeanUtil.map(supplierCompanyAuditingDTO, SupplierCompanyAuditing.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void applyChangeCompanyInfo(SupplierCompanyDTO supplierCompanyDTO) {
        SupplierApiDetailVO supplierApiDetailVO = supplierDetailMapper.getBySupplierId(supplierCompanyDTO.getSupplierId());
        SupplierCompanyVO supplierCompanyVO = new SupplierCompanyVO();
        if (supplierAllinpayService.getIsAllinpay()) {
            if (!Objects.equals(supplierApiDetailVO.getIdCardCollectProcessStatus(), IdCardCollectProcessStatus.ALL.value())) {
                logger.info("获取影印件还未通过的供应商{}工商信息，状态为待审核", supplierCompanyDTO.getSupplierId());
                supplierCompanyVO = supplierCompanyService.getSupplierCompanyBySupplierIdAndStatus(supplierCompanyDTO.getSupplierId(), AuditStatus.WAITAUDIT.value());
            } else {
                logger.info("获取影印件已经通过的供应商{}工商信息，状态为审核成功", supplierCompanyDTO.getSupplierId());
                supplierCompanyVO = supplierCompanyService.getSupplierCompanyBySupplierIdAndStatus(supplierCompanyDTO.getSupplierId(), AuditStatus.SUCCESSAUDIT.value());
            }
        }
        if (StrUtil.isNotBlank(supplierCompanyDTO.getLegalPhone())) {
            if (!PrincipalUtil.isDbPhone(supplierCompanyDTO.getLegalPhone(), supplierCompanyVO.getLegalPhone(), true)) {
                throw new LuckException("请输入正确的手机号");
            }
            if (supplierCompanyDTO.getLegalPhone().contains(Constant.ASTERISK)) {
                supplierCompanyDTO.setLegalPhone(supplierCompanyVO.getLegalPhone());
            }
        }
        // 把之前审核不通过的工商信息删除
        supplierCompanyService.deleteBySupplierIdAndStatus(supplierCompanyDTO.getSupplierId(), AuditStatus.FAILAUDIT.value());
        // 保存变更工商信息
        SupplierCompany supplierCompany = BeanUtil.map(supplierCompanyDTO, SupplierCompany.class);
        supplierCompany.setStatus(AuditStatus.WAITAUDIT.value());
        supplierCompanyService.save(supplierCompany, false);
        // 生成审核信息
        Long companyAuditingId = generateAuditInformation(supplierCompany);
        if (supplierAllinpayService.getIsAllinpay()) {
            if (Objects.equals(supplierApiDetailVO.getCompanyInfoProcessStatus(), CompanyInfoProcessStatus.SUCCESS.value())) {
                logger.info("供应商{}工商信息已经通过审核，更新工商信息内容和状态和后续处理", supplierCompanyDTO.getSupplierId());
                // 审核成功的要进行更改的
                updateCompany(supplierCompanyDTO, companyAuditingId, supplierCompanyVO, supplierApiDetailVO);
            }
        }
    }

    private void updateCompany(SupplierCompanyDTO supplierCompanyDTO, Long companyAuditingId, SupplierCompanyVO supplierCompanyVO, SupplierApiDetailVO supplierApiDetailVO) {
        Long supplierId = supplierCompanyDTO.getSupplierId();
        ServerResponseEntity<Integer> idCardStatus = allinpayFeignClient.getIdCardStatus(AllinpayConstant.SUPPLIER + supplierId);
        if (!idCardStatus.isSuccess()) {
            throw new LuckException(idCardStatus.getMsg());
        }
        if (!Objects.equals(supplierApiDetailVO.getIdCardCollectProcessStatus(), idCardStatus.getData())) {
            logger.info("供应商{}影印件信息状态{}和通联影印件信息状态{}不一致，处理影印件状态", supplierCompanyDTO.getSupplierId(),
                    supplierApiDetailVO.getIdCardCollectProcessStatus(), idCardStatus.getData());
            // 不一致先更新
            supplierDetailService.updateAllinpayIdCardStatus(AllinpayConstant.SUPPLIER + supplierId, idCardStatus.getData());
        }
        // 改了基本信息
        boolean isChangeInfo = getIsChangeInfo(supplierCompanyDTO, supplierCompanyVO);
        // 改了关键信息
        boolean isChangeKeyInfo = getIsChangeKeyInfo(supplierCompanyDTO, supplierCompanyVO);
        // 改了影印件
        boolean isChangeIdCard = getIsChangeIdCard(supplierCompanyDTO, supplierCompanyVO);
        if (!isChangeInfo && !isChangeKeyInfo && !isChangeIdCard) {
            // 没有改通联有关信息直接审核成功
            logger.info("没有修改通联有关信息，供应商{}工商信息审核通过", supplierId);
            audit(new SupplierCompanyAuditingDTO(companyAuditingId, AuditStatus.SUCCESSAUDIT.value(), ""));
            return;
        }

        if (isChangeInfo || isChangeKeyInfo) {
            logger.info("供应商修改了基本信息{}或关键信息{}，交给通联审核", isChangeInfo, isChangeKeyInfo);
            // 对于信息修改操作，上送所有需修改信息均认证成功才操作修改，否则信息均不修改；
            UpdateCompanyInfoDTO updateCompanyInfoDTO = new UpdateCompanyInfoDTO(AllinpayConstant.SUPPLIER + supplierId);
            updateCompanyInfoDTO.setCompanyName(supplierCompanyDTO.getFirmName());
            updateCompanyInfoDTO.setCompanyAddress(supplierCompanyDTO.getResidence());
            updateCompanyInfoDTO.setLegalIds(supplierCompanyDTO.getLegalIds());
            updateCompanyInfoDTO.setLegalPhone(supplierCompanyDTO.getLegalPhone());
            updateCompanyInfoDTO.setLegalName(supplierCompanyDTO.getRepresentative());
            // 通联审核
            ServerResponseEntity<UpdateCompanyInfoResp> response = allinpayFeignClient.updateCompanyInfo(updateCompanyInfoDTO);
            if (!response.isSuccess()) {
                logger.info("通联审核供应商{}修改的工商信息失败，拒绝更新，原因为{}", supplierId, response.getMsg());
                // 审核失败不会更新影印件审核，只需要拒绝更新就可以了
                audit(new SupplierCompanyAuditingDTO(companyAuditingId, AuditStatus.FAILAUDIT.value(), response.getMsg()));
                return;
            }
            UpdateCompanyInfoResp updateCompanyInfoResp = response.getData();
            if (Objects.equals(updateCompanyInfoResp.getResult(), com.tmerclub.cloud.api.payment.constant.AuditStatus.SUCCESS.getCode())) {
                logger.info("通联审核供应商{}修改的工商信息成功", supplierId);
                if (isChangeKeyInfo) {
                    logger.info("供应商{}修改了关键信息，并且审核通过，更新店铺为通联上线待审核，等待影印件重新审核通过", supplierId);
                    // 修改了会触发ocr的
                    //审核成功 通联更新了企业信息 更新会触发ocr，供应商需要下线等待影印件审核通过
                    audit(new SupplierCompanyAuditingDTO(companyAuditingId, AuditStatus.SUCCESSAUDIT.value(), ""));
                    // 更新供应商为通联上线待审核，影印件审核通过即可营业
                    supplierDetailMapper.updateSupplierToWaitAudit(supplierCompanyDTO.getSupplierId());
                } else if (!isChangeIdCard) {
                    logger.info("供应商{}没有修改了关键信息，也没有影印件要上传，直接审核成功", supplierId);
                    // 没有修改关键信息,没有上传影印件可以直接成功
                    audit(new SupplierCompanyAuditingDTO(companyAuditingId, AuditStatus.SUCCESSAUDIT.value(), ""));
                    if (!Objects.equals(supplierApiDetailVO.getIdCardCollectProcessStatus(), IdCardCollectProcessStatus.ALL.value())) {
                        logger.info("供应商{}影印件状态异常，更新影印件", supplierId);
                        supplierDetailService.updateAllinpayIdCardStatus(AllinpayConstant.SUPPLIER + supplierId, idCardStatus.getData());
                    }
                    return;
                }
            } else if (Objects.equals(updateCompanyInfoResp.getResult(), com.tmerclub.cloud.api.payment.constant.AuditStatus.FAIL.getCode())) {
                logger.info("通联审核供应商{}修改的工商信息失败，拒绝更新", supplierId);
                // 审核失败不会更新影印件审核，只需要拒绝更新就可以了
                audit(new SupplierCompanyAuditingDTO(companyAuditingId, AuditStatus.FAILAUDIT.value(), updateCompanyInfoResp.getFailReason()));
                return;
            }
        }

        // 如果有改变影印件，没有改变别的信息，在这里影印件需要重新上传，上传失败拒绝更新
        onlyChangeIdCard(supplierCompanyDTO, companyAuditingId, supplierApiDetailVO, supplierId, idCardStatus, isChangeInfo, isChangeKeyInfo);
    }

    private void onlyChangeIdCard(SupplierCompanyDTO supplierCompanyDTO, Long companyAuditingId, SupplierApiDetailVO supplierApiDetailVO, Long supplierId, ServerResponseEntity<Integer> idCardStatus, boolean isChangeInfo, boolean isChangeKeyInfo) {
        if (!isChangeInfo && !isChangeKeyInfo) {
            logger.info("供应商{}只修改影印件，影印件上传", supplierId);
            if (!Objects.equals(supplierApiDetailVO.getIdCardCollectProcessStatus(), idCardStatus.getData()) && Objects.equals(idCardStatus.getData(), IdCardCollectProcessStatus.ALL.value())) {
                logger.info("供应商{}影印件已经通过审核，但是供应商影印件状态没更新，更新状态并且拒绝影印件上传审核", supplierId);
                // 如果影印件上传成功但是这里没有更新的话要更新一下
                audit(new SupplierCompanyAuditingDTO(companyAuditingId, AuditStatus.FAILAUDIT.value(), "供应商影印件已经审核通过，无需更新"));
                supplierDetailService.updateAllinpayIdCardStatus(AllinpayConstant.SUPPLIER + supplierId, idCardStatus.getData());
                return;
            } else if (!Objects.equals(supplierApiDetailVO.getIdCardCollectProcessStatus(), idCardStatus.getData())) {
                logger.info("供应商{}影印件状态没更新，更新状态", supplierId);
                supplierDetailService.updateAllinpayIdCardStatus(AllinpayConstant.SUPPLIER + supplierId, idCardStatus.getData());
            } else if (Objects.equals(supplierApiDetailVO.getIdCardCollectProcessStatus(), IdCardCollectProcessStatus.ALL.value())) {
                logger.info("供应商{}影印件已经通过审核，拒绝影印件上传审核", supplierId);
                audit(new SupplierCompanyAuditingDTO(companyAuditingId, AuditStatus.FAILAUDIT.value(), "供应商影印件已经审核通过，无需更新"));
                return;
            }
            String result = supplierCompanyService.idCardCollect(BeanUtil.map(supplierCompanyDTO, SupplierCompanyVO.class), supplierId);
            if (!Objects.equals(result, AllinpayConstant.ALLINPAY_AUDIT_SUCCESS)) {
                logger.info("供应商{}影印件上传失败，拒绝影印件更新", supplierId);
                audit(new SupplierCompanyAuditingDTO(companyAuditingId, AuditStatus.FAILAUDIT.value(), "供应商影印件上传失败，请重新上传"));
                return;
            }
            // 更新供应商为通联上线待审核，影印件审核通过即可营业
            supplierDetailMapper.updateSupplierToWaitAudit(supplierCompanyDTO.getSupplierId());
            // 更新供应商影印件采集状态
            supplierDetailMapper.updateAllinpayIdCardStatus(supplierCompanyDTO.getSupplierId(), IdCardCollectProcessStatus.WAIT_AUDIT.value());
        }
    }


    @Override
    public SupplierCompanyAuditingVO getLatestAuditingBySupplierId(Long supplierId) {
        return companyAuditingMapper.getLatestAuditingBySupplierId(supplierId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revoke(Long supplierId) {
        if (supplierAllinpayService.getIsAllinpay()) {
            throw new LuckException("当前无法撤销申请");
        }
        SupplierCompanyAuditingVO companyAuditingVO = getLatestAuditingBySupplierId(supplierId);
        if (Objects.isNull(companyAuditingVO) || !Objects.equals(companyAuditingVO.getStatus(), AuditStatus.WAITAUDIT.value())) {
            throw new LuckException("申请信息未能找到，请刷新页面");
        }
        // 删除申请变更的工商信息
        supplierCompanyService.deleteBySupplierIdAndStatus(supplierId, AuditStatus.WAITAUDIT.value());
        // 删除申请审核信息
        companyAuditingMapper.deleteById(companyAuditingVO.getCompanyAuditingId());
    }

    @Override
    public PageVO<SupplierCompanyAuditingVO> page(PageDTO pageDTO, SupplierCompanyAuditingDTO supplierCompanyAuditingDTO) {
        return PageUtil.doPage(pageDTO, () -> companyAuditingMapper.list(supplierCompanyAuditingDTO));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(SupplierCompanyAuditingDTO supplierCompanyAuditingDTO) {
        boolean isAllinpay = supplierAllinpayService.getIsAllinpay();
        SupplierCompanyAuditing companyAuditing = companyAuditingMapper.getById(supplierCompanyAuditingDTO.getCompanyAuditingId());
        if (Objects.isNull(companyAuditing) || !Objects.equals(companyAuditing.getStatus(), AuditStatus.WAITAUDIT.value())) {
            throw new LuckException("申请信息未能找到，请刷新页面");
        }
        companyAuditing.setAuditorId(isAllinpay ? null : AuthUserContext.get().getUserId());
        // 审核不通过
        if (Objects.equals(supplierCompanyAuditingDTO.getStatus(), AuditStatus.FAILAUDIT.value())) {
            companyAuditing.setStatus(AuditStatus.FAILAUDIT.value());
            companyAuditing.setRemarks(supplierCompanyAuditingDTO.getRemarks());
            companyAuditingMapper.update(companyAuditing);
            // 把对应的工商信息设置为审核不通过状态
            SupplierCompany supplierCompany = new SupplierCompany();
            supplierCompany.setSupplierCompanyId(companyAuditing.getSupplierCompanyId());
            supplierCompany.setStatus(AuditStatus.FAILAUDIT.value());
            supplierCompanyService.updateById(supplierCompany);
            return;
        }
        // 审核通过
        if (Objects.equals(supplierCompanyAuditingDTO.getStatus(), AuditStatus.SUCCESSAUDIT.value())) {
            companyAuditing.setStatus(AuditStatus.SUCCESSAUDIT.value());
            companyAuditingMapper.update(companyAuditing);

            supplierCompanyService.batchUpdateStatus(companyAuditing.getSupplierId(), AuditStatus.FAILAUDIT.value(), null);
            // 把对应的工商信息设置为启用状态
            SupplierCompany supplierCompany = new SupplierCompany();
            supplierCompany.setSupplierCompanyId(companyAuditing.getSupplierCompanyId());
            supplierCompany.setStatus(AuditStatus.SUCCESSAUDIT.value());
            supplierCompanyService.updateById(supplierCompany);
        }

    }

    @Override
    public SupplierCompanyAuditingVO getAuditInfo(Long supplierId) {
        SupplierCompanyAuditingVO companyAuditingVO = getLatestAuditingBySupplierId(supplierId);
        if (Objects.isNull(companyAuditingVO)) {
            if (supplierAllinpayService.getIsAllinpay()) {
                SupplierCompanyVO supplierCompanyVO = supplierCompanyService.getSupplierCompanyBySupplierIdAndStatus(supplierId, AuditStatus.WAITAUDIT.value());
                SupplierCompanyAuditingVO supplierCompanyAuditingVO = new SupplierCompanyAuditingVO();
                supplierCompanyAuditingVO.setSupplierCompanyVO(supplierCompanyVO);
                return supplierCompanyAuditingVO;
            }
            return null;
        }
        SupplierCompanyVO supplierCompanyVO = supplierCompanyService.getById(companyAuditingVO.getSupplierCompanyId());
        companyAuditingVO.setSupplierCompanyVO(supplierCompanyVO);
        return companyAuditingVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCompanyInfoResult(String bizUserId, Integer status, Integer result, String failReason) {
        Long supplierId = Long.valueOf(bizUserId.substring(AllinpayConstant.SUPPLIER_LENGTH));
        SupplierCompanyVO waitAuditShopCompany = supplierCompanyService.getSupplierCompanyBySupplierIdAndStatus(supplierId, AuditStatus.WAITAUDIT.value());
        SupplierCompanyVO successShopCompany = supplierCompanyService.getSupplierCompanyBySupplierIdAndStatus(supplierId, AuditStatus.SUCCESSAUDIT.value());
        SupplierCompanyAuditingVO supplierCompanyAuditingVO = getLatestAuditingBySupplierId(supplierId);
        // 更新失败
        if (Objects.equals(result, com.tmerclub.cloud.api.payment.constant.AuditStatus.FAIL.getCode())) {
            logger.info("通联回调审核供应商{}修改的工商信息失败，拒绝更新，原因为{}", supplierId, failReason);
            audit(new SupplierCompanyAuditingDTO(supplierCompanyAuditingVO.getCompanyAuditingId(), AuditStatus.FAILAUDIT.value(), failReason));
        }
        // 更新成功
        if (Objects.equals(result, com.tmerclub.cloud.api.payment.constant.AuditStatus.SUCCESS.getCode())) {
            // 如果有改变企业名称，法人名称，需要重新签约提现协议
            checkChangeName(waitAuditShopCompany, successShopCompany, supplierId);
            if (getIsChangeIdCard(BeanUtil.map(waitAuditShopCompany, SupplierCompanyDTO.class), successShopCompany)) {
                logger.info("通联回调审核供应商{}修改的工商信息成功，修改了影印件，重新上传影印件", supplierId);
                // 修改了影印件
                String idCardCollectResult = supplierCompanyService.idCardCollect(waitAuditShopCompany, supplierId);
                if (!Objects.equals(idCardCollectResult, AllinpayConstant.ALLINPAY_AUDIT_SUCCESS)) {
                    logger.info("通联回调审核供应商{}修改的工商信息成功，影印件上传异常，拒绝申请", supplierId);
                    audit(new SupplierCompanyAuditingDTO(supplierCompanyAuditingVO.getCompanyAuditingId(), AuditStatus.FAILAUDIT.value(), "影印件上传异常，请重试"));
                    return;
                }
                // 更新供应商为通联上线待审核，影印件审核通过即可营业
                supplierDetailMapper.updateSupplierToWaitAudit(supplierId);
                // 更新供应商影印件采集状态
                supplierDetailMapper.updateAllinpayIdCardStatus(supplierId, IdCardCollectProcessStatus.WAIT_AUDIT.value());
            } else {
                logger.info("通联回调审核供应商{}修改的工商信息成功，没有修改影印件", supplierId);
                if (!Objects.equals(status, IdCardCollectProcessStatus.ALL.value())) {
                    logger.info("通联回调审核供应商{}修改的工商信息成功，没有修改影印件，影印件ocr失败", supplierId);
                    // 影印件ocr失败了，对供应商进行状态更新
                    // 更新供应商为通联上线待审核，影印件审核通过即可营业
                    supplierDetailMapper.updateSupplierToWaitAudit(supplierId);
                    // 更新供应商影印件采集状态
                    supplierDetailMapper.updateAllinpayIdCardStatus(supplierId, status);
                    return;
                }
                // 审核成功
                audit(new SupplierCompanyAuditingDTO(supplierCompanyAuditingVO.getCompanyAuditingId(), AuditStatus.SUCCESSAUDIT.value(), ""));
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAllinpaySupplierStatus(String bizUserId, Integer status, String failReason, Integer isPassShop) {
        Long supplierId = Long.valueOf(bizUserId.substring(AllinpayConstant.SUPPLIER_LENGTH));
        SupplierApiDetailVO supplierDetailVO = supplierDetailService.getBySupplierId(supplierId);
        if (Objects.equals(status, com.tmerclub.cloud.api.payment.constant.AuditStatus.FAIL.getCode())) {
            logger.info("通联审核供应商企业信息失败");
            if (Objects.equals(supplierDetailVO.getAllinpaySupplierStatus(), AllinpayShopStatus.OFFLINE.value()) && Objects.equals(supplierDetailVO.getCompanyInfoProcessStatus(), CompanyInfoProcessStatus.FAIL.value())) {
                logger.info("供应商{}状态已经处理过，无需下线处理", supplierDetailVO.getSupplierName());
                return;
            }
            authFail(bizUserId, failReason, supplierId);
        } else if (Objects.equals(status, com.tmerclub.cloud.api.payment.constant.AuditStatus.SUCCESS.getCode())) {
            logger.info("通联审核供应商企业信息成功");
            if (Objects.equals(supplierDetailVO.getIdCardCollectProcessStatus(), IdCardCollectProcessStatus.ALL.value()) && Objects.equals(supplierDetailVO.getCompanyInfoProcessStatus(), CompanyInfoProcessStatus.SUCCESS.value())) {
                return;
            }
            SupplierCompanyVO supplierCompanyVO = supplierCompanyService.getSupplierCompanyBySupplierIdAndStatus(supplierId, AuditStatus.WAITAUDIT.value());
            // 审核成功 更新供应商状态
            supplierBankCardService.updateSupplierAllinpayStatusToSuccess(supplierId);
            if (!Objects.equals(supplierDetailVO.getIdCardCollectProcessStatus(), IdCardCollectProcessStatus.ALL.value())) {
                // 调用影印件上传接口
                String idCardResult = supplierCompanyService.idCardCollect(supplierCompanyVO, supplierId);
                if (!Objects.equals(idCardResult, AllinpayConstant.ALLINPAY_AUDIT_SUCCESS)) {
                    logger.error("供应商设置企业信息回调，影印件上传审核失败：{}", idCardResult);
                }
            }
        }
        supplierDetailService.removeCacheBySupplierId(supplierId);
    }

    private void authFail(String bizUserId, String failReason, Long supplierId) {
        SupplierCompanyAuditingVO supplierCompanyAuditingVO = getLatestAuditingBySupplierId(supplierId);
        if (Objects.nonNull(supplierCompanyAuditingVO) && Objects.equals(supplierCompanyAuditingVO.getStatus(), AuditStatus.WAITAUDIT.value())) {
            logger.info("供应商Id{}已经开店，通联审核失败，拒绝供应商的工商申请", supplierId);
            // 如果是已经开店来审核的话要把审核拒绝通过
            audit(new SupplierCompanyAuditingDTO(supplierCompanyAuditingVO.getCompanyAuditingId(), AuditStatus.FAILAUDIT.value(), failReason));
        } else {
            logger.info("供应商Id{}还未开店，通联审核失败，拒绝供应商的开店申请", supplierId);
            // 还未开店，审核失败
            SupplierAuditingVO supplierAuditingVO = supplierAuditingService.getBySupplierId(supplierId);
            SupplierAuditingDTO supplierAuditingDTO = BeanUtil.map(supplierAuditingVO, SupplierAuditingDTO.class);
            supplierAuditingDTO.setStatus(AuditStatus.FAILAUDIT.value());
            supplierAuditingDTO.setRemarks(failReason);
            supplierDetailService.audit(supplierAuditingDTO);
        }
        // 更新审核失败——平台下线
        supplierDetailMapper.updateSupplierAuditFail(supplierId);
        logger.info("供应商id为{}通联审核失败原因：{}", bizUserId, failReason);
    }

    private void checkChangeName(SupplierCompanyVO supplierCompanyDTO, SupplierCompanyVO supplierCompanyVO, Long supplierId) {
        if (!Objects.equals(supplierCompanyVO.getFirmName(), supplierCompanyDTO.getFirmName())) {
            logger.info("通联回调审核供应商{}修改的工商信息成功，修改了企业名称，需要重新签约对公提现协议", supplierId);
            supplierDetailMapper.updateAcctProtocolNoByType(supplierId, SignAcctType.COMPANY.getValue(), null);
        }
        if (!Objects.equals(supplierCompanyVO.getRepresentative(), supplierCompanyDTO.getRepresentative())) {
            logger.info("通联回调审核供应商{}修改的工商信息成功，修改了法人姓名，需要重新签约法人提现协议", supplierId);
            supplierDetailMapper.updateAcctProtocolNoByType(supplierId, SignAcctType.LEGAL_PERSON.getValue(), null);
        }
    }

    private boolean getIsChangeIdCard(SupplierCompanyDTO supplierCompanyDTO, SupplierCompanyVO supplierCompanyVO) {
        if (Objects.isNull(supplierCompanyVO)) {
            return true;
        }
        if (!Objects.equals(supplierCompanyVO.getBusinessLicense(), supplierCompanyDTO.getBusinessLicense())) {
            return true;
        }
        if (!Objects.equals(supplierCompanyVO.getIdentityCardFront(), supplierCompanyDTO.getIdentityCardFront())) {
            return true;
        }
        return !Objects.equals(supplierCompanyVO.getIdentityCardLater(), supplierCompanyDTO.getIdentityCardLater());
    }

    private boolean getIsChangeInfo(SupplierCompanyDTO supplierCompanyDTO, SupplierCompanyVO supplierCompanyVO) {
        if (Objects.isNull(supplierCompanyVO)) {
            return true;
        }
        if (!Objects.equals(supplierCompanyDTO.getResidence(), supplierCompanyVO.getResidence())) {
            return true;
        }
        return !Objects.equals(supplierCompanyDTO.getLegalPhone(), supplierCompanyVO.getLegalPhone());
    }

    private boolean getIsChangeKeyInfo(SupplierCompanyDTO supplierCompanyDTO, SupplierCompanyVO supplierCompanyVO) {
        if (Objects.isNull(supplierCompanyVO)) {
            return true;
        }
        if (!Objects.equals(supplierCompanyDTO.getFirmName(), supplierCompanyVO.getFirmName())) {
            return true;
        }
        if (!Objects.equals(supplierCompanyDTO.getRepresentative(), supplierCompanyVO.getRepresentative())) {
            return true;
        }
        return !Objects.equals(supplierCompanyDTO.getLegalIds(), supplierCompanyVO.getLegalIds());
    }

    /**
     * 生成供应商申请记录
     *
     * @param supplierCompany
     */
    private Long generateAuditInformation(SupplierCompany supplierCompany) {
        SupplierCompanyAuditing supplierCompanyAuditing = new SupplierCompanyAuditing();
        supplierCompanyAuditing.setSupplierId(supplierCompany.getSupplierId());
        supplierCompanyAuditing.setSupplierCompanyId(supplierCompany.getSupplierCompanyId());
        supplierCompanyAuditing.setUserId(AuthUserContext.get().getUserId());
        supplierCompanyAuditing.setStatus(AuditStatus.WAITAUDIT.value());
        companyAuditingMapper.insert(supplierCompanyAuditing);
        return supplierCompanyAuditing.getCompanyAuditingId();
    }
}
