package com.hfkj.channel.tianque.service;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.channel.lakala.LaKaLaMerService;
import com.hfkj.channel.tianque.config.TianQueConfig;
import com.hfkj.channel.tianque.utils.RequestUtil;
import com.hfkj.common.exception.BaseException;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.config.CommonSysConst;
import com.hfkj.entity.*;
import com.hfkj.model.MerBasisModel;
import com.hfkj.service.*;
import com.hfkj.sysenum.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: TianQueMerService
 * @author: HuRui
 * @date: 2023/8/23
 **/
@Component
public class TianQueMerService {

    @Resource
    public BsMerService merService;
    @Resource
    private BsMerPlatformNoService merPlatformNoService;
    @Resource
    private BsMerContractRecordService merContractRecordService;
    @Resource
    private BsMerContractRecordMsgService merContractRecordMsgService;
    @Resource
    private BsLklMccService mccService;
    @Resource
    private BsPlatformService platformService;

    /**
     * 商户进件
     * @param merId
     * @throws Exception
     */
    public void addMer(Long merId) {
        String resStr = "";
        String reqStr = "";
        BsMerPlatformNo platform = null;

        platform = merPlatformNoService.getPlatformNo(merId, PlatformTypeEnum.type5, null);
        if (platform != null) {
            if (StringUtils.isNotBlank(platform.getPlatformNo())) {
                updateMerchantInfo(merId);
                return;
            }
            platform.setStatus(MerPlatformNoStatusEnum.status0.getNumber());
            merPlatformNoService.editMerPlatformNo(platform);
        }

        // 增加进件记录
        BsMerContractRecord record = new BsMerContractRecord();
        record.setMerId(merId);
        record.setPlatformType(PlatformTypeEnum.type5.getNumber());
        record.setOrderNo(System.currentTimeMillis()+"");
        record.setStatus(1);
        merContractRecordService.insertRecord(record);

        MerBasisModel basisModel = merService.getMerDetail(merId);

        try {

            platform = new BsMerPlatformNo();
            platform.setCompanyId(basisModel.getCompanyId());
            platform.setAgentId(basisModel.getAgentId());
            platform.setMerId(basisModel.getId());
            platform.setMerNo(basisModel.getMerNo());
            platform.setPlatformType(PlatformTypeEnum.type5.getNumber());
            platform.setStatus(MerPlatformNoStatusEnum.status2.getNumber());
            BsPlatform currentPlatform = platformService.getCurrentPlatform();
            platform.setCurrentPlatform(currentPlatform.getPlatformType().equals(PlatformTypeEnum.type5.getNumber()));
            merPlatformNoService.editMerPlatformNo(platform);

            Map<String,Object> param = new HashMap<>();
            param.put("mecDisNm", basisModel.getMerAbbreviate());
            param.put("mblNo", basisModel.getRegPhone());
            param.put("operationalType", basisModel.getMerOperateType().equals(MerOperateType.type1.getNumber())?"01":"02");
            if (basisModel.getMerOperateType().equals(MerOperateType.type2.getNumber())) {
                param.put("onlineType", "0" + MerOnlineType.getDataByNumber(basisModel.getMerOnlineType()).getNumber());
                param.put("onlineName", basisModel.getMerOnlineName());
            }
            if (basisModel.getMerType().equals(MerTypeEnum.status1.getNumber())) {
                param.put("haveLicenseNo", "02");
            } else if (basisModel.getMerType().equals(MerTypeEnum.status2.getNumber())) {
                param.put("haveLicenseNo", "03");
            } else if (basisModel.getMerType().equals(MerTypeEnum.status3.getNumber())) {
                param.put("haveLicenseNo", "01");
            }
            param.put("mecTypeFlag", "00");

            param.put("cprRegAddr", basisModel.getMerRegion().getAddress());
            param.put("regProvCd", basisModel.getMerRegion().getProvinceCode() + "000000");
            param.put("regCityCd", basisModel.getMerRegion().getCityCode() + "000000");
            param.put("regDistCd", basisModel.getMerRegion().getAreaCode() + "000000");
            param.put("mccCd", mccService.getMccByCode(basisModel.getMerMccCode()).getExt1());
            param.put("csTelNo", basisModel.getStoreModel().getTelephone());

            /********* 功能类信息 ***********/
            List<Map<String,Object>> qrcodeList = new ArrayList<>();
            Map<String,Object> qrcode;
            for (BsMerRate merRate : basisModel.getMerSettleAcct().getMerRateList()) {
                qrcode = new HashMap<>();

                // 微信
                if (merRate.getRateTypeCode().equals(302)) {
                    qrcode.put("rateType", "01");
                    qrcode.put("rate", merRate.getRatePct()+"");
                    qrcodeList.add(qrcode);

                    // 支付宝
                } else if (merRate.getRateTypeCode().equals(303)) {
                    qrcode.put("rateType", "02");
                    qrcode.put("rate", merRate.getRatePct()+"");
                    qrcodeList.add(qrcode);

                    // 云闪付借记卡小于1000
                } else if (merRate.getRateTypeCode().equals(411)) {
                    qrcode.put("rateType", "061");
                    qrcode.put("rate", merRate.getRatePct()+"");
                    qrcodeList.add(qrcode);

                    // 云闪付借记卡大于1000
                } else if (merRate.getRateTypeCode().equals(315)) {
                    qrcode.put("rateType", "071");
                    qrcode.put("rate", merRate.getRatePct()+"");
                    qrcodeList.add(qrcode);

                    qrcode = new HashMap<>();
                    qrcode.put("rateType", "072");
                    qrcode.put("rate", "20");
                    qrcodeList.add(qrcode);

                    // 云闪付贷记卡小于1000
                } else if (merRate.getRateTypeCode().equals(412)) {
                    qrcode.put("rateType", "06");
                    qrcode.put("rate", merRate.getRatePct()+"");
                    qrcodeList.add(qrcode);

                    // 云闪付贷记卡大于1000
                } else if (merRate.getRateTypeCode().equals(314)) {
                    qrcode.put("rateType", "07");
                    qrcode.put("rate", merRate.getRatePct()+"");
                    qrcodeList.add(qrcode);
                }
            }
            param.put("qrcodeList", qrcodeList);
            param.put("callbackUrl", CommonSysConst.getSysConfig().getDomainName() + "/crest/tianqueNotify/merInbound");
            /********* 功能类信息 ***********/

            /********* 资质证照信息 ***********/
            if (!basisModel.getMerType().equals(MerTypeEnum.status3.getNumber())) {
                param.put("cprRegNmCn", basisModel.getBlisName());
                param.put("registCode", basisModel.getBlisNo());
                param.put("licenseMatch", "00");
                param.put("businessLicStt", basisModel.getBlisPeriodStart().replace(".", "").replace("年","").replace("月","").replace("日",""));
                if (basisModel.getBlisPeriodType().equals(2)) {
                    param.put("businessLicEnt", "29991231");
                } else {
                    param.put("businessLicEnt", basisModel.getBlisPeriodEnd().replace(".", "").replace("年","").replace("月","").replace("日",""));
                }
            }

            param.put("identityName", basisModel.getLarName());
            param.put("identityTyp", "00");
            param.put("identityNo", basisModel.getLarIdCard());
            param.put("legalPersonLicStt", basisModel.getLarIdCardPeriodStart().replace(".", "").replace("年","").replace("月","").replace("日",""));
            if (basisModel.getLarIdCardPeriodType().equals(2)) {
                param.put("businessLicStt", "29991231");
            } else {
                param.put("legalPersonLicEnt", basisModel.getLarIdCardPeriodEnd().replace(".", "").replace("年","").replace("月","").replace("日",""));
            }
            /********* 资质证照信息 ***********/


            /********* 结算信息 ***********/
            if (basisModel.getMerSettleAcct().getSettleType().equals(MerSettleType.status1.getNumber())) {
                param.put("actNm", basisModel.getBlisName());
            } else {
                param.put("actNm", basisModel.getMerSettleAcct().getBankCardHolder());
            }
            param.put("actTyp",basisModel.getMerSettleAcct().getSettleType().equals(MerSettleType.status1.getNumber())?"00":"01");
            param.put("actNoType", "00");
            param.put("stmManIdNo", basisModel.getMerSettleAcct().getSettleIdCardNo());
            param.put("accountLicStt", basisModel.getMerSettleAcct().getSettleIdCardPeriodStart().replace(".", "").replace("年","").replace("月","").replace("日",""));
            if (basisModel.getMerSettleAcct().getSettleIdPeriodType().equals(2)) {
                param.put("accountLicEnt", "29991231");
            } else {
                param.put("accountLicEnt", basisModel.getMerSettleAcct().getSettleIdCardPeriodEnd().replace(".", "").replace("年","").replace("月","").replace("日",""));
            }
            param.put("actNo", basisModel.getMerSettleAcct().getBankCardNo());
            param.put("lbnkNo", basisModel.getMerSettleAcct().getOpenningBankCode());
            /********* 结算信息 ***********/

            /********* 图片信息 ***********/
            param.put("legalPersonidPositivePic", RequestUtil.requestUpload("02", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getLarIdCardPortraitImg())).getString("PhotoUrl"));
            param.put("legalPersonidOppositePic", RequestUtil.requestUpload("03", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getLarIdCardNationalEmblemImg())).getString("PhotoUrl"));
            param.put("storePic", RequestUtil.requestUpload("10", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getStoreModel().getDoorHeadImg())).getString("PhotoUrl"));
            param.put("insideScenePic", RequestUtil.requestUpload("11", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getStoreModel().getInternalImg())).getString("PhotoUrl"));

            if (!basisModel.getMerType().equals(MerTypeEnum.status3.getNumber())) {
                param.put("licensePic", RequestUtil.requestUpload("13", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getBlisUrl())).getString("PhotoUrl"));
            }

            if (basisModel.getMerSettleAcct().getSettleType().equals(MerSettleType.status1.getNumber())) {
                param.put("openingAccountLicensePic", RequestUtil.requestUpload("65", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getMerSettleAcct().getOpenningBankLicenseUrl())).getString("PhotoUrl"));
            }
            if (!basisModel.getMerSettleAcct().getSettleType().equals(MerSettleType.status1.getNumber())) {
                param.put("settlePersonIdcardPositive", RequestUtil.requestUpload("08", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getMerSettleAcct().getSettleIdCardPortraitImg())).getString("PhotoUrl"));
                param.put("settlePersonIdcardOpposite", RequestUtil.requestUpload("07", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getMerSettleAcct().getSettleIdCardNationalEmblemImg())).getString("PhotoUrl"));
                param.put("bankCardPositivePic", RequestUtil.requestUpload("05", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getMerSettleAcct().getBankCardImg())).getString("PhotoUrl"));
            }
            if (basisModel.getMerSettleAcct().getSettleType().equals(MerSettleType.status3.getNumber())) {
                param.put("letterOfAuthPic", RequestUtil.requestUpload("26", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getMerSettleAcct().getCommissionImg())).getString("PhotoUrl"));
            }
            /********* 图片信息 ***********/

            JSONObject response = RequestUtil.request(TianQueConfig.requestUrl + "/merchant/income", param);
            reqStr =  JSONObject.toJSONString(param);
            resStr = response.toJSONString();
            if (!"0000".equals(response.getString("bizCode"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, response.getString("bizMsg"));
            }
            platform.setStatus(MerPlatformNoStatusEnum.status2.getNumber());
            platform.setPlatformNo(response.getString("mno"));
            platform.setCupNo(response.getString("mno"));
            merPlatformNoService.editMerPlatformNo(platform);

            // 增加进件记录
            record.setContractId(response.getString("applicationId"));
            merContractRecordService.updateRecord(record);

            // 增加进件记录消息
            BsMerContractRecordMsg recordMsg = new BsMerContractRecordMsg();
            recordMsg.setOrderNo(record.getOrderNo());
            recordMsg.setType(1);
            recordMsg.setRequestContent(JSONObject.toJSONString(param));
            recordMsg.setResponseMsg(null);
            recordMsg.setResponseContent(response.toJSONString());
            merContractRecordMsgService.insertRecordMsg(recordMsg);

        } catch (BaseException baseException) {
            if (basisModel.getMerStatus().intValue() != MerStatusEnum.status4.getNumber().intValue()) {
                basisModel.setMerStatus(MerStatusEnum.status4.getNumber());
                merService.editMer(basisModel);
            }

            platform.setStatus(MerPlatformNoStatusEnum.status3.getNumber());
            platform.setSuggestionReject(baseException.getErrorMsg());
            merPlatformNoService.editMerPlatformNo(platform);

            record.setStatus(3);
            merContractRecordService.updateRecord(record);

            // 增加进件记录消息
            BsMerContractRecordMsg recordMsg = new BsMerContractRecordMsg();
            recordMsg.setOrderNo(record.getOrderNo());
            recordMsg.setType(1);
            recordMsg.setRequestContent(reqStr);
            recordMsg.setResponseCode("REJECT");
            recordMsg.setResponseContent(resStr);
            recordMsg.setResponseMsg(baseException.getErrorMsg());
            merContractRecordMsgService.insertRecordMsg(recordMsg);

        } catch (Exception e) {
            if (basisModel.getMerStatus().intValue() != MerStatusEnum.status4.getNumber().intValue()) {
                basisModel.setMerStatus(MerStatusEnum.status4.getNumber());
                merService.editMer(basisModel);
            }

            platform.setStatus(MerPlatformNoStatusEnum.status3.getNumber());
            platform.setSuggestionReject("进件失败！未知错误");
            merPlatformNoService.editMerPlatformNo(platform);

            record.setStatus(3);
            merContractRecordService.updateRecord(record);

            // 增加进件记录消息
            BsMerContractRecordMsg recordMsg = new BsMerContractRecordMsg();
            recordMsg.setOrderNo(record.getOrderNo());
            recordMsg.setType(1);
            recordMsg.setRequestContent(reqStr);
            recordMsg.setResponseCode("REJECT");
            recordMsg.setResponseContent(resStr);
            recordMsg.setResponseMsg("进件失败！未知错误");
            merContractRecordMsgService.insertRecordMsg(recordMsg);
        }
    }

    /**
     * 商户进件修改
     * 注：商户入驻修改接口应用于入驻审核未通过的情况。
     * @param merId
     * @throws Exception
     */
    public void updateMerchantInfo(Long merId) {
        String resStr = "";
        String reqStr = "";

        BsMerPlatformNo platform = merPlatformNoService.getPlatformNo(merId, PlatformTypeEnum.type5);

        // 增加进件记录
        BsMerContractRecord record = new BsMerContractRecord();
        record.setMerId(merId);
        record.setPlatformType(PlatformTypeEnum.type5.getNumber());
        record.setOrderNo(System.currentTimeMillis()+"");
        record.setStatus(1);
        merContractRecordService.insertRecord(record);

        MerBasisModel basisModel = merService.getMerDetail(merId);

        try {
            platform.setStatus(MerPlatformNoStatusEnum.status2.getNumber());
            merPlatformNoService.editMerPlatformNo(platform);

            Map<String,Object> param = new HashMap<>();
            param.put("mno", platform.getPlatformNo());
            param.put("mecDisNm", basisModel.getMerAbbreviate());
            param.put("mblNo", basisModel.getRegPhone());
            param.put("operationalType", basisModel.getMerOperateType().equals(MerOperateType.type1.getNumber())?"01":"02");
            if (basisModel.getMerOperateType().equals(MerOperateType.type2.getNumber())) {
                param.put("onlineType", "0" + MerOnlineType.getDataByNumber(basisModel.getMerOnlineType()).getNumber());
                param.put("onlineName", basisModel.getMerOnlineName());
            }
            if (basisModel.getMerType().equals(MerTypeEnum.status1.getNumber())) {
                param.put("haveLicenseNo", "02");
            } else if (basisModel.getMerType().equals(MerTypeEnum.status2.getNumber())) {
                param.put("haveLicenseNo", "03");
            } else if (basisModel.getMerType().equals(MerTypeEnum.status3.getNumber())) {
                param.put("haveLicenseNo", "01");
            }
            param.put("mecTypeFlag", "00");

            param.put("cprRegAddr", basisModel.getMerRegion().getAddress());
            param.put("regProvCd", basisModel.getMerRegion().getProvinceCode() + "000000");
            param.put("regCityCd", basisModel.getMerRegion().getCityCode() + "000000");
            param.put("regDistCd", basisModel.getMerRegion().getAreaCode() + "000000");
            param.put("mccCd", mccService.getMccByCode(basisModel.getMerMccCode()).getExt1());
            param.put("csTelNo", basisModel.getStoreModel().getTelephone());

            /********* 功能类信息 ***********/
            List<Map<String,Object>> qrcodeList = new ArrayList<>();
            Map<String,Object> qrcode;
            for (BsMerRate merRate : basisModel.getMerSettleAcct().getMerRateList()) {
                qrcode = new HashMap<>();

                // 微信
                if (merRate.getRateTypeCode().equals(302)) {
                    qrcode.put("rateType", "01");
                    qrcode.put("rate", merRate.getRatePct()+"");
                    qrcodeList.add(qrcode);

                    // 支付宝
                } else if (merRate.getRateTypeCode().equals(303)) {
                    qrcode.put("rateType", "02");
                    qrcode.put("rate", merRate.getRatePct()+"");
                    qrcodeList.add(qrcode);

                    // 云闪付借记卡小于1000
                } else if (merRate.getRateTypeCode().equals(411)) {
                    qrcode.put("rateType", "061");
                    qrcode.put("rate", merRate.getRatePct()+"");
                    qrcodeList.add(qrcode);

                    // 云闪付借记卡大于1000
                } else if (merRate.getRateTypeCode().equals(315)) {
                    qrcode.put("rateType", "071");
                    qrcode.put("rate", merRate.getRatePct()+"");
                    qrcodeList.add(qrcode);

                    qrcode = new HashMap<>();
                    qrcode.put("rateType", "072");
                    qrcode.put("rate", "20");
                    qrcodeList.add(qrcode);

                    // 云闪付贷记卡小于1000
                } else if (merRate.getRateTypeCode().equals(412)) {
                    qrcode.put("rateType", "06");
                    qrcode.put("rate", merRate.getRatePct()+"");
                    qrcodeList.add(qrcode);

                    // 云闪付贷记卡大于1000
                } else if (merRate.getRateTypeCode().equals(314)) {
                    qrcode.put("rateType", "07");
                    qrcode.put("rate", merRate.getRatePct()+"");
                    qrcodeList.add(qrcode);
                }
            }
            param.put("qrcodeList", qrcodeList);
            param.put("callbackUrl", CommonSysConst.getSysConfig().getDomainName() + "/crest/tianqueNotify/merInbound");
            /********* 功能类信息 ***********/

            /********* 资质证照信息 ***********/
            if (!basisModel.getMerType().equals(MerTypeEnum.status3.getNumber())) {
                param.put("cprRegNmCn", basisModel.getBlisName());
                param.put("registCode", basisModel.getBlisNo());
                param.put("licenseMatch", "00");
                param.put("businessLicStt", basisModel.getBlisPeriodStart().replace(".", "").replace("年","").replace("月","").replace("日",""));
                if (basisModel.getBlisPeriodType().equals(2)) {
                    param.put("businessLicEnt", "29991231");
                } else {
                    param.put("businessLicEnt", basisModel.getBlisPeriodEnd().replace(".", "").replace("年","").replace("月","").replace("日",""));
                }
            }

            param.put("identityName", basisModel.getLarName());
            param.put("identityTyp", "00");
            param.put("identityNo", basisModel.getLarIdCard());
            param.put("legalPersonLicStt", basisModel.getLarIdCardPeriodStart().replace(".", "").replace("年","").replace("月","").replace("日",""));
            if (basisModel.getLarIdCardPeriodType().equals(2)) {
                param.put("businessLicStt", "29991231");
            } else {
                param.put("legalPersonLicEnt", basisModel.getLarIdCardPeriodEnd().replace(".", "").replace("年","").replace("月","").replace("日",""));
            }
            /********* 资质证照信息 ***********/


            /********* 结算信息 ***********/
            if (basisModel.getMerSettleAcct().getSettleType().equals(MerSettleType.status1.getNumber())) {
                param.put("actNm", basisModel.getBlisName());
            } else {
                param.put("actNm", basisModel.getMerSettleAcct().getBankCardHolder());
            }
            param.put("actTyp",basisModel.getMerSettleAcct().getSettleType().equals(MerSettleType.status1.getNumber())?"00":"01");
            param.put("actNoType", "00");
            param.put("stmManIdNo", basisModel.getMerSettleAcct().getSettleIdCardNo());
            param.put("accountLicStt", basisModel.getMerSettleAcct().getSettleIdCardPeriodStart().replace(".", "").replace("年","").replace("月","").replace("日",""));
            if (basisModel.getMerSettleAcct().getSettleIdPeriodType().equals(2)) {
                param.put("accountLicEnt", "29991231");
            } else {
                param.put("accountLicEnt", basisModel.getMerSettleAcct().getSettleIdCardPeriodEnd().replace(".", "").replace("年","").replace("月","").replace("日",""));
            }
            param.put("actNo", basisModel.getMerSettleAcct().getBankCardNo());
            param.put("lbnkNo", basisModel.getMerSettleAcct().getOpenningBankCode());
            /********* 结算信息 ***********/

            /********* 图片信息 ***********/
            param.put("legalPersonidPositivePic", RequestUtil.requestUpload("02", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getLarIdCardPortraitImg())).getString("PhotoUrl"));
            param.put("legalPersonidOppositePic", RequestUtil.requestUpload("03", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getLarIdCardNationalEmblemImg())).getString("PhotoUrl"));
            param.put("storePic", RequestUtil.requestUpload("10", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getStoreModel().getDoorHeadImg())).getString("PhotoUrl"));
            param.put("insideScenePic", RequestUtil.requestUpload("11", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getStoreModel().getInternalImg())).getString("PhotoUrl"));

            if (!basisModel.getMerType().equals(MerTypeEnum.status3.getNumber())) {
                param.put("licensePic", RequestUtil.requestUpload("13", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getBlisUrl())).getString("PhotoUrl"));
            }

            if (basisModel.getMerSettleAcct().getSettleType().equals(MerSettleType.status1.getNumber())) {
                param.put("openingAccountLicensePic", RequestUtil.requestUpload("65", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getMerSettleAcct().getOpenningBankLicenseUrl())).getString("PhotoUrl"));
            }
            if (!basisModel.getMerSettleAcct().getSettleType().equals(MerSettleType.status1.getNumber())) {
                param.put("settlePersonIdcardPositive", RequestUtil.requestUpload("08", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getMerSettleAcct().getSettleIdCardPortraitImg())).getString("PhotoUrl"));
                param.put("settlePersonIdcardOpposite", RequestUtil.requestUpload("07", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getMerSettleAcct().getSettleIdCardNationalEmblemImg())).getString("PhotoUrl"));
                param.put("bankCardPositivePic", RequestUtil.requestUpload("05", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getMerSettleAcct().getBankCardImg())).getString("PhotoUrl"));
            }
            if (basisModel.getMerSettleAcct().getSettleType().equals(MerSettleType.status3.getNumber())) {
                param.put("letterOfAuthPic", RequestUtil.requestUpload("26", new File(CommonSysConst.getSysConfig().getFile_url() + "/" + basisModel.getMerSettleAcct().getCommissionImg())).getString("PhotoUrl"));
            }
            /********* 图片信息 ***********/

            JSONObject response = RequestUtil.request(TianQueConfig.requestUrl + "/merchant/updateMerchantInfo", param);
            reqStr =  JSONObject.toJSONString(param);
            resStr = response.toJSONString();
            if (!"0000".equals(response.getString("bizCode"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, response.getString("bizMsg"));
            }
            response.getString("mno");
            response.getString("applicationId");

            platform.setStatus(MerPlatformNoStatusEnum.status2.getNumber());
            platform.setPlatformNo(response.getString("mno"));
            merPlatformNoService.editMerPlatformNo(platform);

            // 增加进件记录
            record.setContractId(response.getString("applicationId"));
            merContractRecordService.updateRecord(record);

            // 增加进件记录消息
            BsMerContractRecordMsg recordMsg = new BsMerContractRecordMsg();
            recordMsg.setOrderNo(record.getOrderNo());
            recordMsg.setType(1);
            recordMsg.setRequestContent(JSONObject.toJSONString(param));
            recordMsg.setResponseMsg(null);
            recordMsg.setResponseContent(response.toJSONString());
            merContractRecordMsgService.insertRecordMsg(recordMsg);

        } catch (BaseException baseException) {
            if (basisModel.getMerStatus().intValue() != MerStatusEnum.status4.getNumber().intValue()) {
                basisModel.setMerStatus(MerStatusEnum.status4.getNumber());
                merService.editMer(basisModel);
            }
            platform.setStatus(MerPlatformNoStatusEnum.status3.getNumber());
            platform.setSuggestionReject(baseException.getErrorMsg());
            merPlatformNoService.editMerPlatformNo(platform);

            record.setStatus(3);
            merContractRecordService.updateRecord(record);

            // 增加进件记录消息
            BsMerContractRecordMsg recordMsg = new BsMerContractRecordMsg();
            recordMsg.setOrderNo(record.getOrderNo());
            recordMsg.setType(1);
            recordMsg.setRequestContent(reqStr);
            recordMsg.setResponseCode("REJECT");
            recordMsg.setResponseContent(resStr);
            recordMsg.setResponseMsg(baseException.getErrorMsg());
            merContractRecordMsgService.insertRecordMsg(recordMsg);

        } catch (Exception e) {
            if (basisModel.getMerStatus().intValue() != MerStatusEnum.status4.getNumber().intValue()) {
                basisModel.setMerStatus(MerStatusEnum.status4.getNumber());
                merService.editMer(basisModel);
            }

            platform.setStatus(MerPlatformNoStatusEnum.status3.getNumber());
            platform.setSuggestionReject("进件失败！未知错误");
            merPlatformNoService.editMerPlatformNo(platform);

            record.setStatus(3);
            merContractRecordService.updateRecord(record);

            // 增加进件记录消息
            BsMerContractRecordMsg recordMsg = new BsMerContractRecordMsg();
            recordMsg.setOrderNo(record.getOrderNo());
            recordMsg.setType(1);
            recordMsg.setRequestContent(reqStr);
            recordMsg.setResponseCode("REJECT");
            recordMsg.setResponseContent(resStr);
            recordMsg.setResponseMsg("进件失败！未知错误");
            merContractRecordMsgService.insertRecordMsg(recordMsg);
        }

    }

    /**
     * 商户进件结果
     * @param applicationId 进件申请ID
     * @throws Exception
     */
    public JSONObject queryMerchantInfo(String applicationId) {
        Map<String,Object> param = new HashMap<>();
        // param.put("applicationId", "4149b8c5dafe4d15a3dd24df5cf9621d");
        param.put("applicationId", applicationId);
        return RequestUtil.request(TianQueConfig.requestUrl + "/merchant/queryMerchantInfo", param);
    }


}
