package com.sifang.modules.smallprogram.huaRong;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.CustomerContractManagementService;
import com.sifang.modules.customer.Service.HuaRongContractService;
import com.sifang.modules.customer.dao.ContractManagementDao;
import com.sifang.modules.customer.entity.CrmCustomerContractManagement;
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.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/main/HuaRongContract")
public class HuaRongContractController extends BaseExceptionController {


    @Autowired
    private ContractManagementDao contractManagementDao;

    @Autowired
    private HuaRongContractService contractService;

    @Autowired
    private CustomerContractManagementService customerContractManagementService;

    //上传合同
    @ResponseBody
    @RequestMapping(value = "submitContract")
    public Result submitContract(@RequestBody @Validated SubmitContractReq req) {
        try {
            return contractService.submitContract(req);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 获取指定合同
     */
    @ResponseBody
    @RequestMapping(value = "getContract")
    public Result getContract(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        Record record=customerContractManagementService.getOneById(map);
        return Result.success(record);
    }


    //下载地址
    //获取合同地址
    @ResponseBody
    @RequestMapping(value = "getContractUrl")
    public Result getContractUrl(String entryId){
        CrmCustomerContractManagement management=contractManagementDao.getContractByEntryId(entryId);
        Map<String,String> resultMap=new HashMap<>();
        resultMap.put("url",management.getContractUrl());
        return Result.success(resultMap);
    }


    /**
     * 获取合同签约链接
     *
     * @param entryId 电站id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getContractSignUrl")
    public Result getContractSignUrl(String entryId) {
        try {
            String contractUrl = contractService.getContractSignUrl(entryId);
            Map<String,Object> resultMap=new HashMap<>();
            resultMap.put("contractUrl",contractUrl);
            return Result.success(resultMap);
        } catch (EsignDemoException e) {
            e.printStackTrace();
            return Result.fail("获取合同签约链接失败:"+e.getMessage());
        }catch (Exception w) {
            w.printStackTrace();
            return Result.fail("获取合同签约链接失败!");
        }
    }

    /**
     * 发起合同
     * @param entryId  电站id
     */
    @ResponseBody
    @RequestMapping(value = "createContract")
    public Result createContract(String entryId,HttpServletRequest request){
        Operator operator = getOperatorBy(request);
        contractService.createContract(entryId,operator);
        return Result.ok();
    }

    /**
     * 预览合同
     * @param entryId  电站id
     */
    @ResponseBody
    @RequestMapping(value = "previewContract")
    public Result previewContract(String entryId){
        CrmCustomerContractManagement contractManagement=contractManagementDao.getContractByEntryId(entryId);
        try {
            String url= ESignSignService.filePreviewUrl(contractManagement.getSignFlowId(),contractManagement.getFileId());
            Map<String,String> resultMap=new HashMap<>();
            resultMap.put("url",url);
            return Result.success(resultMap);
        } catch (EsignDemoException e) {
            e.printStackTrace();
            return Result.fail("获取合同预览地址失败！");
        }
    }

}
