package com.sifang.modules.smallprogram.yuexiu;


import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.constant.CustomerEntryConstants;
import com.sifang.modules.customer.dao.CustomerEntryDao;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.esign.service.RedSunAskAuthContractService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.utils.IdGen;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.hibernate.SQLQuery;
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}/main/EsignAuth")
public class EsignAuthContractController extends BaseExceptionController {

    @Autowired
    private CustomerEntryDao customerEntryDao;

    @Autowired
    private RedSunAskAuthContractService contractService;

    @Autowired
    private RedSunAskAuthContractService askAuthContractService;


    //授权
    @ResponseBody
    @RequestMapping(value = "toAuthContract")
    public Result toAuthContract(String entryId){
        CustomerEntry entry=customerEntryDao.get(entryId);
        String idCard=entry.getIdCode();
        String mobile=entry.getMobile();
        String address=entry.getProvince()+entry.getCity()+entry.getArea()+entry.getAddress();
        //获取电站业主授权信息
        Record customerYxCondition = customerEntryDao.findCustomerYxCondition(idCard,entry.getPartnerId());
        if (StringUtil.equalsAny(customerYxCondition.getStr("preaudit"),CustomerEntryConstants.CREDIT_WAIT,CustomerEntryConstants.CREDIT_REJECT)) {
            if (CustomerEntryConstants.AUTHORIZATION_WAIT.equals(entry.getAuthorization())) { //创建授权合同
                String signFlowId = contractService.createAskAuthContract(idCard,mobile,address,entry.getPartnerId());
                Db.update("update crm_customer_yx_condition set authorization='2',sign_flow_id=? where id=? ",signFlowId,customerYxCondition.getStr("id"));
            }
            //修改电站授权状态
            Db.update(" UPDATE `crm_customer_entry` SET authorization='2' WHERE  id_code=? and partner_id=?  ",idCard,entry.getPartnerId());
            return Result.ok();
        }
        return Result.fail("电站状态不支持授权!");
    }

    //获取授权的合同签署链接  /main/EsignAuth/getEsignUrl?idCode=
    @ResponseBody
    @RequestMapping(value = "getEsignUrl")
    public Result getEsignUrl(String idCode,String mobile,String patnerId){
        Map<String,String> resultMap=askAuthContractService.getEsignUrl(idCode,mobile,patnerId);
        return Result.success(resultMap);
    }

    //因为修改了手机号，重新发起授权合同  参数：entryId 电站id,idCode 身份证号，mobile:手机号  userId 用户id
    @ResponseBody
    @RequestMapping(value = "resetMobileVsAuth")
    public Result resetMobileVsAuth(String idCode, String mobile,String entryId, HttpServletRequest request){
        Operator operator = getOperatorBy(request);
        askAuthContractService.resetMobileVsAuth(idCode,mobile,entryId,operator);
        return Result.ok("授权合同已重新发送!");
    }

}
