package com.sifang.modules.customer.controller;

import com.sifang.modules.customer.Service.SignContractService;
import com.sifang.modules.esign.cn.tsign.hz.exception.EsignDemoException;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
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.util.CrmUtils;
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 javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Controller
@RequestMapping(value = "${adminPath}/customerEntry")
public class SignContractController extends BaseExceptionController {

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

    @Autowired
    private SignContractService signContractService;


    /**
     * 邀请签约
     */
    @ResponseBody
    @RequestMapping(value = "inviteSignQr")
    public Result inviteSignQr(HttpServletRequest request) {
        // 电站id
        String id = request.getParameter("id");
        User user = UserUtils.getUser();
        Operator operator = Operator.of(user.getId(), user.getUserName(),user.getUserType());
        try {
            signContractService.inviteSign(id,operator);
            return Result.ok();
        }catch (BusinessException b){
            logger.error(">>>>>>>>>>>>inviteSignQr  b:{}",b);
            return Result.fail("邀请签约失败：" + b.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>>>inviteSignQr  e:{}",e);
            return Result.fail("邀请签约失败：" + e.getMessage());
        }
    }

    /**
     * 平台签约
     */
    @ResponseBody
    @RequestMapping(value = "platformContracting")
    public Result platformContracting(HttpServletRequest request) {
        // 合同id
        String id = request.getParameter("id");
        try {
            Map<String,String> result= signContractService.platformSign(id);
            return Result.success(result);
        }catch (EsignDemoException b){
            logger.error(">>>>>>>>>>>>platformContracting  b:{}",b);
            return Result.fail("邀请签约失败：" + b.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>>>platformContracting  e:{}",e);
            return Result.fail("获取签署链接失败：" + e.getMessage());
        }
    }

    /**
     * 查看合同
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "looKContractDetail")
    public Result looKContractDetail(String id) {
        try {
            String contractUrl = signContractService.fileDownloadUrl(id);
            return Result.success(contractUrl);
        } catch (EsignDemoException e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>>>>>>>>>looKContractDetail  e:{} ", e);
            return Result.fail("获取合同地址失败:"+e.getMessage());
        } catch (IllegalArgumentException c) {
            logger.error(">>>>>>>>>>>>>>>>>>looKContractDetail  c:{} ", c);
            return Result.fail(c.getMessage());
        } catch (Exception w) {
            w.printStackTrace();
            logger.error(">>>>>>>>>>>>>>>>>>looKContractDetail  w:{} ", w);
            return Result.fail("获取合同链接失败!");
        }
    }
}
