package com.sifang.modules.smallprogram.GFContractController;

import com.sifang.modules.customer.Service.CustomerContractManagementService;
import com.sifang.modules.customer.Service.GuangFaContractService;
import com.sifang.modules.customer.dao.ContractManagementDao;
import com.sifang.modules.customer.entity.CrmCustomerContractManagement;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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

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

    @Autowired
    private ContractManagementDao contractManagementDao;

    @Autowired
    private GuangFaContractService guangFaContractService;

    //生成链接
    @ResponseBody
    @RequestMapping(value = "getSignContractUrl")
    public Result getSignContractUrl(String entryId) {
        try {
            String contractUrl = guangFaContractService.getSignContractUrl(entryId);
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("contractUrl", contractUrl);
            return Result.success(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     *
     *  作定时查询合同状态任务，对应接口：查询合同-广发， 合同状态查询 会获取到合同编号
     * 预览  要求先获取合同编号，对应接口：预览合同-广发
     */
    @ResponseBody
    @RequestMapping(value = "preview")
    public Result preview(String entryId) {
        try {
            String res = guangFaContractService.preview(entryId);
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("url", res);
            return Result.success(resultMap);
        } catch (Exception e) {
            logger.info(">>>>>>>>>>>preview e:{}",e);
            return Result.fail("获取合同地址失败,"+e.getMessage());
        }
    }

    /**
     * 合同落章  对应接口：项目公司合同落章
     * @param entryId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "doSealContract")
    public Result doSealContract(String entryId){
        try {
            String res = guangFaContractService.doSealContract(entryId);
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("res", res);
            return Result.success(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 作废  对应接口：终止合同-广发/顶好
     *
     * @param entryId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "terminateContract")
    public Result terminateContract(String entryId,String reason){
        if (StringUtil.isBlank(reason)){
            return Result.fail("作废原因不能为空!");
        }
        try {
            User user = UserUtils.getUser();
            Operator operator = Operator.of(user);
            String res = guangFaContractService.terminateContract(entryId,reason,operator);
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("res", res);
            return Result.success(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    //下载地址
    //获取合同地址
    @ResponseBody
    @RequestMapping(value = "getContractUrl")
    public Result getContractUrl(String entryId) throws IOException {
        String url = guangFaContractService.download(entryId);
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("url", url);
        return Result.success(resultMap);
    }
    //针对已完成作废的合同，进行合同重新生成操作
    @ResponseBody
    @RequestMapping(value = "reGenerateContract")
    public Result reGenerateContract(String entryIds) {
        if (StringUtil.isBlank(entryIds)){
            return Result.fail("entryIds不能为空");
        }
        String[] entryIdList = entryIds.split(",");
        for (String id : entryIdList) {
            guangFaContractService.reGenerateContract(id);
        }
        return Result.ok();
    }
}
