package com.brilliance.isc.common.ocrmCheck;

import com.brilliance.isc.bo.Pty;
import com.brilliance.isc.bo.Ptycrm;
import com.brilliance.isc.common.cbsmod.service.impl.XrtmodServiceImpl;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.mda.runtime.mda.impl.Argument;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 
 * @date 2024-07-04
 * @description 对公客户信息校验
 */
@Component
public class OcrmCheckComponent {
    private static final String CR = "<br>";

    private static final String CRMTYP_NULL_ERROR_MSG = "客户分类结果为空，请在CRM系统认定客户分类";

    private static final String CRMTYP_03_ERROR_MSG = "请在CRM系统认定客户分类";

    private static final String CRMSTA_2_ERROR_MSG = "客户分类已失效，请重新认定";

    private static final String NULL_CRM_INFO_ERROR_MSG = "CRM客户信息为空，请在CRM系统认定";

    private static final String BUSINESS_ERROR_MSG = "请进行%s业务分类结果认定";

    private static final String CRMTYP_02_WARNING_MSG = "客户分类结果为关注类客户，请在业务办理过程中加强审核，确保业务合规办理!";

    private static final String REFUND_REMIT_MSG = "该客户为贸易便利化试点客户，退汇单笔限额%s，该笔业务金额折美元%s";

    private static final String GOODS_TRADE_MSG = "该客户为贸易便利化试点客户，货物贸易单笔限额%s，该笔业务金额折美元%s";

    private static final String SERVER_TRADE_MSG = "该客户为贸易便利化试点客户，服务贸易单笔限额%s，该笔业务金额折美元%s";

    private static final String LIMIT_EXCEED = "，已超过限额";

    @Resource
    private CodetableService codetableService;

    @Resource
    private XrtmodServiceImpl xrtmodServiceImpl;

    private static Map<String, String> businessMap;

    static {
        businessMap = new HashMap<>();
        businessMap.put("01", "货物贸易");
        businessMap.put("02", "服务贸易");
        businessMap.put("03", "收益与经常转移");
        businessMap.put("04", "境外直接投资");
        businessMap.put("05", "境内直接投资");
        businessMap.put("06", "跨境担保");
        businessMap.put("07", "企业境外放款");
        businessMap.put("08", "外债");
        businessMap.put("09", "境外贷款");
        businessMap.put("10", "国际贸易融资");
    }

    public String checkCrmInfoError(Pty pty, Ptycrm ptycrm, String business){
        if ( pty != null && !StringUtils.equals("1", pty.getIdtype())){ //同业客户不阻断
            return null;
        } else {
            return checkCrmInfoError(pty, ptycrm, business, true);
        }
    }
    /**
     * 校验客户CRM错误信息
     *
     * @param pty      客户信息
     * @param ptycrm   crm客户信息
     * @param business 业务类型
     * @param continueCheck 是否继续校验
     * @return String
     */
    private String checkCrmInfoError(Pty pty, Ptycrm ptycrm, String business, boolean continueCheck) {
        if (pty == null) {
            return NULL_CRM_INFO_ERROR_MSG;
        }
        if (!continueCheck){ //是否继续校验
            return null;
        }
        if (ptycrm == null){
            return NULL_CRM_INFO_ERROR_MSG;
        }
        if (StringUtils.isEmpty(pty.getCrmtyp())) {
            return CRMTYP_NULL_ERROR_MSG;
        }
        //非外汇客户
        if (StringUtils.equals(pty.getCrmtyp(), "03")) {
            return CRMTYP_03_ERROR_MSG;
        }
        //失效
        if (!StringUtils.equals(ptycrm.getCrmSta(), "1")) {
            return CRMSTA_2_ERROR_MSG;
        }
        //对公客户校验适用业务
        if ((StringUtils.equals(pty.getIdtype(), "1") && StringUtils.isNotEmpty(business))
                && (StringUtils.isEmpty(ptycrm.getBusiness()) || !ptycrm.getBusiness().contains(business))) {
            return String.format(BUSINESS_ERROR_MSG, businessMap.get(business));
        }
        return null;
    }


    /**
     * 校验crm warning信息
     * @param pty
     * @param ptycrm
     * @param business
     * @return
     */
    public String checkCrmInfoWarning(Pty pty, Ptycrm ptycrm, String business){
        return checkCrmInfoWarning(pty, ptycrm, null, null, business, false);
    }
    /**
     * 校验crm warning信息
     *
     * @param pty      pty
     * @param ptycrm   ptycrm
     * @param cur      币种
     * @param amt      金额
     * @param business 适用业务
     * @param isCpt    是否汇款交易,是则传true，否则传false
     * @return
     */
    public String checkCrmInfoWarning(
            Pty pty, Ptycrm ptycrm, String cur, BigDecimal amt, String business, boolean isCpt) {
        if (!StringUtils.equals("1", pty.getIdtype())){
            String error = checkCrmInfoError(pty, ptycrm, business, true);
            if (StringUtils.isNotEmpty(error)) {
                return error;
            }
        }
        if (StringUtils.equals(pty.getCrmtyp(), "02")) {
            return CRMTYP_02_WARNING_MSG;
        }
        if (ptycrm == null) {
            return null;
        }
        if (StringUtils.equals(ptycrm.getCreditSort(), "3")&&"01,02,03".contains(business)) {
            BigDecimal amtusd;
            if (StringUtils.equals(cur, "USD")) {
                amtusd = amt;
            } else {
                Argument<BigDecimal> rate = new Argument<>();
                Argument<BigDecimal> sqrRat = new Argument<>();
                xrtmodServiceImpl.getratForENT(cur, "S", "USD", "B", new Date(), rate, sqrRat);
                amtusd = amt.multiply(rate.value);
            }
            StringBuilder msg = new StringBuilder();
            if (StringUtils.equals(business, "01")) {
                msg.append(GOODS_TRADE_MSG);
                if (amtusd.compareTo(ptycrm.getGootradeMaxBal()) > 0) {
                    msg.append(LIMIT_EXCEED);
                }
                if (isCpt) {
                    msg.append(CR).append(REFUND_REMIT_MSG);
                    if (amtusd.compareTo(ptycrm.getRefremitMaxBal()) > 0) {
                        msg.append(LIMIT_EXCEED);
                    }
                }
                return String.format(msg.toString(), ptycrm.getGootradeMaxBal().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString(),
                        amtusd.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString(), ptycrm.getRefremitMaxBal().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString(),
                        amtusd.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
            } else if (StringUtils.equals(business, "02")) {
                msg.append(SERVER_TRADE_MSG);
                if (amtusd.compareTo(ptycrm.getSertradeMaxBal()) > 0) {
                    msg.append(LIMIT_EXCEED);
                }
                if (isCpt) {
                    msg.append(CR).append(REFUND_REMIT_MSG);
                    if (amtusd.compareTo(ptycrm.getRefremitMaxBal()) > 0) {
                        msg.append(LIMIT_EXCEED);
                    }
                }
                return String.format(msg.toString(), ptycrm.getSertradeMaxBal().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString(),
                        amtusd.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString(), ptycrm.getRefremitMaxBal().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString(),
                        amtusd.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
            } else if (StringUtils.equals(business, "03") && isCpt) {
                msg.append(REFUND_REMIT_MSG);
                if (amtusd.compareTo(ptycrm.getRefremitMaxBal()) > 0) {
                    msg.append(LIMIT_EXCEED);
                }
                return String.format(msg.toString(), ptycrm.getRefremitMaxBal().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString(),
                        amtusd.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
            }
        }
        return null;
    }

    /**
     * 是否校验crm客户信息
     *
     * @return
     */
    public boolean isCheckInfoCrm() {
        String tblKey = codetableService.getTblLabel("OCRMCK", "OCRMCK");
        return StringUtils.equals(tblKey, "Y");
    }
}
