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

import com.brilliance.isc.common.component.ActmodComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.transaction.newcheck.*;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.DnfmodVo;
import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.Decimals;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.Date;

@Component
public class DnfmodValidator extends AbstractModuleValidator<DnfmodVo> {

    @Autowired
    private ActmodComponent actmodComponent;

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

    @Validate(path = "dnfmod.kehuzh")
    public ValidResult checkKehuzh(DnfmodVo dnfmodVo){
        if( MdaUtils.compareTo(dnfmodVo.getCcvtyp(),"C") == 0 ) {
            if( MdaUtils.compareTo(dnfmodVo.getJiluzt(),"1") == 0 ) {
                return ValidResult.build(false, Strings.formatText("当前$1账号已经在核心销户，国结不允许进行操作！",dnfmodVo.getKehuzh()));
            }
        }
        return null;
    }

    @Validate(path = "dnfmod.lxzyzh")
    public ValidResult checkLxzyzh(DnfmodVo dnfmodVo){
        //!利息转入账号的校验
        if( ! MdaUtils.isEmpty(dnfmodVo.getZhqtyp()) ) {
            if( MdaUtils.isEmpty(dnfmodVo.getLxzyzh()) ) {
                if( ! MdaUtils.isEmpty(dnfmodVo.getQixirq()) ) {
                    String yy = Strings.mid(dnfmodVo.getQixirq(),1,4);
                    String mm = Strings.mid(dnfmodVo.getQixirq(),5,2);
                    String dd = Strings.mid(dnfmodVo.getQixirq(),7,2);
                    Date dqxrq = Utils.parseDate(yy + "-" + mm + "-" + dd);
                    if( Dates.diff(dqxrq, Dates.today())<0 ) {
                        return errorMandatory();
                    }
                }
            } else {
                if( MdaUtils.compareTo(dnfmodVo.getIntdsp(),"LO") == 0 ) {
                    if( MdaUtils.compareTo(Strings.mid(dnfmodVo.getLxzyzh(),1,2),"FT") != 0 && MdaUtils.compareTo(Strings.mid(dnfmodVo.getLxzyzh(),1,2),"EF") != 0 && MdaUtils.compareTo(Strings.mid(dnfmodVo.getLxzyzh(),1,3),"ZMQ") != 0 ) {
                        if( ! actmodComponent.chkActMac ( dnfmodVo.getLxzyzh() ) ) {
                            return ValidResult.build(false,"客户帐号合法性校验错误");
                        }
                    }
                } else {
                    Argument<String> errBox = new Argument<String>();
                    if( ! actmodComponent.checkAccount(dnfmodVo.getLxzyzh(),dnfmodVo.getIntdsp(), SettleContext.getUserSession().getBch().getBranch(),dnfmodVo.getCur(),"N",errBox) ) {
                        return ValidResult.build(false,errBox.value);
                    }
                }
            }
        }
        return null;
    }

    @Validate(path = "dnfmod.qicuje")
    public ValidResult checkQicuje(DnfmodVo dnfmodVo){
        if( MdaUtils.compareTo(dnfmodVo.getZhqtyp(),"B") == 0 ) {
            if( MdaUtils.isEmpty(dnfmodVo.getQicuje()) ) {
                return ValidResult.build(false,"起存金额为空，请点Get获取！");
            }
        }
        return null;
    }

    @Validate(path = "dnfmod.tizamt")
    public ValidResult checkTizamt(DnfmodVo dnfmodVo){
        //!提支金额校验
        if( MdaUtils.compareTo(dnfmodVo.getZhqtyp(),"B") == 0 ) {
            if( MdaUtils.isEmpty(dnfmodVo.getTizamt()) ) {
                return errorMandatory();
            } else {
                //与余额的校验
                BigDecimal tizMax = Decimals.sub(dnfmodVo.getZhhuye(),dnfmodVo.getQicuje());
                if( MdaUtils.compareTo(dnfmodVo.getTizamt(),tizMax) > 0 ) {
                    return ValidResult.build(false,"金额输入有误!错误原因：部分提支金额<=(帐户余额-起存金额)");
                }
            }
        }
        return null;
    }

    @Validate(path = "dnfmod.yewudh")
    public ValidResult checkYewudh(DnfmodVo dnfmodVo){
        //!检查业务代号
        String rflg = "";
        if( Strings.len(dnfmodVo.getKehuzh()) == 17 ) {
            rflg = Strings.mid(dnfmodVo.getKehuzh(),7,2);
        }
        if( Strings.len(dnfmodVo.getKehuzh()) == 21 ) {
            rflg = Strings.mid(dnfmodVo.getKehuzh(),11,2);
        }
        if( MdaUtils.compareTo(Strings.mid(dnfmodVo.getKehuzh(),1,3),"ZMQ") == 0
                || MdaUtils.compareTo(Strings.mid(dnfmodVo.getKehuzh(),1,3),"NRA") == 0 ) {
            rflg = Strings.mid(dnfmodVo.getKehuzh(),10,2);
        }
        if( MdaUtils.compareTo(rflg,"81") == 0 ) {
            if(!MdaUtils.isEmpty(dnfmodVo.getYewudh())) {
                if((MdaUtils.compareTo(SettleContext.getTransName(),"GITENG") == 0
                    || MdaUtils.compareTo(SettleContext.getTransName(),"NITENG") == 0
                    || MdaUtils.compareTo(SettleContext.getTransName(),"HITENG") == 0) && ! MdaUtils.isEmpty(dnfmodVo.getZhqtyp()) ) {
                    if( MdaUtils.compareTo(dnfmodVo.getYewudh(),"0436") != 0 ) {
                        return ValidResult.build(false,"非担保定期保证金业务代号，不支持此操作，请重新选择账号！");
                    }
                }
                if( ( MdaUtils.compareTo(SettleContext.getTransName(),"LITENG") == 0
                        || MdaUtils.compareTo(SettleContext.getTransName(),"DITENG") == 0 )
                        && ! MdaUtils.isEmpty(dnfmodVo.getZhqtyp()) ) {
                    if( MdaUtils.compareTo(dnfmodVo.getYewudh(),"0432") != 0 )
                    {
                        return ValidResult.build(false,"非信用证定期保证金业务代号，不支持此操作，请重新选择账号！");
                    }
                }
            }
        }
        return null;
    }

    @Validate(path = "dnfmod.zhqtyp")
    public ValidResult checkZhqtyp(DnfmodVo dnfmodVo){
        //!检查账户是否办理部分提支业务
        if( MdaUtils.compareTo(dnfmodVo.getCcvtyp(),"C") == 0 ) {
            if( MdaUtils.isEmpty(dnfmodVo.getZhqtyp()) ) {
                return errorMandatory();
            } else {
                if( MdaUtils.compareTo(dnfmodVo.getZhqtyp(),"B") == 0 && MdaUtils.compareTo(dnfmodVo.getZhqucs(),"0") != 0 ) {
                    return ValidResult.build(false,"该定期保证金账户已经办理过一次部分提支，不能再次办理，请选择\"存款销户");
                }
            }
        }
        return null;
    }

    @Validate(path = "dnfmod.zhruzh")
    public ValidResult checkZhruzh(DnfmodVo dnfmodVo){
        //!操作类型选择部分提支时，注入活期账户必输
        if(!MdaUtils.isEmpty(dnfmodVo.getZhqtyp()) ) {
            if( MdaUtils.isEmpty(dnfmodVo.getZhruzh()) ) {
                return errorMandatory();
            } else {
                Argument<String> errBox = new Argument<String>();
                if(!actmodComponent.checkAccount(dnfmodVo.getZhruzh(),dnfmodVo.getPridsp(),SettleContext.getUserSession().getBch().getBranch(),dnfmodVo.getCur(),"N",errBox) ) {
                    return ValidResult.build(false,errBox.value);
                }
            }
        }
        return null;
    }

    @Warning(path = "dnfmod.kehuzh")
    public WarningResult warningAccountExpiry(DnfmodVo dnfmodVo) {
        if ("C".equals(dnfmodVo.getCcvtyp()) && "C".equals(dnfmodVo.getZhqtyp())) {
            String matdatStr = dnfmodVo.getDaoqrq();
            if (StringUtils.isNotEmpty(matdatStr)) {
                Date matdat = Utils.parseDate(matdatStr, "yyyyMMdd");
                if (MdaUtils.compareTo(Dates.today(), matdat) < 0) {
                    return WarningResult.build("SUBACCT", "该笔定期保证金[ "+dnfmodVo.getKehuzh()+" ]未到期");
                }
            }
        }
        return WarningResult.buildEmpty("SUBACCT");
    }
}
