package com.inkasso.factoring.purchManagement.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.adapter.exception.BusinessException;
import com.inkasso.adapter.util.CodeUtil;
import com.inkasso.factoring.common.util.ElectronicSignatureUtil;
import com.inkasso.factoring.creditmanagement.entity.Enterprise;
import com.inkasso.factoring.electronicSignature.entity.FddSignData;
import com.inkasso.factoring.file.entity.FileStorageDO;
import com.inkasso.factoring.file.enums.FileTypeEnum;
import com.inkasso.factoring.file.service.FileService;
import com.inkasso.factoring.purchManagement.dao.CPurchContractSignConclusionMapper;
import com.inkasso.factoring.purchManagement.entity.CPurchContractBasicInfo;
import com.inkasso.factoring.purchManagement.entity.CPurchContractElectronicInfo;
import com.inkasso.factoring.purchManagement.entity.CPurchContractSignConclusion;
import com.inkasso.factoring.purchManagement.service.ICPurchContractBasicInfoService;
import com.inkasso.factoring.purchManagement.service.ICPurchContractElectronicInfoService;
import com.inkasso.factoring.purchManagement.service.ICPurchContractManagementService;
import com.inkasso.factoring.purchManagement.service.ICPurchContractSignConclusionService;
import com.inkasso.factoring.tradingPlatform.entity.Platform;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 采购合同 合同的签署结论 服务实现类
 * </p>
 *
 * @author yafan
 * @since 2023-12-13
 */
@Service
public class CPurchContractSignConclusionServiceImpl extends ServiceImpl<CPurchContractSignConclusionMapper, CPurchContractSignConclusion> implements ICPurchContractSignConclusionService {

    @Autowired
    private ICPurchContractManagementService purchContractService;
    @Autowired
    private ICPurchContractElectronicInfoService purchContractElectronicInfoService;
    @Autowired
    private ICPurchContractBasicInfoService purchContractBasicInfoService;
    @Autowired
    private FileService fileService;
    @Autowired
    private RestTemplate restTemplate;
    @Value("${trade.purchContractsignPageUrl}")
    private String purchContractsignPageUrl;
    @Value("${trade.purchContractdoSignUrl}")
    private String purchContractdoSignUrl;


    @Override
    public Object signPage(Long businessKey) {

        JSONObject json = restTemplate.getForObject(purchContractsignPageUrl + "?businessKey=" + businessKey, JSONObject.class);
        if (json.get("code").equals("500")) {
            throw new BusinessException(CodeUtil.FAIL, json.get("msg").toString());
        }
        Object data = json.get("data");
        return data;
    }


    @Override
    public void sign(CPurchContractSignConclusion purchContractSignConclusion) {

        try {
            restTemplate.postForObject(purchContractdoSignUrl, purchContractSignConclusion, String.class);
        } catch (Exception e) {
            String result = e.getMessage().substring(7, e.getMessage().length() - 1);
            JSONObject jsonObject = new JSONObject(result);
            throw new BusinessException(CodeUtil.FAIL, jsonObject.get("msg").toString());
        }


    }

}
