package com.sunxung.factoring.service.signconfirmation.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.signconfirmation.SignConfirmationElectronicInfo;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.signconfirmation.SignConfirmationElectronicInfoMapper;
import com.sunxung.factoring.service.signconfirmation.ISignConfirmationElectronicInfoService;
import com.sunxung.factoring.service.sys.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * --销售合同审核人员信息历史表 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2023-05-16
 */
@Service
public class SignConfirmationElectronicInfoServiceImpl extends ServiceImpl<SignConfirmationElectronicInfoMapper, SignConfirmationElectronicInfo> implements ISignConfirmationElectronicInfoService {

    @Autowired
    private ISignConfirmationElectronicInfoService signConfirmationElectronicInfoService;

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public void doSignCallBack(SignConfirmationElectronicInfo signConfirmationElectronicInfo) {
        String contractId = signConfirmationElectronicInfo.getContractId();
        //合同下载
        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());
        signConfirmationElectronicInfoService.updateById(signConfirmationElectronicInfo);
    }
}
