package com.sifang.modules.customer.Service;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.dao.ContractManagementDao;
import com.sifang.modules.customer.entity.CrmCustomerContractManagement;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.customer.form.SubmitContractReq;
import com.sifang.modules.esign.cn.tsign.hz.exception.EsignDemoException;
import com.sifang.modules.esign.constants.ESignConstants;
import com.sifang.modules.esign.service.ESignSignService;
import com.sifang.modules.esign.service.PartnerContractESignSignService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.entity.Operator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class HuaRongContractService {

    Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private ContractManagementDao contractManagementDao;

    @Autowired
    private CustomerEntryService  customerEntryService;

    @Autowired
    PartnerContractESignSignService partnerContractESignSignService;

    public Result submitContract(SubmitContractReq req) {
        CustomerEntry customerEntry = customerEntryService.getById(req.getId());

        CrmCustomerContractManagement contractManagement = contractManagementDao.getContractByEntryId(customerEntry.getId());
        if (contractManagement == null) {
            return Result.fail("未查询到合同信息");
        }
        contractManagement.setContractNumber(req.getContractNumber());
        contractManagement.setContractUrl(req.getContractUrl());
        contractManagement.setTenantSignTime(req.getTenantSignTime());
        contractManagement.setStatus(ESignConstants.GF_CONTRACT_STATUS_YSC);
        contractManagementDao.save(contractManagement);

        customerEntry.setSignStatus(ESignConstants.GF_CONTRACT_STATUS_YSC);
        customerEntryService.saveByUpdate(customerEntry);
        return Result.ok();
    }


    //获取签约链接
    public String getContractSignUrl(String entryId) throws EsignDemoException {
        CustomerEntry customerEntry = customerEntryService.getById(entryId);
        CrmCustomerContractManagement contractManagement = contractManagementDao.getContractByEntryId(customerEntry.getId());
        String contractUrl = partnerContractESignSignService.getSignUrl(contractManagement.getId());
        return contractUrl;
    }

    //变更导致的，删除原合同，重新发起合同
    public void changeCauseContractSignAgain(String entryId, String partnersContractNumber, Operator operator, String content, List<Record> componentList){
        logger.info(">>>>>changeCauseContractSignAgain  entryId:{},partnersContractNumber:{},content:{}",entryId,partnersContractNumber,content);
        CustomerEntry customerEntry = customerEntryService.getById(entryId);
        CrmCustomerContractManagement contractManagement = contractManagementDao.getContractByEntryId(customerEntry.getId());
        if(contractManagement==null || StringUtil.isBlank(contractManagement.getSignFlowId())){
               return;
        }
        //撤销之前的线上合同
        String signFlowId=contractManagement.getSignFlowId();
        try {
            ESignSignService.revoke(signFlowId);
        } catch (EsignDemoException e) {
            e.printStackTrace();
            logger.error("变更导致的，撤回原合同的时候，撤销合同失败 e:{}",e);
        }
        contractManagement.setStatus("0");
        contractManagement.setReason("变更导致合同重签");
        contractManagement.setUpdateTime(new Date());
        contractManagementDao.save(contractManagement);
        customerEntry.setSignStatus("0");
        partnerContractESignSignService.InviteSign(partnersContractNumber,customerEntry,operator,content,componentList,null);
        logger.info(">>>>changeCauseContractSignAgain  华融变更引起了合同重签");
    }

    //发起合同
    public void createContract(String entryId,Operator operator){
        CrmCustomerContractManagement contractManagement = contractManagementDao.getContract(entryId);
        partnerContractESignSignService.createHuaRongContract(contractManagement.getId(),operator);
    }
}
