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

import com.brilliance.isc.bo.Ear;
import com.brilliance.isc.bo.Ecb;
import com.brilliance.isc.bo.model.Cfabop;
import com.brilliance.isc.common.transaction.newcheck.AbstractModuleValidator;
import com.brilliance.isc.common.transaction.newcheck.bopset.cfacheck.*;
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.List;
import java.util.Map;

/**
 * RMB报送检核
 */
@Component
public class CfaValidator extends AbstractModuleValidator<Cfabop> {

    @Autowired
    private CfaecaValidator cfaecaValidator;

    @Autowired
    private CfaecbValidator cfaecbValidator;

    @Autowired
    private CfaeadValidator cfaeadValidator;

    @Autowired
    private CfaeafValidator cfaeafValidator;

    @Autowired
    private CfaEadearValidator cfaEadearValidator;

    @Autowired
    private CfaEafearValidator cfaEafearValidator;


    @Override
    @PostConstruct
    public void register() {
        super.register();
        cfaecaValidator.register();
        cfaecbValidator.register();
        cfaeadValidator.register();
        cfaEadearValidator.register();
        cfaeafValidator.register();
        cfaEafearValidator.register();
    }

    @Override
    public Map<String, String> validate(Cfabop cfabop) {
        cfabop.reset();
        Map<String, String> maps = super.validate(cfabop);
        //国内外汇贷款 签约 检核
        cfaecaValidator.registerParent(cfabop);
        if (!MdaUtils.isEmpty(cfabop.getCfaeca())) {
            if (MdaUtils.compareTo(cfabop.getCfaeca().getBasflg(), "X") == 0) {
                maps.putAll(cfaecaValidator.validate(cfabop.getCfaeca().getRecgrp().getBas()));
            }
        }

        cfaecaValidator.removeTl();

        //国内外汇贷款 变动 检核
        cfaecbValidator.registerParent(cfabop.getCfaeca());
        if (!MdaUtils.isEmpty(cfabop.getCfaeca())) {
            if (MdaUtils.compareTo(cfabop.getCfaeca().getDclflg(), "X") == 0) {
                List<Ecb> ecblist = cfabop.getCfaeca().getRecgrp().getDcl();
                for (int i = 0; i < ecblist.size(); i++) {
                    maps.putAll(cfaecbValidator.validate(ecblist.get(i), i));
                }
            }
        }

        cfaecbValidator.removeTl();

        //海外代付 签约 检核
        cfaeadValidator.registerParent(cfabop);
        if (!MdaUtils.isEmpty(cfabop.getCfaead())) {
            if (MdaUtils.compareTo(cfabop.getCfaead().getBasflg(), "X") == 0) {
                maps.putAll(cfaeadValidator.validate(cfabop.getCfaead().getRecgrp().getBas()));
            }
        }

        cfaeadValidator.removeTl();

        //海外代付 变动 检核
        cfaEadearValidator.registerParent(cfabop.getCfaead());
        if (!MdaUtils.isEmpty(cfabop.getCfaead())) {
            if (MdaUtils.compareTo(cfabop.getCfaead().getDclflg(), "X") == 0) {
                List<Ear> earlist = cfabop.getCfaead().getRecgrp().getDcl();
                for (int i = 0; i < earlist.size(); i++) {
                    maps.putAll(cfaEadearValidator.validate(earlist.get(i), i));
                }
            }
        }

        cfaEadearValidator.removeTl();

        //远期信用证 签约 检核
        cfaeafValidator.registerParent(cfabop);
        if (!MdaUtils.isEmpty(cfabop.getCfaeaf())) {
            if (MdaUtils.compareTo(cfabop.getCfaeaf().getBasflg(), "X") == 0) {
                maps.putAll(cfaeafValidator.validate(cfabop.getCfaeaf().getRecgrp().getBas()));
            }
        }

        cfaeafValidator.removeTl();

        //远期信用证 变动 检核
        cfaEafearValidator.registerParent(cfabop.getCfaeaf());
        if (!MdaUtils.isEmpty(cfabop.getCfaeaf())) {
            if (MdaUtils.compareTo(cfabop.getCfaeaf().getDclflg(), "X") == 0) {
                List<Ear> earlist = cfabop.getCfaeaf().getRecgrp().getDcl();
                for (int i = 0; i < earlist.size(); i++) {
                    maps.putAll(cfaEafearValidator.validate(earlist.get(i), i));
                }
            }
        }

        cfaEafearValidator.removeTl();

        return maps;
    }


}
