package com.sunxung.factoring.service.salescontractmanagement.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.util.ElectronicSignatureUtil;
import com.sunxung.factoring.dict.impl.FileModuleDict;
import com.sunxung.factoring.entity.salescontractmanagement.CSalesSupplementSignConfirmationElectronicInfo;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.salescontractmanagement.CSalesSupplementSignConfirmationElectronicInfoMapper;
import com.sunxung.factoring.service.salescontractmanagement.ICSalesSupplementSignConfirmationElectronicInfoService;
import com.sunxung.factoring.service.sys.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 销售合同变更-签署确认合同信息 服务实现类
 * </p>
 *
 * @author yafan
 * @since 2023-11-28
 */
@Service
public class CSalesSupplementSignConfirmationElectronicInfoServiceImpl extends ServiceImpl<CSalesSupplementSignConfirmationElectronicInfoMapper,
        CSalesSupplementSignConfirmationElectronicInfo> implements ICSalesSupplementSignConfirmationElectronicInfoService {

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public void doSignCallBack(CSalesSupplementSignConfirmationElectronicInfo signConfirmationElectronicInfo) {
        String contractId = signConfirmationElectronicInfo.getContractId();
        if (signConfirmationElectronicInfo.getCallBack()== 0){
            //合同下载
            String contDownUrl = ElectronicSignatureUtil.downloadContract(contractId);
            FileStorage backFile = fileStorageService.addFileStorageWithBelongAndName(contractId, FileModuleDict.ChildEnum.SALES_SUPPLEMENT_SIGN_CONFIRM_BACK_FILE, contDownUrl, null);
            fileStorageService.saveAttachmentRefByFileStorage(backFile, FileModuleDict.ChildEnum.SALES_SUPPLEMENT_SIGN_CONFIRM_BACK_FILE, signConfirmationElectronicInfo.getId());

            //合同归档
            ElectronicSignatureUtil.contractFiled(contractId);

            //电子文件签署线上出证下载
            String reportDownUrl = ElectronicSignatureUtil.complianceContractReport(contractId);
            FileStorage reportFile = fileStorageService.addFileStorageWithBelongAndName(contractId, FileModuleDict.ChildEnum.SSALES_SUPPLEMENT_SIGN_CONFIRM_BACK_REPORT_FILE, reportDownUrl, null);
            fileStorageService.saveAttachmentRefByFileStorage(reportFile, FileModuleDict.ChildEnum.SSALES_SUPPLEMENT_SIGN_CONFIRM_BACK_REPORT_FILE, signConfirmationElectronicInfo.getId());

            signConfirmationElectronicInfo.setSignStatus(1);
            signConfirmationElectronicInfo.setBackFileId(backFile.getId());
            signConfirmationElectronicInfo.setReportFileId(reportFile.getId());
            signConfirmationElectronicInfo.setCallBack(1);
            updateById(signConfirmationElectronicInfo);
        }
    }
}
