package com.brilliance.isc.common.transaction.check;

import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.trndoc.vo.DoceotVo;
import com.brilliance.isc.common.trndoc.vo.TrndocVo;
import com.brilliance.isc.common.util.I18nUtil;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.mda.runtime.mda.util.Inis;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@Component
public class TrndocCheck implements ICheck<TrndocVo> {

    @Resource
    DoceotCheck doceotCheck;

    @Resource
    PtsptaCheck ptsptaCheck;

    @Override
    public Map<String, String> check(TrndocVo vo) {
        Map<String, String> resultMap = new HashMap<>(checkDoceotN1000(vo));
        for (int i = 0; i < vo.getDoceot().size(); i++) {
            resultMap.putAll(doceotCheck.check(vo.getDoceot().get(i), i));
//    暂时屏蔽，等凯总定夺（国内名称地址拆开，界面不保留此栏位，但是国际药保留，国际是本地写还是在公共逻辑写？需加控制
//            resultMap.putAll(ptsptaCheck.check(vo.getDoceot().get(i).getRcv(), i));
        }
        return resultMap;
    }

    public boolean conCheck() {
        //如果是收单行做业务则返回false
        //其他返回true
        String lev = SettleContext.getUserSession().getBch().getLev();
        String upbchkey = SettleContext.getUserSession().getBch().getBranch();
        String userBachKey = SettleContext.getUserSession().getBch().getBchkey();
        String negflg = SettleContext.getUserSession().getAtp().getNegflg();
        if ("0".equals(lev) || StringUtils.isEmpty(upbchkey)) {
            return true;
        }
        return !"X".equals(negflg) || !StringUtils.isNotEmpty(userBachKey) || userBachKey.equals(upbchkey);
    }

    public Map<String, String> checkDoceotN1000(TrndocVo trndocVo) {
        Map<String, String> resultMap = new HashMap<>();
        //收单行Message页面和Attachment页面是看不见的，
        //所以检核只在非收单行和押汇中心起效。
        //要求只把SWIFT作为附件绑定到TCO报文上。
        String a;
        String b;
        boolean swtflg = false;
        String flg;
        if (conCheck()) {
            //非收单行经办业务
            int size = trndocVo.getDoceot().size();
            a = "";
            b = "";
            if (size > 0) {
                for (DoceotVo doceotVo : trndocVo.getDoceot()) {
                    if (!MdaUtils.isEmpty(doceotVo.getId())) {
                        if (MdaUtils.compareTo(doceotVo.getCortyp(), "TCO") == 0) {
                            a = "A";
                        }
                        if (MdaUtils.compareTo(doceotVo.getCortyp(), "TCA") == 0) {
                            b = "B";
                        }
                        if (MdaUtils.compareTo(doceotVo.getCortyp(), "SWT") == 0) {
                            swtflg = true;
                        }
                    }
                }
            }
            if (swtflg) {
                if (MdaUtils.compareTo(a, "A") == 0 && MdaUtils.compareTo(b, "B") != 0) {
                    flg = Inis.getIniStr("TCD.INI", SettleContext.getTransName(), "FLG");
                    if (MdaUtils.compareTo(flg, "1") == 0) {
                        resultMap.put("trndoc.doceot[c].cortyp", I18nUtil.getMessage("0047"));
                    }
                }
            }

        }
        return resultMap;
    }
}
