package com.inkasso.factoring.financingmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.factoring.common.util.ElectronicSignatureUtil;
import com.inkasso.factoring.file.entity.FileStorageDO;
import com.inkasso.factoring.file.enums.FileTypeEnum;
import com.inkasso.factoring.file.service.FileService;
import com.inkasso.factoring.financingmanagement.dao.SysCustomerMapper;
import com.inkasso.factoring.financingmanagement.entity.ContractElectronicCustomerInfo;
import com.inkasso.factoring.financingmanagement.entity.ContractElectronicInfo;
import com.inkasso.factoring.financingmanagement.entity.SysCustomer;
import com.inkasso.factoring.financingmanagement.service.IContractElectronicCustomerInfoService;
import com.inkasso.factoring.financingmanagement.service.IContractElectronicInfoService;
import com.inkasso.factoring.financingmanagement.service.ISysCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 企业信息管理表 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2022-06-20
 */
@Service
public class SysCustomerServiceImpl extends ServiceImpl<SysCustomerMapper, SysCustomer> implements ISysCustomerService {

    @Autowired
    private IContractElectronicInfoService contractElectronicInfoService;
    @Autowired
    private IContractElectronicCustomerInfoService contractElectronicCustomerInfoService;
    @Autowired
    private FileService fileService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doAuthBack(String customerId) {
        SysCustomer sysCustomer = getOne(new QueryWrapper<SysCustomer>().eq("fdd_customer_id", customerId));
        sysCustomer.setAuthStatus(true);
        updateById(sysCustomer);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doSignBack(String contractId, String transactionId) {
        SysCustomer sysCustomer = getOne(new QueryWrapper<SysCustomer>().eq("transaction_id", transactionId));
        SysCustomer certificateSysCustomer = getOne(new QueryWrapper<SysCustomer>().eq("certificate_transaction_id", transactionId));
        ContractElectronicInfo contractElectronicInfo = contractElectronicInfoService.getOne(new QueryWrapper<ContractElectronicInfo>().eq("auto_sign_transaction_id", transactionId));
        ContractElectronicCustomerInfo contractElectronicCustomerInfo = contractElectronicCustomerInfoService.getOne(new QueryWrapper<ContractElectronicCustomerInfo>().eq("transaction_id", transactionId));
        //合同下载
        String contDownUrl = ElectronicSignatureUtil.downloadContract(contractId);
        FileStorageDO signedFile = fileService.addFileStorageWithBelongAndName(contractId, transactionId, FileTypeEnum.CONTRACT_SIGNED_FILE, contDownUrl);
        //开启自动签署回调
        if (sysCustomer != null) {
            //合同归档
            ElectronicSignatureUtil.contractFiled(contractId);
            //电子文件签署线上出证下载
            String reportDownUrl = ElectronicSignatureUtil.complianceContractReport(contractId);
            FileStorageDO reportFile = fileService.addFileStorageWithBelongAndName(contractId, transactionId, FileTypeEnum.CONTRACT_REPORT_FIlE, reportDownUrl);
            //保存开启自动签的合同
            sysCustomer.setFileId(signedFile.getId());
            //获取最新签署信息，更新签署状态
            sysCustomer.setAutoSignStatus(1);
            updateById(sysCustomer);
            //保存出证文件
            fileService.updateBusinessId(reportFile.getId(), sysCustomer.getId());
            //长银自动签回调
        } else if (contractElectronicInfo != null) {
            //保存合同
            contractElectronicInfo.setFileId(signedFile.getId());
            contractElectronicInfoService.updateById(contractElectronicInfo);
            //其他公司自动、手动签署回调
        } else if (contractElectronicCustomerInfo != null) {
            //更新电子签保存的文件
            ContractElectronicInfo electronicInfo = contractElectronicInfoService.getById(contractElectronicCustomerInfo.getContractElectronicInfoId());
            electronicInfo.setFileId(signedFile.getId());
            contractElectronicInfoService.updateById(electronicInfo);
            //更新签署状态
            contractElectronicCustomerInfo.setSignStatus(true);
            //保存合同
            contractElectronicCustomerInfo.setFileId(signedFile.getId());
            contractElectronicCustomerInfoService.updateById(contractElectronicCustomerInfo);
            //下载出证统一归档
        } else if (certificateSysCustomer != null) {
            //授权书归档
            ElectronicSignatureUtil.contractFiled(contractId);
            //电子文件签署线上出证下载
            String reportDownUrl = ElectronicSignatureUtil.complianceContractReport(contractId);
            FileStorageDO certificateFile = fileService.addFileStorageWithBelongAndName(contractId, FileTypeEnum.CONTRACT_CERTIFICATE_REPORT_FILE, reportDownUrl);
            certificateSysCustomer.setCertificateSupplementReportFile(certificateFile.getId());
            //获取最新签署信息，更新签署状态
            certificateSysCustomer.setCertificateSignStatus(1);
            //保存出证文件
            fileService.updateBusinessId(certificateFile.getId(), certificateSysCustomer.getId());

            certificateSysCustomer.setCertificateFileId(signedFile.getId());
            certificateSysCustomer.setCertificateSupplementReportFile(certificateFile.getId());
            updateById(certificateSysCustomer);
        }
    }
}
