package com.sifang.modules.customer.Service;

import com.sifang.modules.alioss.AliyunOSSClientUtil;
import com.sifang.modules.customer.constant.CustomerEntryConstants;
import com.sifang.modules.customer.dao.ContractManagementDao;
import com.sifang.modules.customer.dao.CrmCustomerIncomingApplicationDao;
import com.sifang.modules.customer.entity.CrmCustomerContractManagement;
import com.sifang.modules.customer.entity.CrmCustomerIncomingApplication;
import com.sifang.modules.esign.constants.ESignConstants;
import com.sifang.modules.partner.service.GuangfaOperatingService;
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 org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

@Service
public class GuangFaContractService {


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

    @Autowired
    private GuangfaOperatingService guangfaOperatingService;

    @Autowired
    private CrmCustomerIncomingApplicationDao incomingApplicationDao;

    @Autowired
    private ContractManagementDao contractManagementDao;

    @Autowired
    private CustomerEntryMsgService msgService;


    //获取合同签约
    public String getSignContractUrl(String entryId) {
        CrmCustomerIncomingApplication application = incomingApplicationDao.getByEntryId(entryId);
        String inputPieceId=application.getInputPieceId();
        String partnerId = "03";//广发资方id
        String signContract = guangfaOperatingService.guangFaSignContract(partnerId, inputPieceId);
        return signContract;
    }
    @Transactional(rollbackFor = Exception.class)
    public String terminateContract(String entryId, String reason, Operator operator) {
        CrmCustomerIncomingApplication application = incomingApplicationDao.getByEntryId(entryId);
        String partnerId = "03";//广发资方id
        String inputPieceId = application.getInputPieceId();
        CrmCustomerContractManagement management = contractManagementDao.getContractByEntryId(entryId);
        management.setStatus(ESignConstants.GF_CONTRACT_STATUS_ZFZ);
        management.setContractUrl(null);
        contractManagementDao.save(management);
        msgService.saveCustomerEntryMsg(operator, entryId, CustomerEntryConstants.SIGN_REVOKE, reason, null);
        return guangfaOperatingService.guangFaTerminateContract(partnerId, inputPieceId, reason);
    }


    public String doSealContract(String entryId) {
        CrmCustomerIncomingApplication application = incomingApplicationDao.getByEntryId(entryId);
        String partnerId = "03";//广发资方id
        String inputPieceId = application.getInputPieceId();
        return guangfaOperatingService.guangFaDoSealContract(partnerId, inputPieceId);
    }

    public String preview(String entryId) throws IOException {
        CrmCustomerContractManagement contract = contractManagementDao.getContractByEntryId(entryId);
        if(StringUtil.isBlank(contract.getContractUrl())) {
            String partnerId = "03";//广发资方id
            String contractNo = contract.getContractNumber();
            String url = guangfaOperatingService.guangFaPreviewInpContract(partnerId, contractNo);
            if(contract.getStatus().equals(ESignConstants.GF_CONTRACT_STATUS_YWC)) {
                // 转 存
                logger.info("合同文本获取 getContractDownloadUrl -------fileMessage:{} ", url);
                String path = AliyunOSSClientUtil.saveUrlWeb(url);
                logger.info(">>>>>>>>>>>>>getContractDownloadUrl  path:{}", path);
                contract.setContractUrl(path);
                contractManagementDao.save(contract);
            }
            return url;
        }
        return contract.getContractUrl();
    }
    public String download(String entryId) throws IOException {
        CrmCustomerContractManagement contract = contractManagementDao.getContractByEntryId(entryId);
        if(StringUtil.isBlank(contract.getContractUrl())) {
            String partnerId = "03";//广发资方id
            String contractNo = contract.getContractNumber();
            String url = guangfaOperatingService.guangFaPreviewInpContract(partnerId, contractNo);
            // 转 存
            logger.info("合同文本获取 getContractDownloadUrl -------fileMessage:{} ", url);
            String path = AliyunOSSClientUtil.saveUrlWeb(url);
            logger.info(">>>>>>>>>>>>>getContractDownloadUrl  path:{}", path);
            contract.setContractUrl(path);
            contractManagementDao.save(contract);
            return path;
        }
        return contract.getContractUrl();
    }
    @Transactional(rollbackFor = Exception.class)
    public Result reGenerateContract(String entryId){
        if (StringUtil.isBlank(entryId)) {
            throw new IllegalArgumentException("entryId不能为空");
        }
        CrmCustomerContractManagement contract = contractManagementDao.getContractByEntryId(entryId);
        contract.setStatus("0");
        CrmCustomerContractManagement newContract = new CrmCustomerContractManagement();
        newContract.setStatus("6");
        newContract.setEntryId(entryId);
        newContract.setPowerStationCode(contract.getPowerStationCode());
        newContract.setTraderId(contract.getTraderId());
        contractManagementDao.save(contract);
        contractManagementDao.save(newContract);
        return Result.ok();
    }
}
