package com.yunzhi.marketing.eContract.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fadada.sdk.client.FddClientBase;
import com.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.common.CommConstant;
import com.yunzhi.marketing.eContract.entity.TaCompanySeal;
import com.yunzhi.marketing.eContract.entity.TaContract;
import com.yunzhi.marketing.eContract.service.ITaCompanySealService;
import com.yunzhi.marketing.config.FadadaProperties;
import com.yunzhi.marketing.eContract.service.ITaContractService;
import com.yunzhi.marketing.eContract.entity.TaContractUser;
import com.yunzhi.marketing.eContract.service.ITaContractUserService;
import com.yunzhi.marketing.eContract.service.impl.TaCompanySealUtil;
import com.yunzhi.marketing.service.impl.TaMiniappServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yunzhi.marketing.eContract.service.ITaCompanyService;
import com.yunzhi.marketing.eContract.entity.TaCompany;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 企业认证表  前端控制器
 * </p>
 *
 * @author fxf
 * @since 2020-03-26
 */
@RestController
@RequestMapping("/api")
public class TaCompanyController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaCompanyController.class);

    @Autowired
    private FadadaProperties fadadaProperties;

    @Autowired
    public ITaCompanyService iTaCompanyService;

    @Autowired
    private ITaCompanySealService iTaCompanySealService;

    @Autowired
    private ITaContractService iTaContractService;

    @Autowired
    public ITaContractUserService iTaContractUserService;

    @Autowired
    private TaMiniappServiceImpl taMiniappService;

    @Autowired
    private TaCompanySealUtil taCompanySealUtil;


    /**
     * 条件查询企业列表
     *
     * @param pageNum
     * @param pageSize
     * @param companyName
     * @param companyCode
     * @param certifiedStatus
     * @return
     */
    @RequestMapping(value = "/channel/contract/listCompany", method = RequestMethod.GET)
    public ResponseBean listCompany(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    String companyName, String companyCode, String certifiedStatus) {
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
            IPage<TaCompany> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaCompany> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(companyName), "company_name", companyName);
            queryWrapper.like(StringUtils.isNotBlank(companyCode), "company_code", companyCode);
            queryWrapper.eq(StringUtils.isNotBlank(certifiedStatus), "certified_status", certifiedStatus);
            queryWrapper.eq("status", 4);
            queryWrapper.orderByDesc("create_date");

            IPage<TaCompany> result = iTaCompanyService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        } catch (Exception e) {
            logger.error("listCompany -=- {}", e);
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 条件查询企业列表
     *
     * @param pageNum
     * @param pageSize
     * @param companyName
     * @return
     */
    @RequestMapping(value = "/admin/taCompanyList", method = RequestMethod.GET)
    public ResponseBean companyList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                   String companyName, String companyCode, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
            IPage<TaCompany> pg = new Page<>(pageNum, pageSize);
            IPage<TaCompany> result = iTaCompanyService.selectListByOrgId(pg, companyName, companyCode, getOrgId(request));
            List<TaCompany> taCompanyList = result.getRecords();
            for (TaCompany taCompany : taCompanyList){
                QueryWrapper<TaCompanySeal> sealQueryWrapper = new QueryWrapper<>();
                sealQueryWrapper.eq("company_id", taCompany.getCompanyId());
                sealQueryWrapper.ne("status", CommConstant.STATUS_DELETE);
                taCompany.setSealNum(iTaCompanySealService.count(sealQueryWrapper));
            }
            responseBean.addSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taCompanyList -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 条件查询企业列表
     *
     * @param pageNum
     * @param pageSize
     * @param companyName
     * @return
     */
    @RequestMapping(value = "/channel/taCompanyList", method = RequestMethod.GET)
    public ResponseBean channelCompanyList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                           @RequestParam(value = "companyName", required = false) String companyName,
                                           @RequestParam(value = "companyCode", required = false)String companyCode,
                                           @RequestParam(value = "status", required = false)String status,
                                           @RequestParam(value = "authorizeStatus", required = false)String authorizeStatus) {
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
            IPage<TaCompany> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaCompany> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(companyName), "company_name", companyName);
            queryWrapper.like(StringUtils.isNotBlank(companyCode), "company_code", companyCode);
            queryWrapper.eq(StringUtils.isNotBlank(status), "status", status);
            if (CommConstant.AUTO_AUTH_STATUS.equals(authorizeStatus)){
                queryWrapper.eq(StringUtils.isNotBlank(authorizeStatus), "authorize_status", authorizeStatus);
            }
            if (CommConstant.AUTO_UNAUTH_STATUS.equals(authorizeStatus)){
                queryWrapper.nested(i -> i.eq("authorize_status", CommConstant.AUTO_UNAUTH_STATUS).or().isNull("authorize_status"));
            }

            queryWrapper.ne("status", CommConstant.STATUS_DELETE);
            queryWrapper.orderByDesc("create_date");

            IPage<TaCompany> result = iTaCompanyService.page(pg, queryWrapper);
            List<TaCompany> taCompanyList = result.getRecords();
            for (TaCompany taCompany : taCompanyList){
                QueryWrapper<TaCompanySeal> sealQueryWrapper = new QueryWrapper<>();
                sealQueryWrapper.eq("company_id", taCompany.getCompanyId());
                sealQueryWrapper.ne("status", CommConstant.STATUS_DELETE);
                taCompany.setSealNum(iTaCompanySealService.count(sealQueryWrapper));
            }
            responseBean.addSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taCompanyList -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     *
     * @param taCompany 实体对象
     * @return
     */
    @RequestMapping(value = "/taCompany", method = RequestMethod.POST)
    public ResponseBean taCompanyAdd(@RequestBody TaCompany taCompany) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaCompanyService.save(taCompany)) {
                responseBean.addSuccess(taCompany);
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taCompanyAdd -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     */
    @ResponseBody
    @RequestMapping(value = "/taCompany/{id}", method = RequestMethod.DELETE)
    public ResponseBean taCompanyDelete(@PathVariable Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaCompanyService.removeById(id)) {
                responseBean.addSuccess("success");
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taCompanyDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     *
     * @param id        实体ID
     * @param taCompany 实体对象
     * @return
     */
    @RequestMapping(value = "/taCompany/{id}", method = RequestMethod.PUT)
    public ResponseBean taCompanyUpdate(@PathVariable Integer id,
                                        @RequestBody TaCompany taCompany) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaCompanyService.updateById(taCompany)) {
                responseBean.addSuccess(taCompany);
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taCompanyUpdate -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     *
     * @param id 实体ID
     */
    @RequestMapping(value = "/channel/taCompany/{id}", method = RequestMethod.GET)
    public ResponseBean taCompanyGet(@PathVariable Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            TaCompany taCompany = iTaCompanyService.getCompanyAndContractInfo(id);
            responseBean.addSuccess(taCompany);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taCompanyDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 条件查询企业列表
     *
     * @param pageNum
     * @param pageSize
     * @param companyName
     * @param companyCode
     * @param status
     * @return
     */
    @RequestMapping(value = "/channel/company/list", method = RequestMethod.GET)
    public ResponseBean getCompanyList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                       @RequestParam(value = "companyName", required = false) String companyName,
                                       @RequestParam(value = "companyCode", required = false) String companyCode,
                                       @RequestParam(value = "status", required = false) String status) {
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
            IPage<TaCompany> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaCompany> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(companyName), "company_name", companyName);
            queryWrapper.like(StringUtils.isNotBlank(companyCode), "company_code", companyCode);
            queryWrapper.eq(StringUtils.isNotBlank(status), "status", status);
            queryWrapper.gt("status", CommConstant.STATUS_DELETE);
            queryWrapper.orderByDesc("create_date");

            IPage<TaCompany> result = iTaCompanyService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taCompanyList -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 第一次自动创建企业信息
     */
    @RequestMapping(value = "/channel/company/add", method = RequestMethod.POST)
    public ResponseBean addCompanyFirstInfo() {
        ResponseBean responseBean = new ResponseBean();
        try {
            TaCompany taCompany = iTaCompanyService.newFirstCompanyInfo();
            responseBean.addSuccess(taCompany);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taCompanyList -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     *
     * @param id 实体ID
     */
    @RequestMapping(value = "/channel/company/{id}", method = RequestMethod.GET)
    public ResponseBean getCompanyInfo(@PathVariable Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaCompanyService.getById(id));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taCompanyDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id更新数据
     *
     * @param id 实体ID
     */
    @RequestMapping(value = "/channel/company/{id}", method = RequestMethod.PUT)
    public ResponseBean updateCompanyInfo(@PathVariable Integer id,@RequestBody TaCompany taCompany) {
        ResponseBean responseBean = new ResponseBean();
        try {
            taCompany.setCompanyId(id);
            TaCompany newCompany = iTaCompanyService.updateCompanyInfo(taCompany);
            responseBean.addSuccess(newCompany);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taCompanyDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id去法大大那边拿最新数据然后更新
     *
     * @param id 实体ID
     */
    @RequestMapping(value = "/channel/company/fdd/{id}", method = RequestMethod.GET)
    public ResponseBean updateCompanyInfoByFdd(@PathVariable Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            TaCompany newCompany = iTaCompanyService.updateCompanyInfoByFdd(id);
            responseBean.addSuccess(newCompany);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taCompanyDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id更新数据
     *
     * @param
     */
    @RequestMapping(value = "/channel/company/delete", method = RequestMethod.DELETE)
    public ResponseBean updateCompanyInfo(@RequestBody String ids) {
        ResponseBean responseBean = new ResponseBean();
        try {
            int successNum = 0;
            int failNum = 0;
            JSONArray jsonArray = JSONObject.parseObject(ids).getJSONArray("ids");
            for (Object id : jsonArray) {
                TaCompany company = iTaCompanyService.getById((Integer) id);
                if (!CommConstant.STATUS_VERIFY_PASS.equals(company.getStatus())){
                    company.setStatus(CommConstant.STATUS_DELETE);
                    iTaCompanyService.updateById(company);
                    successNum++;
                }else{
                    failNum++;
                }
            }
            Map<String,Object> map = new HashMap<>();
            map.put("successNum",successNum);
            map.put("failNum",failNum);
            responseBean.addSuccess(map);
        }catch (Exception e){
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 企业认证回调函数
     *
     */
    @RequestMapping(value = "/fadd/company", method = RequestMethod.POST)
    public void faddCompanyNotify(@RequestParam(value = "serialNo",required = false) String serialNo,
                                  @RequestParam(value = "authenticationType",required = false) String authenticationType,
                                  @RequestParam(value = "status",required = false) String status,
                                  HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        logger.info("企业认证回调函数参数:交易号{}，认证类型{}，状态，{}, 人员名称{}",serialNo,authenticationType,status);
        //企业认证
        if ("2".equals(authenticationType)){
            QueryWrapper<TaCompany> taCompanyQueryWrapper = new QueryWrapper<>();
            taCompanyQueryWrapper.eq("certified_serial_number",serialNo);
            TaCompany taCompany = iTaCompanyService.getOne(taCompanyQueryWrapper);

            try {
                //更新企业状态
                iTaCompanyService.updateCompanyInfoByFdd(taCompany.getCompanyId());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if ("1".equals(authenticationType)){
            QueryWrapper<TaContractUser> taContractUserQueryWrapper = new QueryWrapper<>();
            taContractUserQueryWrapper.eq("certified_serial_number",serialNo);
            TaContractUser taContractUser = iTaContractUserService.getOne(taContractUserQueryWrapper);
            taContractUser.setStatus(Integer.valueOf(status));
            taContractUser.setAuthStatus("autoSign");
            iTaContractUserService.updateById(taContractUser);
//            //如果状态是失败的情况下就发送短信  先注释如果没问题就删除
//            if (status.equals("1")){
//                logger.info("发送短信");
//                //获取个人信息
//                logger.info("个人正在从法大大获取最新数据，企业编号是 {}", taContractUser.getFadadaCode());
//                FindCertInfo personCertInfo = new FindCertInfo(fadadaProperties.getAppId(),fadadaProperties.getAppSecret(),fadadaProperties.getVersion(),fadadaProperties.getServeHost());
//                String verified_serialno = taContractUser.getCertifiedSerialNumber();
//                String result = personCertInfo.invokeFindPersonCert(verified_serialno,
//                        "1");
//                logger.info("个人最新数据结果 {}",result);
//                JSONObject jsonObject = JSONObject.parseObject(result);
//                //更新数据
//                String data = jsonObject.getString("data");
//                if (null !=data){
//                    JSONObject dataJson = JSONObject.parseObject(data);
//                    //个人信息
//                    JSONObject personJson = dataJson.getJSONObject("person");
//                    if (null != personJson){
//                        taCompanySealUtil.sendFddSms(taContractUser.getCertifiedAddress(),personJson.getString("mobile"));
//                    }
//                }
//
//            }


        }
    }

    /**
     * 自动授权签章回调
     */
    @RequestMapping(value = "/fadd/autoAuthCompanySeal", method = RequestMethod.POST)
    public void autoAuthCompanySealNotify(@RequestParam(value = "transaction_id",required = false) String transactionId,
                                          @RequestParam(value = "contract_id",required = false) Integer contractId,
                                          @RequestParam(value = "download_url",required = false) String downloadUrl,
                                          @RequestParam(value = "viewpdf_url",required = false) String viewpdfUrl,
                                          @RequestParam(value = "result_code",required = false) String resultCode,
                                          @RequestParam(value = "result_desc",required = false) String resultDesc,
                                          HttpServletRequest request){
        TaContract taContract = new TaContract();
        taContract.setContractId(contractId);
        taContract.setContractDownloadUrl(downloadUrl);
        taContract.setContractViewUrl(viewpdfUrl);
        taContract.setStatus(CommConstant.STATUS_NORMAL);
        iTaContractService.updateById(taContract);

        if (CommConstant.AUTO_AUTH_STATUS.equals(resultCode) && CommConstant.AUTH_SUCCESS_DESC.equals(resultDesc)){
            TaCompany taCompany = new TaCompany();
            taCompany.setCompanyId(iTaContractService.getById(contractId).getCompanyId());
            taCompany.setAuthorizeStatus(resultCode);
            taCompany.setAuthorizeTransactionId(transactionId);
            taCompany.setAuthorizeContractId(String.valueOf(contractId));
            taCompany.setAuthorizeDownloadUrl(downloadUrl);
            taCompany.setAuthorizeViewUrl(viewpdfUrl);
            taCompany.setAuthorizeDescription(resultDesc);
            taCompany.setAuthorizeType(CommConstant.ONLINEAUTH);
            iTaCompanyService.updateById(taCompany);
        }
    }

    /**
     * 自动签章授权
     */
    @RequestMapping(value = "/channel/company/autoAuthCompany/{id}", method = RequestMethod.PUT)
    public ResponseBean autoAuthCompanySeal(@PathVariable Integer id, HttpServletRequest request) throws Exception {
        ResponseBean responseBean = new ResponseBean();
        String res = iTaCompanyService.autoAuthCompanySeal(id, getOrgId(request));
        TaCompany taCompany = new TaCompany();
        taCompany.setCompanyId(id);
        taCompany.setAuthorizeAddress(res);
        iTaCompanyService.updateById(taCompany);
        responseBean.addSuccess((Object)res);
        return responseBean;
    }

    /**
     * 设置默认章
     */
    @RequestMapping(value = "/channel/company/setDefaultSeal/{id}", method = RequestMethod.POST)
    public ResponseBean setDefaultSeal(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        TaCompanySeal taCompanySeal = iTaCompanySealService.getById(id);
        TaCompany taCompany = iTaCompanyService.getById(taCompanySeal.getCompanyId());
        String res = taCompanySealUtil.setDefaultSeal(taCompanySeal.getSignatureId(), taCompany.getFadadaCode());
        JSONObject result = JSONObject.parseObject(res);
        if ((Integer) result.get("code") != 1){
            responseBean.addError(result.getString("msg"));
            return responseBean;
        }
        //更新旧的印章为否
        QueryWrapper<TaCompanySeal> taCompanySealQueryWrapper = new QueryWrapper<>();
        taCompanySealQueryWrapper.eq("company_id", taCompany.getCompanyId());
        taCompanySealQueryWrapper.eq("default_seal", CommConstant.STATUS_NORMAL);
        TaCompanySeal oldDefaultSeal = iTaCompanySealService.getOne(taCompanySealQueryWrapper);
        if (oldDefaultSeal != null){
            oldDefaultSeal.setDefaultSeal(CommConstant.STATUS_UNACCALIMED);
            iTaCompanySealService.updateById(oldDefaultSeal);
        }

        //更新默认章
        taCompanySeal.setDefaultSeal(CommConstant.STATUS_NORMAL);
        iTaCompanySealService.updateById(taCompanySeal);


        responseBean.addSuccess(result.getString("msg"));
        return responseBean;
    }


    /**
     * 手动签署回调函数
     *
     */
    @RequestMapping(value = "/fadd/sign", method = {RequestMethod.GET,RequestMethod.POST})
    public void faddSignNotify(@RequestParam(value = "transaction_id ",required = false) String transaction_id,
                                  @RequestParam(value = "contract_id ",required = false) String contract_id,
                                  @RequestParam(value = "result_code",required = false) String result_code,
                                  @RequestParam(value = "result_desc",required = false) String result_desc,
                                  @RequestParam(value = "download_url",required = false) String download_url,
                                  @RequestParam(value = "viewpdf_url",required = false) String viewpdf_url,
                                  @RequestParam(value = "timestamp",required = false) String timestamp,
                                  @RequestParam(value = "msg_digest",required = false) String msg_digest,
                                  HttpServletRequest request) {
        logger.info("企业认证回调函数参数:交易号 {},合同编号 {}，签章结果代码 {}，签章结果描述 ，{}, 下载地址{}, 查看地址 {},请求时间 {}, 摘要 {} ",transaction_id,contract_id,result_code,result_desc,download_url,viewpdf_url,timestamp,msg_digest);
        Map<String, String[]> parameterMap = request.getParameterMap();
        if ("3000".equals(result_code)){
            TaContract taContract = iTaContractService.getById(parameterMap.get("contract_id")[0]);
//            taContract.setContractDownloadUrl(download_url);
//            taContract.setContractViewUrl(viewpdf_url);
            taContract.setStatus(CommConstant.STATUS_NORMAL);
            taContract.setCustomerTime(LocalDateTime.now());
            taContract.setReason(parameterMap.get("result_desc")[0]);

            //自动归档
            FddClientBase base = new FddClientBase(fadadaProperties.getAppId(),fadadaProperties.getAppSecret(),fadadaProperties.getVersion(),fadadaProperties.getServeHost());
            String result = base.invokeContractFilling(taContract.getContractId().toString());
            logger.info("合同归档结果{}",result);
            taContract.setArchives(true);
            taContract.setArchivesTime(LocalDateTime.now());
            iTaContractService.updateById(taContract);
        }else{
            TaContract taContract = iTaContractService.getById(parameterMap.get("contract_id")[0]);
//            taContract.setContractDownloadUrl(download_url);
//            taContract.setContractViewUrl(viewpdf_url);
            //签署失败
            taContract.setStatus(2);
            taContract.setReason(parameterMap.get("result_desc")[0]);
            iTaContractService.updateById(taContract);
        }
    }

    /**
     * 查询授权状态接口
     * @param id
     * @return
     */
    @RequestMapping(value = "/channel/company/getAuthStatus/{id}", method = RequestMethod.GET)
    public ResponseBean getAuthStatus(@PathVariable Integer id, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        String res = iTaCompanyService.getAuthStatus(id);
        JSONObject result = JSONObject.parseObject(res);

        if ((Integer) result.get("code") != 1){
            responseBean.addError(result.getString("msg"));
            return responseBean;
        }
        responseBean.addSuccess(result.get("data"));
        return responseBean;
    }
}
