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.auth.response.ResponseJson;
import com.inkasso.factoring.electronicSignature.entity.ElectronicSignaturePersonal;
import com.inkasso.factoring.electronicSignature.service.IElectronicSignaturePersonalService;
import com.inkasso.factoring.salesManagement.dao.CSalesSupplementSignConfirmationMapper;
import com.inkasso.factoring.salesManagement.entity.CSalesContractSupplementInfo;
import com.inkasso.factoring.salesManagement.entity.CSalesSupplementSignConfirmation;
import com.inkasso.factoring.salesManagement.entity.CSalesSupplementSignConfirmationElectronicInfo;
import com.inkasso.factoring.salesManagement.service.ICSalesContractManagementService;
import com.inkasso.factoring.salesManagement.service.ICSalesContractSupplementInfoService;
import com.inkasso.factoring.salesManagement.service.ICSalesSupplementSignConfirmationElectronicInfoService;
import com.inkasso.factoring.salesManagement.service.ICSalesSupplementSignConfirmationService;
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 CSalesSupplementSignConfirmationServiceImpl extends ServiceImpl<CSalesSupplementSignConfirmationMapper, CSalesSupplementSignConfirmation> implements ICSalesSupplementSignConfirmationService {

    @Autowired
    private ICSalesContractManagementService salesContractManagementService;
    @Autowired
    private ICSalesContractSupplementInfoService salesContractSupplementInfoService;
    @Autowired
    private ICSalesSupplementSignConfirmationElectronicInfoService supplementSignConfirmationElectronicInfoService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private IElectronicSignaturePersonalService electronicSignaturePersonalService;
    @Value("${trade.salesContractChangeUploadContractUrl}")
    private String salesContractChangeUploadContractUrl;
    @Value(("${trade.salesContractChangeDoSignUrl}"))
    private String salesContractChangeDoSignUrl;
    @Value("${trade.salesContractChangeSignViewUrl}")
    private String salesContractChangeSignViewUrl;

    @Override
    public CSalesSupplementSignConfirmation add(Long cSalesContractManagementId) {

        //获取签署确认信息
        CSalesContractSupplementInfo supplementInfo = salesContractSupplementInfoService.getOne(new QueryWrapper<CSalesContractSupplementInfo>()
                .eq("c_sales_contract_management_id", cSalesContractManagementId).orderByDesc("gmt_create").last("limit 1"));
        CSalesSupplementSignConfirmation supplementSignConfirmation = getOne(new QueryWrapper<CSalesSupplementSignConfirmation>()
                .eq("c_sales_contract_supplement_info_id", supplementInfo.getId()));

        //电子签信息
        if (supplementSignConfirmation != null) {
            List<CSalesSupplementSignConfirmationElectronicInfo> signElectronicInfos = supplementSignConfirmationElectronicInfoService
                    .list(new QueryWrapper<CSalesSupplementSignConfirmationElectronicInfo>()
                            .eq("c_sales_supplement_sign_confirmation_id", supplementSignConfirmation.getId()));
            CSalesSupplementSignConfirmationElectronicInfo confirmationElectronicInfo = signElectronicInfos.get(0);

            //签署人信息
            ElectronicSignaturePersonal signaturePersonal = electronicSignaturePersonalService.getById(confirmationElectronicInfo.getElectronicSignaturePersonalId());
            supplementSignConfirmation.setElectronicSignaturePersonal(signaturePersonal);
            //误删
            supplementSignConfirmation.setSignConfirmationElectronicInfos(signElectronicInfos);

            //上传文件 处理
            if (confirmationElectronicInfo != null && confirmationElectronicInfo.getSignStatus() == 0) {
                restTemplate.postForObject(salesContractChangeUploadContractUrl, supplementSignConfirmation, ResponseJson.class);
                supplementSignConfirmation = getOne(new QueryWrapper<CSalesSupplementSignConfirmation>()
                        .eq("c_sales_contract_supplement_info_id", supplementInfo.getId()));
                //签署人信息
                supplementSignConfirmation.setElectronicSignaturePersonal(signaturePersonal);
            }
            supplementSignConfirmation.setSignConfirmationElectronicInfos(signElectronicInfos);

        }

        return supplementSignConfirmation;
    }

    @Override
    public void doAdd(CSalesSupplementSignConfirmation supplementSignConfirmation) {

        try {
            restTemplate.postForObject(salesContractChangeDoSignUrl, supplementSignConfirmation, 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());
        }

    }

    @Override
    public Object getView(Long id) {
        //获取签署确认信息
        CSalesContractSupplementInfo supplementInfo = salesContractSupplementInfoService
                .getOne(new QueryWrapper<CSalesContractSupplementInfo>().eq("id", id));
        CSalesSupplementSignConfirmation supplementSignConfirmation = null;

        if (supplementInfo != null) {
            //获取签署确认信息
            supplementSignConfirmation = getOne(new QueryWrapper<CSalesSupplementSignConfirmation>()
                    .eq("c_sales_contract_supplement_info_id", supplementInfo.getId()));
            if (supplementSignConfirmation == null || supplementSignConfirmation.getSignConclusion() == null) {
                return null;
            }
            //电子签信息
            ResponseJson responseJson = restTemplate.getForObject(salesContractChangeSignViewUrl + "?id=" +
                    supplementSignConfirmation.getId(), ResponseJson.class);

            return responseJson.getData();
        } else {
            return null;
        }

    }
}
