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

import com.brilliance.isc.bo.Bopcty;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.transaction.newcheck.ValidResult;
import com.brilliance.isc.common.transaction.newcheck.WarningResult;
import com.brilliance.isc.mda.dao.BopctyMapper;
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 CfapubpCheckRules {

    @Autowired(required = false)
    CodetableService codetableService;

    @Autowired
    BopctyMapper bopctyMapper;

    public ValidResult checkContractamountN1000(BigDecimal contractamount) {

        if (MdaUtils.isEmpty(contractamount)) {
            return ValidResult.build(false, "必输项");
        } else {
            if (!MdaUtils.isEmpty(contractamount) && MdaUtils.compareTo(contractamount, 0) < 0) {
                return ValidResult.build(false, "签约金额必须大于等于0");
            }
        }

        return null;
    }

    public ValidResult checkCreditorcodeN1000(String creditortype,String creditorcode,String debtype) {
        String subcreditortype = Strings.mid(creditortype, 1, 6);
        String sub1 = Strings.mid(creditorcode, 1, 3);
        String sub2 = codetableService.getTblKey(Strings.mid(creditorcode, 4, 3), "CTYCOD");
        if (!MdaUtils.isEmpty(creditorcode)) {
            switch (subcreditortype) {
                case "200011":
                    if (MdaUtils.compareTo(sub1, "GOV") != 0 || MdaUtils.isEmpty(sub2) || creditorcode.length() != 6) {
                        return ValidResult.build(false, "填写不正确，请按照提示内容填写");
                    }
                    break;
                case "200013":
                    if (MdaUtils.compareTo(sub1, "CEB") != 0 || MdaUtils.isEmpty(sub2) || creditorcode.length() != 6) {
                        return ValidResult.build(false, "填写不正确，请按照提示内容填写");
                    }
                    break;
                default:
            }
        } else {
            if (MdaUtils.compareTo(debtype, "1106") == 0) {
                return ValidResult.build(false, "必输项");
            }
        }

        return null;
    }

    public ValidResult checkCreditortypeN1000(String creditortype,String debtype){

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

        return null;
    }

    public WarningResult warnCreditortypeN1000(String creditortype,String debtype,String subid){

        if( !MdaUtils.isEmpty(creditortype) )
        {
            String creditortypem = Strings.mid(creditortype,1,6);
            switch(debtype)
            {
                case "1106":
                    if( MdaUtils.compareTo(creditortypem,"200011") == 0 || MdaUtils.compareTo(creditortypem,"200012") == 0 || MdaUtils.compareTo(creditortypem,"200013") == 0 || MdaUtils.compareTo(creditortypem,"200015") == 0 || MdaUtils.compareTo(creditortypem,"200016") == 0 || MdaUtils.compareTo(creditortypem,"200017") == 0 || MdaUtils.compareTo(creditortypem,"200018") == 0 || MdaUtils.compareTo(creditortypem,"200099") == 0 )
                    {
                        return WarningResult.build(subid, "债权人类型代码不能选择以200011、200012、200013、200015、200016、200017、200018、200099开头的类型");
                    }
                    break;
                case "1108":
                    if( MdaUtils.compareTo(creditortypem,"200016") == 0 )
                    {
                        if( MdaUtils.compareTo(creditortype,"20001603") == 0 )
                        {
                            return WarningResult.build(subid, "债权人类型通常不选择关联企业 ，请再次确认选择是否正确");
                        }
                    }
                    else
                    {
                        return WarningResult.build(subid, "债权人类型通常为企业 ，请再次确认选择是否正确");
                    }
                    break;
            }
        }


        return WarningResult.buildEmpty(subid);
    }

    public ValidResult checkMaturityN1000(Date maturity,Date valuedate){

            if( MdaUtils.isEmpty(maturity) )
            {
                return ValidResult.build(false, "必输项");
            }
            else
            {
                if( Dates.diff(maturity,valuedate) < 0 )
                {
                    return ValidResult.build(false, "到期日必须大于等于起息日");
                }
            }


        return null;
    }

    public ValidResult checkOpercodeN1000(String opercode,String creditortype){

            if( MdaUtils.isEmpty(opercode) )
            {
                return ValidResult.build(false, "必输项");
            }
            else
            {
                //"债权人经营地所在国家（地区）代码"：控制方法由"不能为中国CHN"调整为"当债权人类型代码不为"境内银行离岸部（20001403）"时 ，不能为中国CHN。"
                if( MdaUtils.compareTo(opercode,"CHN") == 0 && MdaUtils.compareTo(creditortype,"20001403") != 0 )
                {
                    return ValidResult.build(false, "债权人经营地所在国家（地区）代码不能为CHN");
                }

                //国别必须合法
                    Bopcty bopcty =bopctyMapper.queryBopCod(opercode);
                    if(MdaUtils.isEmpty(bopcty)){
                        return ValidResult.build(false, "请输入正确的国别");
                    }
            }

        return null;
    }

    public ValidResult checkValuedateN1000(Date valuedate){

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

                if( Dates.diff(valuedate,Dates.today())>0 )
                {
                    return ValidResult.build(false, "起息日不能晚于系统日期");
                }
            }


        return null;
    }

    public ValidResult checkCrehqcodeN1000(String crehqcode) {
        //国别必须合法
        if (!MdaUtils.isEmpty(crehqcode)) {
            Bopcty bopcty =bopctyMapper.queryBopCod(crehqcode);
            if(MdaUtils.isEmpty(bopcty)){
                return ValidResult.build(false, "请输入正确的国别");
            }
        }

        return null;
    }










}
