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

import com.brilliance.isc.bo.Ecb;
import com.brilliance.isc.bo.model.Cfaeca;
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.Dates;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class CfaecbValidator extends AbstractModuleValidator<Ecb> {

    @Autowired(required = false)
    EblmodService eblmodService;

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.actiondesc")
    public ValidResult checkActiondescN1000(Ecb ecb) {
        if ("D".equals(ecb.getActiontype())) {
            if (MdaUtils.isEmpty(ecb.getActiondesc())) {
                return ValidResult.build(false, "必输项");
            }
        }
        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.actiontype")
    public ValidResult checkActiontypeN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }

        if (MdaUtils.isEmpty(ecb.getActiontype())) {
            return ValidResult.build(false, "必输项");
        }
        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.buscode")
    public ValidResult checkBuscodeN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }

        //原逻辑会找 trnmod 进行判断，这里直接按照规范设置必填
        if (MdaUtils.isEmpty(ecb.getBuscode())) {
            return ValidResult.build(false, "必输项");
        }
        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.changedate")
    public ValidResult checkChangedateN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }

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

        //变动日期不能小于起息日
        Cfaeca cfaeca = (Cfaeca) getParent();
        if (!MdaUtils.isEmpty(cfaeca.getRecgrp().getBas().getValuedate())) {
            if (Dates.diff(ecb.getChangedate(), cfaeca.getRecgrp().getBas().getValuedate()) < 0) {
                return ValidResult.build(false, "变动日期不能小于起息日");
            }
        }
        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.endbalan")
    public ValidResult checkEndbalanN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }

        //金额均会默认 0.00, MdaUtils.isEmpty 会将 0.00识别为空， 因此不进行必填校验
//        if (MdaUtils.isEmpty(ecb.getEndbalan())) {
//            return ValidResult.build(false, "必输项");
//        }

        //期末余额不能大于签约余额
        Cfaeca cfaeca = (Cfaeca) getParent();
        if (!MdaUtils.isEmpty(cfaeca.getRecgrp().getBas().getContractamount())) {
            if (MdaUtils.compareTo(ecb.getEndbalan(), cfaeca.getRecgrp().getBas().getContractamount()) > 0) {
                return ValidResult.build(false, "期末余额不能大于签约金额");
            }
        }

        //第一次报送变动信息中期末余额不应为 0
        if (MdaUtils.isEmpty(cfaeca.getRecgrp().getBas().getInr()) || "0001".equals(ecb.getChangeno())) {
            if (MdaUtils.compareTo(ecb.getEndbalan(), 0) == 0) {
                return ValidResult.build(false, "第一次报送变动信息中期末余额不应为 0");
            }
        }
        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.inpayamount")
    public ValidResult checkInpayamountN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }

        if (MdaUtils.compareTo(ecb.getInpayamount(), 0) < 0) {
            return ValidResult.build(false, "付息金额必须大于等于0");
        }
        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.inpaycurr")
    public ValidResult checkInpaycurrN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }

        if (MdaUtils.compareTo(ecb.getInpayamount(), 0) > 0 || MdaUtils.compareTo(ecb.getPinpayamount(), 0) > 0) {
            if (MdaUtils.isEmpty(ecb.getInpaycurr())) {
                return ValidResult.build(false, "必输项");
            }
        }
        //付息币种应等于贷款币种
        Cfaeca cfaeca = (Cfaeca) getParent();
        if (!MdaUtils.isEmpty(ecb.getInpaycurr())) {
            if (MdaUtils.compareTo(ecb.getInpaycurr(), cfaeca.getRecgrp().getBas().getCurrence()) != 0) {
                return ValidResult.build(false, "付息币种应等于贷款币种");
            }
        }
        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.loanopenbalan")
    public ValidResult checkLoanopenbalanN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }

        //金额均会默认 0.00, MdaUtils.isEmpty 会将 0.00识别为空， 因此不进行必填校验
//        if (MdaUtils.isEmpty(ecb.getLoanopenbalan())) {
//            return ValidResult.build(false, "必输项");
//        }

        //第一次报送变动信息中期初余额应为 0
        Cfaeca cfaeca = (Cfaeca) getParent();
        if (MdaUtils.isEmpty(cfaeca.getRecgrp().getBas().getInr()) || "0001".equals(ecb.getChangeno())) {
            if (MdaUtils.compareTo(ecb.getLoanopenbalan(), 0) != 0) {
                return ValidResult.build(false, "第一次报送变动信息中期初余额应为 0");
            }
        }

        if (MdaUtils.compareTo(ecb.getLoanopenbalan(), cfaeca.getRecgrp().getBas().getContractamount()) > 0) {
            return ValidResult.build(false, "期初余额不能大于签约金额");
        }


        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.pinpayamount")
    public ValidResult checkPinpayamountN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }

        if (MdaUtils.compareTo(ecb.getPinpayamount(), 0) < 0) {
            return ValidResult.build(false, "购汇付息金额必须大于等于0");
        }

        if (MdaUtils.compareTo(ecb.getPinpayamount(), ecb.getInpayamount()) > 0) {
            return ValidResult.build(false, "购汇付息金额必须小于等于付息金额");
        }

        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.prepayamount")
    public ValidResult checkPrepayamountN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }

        if (!MdaUtils.isEmpty(ecb.getPrepayamount())) {
            Cfaeca cfaeca = (Cfaeca) getParent();
            if (MdaUtils.isEmpty(cfaeca.getRecgrp().getBas().getInr()) || "0001".equals(ecb.getChangeno())) {
                return ValidResult.build(false, "第一次报送变动信息不能包含还本信息");
            }

            if (MdaUtils.compareTo(ecb.getPrepayamount(), 0) < 0) {
                return ValidResult.build(false, "购汇还本金额必须大于等于0");
            }

            if (MdaUtils.compareTo(ecb.getPrepayamount(), ecb.getRepayamount()) > 0) {
                return ValidResult.build(false, "购汇还本金额必须小于等于还本金额");
            }
        }


        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.princurr")
    public ValidResult checkPrincurrN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }

        if (MdaUtils.compareTo(ecb.getRepayamount(), 0) > 0) {
            if(MdaUtils.isEmpty(ecb.getPrincurr())){
                return ValidResult.build(false, "必输项");
            }
        }

        if (!MdaUtils.isEmpty(ecb.getPrincurr())) {
            Cfaeca cfaeca = (Cfaeca) getParent();
            if (MdaUtils.isEmpty(cfaeca.getRecgrp().getBas().getInr()) || "0001".equals(ecb.getChangeno())) {
                return ValidResult.build(false, "第一次报送变动信息不能包含还本信息");
            }

            if (MdaUtils.compareTo(ecb.getPrincurr(), cfaeca.getRecgrp().getBas().getCurrence()) != 0) {
                return ValidResult.build(false, "还本币种应等于贷款币种");
            }

        }

        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.repayamount")
    public ValidResult checkRepayamountN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }

        if (!MdaUtils.isEmpty(ecb.getRepayamount())) {
            Cfaeca cfaeca = (Cfaeca) getParent();
            if (MdaUtils.isEmpty(cfaeca.getRecgrp().getBas().getInr()) || "0001".equals(ecb.getChangeno())) {
                return ValidResult.build(false, "第一次报送变动信息不能包含还本信息");
            }

            if (MdaUtils.compareTo(ecb.getRepayamount(), 0) < 0) {
                return ValidResult.build(false, "还本金额必须大于等于0");
            }
        }

        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.settamount")
    public ValidResult checkSettamountN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }

        if (MdaUtils.compareTo(ecb.getSettamount(), 0) < 0) {
            return ValidResult.build(false, "结汇金额必须大于等于0");
        }

        if (!MdaUtils.isEmpty(ecb.getSettamount())) {
            Cfaeca cfaeca = (Cfaeca) getParent();

            if (MdaUtils.compareTo(ecb.getSettamount(), cfaeca.getRecgrp().getBas().getContractamount()) > 0) {
                return ValidResult.build(false, "结汇金额不能大于签约金额");
            }
        }


        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.useofunds")
    public ValidResult checkUseofundsN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }

        if (MdaUtils.compareTo(ecb.getWithamount(), 0) > 0 || MdaUtils.compareTo(ecb.getSettamount(), 0) > 0) {
            if (MdaUtils.isEmpty(ecb.getUseofunds())) {
                return ValidResult.build(false, "必输项");
            }

        }

        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.withamount")
    public ValidResult checkWithamountN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }
        //第一次报送变动信息必须包含提款信息
        Cfaeca cfaeca = (Cfaeca) getParent();
        if (MdaUtils.isEmpty(cfaeca.getRecgrp().getBas().getInr()) || "0001".equals(ecb.getChangeno())) {
            //老国结金额为 0 也是判断为空， 新国结一样判断 0 为空
            if (MdaUtils.isEmpty(ecb.getWithamount())) {
                return ValidResult.build(false, "第一次报送变动信息必须包含提款信息");
            }

        }

        if (!MdaUtils.isEmpty(ecb.getWithamount())) {
            if (MdaUtils.compareTo(ecb.getWithamount(), cfaeca.getRecgrp().getBas().getContractamount()) > 0) {
                return ValidResult.build(false, "提款金额不能大于签约金额");
            }

            if (MdaUtils.compareTo(ecb.getWithamount(), 0) < 0) {
                return ValidResult.build(false, "提款金额必须大于等于0");
            }
        }

        return null;
    }

    @Validate(path = "cfabop.cfaeca.recgrp.dcl.%s.withcurrence")
    public ValidResult checkWithcurrenceN1000(Ecb ecb) {
        if(!eblmodService.isCheckField("X",ecb.getActiontype())){
            return null;
        }
        //第一次报送变动信息必须包含提款信息
        Cfaeca cfaeca = (Cfaeca) getParent();
        if (MdaUtils.isEmpty(cfaeca.getRecgrp().getBas().getInr()) || "0001".equals(ecb.getChangeno())) {
            //金额均会默认 0.00, MdaUtils.isEmpty 会将 0.00识别为空， 因此不进行必填校验
            if (MdaUtils.isEmpty(ecb.getWithcurrence())) {
                return ValidResult.build(false, "第一次报送变动信息必须包含提款信息");
            }

        }

        if (MdaUtils.compareTo(ecb.getWithamount(), 0) > 0) {
            if(MdaUtils.isEmpty(ecb.getWithcurrence())){
                return ValidResult.build(false, "必输项");
            }
        }

        if (!MdaUtils.isEmpty(ecb.getWithcurrence())) {
            if (MdaUtils.compareTo(ecb.getWithcurrence(), cfaeca.getRecgrp().getBas().getCurrence()) != 0) {
                return ValidResult.build(false, "提款币种应等于贷款币种");
            }
        }

        return null;
    }


}
