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

import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.transaction.newcheck.ValidResult;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;


@Component
public class CfaearpCheckRules {

    @Autowired(required = false)
    CodetableService codetableService;

    public ValidResult checkActiondescN1000(String actiondesc,String actiontype) {

        if( MdaUtils.compareTo(actiontype,"D") == 0 )
        {
            if( MdaUtils.isEmpty(actiondesc) )
            {
                return ValidResult.build(false, "必输项");
            }
        }

        return null;
    }

    public ValidResult checkActiontypeN1000(String actiontype){

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

        return null;
    }

    public ValidResult checkBuscodeN1000(String buscode){
        //调整检核逻辑，原建和逻辑判断无 TRNMOD 不校验必填，新逻辑按照接口规范，直接校验必填
            if(MdaUtils.isEmpty(buscode) )
            {
                return ValidResult.build(false, "必输项");
            }
        return null;
    }

    public ValidResult checkChamountN1000(BigDecimal chamount,BigDecimal contractamount,String changtype){
        //!变动金额的校验规则(业务提的需求)
        //!1.变动金额不能大于等于签约金额
        //!2.如果变动类型为“提款”，变动金额应等于签约金额
        //!========================================================

            if( MdaUtils.isEmpty(chamount) )
            {
                return ValidResult.build(false, "必输项");
            }
            else
            {
                if( ! MdaUtils.isEmpty(chamount) && MdaUtils.compareTo(chamount,0)<0 )
                {
                    return ValidResult.build(false, "变动金额必须大于等于0");
                }
                if( MdaUtils.compareTo(chamount,contractamount)>0 )
                {
                    return ValidResult.build(false, "变动金额不能大于签约金额");
                }
                else
                {
                    String subchangtype = Strings.mid(changtype,1,2);
                    if( MdaUtils.compareTo(subchangtype,"11") == 0 )
                    {
                        if( MdaUtils.compareTo(chamount,contractamount) != 0 )
                        {
                            return ValidResult.build(false, "变动金额应等于签约金额");
                        }
                    }
                }
            }

            //剔除 维护交易 AME 的检核判断，没意义

        return null;
    }

    public ValidResult checkChangtypeN1000(String changtype,String changeno,String basinr){

            if( MdaUtils.isEmpty(changtype) )
            {
                return ValidResult.build(false, "必输项");
            }
            else
            {
                String subchangtype = Strings.mid(changtype,1,2);
                if( MdaUtils.isEmpty(basinr) || MdaUtils.compareTo(changeno,"0001") == 0 )
                {
                    if( MdaUtils.compareTo(subchangtype,"11") != 0 )
                    {
                        return ValidResult.build(false, "第一次报送变动信息的变动类型代码必须为提款");
                    }
                }
            }

        return null;
    }

    public ValidResult checkChcurrencyN1000(String chcurrency,String contractcurr){
        //!变动币种校验规则(业务的需求)：
        //!1. 变动币种应=签约币种
        //!========================================

            if( MdaUtils.isEmpty(chcurrency) )
            {
                return ValidResult.build(false, "必输项");
            }
            else
            {
                if( MdaUtils.compareTo(chcurrency,contractcurr) != 0 )
                {
                    return ValidResult.build(false, "变动币种应等于签约币种");
                }
            }

        //剔除 维护交易 AME 的检核判断，没意义

        return null;
    }

    public ValidResult checkChdateN1000(Date chdate,Date valuedate){

            if( MdaUtils.isEmpty(chdate) )
            {
                return ValidResult.build(false, "必输项");
            }
            else
            {
                if( Dates.diff(valuedate,chdate)>0 )
                {
                    return ValidResult.build(false, "变动日期不能小于起息日");
                }
            }

        return null;
    }










}
