package com.sunxung.factoring.service.acceptancesheet.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.acceptance.AcceptanceSignConfirmationElectronicInfo;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.acceptancesheet.AcceptanceSignConfirmationElectronicInfoMapper;
import com.sunxung.factoring.service.acceptancesheet.IAcceptanceSignConfirmationElectronicInfoService;
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-06-07
 */
@Service
public class AcceptanceSignConfirmationElectronicInfoServiceImpl extends ServiceImpl<AcceptanceSignConfirmationElectronicInfoMapper, AcceptanceSignConfirmationElectronicInfo> implements IAcceptanceSignConfirmationElectronicInfoService {

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public synchronized  void doSignCallBack(AcceptanceSignConfirmationElectronicInfo acceptanceSignConfirmationElectronicInfo) {
       // String contractId = acceptanceSignConfirmationElectronicInfo.getContractId();
        AcceptanceSignConfirmationElectronicInfo byId = this.getById(
            acceptanceSignConfirmationElectronicInfo.getId());
        String contractId = byId.getContractId();

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

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

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

            byId.setCallBack(1);
            byId.setSignStatus(1);

            byId.setBackFileId(backFile.getId());
            byId.setReportFileId(reportFile.getId());
            updateById(byId);
        }
    }
}
