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

import com.brilliance.isc.bo.Eaf;
import com.brilliance.isc.common.bopset.service.EblmodService;
import com.brilliance.isc.common.transaction.newcheck.*;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class CfaeafValidator extends AbstractModuleValidator<Eaf> {

    @Autowired(required = false)
    EblmodService eblmodService;

    @Autowired(required = false)
    CfapubpCheckRules cfapubpCheckRules;

    @Validate(path = "cfabop.cfaeaf.recgrp.bas.actiondesc")
    public ValidResult checkActiondescN1000(Eaf eaf) {
        if ("D".equals(eaf.getActiontype())) {
            if (MdaUtils.isEmpty(eaf.getActiondesc())) {
                return ValidResult.build(false, "必输项");
            }
        }
        return null;
    }
    

    @Validate(path = "cfabop.cfaeaf.recgrp.bas.contractamount")
    public ValidResult checkContractamountN1000(Eaf eaf) {
        if (!eblmodService.isCheckField("X", eaf.getActiontype())) {
            return null;
        }
        return cfapubpCheckRules.checkContractamountN1000(eaf.getContractamount());
    }

    @Validate(path = "cfabop.cfaeaf.recgrp.bas.creditorcode")
    public ValidResult checkCreditorcodeN1000(Eaf eaf) {
        if (!eblmodService.isCheckField("X", eaf.getActiontype())) {
            return null;
        }
        return cfapubpCheckRules.checkCreditorcodeN1000(eaf.getCreditortype(), eaf.getCreditorcode(), eaf.getDebtype());
    }

    @Validate(path = "cfabop.cfaeaf.recgrp.bas.creditortype")
    public ValidResult checkCreditortypeN1000(Eaf eaf) {
        if (!eblmodService.isCheckField("X", eaf.getActiontype())) {
            return null;
        }
        return cfapubpCheckRules.checkCreditortypeN1000(eaf.getCreditortype(), eaf.getDebtype());
    }

    @Warning(path = "cfabop.cfaeaf.recgrp.bas.creditortype")
    public WarningResult warnCreditortypeN1000(Eaf eaf) {
        String subid = "CFAEAFCREDITORTYPE";
        if (!eblmodService.isCheckField("X", eaf.getActiontype())) {
            return WarningResult.buildEmpty(subid);
        }
        return cfapubpCheckRules.warnCreditortypeN1000(eaf.getCreditortype(), eaf.getDebtype(),subid);
    }

    @Validate(path = "cfabop.cfaeaf.recgrp.bas.maturity")
    public ValidResult checkMaturityN1000(Eaf eaf) {
        if (!eblmodService.isCheckField("X", eaf.getActiontype())) {
            return null;
        }
        return cfapubpCheckRules.checkMaturityN1000(eaf.getMaturity(), eaf.getValuedate());
    }

    @Validate(path = "cfabop.cfaeaf.recgrp.bas.opercode")
    public ValidResult checkOpercodeN1000(Eaf eaf) {
        if (!eblmodService.isCheckField("X", eaf.getActiontype())) {
            return null;
        }
        return cfapubpCheckRules.checkOpercodeN1000(eaf.getOpercode(), eaf.getCreditortype());
    }

    @Validate(path = "cfabop.cfaeaf.recgrp.bas.valuedate")
    public ValidResult checkValuedateN1000(Eaf eaf) {
        if (!eblmodService.isCheckField("X", eaf.getActiontype())) {
            return null;
        }
        return cfapubpCheckRules.checkValuedateN1000(eaf.getValuedate());
    }

    @Validate(path = "cfabop.cfaeaf.recgrp.bas.crehqcode")
    public ValidResult checkRecgrpBasCrehqcodeN1000(Eaf eaf) {
        if (!eblmodService.isCheckField("X", eaf.getActiontype())) {
            return null;
        }

        return cfapubpCheckRules.checkCrehqcodeN1000(eaf.getCrehqcode());

    }

    //远期信用证专属检核
    @Validate(path = "cfabop.cfaeaf.recgrp.bas.creditorname")
    public ValidResult checkCreditornamN1000(Eaf eaf) {
        if (!eblmodService.isCheckField("X", eaf.getActiontype())) {
            return null;
        }

        if (MdaUtils.isEmpty(eaf.getCreditorname()) && MdaUtils.isEmpty(eaf.getCreditornamen())) {
            return ValidResult.build(false, "债权人中文名称和英文名称至少填写一个");
        }

        return null;
    }

    @Validate(path = "cfabop.cfaeaf.recgrp.bas.appcode")
    public ValidResult checkRecgrpBasAppcodeN1000(Eaf eaf) {
        if (!eblmodService.isCheckField("X", eaf.getActiontype())) {
            return null;
        }

        if (MdaUtils.isEmpty(eaf.getAppcode())) {
            return ValidResult.build(false, "必输项");
        }


        if (!MdaUtils.isEmpty(eaf.getAppcode())) {
            //校验位数 和 合法性
            if (eaf.getAppcode().length() != 9) {
                return ValidResult.build(false, "组织机构代码位数错误(9位)");
            } else {
                if (!eblmodService.isValidEntpCode(eaf.getAppcode())) {
                    return ValidResult.build(false, "组织机构代码校验失败");
                }

            }
        }


        return null;
    }




}
