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.CSalesSignConfirmationElectronicInfo;
import com.sunxung.factoring.entity.signconfirmation.SignConfirmationElectronicInfo;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.salescontractmanagement.CSalesSignConfirmationElectronicInfoMapper;
import com.sunxung.factoring.service.salescontractmanagement.ICSalesSignConfirmationElectronicInfoService;
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 CSalesSignConfirmationElectronicInfoServiceImpl extends ServiceImpl<CSalesSignConfirmationElectronicInfoMapper,
        CSalesSignConfirmationElectronicInfo> implements ICSalesSignConfirmationElectronicInfoService {

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public void doSignCallBack(CSalesSignConfirmationElectronicInfo signConfirmationElectronicInfo) {
        String contractId = signConfirmationElectronicInfo.getContractId();

        if (signConfirmationElectronicInfo.getCallBack() == 0){
            //合同下载
            String contDownUrl = ElectronicSignatureUtil.downloadContract(contractId);
            FileStorage backFile = fileStorageService.addFileStorageWithBelongAndName(contractId, FileModuleDict.ChildEnum.SIGN_CONFIRM_BACK_FILE, contDownUrl, null);
            fileStorageService.saveAttachmentRefByFileStorage(backFile, FileModuleDict.ChildEnum.SIGN_CONFIRM_BACK_FILE, signConfirmationElectronicInfo.getId());

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

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

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

            updateById(signConfirmationElectronicInfo);
        }

    }
}
