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

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.adapter.exception.BusinessException;
import com.inkasso.adapter.util.CodeUtil;
import com.inkasso.adapter.util.CollectionUtil;
import com.inkasso.factoring.common.entity.ResponseJson;
import com.inkasso.factoring.electronicSignature.entity.ElectronicSignaturePersonal;
import com.inkasso.factoring.electronicSignature.service.IElectronicSignaturePersonalService;
import com.inkasso.factoring.salesManagement.dao.CSalesSignConfirmationMapper;
import com.inkasso.factoring.salesManagement.entity.CSalesSignConfirmation;
import com.inkasso.factoring.salesManagement.entity.CSalesSignConfirmationElectronicInfo;
import com.inkasso.factoring.salesManagement.service.ICSalesContractPersonInfoService;
import com.inkasso.factoring.salesManagement.service.ICSalesSignConfirmationElectronicInfoService;
import com.inkasso.factoring.salesManagement.service.ICSalesSignConfirmationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * <p>
 * 销售合同销售-签署确认表 服务实现类
 * </p>
 *
 * @author yafan
 * @since 2023-11-28
 */
@Service
public class CSalesSignConfirmationServiceImpl extends ServiceImpl<CSalesSignConfirmationMapper, CSalesSignConfirmation> implements ICSalesSignConfirmationService {

    @Autowired
    private ICSalesSignConfirmationElectronicInfoService signConfirmationElectronicInfoService;
    @Autowired
    private ICSalesContractPersonInfoService salesContractPersonInfoService;
    @Value("${trade.salesContractUploadContractUrl}")
    private String salesContractUploadContractUrl;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private IElectronicSignaturePersonalService electronicSignaturePersonalService;
    @Value("${trade.salesContractDoSignUrl}")
    private String salesContractDoSignUrl;
    @Value("${trade.salesContractSignViewUrl}")
    private String salesContractSignViewUrl;


    @Override
    public CSalesSignConfirmation add(Long cSalesContractManagementId) {

        CSalesSignConfirmation salesSignConfirmation = getOne(new QueryWrapper<CSalesSignConfirmation>()
                .eq("c_sales_contract_management_id", cSalesContractManagementId));

        if (salesSignConfirmation != null) {
            //电子签信息
            List<CSalesSignConfirmationElectronicInfo> signElectronicInfos = signConfirmationElectronicInfoService
                    .list(new QueryWrapper<CSalesSignConfirmationElectronicInfo>().eq("c_sales_sign_confirmation_id", salesSignConfirmation.getId()));

            if (CollectionUtil.isNotEmpty(signElectronicInfos)) {
                CSalesSignConfirmationElectronicInfo confirmationElectronicInfo = signElectronicInfos.get(0);
                ElectronicSignaturePersonal signaturePersonal = electronicSignaturePersonalService.getById(confirmationElectronicInfo.getElectronicSignaturePersonalId());
                //B端接口用到数据
                salesSignConfirmation.setElectronicSignaturePersonal(signaturePersonal);
                salesSignConfirmation.setSignConfirmationElectronicInfos(signElectronicInfos);
                //上传文件 处理
                if (confirmationElectronicInfo.getSignStatus() == 0) {
                    restTemplate.postForObject(salesContractUploadContractUrl, salesSignConfirmation, ResponseJson.class);
                    salesSignConfirmation = getOne(new QueryWrapper<CSalesSignConfirmation>()
                            .eq("c_sales_contract_management_id", cSalesContractManagementId));
                }
                salesSignConfirmation.setElectronicSignaturePersonal(signaturePersonal);
                salesSignConfirmation.setSignConfirmationElectronicInfos(signElectronicInfos);
            }
        }

        return salesSignConfirmation;
    }

    @Override
    public void doAdd(CSalesSignConfirmation signConfirmation) {

        try{
            restTemplate.postForObject(salesContractDoSignUrl, signConfirmation, ResponseJson.class);
        }catch (Exception e){
            String result = e.getMessage().substring(7, e.getMessage().length() - 1);
            JSONObject jsonObject = new JSONObject(result);
        }


    }

    @Override
    public Object getView(Long cSalesContractManagementId) {

        //获取签署确认信息
        CSalesSignConfirmation salesSignConfirmation = getOne(new QueryWrapper<CSalesSignConfirmation>()
                .eq("c_sales_contract_management_id", cSalesContractManagementId));

        if (salesSignConfirmation != null) {
            ResponseJson responseJson = restTemplate.getForObject(salesContractSignViewUrl + "?id=" + salesSignConfirmation.getId(), ResponseJson.class);
            return responseJson.getData();
        }else {
            return null;
        }


    }
}
