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

import com.brilliance.isc.bo.model.Dfdgrp;
import com.brilliance.isc.bo.model.Dftp;
import com.brilliance.isc.common.context.SettleContext;
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.Dates;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

import static com.brilliance.mda.runtime.mda.Constants.PanStaAdd;
import static com.brilliance.mda.runtime.mda.Constants.PanStaEdit;

@Component
public class DftpValidator extends AbstractModuleValidator<Dftp> {

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

    /**
     *   source:dftp.0004.script
     */
    @Validate(path = "dftcre.dfdgrp.rec.stttendat")
    public ValidResult checkDfdgrpRecStttendatN1000(Dftp vo){
        Dfdgrp dfdgrp = (Dfdgrp)getParent();
        if(!MdaUtils.isEmpty(dfdgrp.getRec().getDfuflg())) {
            if( MdaUtils.isEmpty(dfdgrp.getRec().getStttendat()) ) {
                return errorMandatory();
            } else {
                if( Dates.diff(dfdgrp.getRec().getStttendat(), dfdgrp.getRec().getMatdat()) > 0 && ! MdaUtils.isEmpty(dfdgrp.getRec().getMatdat())) {
                    return ValidResult.build(false, "代付起始日不得大于代付到期日");
                }
            }
        }
        return null;
    }

    /**
     *   source:dftp.0007.script
     */
    @Validate(path = "dftcre.dfdgrp.rec.matdat")
    public ValidResult checkDfdgrpRecMatdatN1000(Dftp vo){
        Dfdgrp dfdgrp = (Dfdgrp)getParent();
        if(!MdaUtils.isEmpty(dfdgrp.getRec().getDfuflg())) {
            if(MdaUtils.isEmpty(dfdgrp.getRec().getMatdat())) {
                return errorMandatory();
            }
        }
        /**
         if DFDGRP\\REC\\MATDAT < DFDGRP\\REC\\STTTENDAT then
         Error( 'L0000001' )
         endif
         **/

        return null;
    }

    /**
     *   source:dftp.0009.script
     */
    @Validate(path = "dftcre.dfdgrp.cbs.max.cur")
    public ValidResult checkDfdgrpCbsMaxCurN1000(Dftp vo){
        if(MdaUtils.compareTo(vo.getPansta(),PanStaAdd) == 0
                || MdaUtils.compareTo(vo.getPansta(),PanStaEdit) == 0) {
            Dfdgrp dfdgrp = (Dfdgrp)getParent();
            if(MdaUtils.isEmpty(dfdgrp.getCbs().getMax().getCur())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:dftp.0010.script
     */
    @Validate(path = "dftcre.dfdgrp.cbs.max.amt")
    public ValidResult checkDfdgrpCbsMaxAmtN1000(Dftp vo){
        if(MdaUtils.compareTo(vo.getPansta(), PanStaAdd) == 0
                || MdaUtils.compareTo(vo.getPansta(), PanStaEdit) == 0) {
            Dfdgrp dfdgrp = (Dfdgrp)getParent();
            if(MdaUtils.isEmpty(dfdgrp.getCbs().getMax().getAmt())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:dftp.0011.script
     */
    @Validate(path = "dftcre.dfdgrp.rec.trntyp")
    public ValidResult checkDfdgrpRecTrntypN1000(Dftp vo){
        if(MdaUtils.compareTo(vo.getPansta(),PanStaAdd) == 0
                || MdaUtils.compareTo(vo.getPansta(),PanStaEdit) == 0) {
            Dfdgrp dfdgrp = (Dfdgrp)getParent();
            if(MdaUtils.isEmpty(dfdgrp.getRec().getTrntyp())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:dftp.0012.script
     */
    @Validate(path = "dftcre.dfdgrp.apl.pts.extkey")
    public ValidResult checkDfdgrpAplPtsExtkeyN1000(Dftp vo){
        if(MdaUtils.compareTo(vo.getPansta(),PanStaAdd) == 0
                || MdaUtils.compareTo(vo.getPansta(),PanStaEdit) == 0) {
            Dfdgrp dfdgrp = (Dfdgrp)getParent();
            if(MdaUtils.isEmpty(dfdgrp.getApl().getPts().getExtkey())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:dftp.0013.script
     */
    @Validate(path = "dftcre.dfdgrp.dff.pts.extkey")
    public ValidResult checkDfdgrpDffPtsExtkeyN1000(Dftp vo){
        if(MdaUtils.compareTo(vo.getPansta(),PanStaAdd) == 0
                || MdaUtils.compareTo(vo.getPansta(),PanStaEdit) == 0) {
            Dfdgrp dfdgrp = (Dfdgrp)getParent();
            if(MdaUtils.isEmpty(dfdgrp.getDff().getPts().getExtkey())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:dftp.0016.script
     */
    @Validate(path = "dftcre.dfdgrp.rec.actrat")
    public ValidResult checkDfdgrpRecActratN1000(Dftp vo){
        Dfdgrp dfdgrp = (Dfdgrp)getParent();
        if(!MdaUtils.isEmpty(dfdgrp.getRec().getDfuflg())) {
            if(MdaUtils.compareTo(dfdgrp.getRec().getActrat(),0) == 0
                    || MdaUtils.isEmpty(dfdgrp.getRec().getActrat())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:dftp.0017.script
     */
    @Validate(path = "dftcre.dfdgrp.rec.intprd")
    public ValidResult checkDfdgrpRecIntprdN1000(Dftp vo){
        if( MdaUtils.compareTo(vo.getPansta(),PanStaAdd) == 0
                || MdaUtils.compareTo(vo.getPansta(),PanStaEdit) == 0 ) {
            Dfdgrp dfdgrp = (Dfdgrp)getParent();
            if(MdaUtils.compareTo(dfdgrp.getRec().getFintyp(),"A") == 0
                    && MdaUtils.isEmpty(dfdgrp.getRec().getIntprd())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:dftp.0018.script
     */
    @Validate(path = "dftcre.dfdgrp.rec.flttyp")
    public ValidResult checkDfdgrpRecFlttypN1000(Dftp vo){
        if(MdaUtils.compareTo(vo.getPansta(),PanStaAdd) == 0 || MdaUtils.compareTo(vo.getPansta(),PanStaEdit) == 0) {
            Dfdgrp dfdgrp = (Dfdgrp)getParent();
            if(MdaUtils.compareTo(dfdgrp.getRec().getFintyp(),"A") == 0
                    && MdaUtils.isEmpty(dfdgrp.getRec().getFlttyp())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:dftp.0019.script
     */
    @Validate(path = "dftcre.dfdgrp.rec.bankno")
    public ValidResult checkDfdgrpRecBanknoN1000(Dftp vo){
        if( MdaUtils.compareTo(vo.getPansta(),PanStaAdd) == 0 || MdaUtils.compareTo(vo.getPansta(),PanStaEdit) == 0 ) {
            Dfdgrp dfdgrp = (Dfdgrp)getParent();
            if( MdaUtils.compareTo(dfdgrp.getRec().getFintyp(),"A") == 0
                    && MdaUtils.isEmpty(dfdgrp.getRec().getBankno())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:dftp.0020.script
     */
    @Validate(path = "dftcre.dfdgrp.rec.bankcn")
    public ValidResult checkDfdgrpRecBankcnN1000(Dftp vo){
        if(MdaUtils.compareTo(vo.getPansta(),PanStaAdd) == 0
                || MdaUtils.compareTo(vo.getPansta(),PanStaEdit) == 0) {
            Dfdgrp dfdgrp = (Dfdgrp)getParent();
            if( MdaUtils.compareTo(dfdgrp.getRec().getFintyp(),"A") == 0 && MdaUtils.isEmpty(dfdgrp.getRec().getBankcn())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:dftp.0023.script
     */
    /*@Validate(path = "msgtyp")
    public ValidResult checkMsgtypN1000(Dftp vo){
        if( Systems.isEnabled(this,"msgtyp") && Systems.isVisible(this,"msgtyp") && ( MdaUtils.compareTo(ctx.getTransName(),"DFTSET") == 0 || MdaUtils.compareTo(ctx.getTransName(),"DFTMSG") == 0 ) )
        {
            if( MdaUtils.isEmpty(this.getMsgtyp()) )
            {
                Platform.errorMandatory();
            }
            else
            {
                //校验是否2.0和报文类型是否匹配
                if( ( MdaUtils.compareTo(this.getUpdsta(),"1") == 0 && MdaUtils.compareTo(this.getMsgtyp(),"111") != 0 ) || ( MdaUtils.compareTo(this.getUpdsta(),"0") == 0 && MdaUtils.compareTo(this.getMsgtyp(),"111") == 0 ) )
                {
                    if( MdaUtils.compareTo(this.getUpdsta(),"1") == 0 )
                    {
                        ctx.setErrorMessage(MdaUtils.getI18NString("dftp","CT000149"));
                    }
                    else
                    {
                        ctx.setErrorMessage(MdaUtils.getI18NString("dftp","CT000150"));
                    }
                }
            }
        }


        return true;
    }*/

    /**
     *   source:dftp.0027.script
     */
    @Validate(path = "commet")
    public ValidResult checkCommetN1000(Dftp vo){
        if( MdaUtils.compareTo(vo.getFmttyp(),"112") == 0 ) {
            //2.0升级，112类型报文附言最大长度256，不需要限制此栏位长度
        } else {
            if(!MdaUtils.isEmpty(vo.getCommet()) && Strings.len(vo.getCommet()) > 136) {
                return ValidResult.build(false, "附言太长，必须<=136个字符！");
            }
        }
        return null;
    }

    /**
     *   source:dftp.0028.script
     */
    /*@Check(target="resbchnam",order=1000)
    public boolean checkResbchnamN1000(){
        IContext ctx = MdaEnv.getContext();

        if( MdaUtils.isEmpty(this.getResbchnam()) && Systems.isEnabled(this,"resbchnam") && Systems.isVisible(this,"resbchnam") )
        {
            Platform.errorMandatory();
        }


        return true;
    }*/

    /**
     *   source:dftp.0029.script
     */
    @Validate(path="dftcre.dfdgrp.rec.resamt")
    public ValidResult checkDfdgrpRecResamtN1001(Dftp vo){
        if(MdaUtils.compareTo(SettleContext.getTransName(),"DFTOPN") == 0 || MdaUtils.compareTo(SettleContext.getTransName(),"DFTADD") == 0) {
            Dfdgrp dfdgrp = (Dfdgrp)getParent();
            if( MdaUtils.compareTo(dfdgrp.getRec().getDfuflg(),"2") == 0 ) {
                if(!dfdgrp.getRec().isModified("resamt") ) {
                    if( MdaUtils.isEmpty(dfdgrp.getRec().getResamt()) ) {
                        return errorMandatory();
                    }
                }
            }
        }
        return null;
    }

    /**
     *   source:dftp.0034.script
     */
    /*@Check(target="updsta",order=1000)
    public boolean checkUpdstaN1000(){
        IContext ctx = MdaEnv.getContext();

        if( MdaUtils.compareTo(ctx.getTransName(),"DFTMSG") == 0 || MdaUtils.compareTo(ctx.getTransName(),"DFTSET") == 0 )
        {
            if( Systems.isVisible(this,"fmtpan") && ! MdaUtils.isEmpty(this.getMsgtyp()) && MdaUtils.isEmpty(this.getUpdsta()) )
            {
                Platform.errorMandatory();
            }
        }


        return true;
    }*/

    /**
     *   source:dftp.0036.script
     */
    /*@Check(target="fmttyp",order=1000)
    public boolean checkFmttypN1000(){
        IContext ctx = MdaEnv.getContext();

        if( Systems.isVisible(this,"fmttyp") && Systems.isEnabled(this,"fmttyp") && MdaUtils.isEmpty(this.getFmttyp()) )
        {
            Platform.errorMandatory();
        }


        return true;
    }*/

    /**
     *   source:dftp.0038.script
     */
    /*@Check(target="orcnam",order=1000)
    public boolean checkOrcnamN1000(){
        IContext ctx = MdaEnv.getContext();

        String orcnam = NULLSTR;
        String errMarks = NULLSTR;
        if( Systems.isVisible(this,"orcnam") && Systems.isEnabled(this,"orcnam") )
        {
            if( MdaUtils.compareTo(this.getFmttyp(),"112") == 0 )
            {
                if( ! MdaUtils.isEmpty(this.getOrcnam()) )
                {
                    orcnam = Strings.getLine(this.getOrcnam(),1) + Strings.getLine(this.getOrcnam(),2);
                    Argument<String> errMarksBox = new Argument<String>();
                    if( checkmarks2( orcnam, errMarksBox ) )
                    {
                        Platform.errorUnavailableChars(MdaDriver.getDatafield(this,"orcnam"),errMarks);
                    }

                    errMarks = errMarksBox.value;

                    if( MdaUtils.compareTo(getCharNum( orcnam ),60)>0 )
                    {
                        ctx.setFieldError(this,"orcnam",MdaUtils.getI18NString("dftp","CT000134"));
                    }
                }
                else
                {
                    if( MdaUtils.isEmpty(this.getOrcadr()) && Systems.isVisible(this,"orcadr") && Systems.isEnabled(this,"orcadr") )
                    {
                        ctx.setFieldError(this,"orcnam",MdaUtils.getI18NString("dftp","CT000135"));
                        ctx.setFieldError(this,"orcnam",MdaUtils.getI18NString("dftp","CT000136"));
                    }
                }
            }
        }


        return true;
    }*/

    /**
     *   source:dftp.0039.script
     */
    /*@Check(target="pyenam",order=1000)
    public boolean checkPyenamN1000(){
        IContext ctx = MdaEnv.getContext();

        String pyenam = NULLSTR;
        String errMarks = NULLSTR;
        if( Systems.isVisible(this,"pyenam") && Systems.isEnabled(this,"pyenam") )
        {
            if( MdaUtils.compareTo(this.getFmttyp(),"112") == 0 )
            {
                if( ! MdaUtils.isEmpty(this.getPyenam()) )
                {
                    pyenam = Strings.getLine(this.getPyenam(),1) + Strings.getLine(this.getPyenam(),2);
                    Argument<String> errMarksBox = new Argument<String>();
                    if( checkmarks2( pyenam, errMarksBox ) )
                    {
                        Platform.errorUnavailableChars(MdaDriver.getDatafield(this,"pyenam"),errMarks);
                    }

                    errMarks = errMarksBox.value;

                    if( MdaUtils.compareTo(getCharNum( pyenam ),60)>0 )
                    {
                        ctx.setFieldError(this,"pyenam",MdaUtils.getI18NString("dftp","CT000139"));
                    }
                }
                else
                {
                    if( MdaUtils.isEmpty(this.getPyeadr()) && Systems.isVisible(this,"pyeadr") && Systems.isEnabled(this,"pyeadr") )
                    {
                        ctx.setFieldError(this,"pyenam",MdaUtils.getI18NString("dftp","CT000140"));
                        ctx.setFieldError(this,"pyenam",MdaUtils.getI18NString("dftp","CT000141"));
                    }
                }
            }
        }


        return true;
    }*/

    /**
     *   source:dftp.0042.script
     */
    /*@Check(target="orcadr",order=1000)
    public boolean checkOrcadrN1000(){
        IContext ctx = MdaEnv.getContext();

        String orcadr = NULLSTR;
        String errMarks = NULLSTR;
        if( Systems.isVisible(this,"orcadr") && Systems.isEnabled(this,"orcadr") )
        {
            if( MdaUtils.compareTo(this.getFmttyp(),"112") == 0 )
            {
                if( ! MdaUtils.isEmpty(this.getOrcadr()) )
                {
                    orcadr = Strings.getLine(this.getOrcadr(),1) + Strings.getLine(this.getOrcadr(),2);
                    Argument<String> errMarksBox = new Argument<String>();
                    if( checkmarks2( orcadr, errMarksBox ) )
                    {
                        Platform.errorUnavailableChars(MdaDriver.getDatafield(this,"orcadr"),errMarks);
                    }

                    errMarks = errMarksBox.value;

                    if( MdaUtils.compareTo(getCharNum( orcadr ),70)>0 )
                    {
                        ctx.setFieldError(this,"orcadr",MdaUtils.getI18NString("dftp","CT000143"));
                    }
                }
            }
        }


        return true;
    }*/

    /**
     *   source:dftp.0043.script
     */
    /*@Check(target="pyeadr",order=1000)
    public boolean checkPyeadrN1000(){
        IContext ctx = MdaEnv.getContext();

        String pyeadr = NULLSTR;
        String errMarks = NULLSTR;
        if( Systems.isVisible(this,"pyeadr") && Systems.isEnabled(this,"pyeadr") )
        {
            if( MdaUtils.compareTo(this.getFmttyp(),"112") == 0 )
            {
                if( ! MdaUtils.isEmpty(this.getPyeadr()) )
                {
                    pyeadr = Strings.getLine(this.getPyeadr(),1) + Strings.getLine(this.getPyeadr(),2);
                    Argument<String> errMarksBox = new Argument<String>();
                    if( checkmarks2( pyeadr, errMarksBox ) )
                    {
                        Platform.errorUnavailableChars(MdaDriver.getDatafield(this,"pyeadr"),errMarks);
                    }

                    errMarks = errMarksBox.value;

                    if( MdaUtils.compareTo(getCharNum( pyeadr ),70)>0 )
                    {
                        ctx.setFieldError(this,"pyeadr",MdaUtils.getI18NString("dftp","CT000147"));
                    }
                }
            }
        }


        return true;
    }*/
}
