package com.brilliance.isc.common.transaction.newcheck.trndoc;

import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.sysmod.SysmodService;
import com.brilliance.isc.common.transaction.newcheck.AbstractModuleValidator;
import com.brilliance.isc.common.transaction.newcheck.ValidResult;
import com.brilliance.isc.common.transaction.newcheck.Validate;
import com.brilliance.isc.common.trndoc.vo.DoceotVo;
import com.brilliance.isc.common.trndoc.vo.TrndocVo;
import com.brilliance.isc.common.trnmod.service.TrnmodService;
import com.brilliance.mda.runtime.mda.util.Inis;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class TrndocValidator extends AbstractModuleValidator<TrndocVo> {

    @Autowired
    private DoceotValidator doceotValidator;

    @Autowired
    private TrnmodService trnmodService;

    @Autowired
    private SysmodService sysmodService;

    @Override
    @PostConstruct
    public void register() {
        super.register();
        doceotValidator.register();
    }

    @Override
    public Map<String, String> validate(TrndocVo vo) {
        Map<String, String> maps = new HashMap<>();
        maps.putAll(super.validate(vo));
        //验证报文面函条目
        doceotValidator.registerParent(vo);
        List<DoceotVo> doceots = vo.getDoceot();
        for (int i = 0; i < doceots.size(); i++) {
            maps.putAll(doceotValidator.validate(doceots.get(i), i));
        }
        doceotValidator.removeTl();
        return maps;
    }

    @Validate(path = "docpan.doceot")
    public ValidResult checkDoceot(TrndocVo vo) {
        //收单行Message页面和Attachment页面是看不见的，
        //所以检核只在非收单行和押汇中心起效。
        //要求只把SWIFT作为附件绑定到TCO报文上。
        if (!sysmodService.isSdhBusiness()) {
            //非收单行经办业务
            int gridcnt = vo.getDoceot().size();
            String a = "";
            String b = "";
            boolean swtflg = false;
            if (gridcnt > 0) {
                for (int i = 0; i < gridcnt; i++) {
                    if (!MdaUtils.isEmpty(vo.getDoceot().get(i).getId())) {
                        if (MdaUtils.compareTo(vo.getDoceot().get(i).getCortyp(), "TCO") == 0) {
                            a = "A";
                        }
                        if (MdaUtils.compareTo(vo.getDoceot().get(i).getCortyp(), "TCA") == 0) {
                            b = "B";
                        }
                        if (MdaUtils.compareTo(vo.getDoceot().get(i).getCortyp(), "SWT") == 0) {
                            swtflg = true;
                        }
                    }
                }
            }
            if (swtflg) {
                if (MdaUtils.compareTo(a, "A") == 0 && MdaUtils.compareTo(b, "B") != 0) {
                    String frame = SettleContext.getTransName();
                    String flg = Inis.getIniStr("TCD.INI", frame, "FLG");
                    if (MdaUtils.compareTo(flg, "1") == 0) {
                        return ValidResult.build(false, "请在Attachments页面下，将SWIFT作为附件绑定到TCO报文下！");
                    }
                }
            }
        }
        return null;
    }
}
