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

import com.brilliance.isc.bo.Eca;
import com.brilliance.isc.common.bopset.service.EblmodService;
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.mda.runtime.mda.util.MdaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class CfaecaValidator extends AbstractModuleValidator<Eca> {

    @Autowired(required = false)
    EblmodService eblmodService;

    @Validate(path="cfabop.cfaeca.recgrp.bas.debtorcode")
    public ValidResult checkRecgrpBasDebtorcodeN1000(Eca eca){
        if (!eblmodService.isCheckField("X", eca.getActiontype())) {
            return null;
        }

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

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

        }

        return null;
    }

    @Validate(path="cfabop.cfaeca.recgrp.bas.maturity")
    public ValidResult checkRecgrpBasMaturityN1000(Eca eca){
        if (!eblmodService.isCheckField("X", eca.getActiontype())) {
            return null;
        }

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

        //校验到期日必须大于等于起息日
        if(!MdaUtils.isEmpty(eca.getValuedate())){
            if(MdaUtils.compareTo(eca.getMaturity(),eca.getValuedate()) < 0){
                return ValidResult.build(false, "到期日必须大于等于起息日");
            }
        }

        return null;
    }


}
