package com.jwipc.contract.impl;

import cn.hutool.core.lang.Pair;
import com.jwipc.constant.SignProcessType;
import com.jwipc.contract.base.ContractSendSignClientDTO;
import com.jwipc.contract.base.ContractSendSignResultClientDTO;
import com.jwipc.contract.base.DigitalSealConfigClientDTO;
import com.jwipc.contract.client.ContractSignClient;
import com.jwipc.contract.convert.ContractClientConvert;
import com.jwipc.contract.dto.ContractSendSignDTO;
import com.jwipc.contract.logic.ContractLogic;
import com.jwipc.api.esign.SignUtils;
import com.jwipc.contract.model.po.DigitalSealConfigDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author shenyang.ou
 * @date 2024/4/29 16:19
 * @desc
 */
@Service
@RequiredArgsConstructor
public class ContractSignClientImpl implements ContractSignClient {
    private final ContractLogic contractLogic;
    private final ContractClientConvert contractClientConvert;

    /**
     * 上传盖章附件
     * @param file 附件
     * @param fileId 附件id
     * @return 返回fileKey
     */
    @Override
    public String uploadSignFile(MultipartFile file, String fileId) {
        return contractLogic.uploadSignFile(file,fileId);
    }

    /**
     * 一键发起签署
     *
     * @param contractSendSignClientDTO 签署信息
     * @param signProcessType          签署流程业务枚举
     * @return 返回fileKey
     */
    @Override
    public ContractSendSignResultClientDTO sendSignClient(ContractSendSignClientDTO contractSendSignClientDTO, SignProcessType signProcessType) {
        ContractSendSignDTO contractSendSignDTO = contractClientConvert.clientDTO2DTO(contractSendSignClientDTO);
        return contractClientConvert.resultClientDTO2DTO(contractLogic.sendSignClient(contractSendSignDTO,signProcessType));
    }

    @Override
    public DigitalSealConfigClientDTO selectESignDetailClient(String companyCode, String sealType) {
        DigitalSealConfigDTO digitalSealConfigDTO = contractLogic.selectESignDetailClient(companyCode,sealType);
        return contractClientConvert.digitalSealConfigDTO2CDTO(digitalSealConfigDTO);
    }

    @Override
    public String getFile(String fileKey, Pair<String, String> pair, String ServerIP, String api, String ProjectSecret1, String ProjectID1) {
        return SignUtils.getFile(fileKey, pair, ServerIP, "/file/v1/getDownloadUrl",
                ProjectSecret1, ProjectID1);
    }
}
