/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.guosen.bussinessRegisterRequest.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.SystemConstant;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.guosen.bussiness.entity.Bussiness;
import org.springblade.modules.guosen.bussinessContactInfo.entity.BussinessContactInfo;
import org.springblade.modules.guosen.bussinessInfo.entity.BussinessInfo;
import org.springblade.modules.guosen.bussinessInfo.service.IBussinessInfoService;
import org.springblade.modules.guosen.bussinessProductQualInfo.entity.BussinessProductQualInfo;
import org.springblade.modules.guosen.bussinessProductQualInfo.service.IBussinessProductQualInfoService;
import org.springblade.modules.guosen.bussinessRegisterRequest.dto.BussinessRegisterProductInfoDTO;
import org.springblade.modules.guosen.bussinessRegisterRequest.entity.BussinessRegisterRequest;
import org.springblade.modules.guosen.bussinessRegisterRequest.vo.BussinessRegisterRequestVO;
import org.springblade.modules.guosen.bussinessRegisterRequest.mapper.BussinessRegisterRequestMapper;
import org.springblade.modules.guosen.bussinessRegisterRequest.service.IBussinessRegisterRequestService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.guosen.bussinessuserinfo.entity.BussinessUserInfo;
import org.springblade.modules.guosen.bussinessuserinfo.service.IBussinessUserInfoService;
import org.springblade.modules.guosen.productratestrategy.entity.ProductRateStrategy;
import org.springblade.modules.guosen.productratestrategy.service.IProductRateStrategyService;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.service.IDictBizService;
import org.springblade.modules.system.service.IParamService;
import org.springblade.yeePay.entity.ProductFeeModifyDto;
import org.springblade.yeePay.service.YeePayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-09-22
 */
@Service
@Slf4j
public class BussinessRegisterRequestServiceImpl extends BaseServiceImpl<BussinessRegisterRequestMapper, BussinessRegisterRequest> implements IBussinessRegisterRequestService {

	@Value("${pay.registerNoticeUrl}")
	private String registerNoticeUrl;
	@Autowired
	private YeePayService yeePayService;
	@Autowired
	private IBussinessInfoService bussinessInfoService;
	@Autowired
	private IBussinessProductQualInfoService bussinessProductQualInfoService;
	@Autowired
	private IParamService paramService;
	@Autowired
	private IBussinessUserInfoService userInfoService;
	@Autowired
	private IProductRateStrategyService productRateStrategyService;

	@Override
	public IPage<BussinessRegisterRequestVO> selectBussinessRegisterRequestPage(IPage<BussinessRegisterRequestVO> page, BussinessRegisterRequestVO bussinessRegisterRequest) {
		return page.setRecords(baseMapper.selectBussinessRegisterRequestPage(page, bussinessRegisterRequest));
	}

	@Override
	public BussinessRegisterRequest getByRequestNo(String requestNo) {
		LambdaQueryWrapper<BussinessRegisterRequest> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(BussinessRegisterRequest::getRequestNo, requestNo);
		return baseMapper.selectOne(lambdaQueryWrapper);
	}

	@Override
	public R merchantRegister(Bussiness bussiness, BussinessInfo bussinessInfo, BussinessContactInfo legalInfo, BussinessContactInfo contactInfo) {
		//上传商户证件照片到第三方、上传商户手持照片到第三方、上传商户开户照片到第三方、上传法人身份证件正面照片、上传法人身份证反面照片
		BussinessRegisterRequest bussinessRegisterRequest = new BussinessRegisterRequest();
		ProductRateStrategy rateStrategy = null;
		try{
			R licenceUrlR = yeePayService.qualUpload(bussinessInfo.getLicenceUrl());
			if(licenceUrlR.isSuccess()){
				bussinessInfo.setThirdLicenceUrl(this.getMerQualUrl(licenceUrlR));
			}else{
				return R.fail("商户证件上传失败");
			}
			R handLicenceUrlR = yeePayService.qualUpload(bussinessInfo.getHandLicenceUrl());
			if(handLicenceUrlR.isSuccess()){
				bussinessInfo.setThirdHandLicenceUrl(this.getMerQualUrl(handLicenceUrlR));
			}else{
				return R.fail("手持照片上传失败");
			}
			R openAccountLicenceUrlR = yeePayService.qualUpload(bussinessInfo.getOpenAccountLicenceUrl());
			if(openAccountLicenceUrlR.isSuccess()){
				bussinessInfo.setThirdOpenAccountLicenceUrl(this.getMerQualUrl(openAccountLicenceUrlR));
			}else{
				return R.fail("开户照片上传失败");
			}
			R legalLicenceFrontUrlR = yeePayService.qualUpload(legalInfo.getLegalLicenceFrontUrl());
			if(legalLicenceFrontUrlR.isSuccess()){
				legalInfo.setThirdLegalLicenceFrontUrl(this.getMerQualUrl(legalLicenceFrontUrlR));
			}else{
				return R.fail("法人证件正面照上传失败");
			}
			R legalLicenceBackUrlR = yeePayService.qualUpload(legalInfo.getLegalLicenceBackUrl());
			if(legalLicenceBackUrlR.isSuccess()){
				legalInfo.setThirdLegalLicenceBackUrl(this.getMerQualUrl(legalLicenceBackUrlR));
			}else{
				return R.fail("法人证件反面照上传失败");
			}
			//产品资质信息
			rateStrategy = this.productRateStrategyService.getById(bussiness.getRateStrategyId());
			this.handleBussinessInfoProductQual(bussinessRegisterRequest, bussinessInfo, rateStrategy.getPaymentScene());
		}catch (Exception e){
			bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_FAIL.getValue());
			bussinessInfoService.updateById(bussinessInfo);
			e.printStackTrace();
			return R.fail("图片上传失败");
		}
		bussinessRegisterRequest.setBussinessInfoId(bussinessInfo.getId());
		bussinessRegisterRequest.setProductInfo(rateStrategy.getProductRateJson());
		bussinessRegisterRequest.setMerchantSubjectInfo(bussinessInfo.toBusinessInfoString());
		bussinessRegisterRequest.setMerchantCorporationInfo(legalInfo.toLegalString());
		bussinessRegisterRequest.setMerchantContactInfo(contactInfo.toContactString());
		bussinessRegisterRequest.setBusinessAddressInfo(bussinessInfo.toBusinessAddressInfo());
		bussinessRegisterRequest.setBusinessRole(bussinessInfo.getBusinessRole());
		bussinessRegisterRequest.setRequestNo("MR" + UUID.randomUUID().toString().replaceAll("-","").toUpperCase());
		bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_SUCCESS.getValue());
		bussinessInfoService.updateById(bussinessInfo);
		boolean save = this.save(bussinessRegisterRequest);

		if(save){
			try {
				R result = yeePayService.merchantRegister(bussinessRegisterRequest, bussinessInfo.getParentMerchantNo());
				JSONObject resultData = JSONUtil.parseObj(result.getData());
				String returnCode = resultData.getStr("returnCode");
				bussinessRegisterRequest.setApplicationNo(resultData.getStr("applicationNo"));
				bussinessRegisterRequest.setThirdLog(JSONUtil.toJsonStr(resultData));
				Integer registerStatus = this.getApplicationStatus(resultData.getStr("applicationStatus"));
				bussinessRegisterRequest.setApplicationStatus(registerStatus);
				bussinessInfo.setRegisterStatus(registerStatus);
				bussinessInfo.setAuditOpinion(resultData.getStr("auditOpinion"));
				if(returnCode.equals("NIG00000")){
					this.bussinessInfoService.updateById(bussinessInfo);
					this.baseMapper.updateById(bussinessRegisterRequest);
					return R.success("送审成功，请等待审核");
				}else{
					String returnMsg = resultData.getStr("returnMsg");
					bussinessRegisterRequest.setReturnMsg(returnMsg);
					bussinessRegisterRequest.setAuditOpinion(returnMsg);
					bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_FAIL.getValue());
					bussinessInfoService.updateById(bussinessInfo);
					this.baseMapper.updateById(bussinessRegisterRequest);
					return R.fail(returnMsg);
				}
			}catch (Exception e){
				bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_FAIL.getValue());
				bussinessRegisterRequest.setThirdLog(e.getMessage());
				bussinessInfoService.updateById(bussinessInfo);
				this.baseMapper.updateById(bussinessRegisterRequest);
				return R.fail("送审失败");
			}
		}else{
			bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_FAIL.getValue());
			bussinessRegisterRequest.setThirdLog("业务保存失败");
			bussinessInfoService.updateById(bussinessInfo);
			this.baseMapper.updateById(bussinessRegisterRequest);
			return R.fail("申请提交失败");
		}
	}

	@Override
	public R userRegister(Long registerRequestId, Long userInfoId) {
		BussinessUserInfo userInfo = this.userInfoService.getById(userInfoId);
		ProductRateStrategy rateStrategy = this.productRateStrategyService.getById(userInfo.getRateStrategyId());
		if(userInfo == null){
			return R.fail("用户不存在");
		}else {
			Integer registerStatus = userInfo.getRegisterStatus();
			if (registerStatus != null &&
					(registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.SUCCESS.getValue()
							|| registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.SUBMIT_SUCCESS.getValue())
					|| registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.WAIT_AUDIT.getValue()) {
				return R.fail("该用户已完成审核或审核中，请勿重复提交");
			}
			BussinessRegisterRequest bussinessRegisterRequest = this.getById(registerRequestId);
			bussinessRegisterRequest.setMerchantSubjectInfo(userInfo.toMerchantSubjectInfo());
			bussinessRegisterRequest.setBusinessAddressInfo(userInfo.toBusinessAddressInfo());
			bussinessRegisterRequest.setAccountInfo(userInfo.toAccountInfo());
			bussinessRegisterRequest.setProductInfo(rateStrategy.getProductRateJson());
			bussinessRegisterRequest.setRequestNo(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
			this.handleUserProductInfo(userInfo);
			bussinessRegisterRequest.setMerchantCorporationInfo(userInfo.toMerchantCorporationInfo());
			R result = this.yeePayService.userRegister(bussinessRegisterRequest, userInfo.getParentMerchantNo());
			JSONObject resultData = JSONUtil.parseObj(result.getData());
			String returnCode = resultData.getStr("returnCode");
			bussinessRegisterRequest.setApplicationNo(resultData.getStr("applicationNo"));
			bussinessRegisterRequest.setThirdLog(JSONUtil.toJsonStr(resultData));
			bussinessRegisterRequest.setApplicationStatus(this.getApplicationStatus(resultData.getStr("applicationStatus")));
			userInfo.setRegisterStatus(this.getApplicationStatus(resultData.getStr("applicationStatus")));
			if(returnCode.equals("NIG00000")){
				this.userInfoService.updateById(userInfo);
				this.baseMapper.updateById(bussinessRegisterRequest);
				return R.success("送审成功，请等待审核");
			}else{
				String returnMsg = resultData.getStr("returnMsg");
				bussinessRegisterRequest.setReturnMsg(returnMsg);
				bussinessRegisterRequest.setAuditOpinion(returnMsg);
				userInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_FAIL.getValue());
				userInfo.setAuditOpinion(returnMsg);
				userInfoService.updateById(userInfo);
				this.baseMapper.updateById(bussinessRegisterRequest);
				return R.fail(returnMsg);
			}
		}
	}

	//调试专用
	@Override
	public R userRegisterDebug(Long registerRequestId, Long userInfoId) {
		BussinessUserInfo userInfo = this.userInfoService.getById(userInfoId);
		ProductRateStrategy rateStrategy = this.productRateStrategyService.getById(userInfo.getRateStrategyId());
		if(userInfo == null){
			return R.fail("用户不存在");
		}else {
			Integer registerStatus = userInfo.getRegisterStatus();
			if (registerStatus != null &&
					(registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.SUCCESS.getValue()
							|| registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.SUBMIT_SUCCESS.getValue())
					|| registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.WAIT_AUDIT.getValue()) {
				return R.fail("该用户已完成审核或审核中，请勿重复提交");
			}
			BussinessRegisterRequest bussinessRegisterRequest = this.getById(registerRequestId);
			bussinessRegisterRequest.setMerchantSubjectInfo(userInfo.toMerchantSubjectInfo());
			bussinessRegisterRequest.setBusinessAddressInfo(userInfo.toBusinessAddressInfo());
			userInfo.setThirdUserCardFrontUrl("abcdd");
			userInfo.setThirdUserCardBackUrl("12345");
//			this.handleUserProductInfo(userInfo);
			bussinessRegisterRequest.setMerchantCorporationInfo(userInfo.toMerchantCorporationInfo());
			bussinessRegisterRequest.setAccountInfo(userInfo.toAccountInfo());
			bussinessRegisterRequest.setProductInfo(rateStrategy.getProductRateJson());
			bussinessRegisterRequest.setRequestNo(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
			R result = this.yeePayService.userRegister(bussinessRegisterRequest, userInfo.getParentMerchantNo());
			JSONObject resultData = JSONUtil.parseObj(result.getData());
			String returnCode = resultData.getStr("returnCode");
			bussinessRegisterRequest.setApplicationNo(resultData.getStr("applicationNo"));
			bussinessRegisterRequest.setThirdLog(JSONUtil.toJsonStr(resultData));
			bussinessRegisterRequest.setApplicationStatus(this.getApplicationStatus(resultData.getStr("applicationStatus")));
			userInfo.setRegisterStatus(this.getApplicationStatus(resultData.getStr("applicationStatus")));
			if(returnCode.equals("NIG00000")){
				this.userInfoService.updateById(userInfo);
				this.baseMapper.updateById(bussinessRegisterRequest);
				return R.success("送审成功，请等待审核");
			}else{
				String returnMsg = resultData.getStr("returnMsg");
				bussinessRegisterRequest.setReturnMsg(returnMsg);
				bussinessRegisterRequest.setAuditOpinion(returnMsg);
				userInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_FAIL.getValue());
				userInfoService.updateById(userInfo);
				this.baseMapper.updateById(bussinessRegisterRequest);
				return R.fail(returnMsg);
			}
		}
	}

	@Override
	public R productFeeModify(BussinessInfo bussinessInfo, BussinessRegisterProductInfoDTO bussinessRegisterProductInfoDTO) {
		String requestNo = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
		BussinessRegisterRequest registerRequest = new BussinessRegisterRequest();
		registerRequest.setRequestNo(requestNo);
		registerRequest.setBussinessInfoId(bussinessInfo.getId());
		registerRequest.setApplicationStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_SUCCESS.getValue());
		registerRequest.setMerchantNo(bussinessInfo.getMerchantNo());
		bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_SUCCESS.getValue());
		this.save(registerRequest);
		this.bussinessInfoService.updateById(bussinessInfo);
		ProductFeeModifyDto productFeeModifyDto = ProductFeeModifyDto.builder()
				.merchantNo(bussinessInfo.getMerchantNo())
				.requestNo(requestNo)
				.productInfo(JSONUtil.toJsonStr(bussinessRegisterProductInfoDTO.getProductInfo()))
				.build();
		try {
			R result = this.yeePayService.productFeeModify(productFeeModifyDto, bussinessInfo.getParentMerchantNo());
			JSONObject resultData = JSONUtil.parseObj(result.getData());
			String returnCode = resultData.getStr("returnCode");
			registerRequest.setApplicationNo(resultData.getStr("applicationNo"));
			registerRequest.setThirdLog(JSONUtil.toJsonStr(resultData));
			Integer registerStatus = this.getApplicationStatus(resultData.getStr("applicationStatus"));
			registerRequest.setApplicationStatus(registerStatus);
			bussinessInfo.setRegisterStatus(registerStatus);
			bussinessInfo.setAuditOpinion(resultData.getStr("auditOpinion"));
			if(returnCode.equals("NIG00000")){
				this.bussinessInfoService.updateById(bussinessInfo);
				this.baseMapper.updateById(registerRequest);
				return R.success("送审成功，请等待审核");
			}else{
				String returnMsg = resultData.getStr("returnMsg");
				registerRequest.setReturnMsg(returnMsg);
				registerRequest.setAuditOpinion(returnMsg);
				bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_FAIL.getValue());
				bussinessInfoService.updateById(bussinessInfo);
				this.baseMapper.updateById(registerRequest);
				return R.fail(returnMsg);
			}
		}catch (Exception e){
			bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_FAIL.getValue());
			registerRequest.setThirdLog(e.getMessage());
			bussinessInfoService.updateById(bussinessInfo);
			this.baseMapper.updateById(registerRequest);
			return R.fail("送审失败");
		}
	}

	@Override
	public R testMerchantRegister(Bussiness bussiness, BussinessInfo bussinessInfo, BussinessContactInfo legalInfo, BussinessContactInfo contactInfo) {
		BussinessRegisterRequest bussinessRegisterRequest = new BussinessRegisterRequest();
		bussinessInfo.setThirdLicenceUrl(bussinessInfo.getLicenceUrl());
		bussinessInfo.setThirdHandLicenceUrl(bussinessInfo.getHandLicenceUrl());
		bussinessInfo.setThirdOpenAccountLicenceUrl(bussinessInfo.getOpenAccountLicenceUrl());
		legalInfo.setThirdLegalLicenceFrontUrl(legalInfo.getLegalLicenceFrontUrl());
		legalInfo.setThirdLegalLicenceBackUrl(legalInfo.getLegalLicenceBackUrl());
		JSONObject qualData = new JSONObject();
		ProductRateStrategy rateStrategy = this.productRateStrategyService.getById(bussiness.getRateStrategyId());
		qualData.set("paymentScene", rateStrategy.getPaymentScene());
		qualData.set("agreementPhotoUrl", bussinessInfo.getAgreementPhotoUrl());
		qualData.set("systemScreenshotUrl", bussinessInfo.getSystemScreenshotUrl());
		qualData.set("specialPermitProcessUrl", bussinessInfo.getSpecialPermitProcessUrl());
		bussinessRegisterRequest.setProductQualificationInfo(qualData.toString());
		bussinessRegisterRequest.setProductInfo(rateStrategy.getProductRateJson());
		bussinessRegisterRequest.setMerchantSubjectInfo(bussinessInfo.toBusinessInfoString());
		bussinessRegisterRequest.setMerchantCorporationInfo(legalInfo.toLegalString());
		bussinessRegisterRequest.setMerchantContactInfo(contactInfo.toContactString());
		bussinessRegisterRequest.setBusinessAddressInfo(bussinessInfo.toBusinessAddressInfo());
		bussinessRegisterRequest.setBusinessRole(bussinessInfo.getBusinessRole());
		bussinessRegisterRequest.setRequestNo("MR" + UUID.randomUUID().toString().replaceAll("-","").toUpperCase());
		bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_SUCCESS.getValue());
		bussinessInfo.setAgreementSignUrl("https://www.baidu.com");
		bussinessInfoService.updateById(bussinessInfo);
		boolean save = this.save(bussinessRegisterRequest);

		if(save){
			try {
				//调试专用
				JSONObject resultData = new JSONObject();
				resultData.set("returnCode", "NIG00000");
				resultData.set("applicationNo", System.currentTimeMillis());
				resultData.set("applicationStatus", "REVIEWING");
				String returnCode = resultData.getStr("returnCode");
				bussinessRegisterRequest.setApplicationNo(resultData.getStr("applicationNo"));
				bussinessRegisterRequest.setThirdLog(JSONUtil.toJsonStr(resultData));
				Integer registerStatus = this.getApplicationStatus(resultData.getStr("applicationStatus"));
				bussinessRegisterRequest.setApplicationStatus(registerStatus);
				bussinessInfo.setRegisterStatus(registerStatus);
				bussinessInfo.setAuditOpinion(resultData.getStr("auditOpinion"));
				if(returnCode.equals("NIG00000")){
					this.bussinessInfoService.updateById(bussinessInfo);
					this.baseMapper.updateById(bussinessRegisterRequest);
					return R.success("送审成功，请等待审核");
				}else{
					String returnMsg = resultData.getStr("returnMsg");
					bussinessRegisterRequest.setReturnMsg(returnMsg);
					bussinessRegisterRequest.setAuditOpinion(returnMsg);
					bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_FAIL.getValue());
					bussinessInfoService.updateById(bussinessInfo);
					this.baseMapper.updateById(bussinessRegisterRequest);
					return R.fail(returnMsg);
				}
			}catch (Exception e){
				bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_FAIL.getValue());
				bussinessRegisterRequest.setThirdLog(e.getMessage());
				bussinessInfoService.updateById(bussinessInfo);
				this.baseMapper.updateById(bussinessRegisterRequest);
				return R.fail("送审失败");
			}
		}else{
			bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUBMIT_FAIL.getValue());
			bussinessRegisterRequest.setThirdLog("业务保存失败");
			bussinessInfoService.updateById(bussinessInfo);
			this.baseMapper.updateById(bussinessRegisterRequest);
			return R.fail("申请提交失败");
		}
	}

	private String getProductInfo(Long rateStrategyId){
		ProductRateStrategy rateStrategy = this.productRateStrategyService.getById(rateStrategyId);
		return rateStrategy.getProductRateJson();
	}

	/*private String getProductInfo(List<DictBiz> rateList, boolean isPlatform){
		String baseRate = paramService.getValue("yee_base_rate");
		String saasRate = paramService.getValue("yee_saas_rate");
		String withdrawRate = paramService.getValue("yee_withdraw_rate");
		String transferRate = paramService.getValue("yee_transfer_rate");
		String payRate = paramService.getValue("yee_pay_rate");
		BigDecimal percentRate = new BigDecimal(baseRate).add(new BigDecimal(saasRate));
		List<JSONObject> realRateList = new ArrayList<>();
		for(DictBiz rate : rateList){
			if(rate.getDictKey().equals("ENTERPRISE_WITHDRAW_STANDARD_REALTIME")){
				JSONObject obj1 = new JSONObject();
				obj1.set("productCode", rate.getDictKey());
				obj1.set("rateType", "SINGLE_FIXED");
				obj1.set("fixedRate", withdrawRate);
				realRateList.add(obj1);
			}else if(rate.getDictKey().equals("ENTERPRISE_TRANSFER_B2B")){
				JSONObject obj2 = new JSONObject();
				obj2.set("productCode", rate.getDictKey());
				obj2.set("rateType", "SINGLE_FIXED");
				obj2.set("fixedRate", transferRate);
				realRateList.add(obj2);
			}else if(rate.getDictKey().equals("ENTERPRISE_PAY_REALTIME")){
				if(isPlatform){
					JSONObject obj3 = new JSONObject();
					obj3.set("productCode", rate.getDictKey());
					obj3.set("rateType", "SINGLE_FIXED");
					obj3.set("fixedRate", payRate);
					realRateList.add(obj3);
				}
			}else if(rate.getDictKey().equals("ENTERPRISE_RECHARGE_STANDARD_B2B")){
				JSONObject obj4 = new JSONObject();
				obj4.set("productCode", rate.getDictKey());
				obj4.set("rateType", "SINGLE_PERCENT");
				obj4.set("percentRate", percentRate.toString());
				realRateList.add(obj4);
			}
		}
		String rateListStr = JSON.toJSONString(realRateList);
		return rateListStr;
	}*/

	private String getUserProductInfo(List<DictBiz> rateList){
		String withdrawRate = paramService.getValue("yee_withdraw_rate");
		String d1Rate = paramService.getValue("yee_d1_rate");
		List<JSONObject> realRateList = new ArrayList<>();
		for(DictBiz rate : rateList){
			if(rate.getDictKey().equals("D1")){
				JSONObject obj1 = new JSONObject();
				obj1.set("productCode", rate.getDictKey());
				obj1.set("rateType", "SINGLE_FIXED");
				obj1.set("fixedRate", d1Rate);
				realRateList.add(obj1);
			}else if(rate.getDictKey().equals("ENTERPRISE_WITHDRAW_STANDARD_REALTIME")){
				JSONObject obj2 = new JSONObject();
				obj2.set("productCode", rate.getDictKey());
				obj2.set("rateType", "SINGLE_FIXED");
				obj2.set("fixedRate", withdrawRate);
				realRateList.add(obj2);
			}
		}
		String rateListStr = JSON.toJSONString(realRateList);
		return rateListStr;
	}

	private R handleBussinessInfoProductQual(BussinessRegisterRequest bussinessRegisterRequest, BussinessInfo bussinessInfo, String paymentScene) {
		String agreementPhotoUrl = "";
		String systemScreenshotUrl = "";
		String specialPermitProcessUrl = "";
		JSONObject data = new JSONObject();
		data.set("paymentScene", paymentScene);
		if(StringUtil.isNotBlank(bussinessInfo.getAgreementPhotoUrl())){
			R agreementPhotoUrlR = yeePayService.qualUpload(bussinessInfo.getAgreementPhotoUrl());
			if(agreementPhotoUrlR.isSuccess()){
				agreementPhotoUrl = this.getMerQualUrl(agreementPhotoUrlR);
				data.set("agreementPhotoUrl", agreementPhotoUrl);
			}else{
				return R.fail("商户付款业务协议图片/附件");
			}
		}
		if(StringUtil.isNotBlank(bussinessInfo.getSystemScreenshotUrl())) {
			R systemScreenshotUrlR = yeePayService.qualUpload(bussinessInfo.getSystemScreenshotUrl());
			if (systemScreenshotUrlR.isSuccess()) {
				systemScreenshotUrl = this.getMerQualUrl(systemScreenshotUrlR);
				data.set("systemScreenshotUrl", systemScreenshotUrl);
			} else {
				return R.fail("商户付款业务系统截图/附件");
			}
		}
		if(StringUtil.isNotBlank(bussinessInfo.getSpecialPermitProcessUrl())){
			R specialPermitProcessUrlR = yeePayService.qualUpload(bussinessInfo.getSpecialPermitProcessUrl());
			if(specialPermitProcessUrlR.isSuccess()){
				specialPermitProcessUrl = this.getMerQualUrl(specialPermitProcessUrlR);
				data.set("specialPermitProcessUrl", specialPermitProcessUrl);
			}else{
				return R.fail("商户特批开通流程/截图");
			}
			return R.status(true);
		}
		bussinessRegisterRequest.setProductQualificationInfo(data.toString());
		return R.status(true);
	}

	private R handleUserProductInfo(BussinessUserInfo userInfo) {
		if(StringUtil.isNotBlank(userInfo.getUserCardFrontUrl())){
			R userCardFrontUrlR = yeePayService.qualUpload(userInfo.getUserCardFrontUrl());
			if(userCardFrontUrlR.isSuccess()){
				userInfo.setThirdUserCardFrontUrl(this.getMerQualUrl(userCardFrontUrlR));
			}else{
				return R.fail("法人身份证正面照上传失败");
			}
			R userCardBackUrlR = yeePayService.qualUpload(userInfo.getUserCardBackUrl());
			if(userCardBackUrlR.isSuccess()){
				userInfo.setThirdUserCardBackUrl(this.getMerQualUrl(userCardBackUrlR));
			}else{
				return R.fail("法人身份证反面照上传失败");
			}
			return R.status(true);
		}else{
			return R.status(true);
		}
	}

	private R handlePlatformBussinessProductQualInfo(BussinessRegisterRequest bussinessRegisterRequest, Long bussinessInfoId) {
		BussinessProductQualInfo bussinessProductQualInfo = this.bussinessProductQualInfoService.getByBussinessInfoId(bussinessInfoId);
		if(bussinessProductQualInfo != null){
			if(StringUtil.isNotBlank(bussinessProductQualInfo.getAgreementPhotoUrl())){
				R agreementPhotoUrlR = yeePayService.qualUpload(bussinessProductQualInfo.getAgreementPhotoUrl());
				if(agreementPhotoUrlR.isSuccess()){
					bussinessProductQualInfo.setThirdAgreementPhotoUrl(this.getMerQualUrl(agreementPhotoUrlR));
				}else{
					return R.fail("商户付款业务协议图片/附件");
				}
			}
			if(StringUtil.isNotBlank(bussinessProductQualInfo.getSystemScreenshotUrl())){
				R systemScreenshotUrlR = yeePayService.qualUpload(bussinessProductQualInfo.getSystemScreenshotUrl());
				if(systemScreenshotUrlR.isSuccess()){
					bussinessProductQualInfo.setThirdSystemScreenshotUrl(this.getMerQualUrl(systemScreenshotUrlR));
				}else{
					return R.fail("商户付款业务系统截图/附件");
				}
			}
			if(StringUtil.isNotBlank(bussinessProductQualInfo.getSpecialPermitProcessUrl())){
				R specialPermitProcessUrlR = yeePayService.qualUpload(bussinessProductQualInfo.getSpecialPermitProcessUrl());
				if(specialPermitProcessUrlR.isSuccess()){
					bussinessProductQualInfo.setThirdSpecialPermitProcessUrl(this.getMerQualUrl(specialPermitProcessUrlR));
				}else{
					return R.fail("商户特批开通流程/截图");
				}
			}
			bussinessRegisterRequest.setProductQualificationInfo(bussinessProductQualInfo.toProductQualificationInfo());
			return R.status(true);
		}else{
			return R.status(true);
		}
	}

	private Integer getApplicationStatus(String applicationStatus){
		if(StringUtil.isNotBlank(applicationStatus)){
			switch (applicationStatus){
				case "REVIEWING":
					return SystemConstant.BussinessRegisterStatus.WAIT_AUDIT.getValue();
				case "REVIEW_BACK":
					return SystemConstant.BussinessRegisterStatus.FAIL.getValue();
				case "AGREEMENT_SIGNING":
					return SystemConstant.BussinessRegisterStatus.WAIT_SIGNING.getValue();
				case "BUSINESS_OPENING":
					return SystemConstant.BussinessRegisterStatus.WAIT_AUDIT.getValue();
				case "COMPLETED":
					return SystemConstant.BussinessRegisterStatus.SUCCESS.getValue();
				default:
					return SystemConstant.BussinessRegisterStatus.SUBMIT_SUCCESS.getValue();
			}
		}else{
			return SystemConstant.BussinessRegisterStatus.SUBMIT_FAIL.getValue();
		}
	}

	private String getMerQualUrl(R uploadResult){
		JSONObject data = JSONUtil.parseObj(uploadResult.getData());
		return data.getStr("merQualUrl");
	}

}
