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

import com.brilliance.isc.bo.Ead;
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 CfaeadValidator extends AbstractModuleValidator<Ead> {

    @Autowired(required = false)
    EblmodService eblmodService;

    @Autowired(required = false)
    CfapubpCheckRules cfapubpCheckRules;


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

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

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

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

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

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

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

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

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

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

    }


    //海外代付专属检核
    @Validate(path = "cfabop.cfaead.recgrp.bas.creditorname")
    public ValidResult checkCreditornamN1000(Ead ead) {
        if (!eblmodService.isCheckField("X", ead.getActiontype())) {
            return null;
        }

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

        return null;
    }

    @Validate(path = "cfabop.cfaead.recgrp.bas.anninrate")
    public ValidResult checkRecgrpBasAnninrateN1000(Ead ead) {
        if (!eblmodService.isCheckField("X", ead.getActiontype())) {
            return null;
        }

        //金额 不判断必填

        if (!MdaUtils.isEmpty(ead.getAnninrate()) && MdaUtils.compareTo(ead.getAnninrate(), 0) < 0) {
            return ValidResult.build(false, "年华利率必须大于等于0");
        }

        return null;
    }

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

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

            }
        }


        return null;
    }





}
