package com.brilliance.isc.common.forexmod.service.impl;

import com.brilliance.isc.bo.Pty;
import com.brilliance.isc.bo.Ptyrsk;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.forexmod.ForexmodVo;
import com.brilliance.isc.common.forexmod.RiskCustomerDisposeComponent;
import com.brilliance.isc.common.forexmod.service.ForexmodService;
import com.brilliance.isc.esb.model.ccrm.ResOC51;
import com.brilliance.isc.esb.model.ccrm.ResOC52;
import com.brilliance.isc.esb.model.inner.InnerRes;
import com.brilliance.isc.mda.dao.PtyMapper;
import com.brilliance.isc.mda.dao.PtyrskMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 
 * @date 2024-08-23
 * @description
 */
@Service
public class ForexmodServiceImpl implements ForexmodService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ForexmodServiceImpl.class);

    @Resource
    private RiskCustomerDisposeComponent riskCustomerDisposeComponent;

    @Resource
    private PtyMapper ptyMapper;

    @Resource
    private PtyrskMapper ptyrskMapper;

    private static Map<String, String> customLabelMap;

    static {
        customLabelMap = new HashMap<>();
        customLabelMap.put("01","贸易外汇收支便利化试点客户");
        customLabelMap.put("02","高水平开放优质企业");
        customLabelMap.put("03","无");
    }

    @Override
    public ForexmodVo search(final String ptyinr) {
        if (StringUtils.isEmpty(ptyinr)) {
            ForexmodVo forexmodVo = new ForexmodVo();
            forexmodVo.setErrorMsg("请输入外汇展业客户号！");
            return forexmodVo;
        }
        Pty pty = ptyMapper.selectByPtyinr(ptyinr);
        InnerRes<ResOC51> resOC51InnerRes = riskCustomerDisposeComponent.OC51(pty.getExtkey());
        if (resOC51InnerRes!=null&& "AAAAAAA".equals(resOC51InnerRes.header.code)){
            ResOC51 resOC51 = resOC51InnerRes.DATA;
            if (resOC51 != null){
                ForexmodVo forexmod = new ForexmodVo();
                forexmod.setType(resOC51.CUST_FOREIGN_CROSS_TYPE);
                forexmod.setRiskLevel(resOC51.RISK_LEVEL_CRM);
                forexmod.setState(resOC51.CUST_STATUS);
                if (StringUtils.isNotEmpty(resOC51.APPROVE_TIME)){
                    try {
                        forexmod.setApproveTime(new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("yyyyMMddHHmmss").parse(resOC51.APPROVE_TIME)));
                    } catch (ParseException e) {
                        LOGGER.error("resOC51.APPROVE_TIME不存在；{}",e);
                    }
                }
                forexmod.setState(resOC51.CUST_STATUS);
                Ptyrsk ptyrsk = ptyrskMapper.queryByPtyinr(ptyinr);
                if (ptyrsk != null&&StringUtils.isNotEmpty(ptyrsk.getCustomlabel())) {
                    String[] split = ptyrsk.getCustomlabel().split(",");
                    StringBuilder sb = new StringBuilder();
                    for (String s : split) {
                        sb.append(customLabelMap.get(s)).append("；");
                    }
                    forexmod.setCustomTags(sb.toString());
                }
                return forexmod;
            }
        }
        return null;
    }

    @Override
    public String downgrade(final String ptyinr, final String reason) {
        Pty pty = ptyMapper.selectByPtyinr(ptyinr);
        InnerRes<ResOC52> resOC52InnerRes = riskCustomerDisposeComponent.OC52(pty.getExtkey(), reason, SettleContext.getSessionUserVo().getLogName());
        if (resOC52InnerRes == null||resOC52InnerRes.DATA == null) {
            return "调用远程降级失败";
        }
        if (!"AAAAAAA".equals(resOC52InnerRes.header.code)){
            return resOC52InnerRes.header.description;
        }
        ResOC52 resOC52 = resOC52InnerRes.DATA;
        if ("1".equals(resOC52.RESULTFLAG)){
            return "";
        }else {
            return resOC52.MSG;
        }
    }


}
