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

import com.brilliance.isc.common.cbsmod.service.XrtmodService;
import com.brilliance.isc.common.sysmod.SysmodService;
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.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.vo.component.bptbck.BptbckVo;
import com.brilliance.isc.vo.component.bptbck.BptbckgVo;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class BptbckValidator extends AbstractModuleValidator<BptbckVo> {

    @Autowired
    private XrtmodService xrtmodService;

    @Autowired
    private SysmodService sysmodService;

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

    /**
     *   source:bptbck.0021.script
     */
    @Validate(path = "bptbck.setamt")
    public ValidResult checkSetamtN1000(BptbckVo vo){
        List<BptbckgVo> bptbckgs = vo.getBptbckg();
        if (bptbckgs != null && bptbckgs.size() > 0) {
            BigDecimal setAmt = new BigDecimal("0");
            if( MdaUtils.compareTo(vo.getWrkfinmod().getIntprd(),"6") == 0 ) { // 利随本清特殊处理
                setAmt = vo.getWrkfinmod().getCbs().getCbe().getAmt();
            } else {
                setAmt = Decimals.add(vo.getWrkfinmod().getCbs().getCbe().getAmt(), vo.getWrkfinmod().getCbs().getTotint());
            }
            if(MdaUtils.isEmpty(vo.getWrkfinmod().getIntprd())) {
                if( MdaUtils.compareTo(vo.getSetamt(), vo.getWrkfinmod().getCbs().getCbe().getAmt()) != 0
                        && MdaUtils.compareTo(vo.getSetamt(), Decimals.add(vo.getWrkfinmod().getCbs().getCbe().getAmt(), vo.getWrkfinmod().getCbs().getTotint())) != 0) {
                    return ValidResult.build(false, "还款金额计算有误，请重算");
                }
            } else {
                if(MdaUtils.compareTo(vo.getSetamt(), setAmt) != 0) {
                    return ValidResult.build(false, "还款金额计算有误，请重算");
                }
            }
            String doccur = Utils.getFieldValue(vo.getRec(), "doccur", "");
            BigDecimal docamt = Utils.getFieldValue(vo.getRec(), "docamt", BigDecimal.ZERO);
            String opncur = "";
            if (vo.getBptbckg() != null && vo.getBptbckg().size() > 0) {
                opncur = vo.getBptbckg().get(0).getOpncur();
                if(!MdaUtils.isEmpty(vo.getWrkbpdgrp().getRec().getInr()) ) {
                    if(MdaUtils.isEmpty(vo.getWrkfinmod().getCbs().getCalflg())) {
                        return ValidResult.build(false, "融资还款试算有误或者未试算，请重试");
                    }
                }
            }
            if (StringUtils.isNotEmpty(opncur)) {
                BigDecimal docAmt = xrtmodService.amtConvTypes(doccur, docamt,"B", opncur, "S", Dates.today());
                if(MdaUtils.compareTo(docAmt, Decimals.add(vo.getWrkfinmod().getCbs().getCbe().getAmt(), vo.getWrkfinmod().getCbs().getTotint())) > 0 && MdaUtils.compareTo(vo.getWrkfinmod().getCbs().getCbb().getAmt(), vo.getWrkfinmod().getCbs().getCbe().getAmt()) > 0) {
                    return ValidResult.build(false, "未还清融资时，不能结汇入客户账");
                }
            }
        }
        return null;
    }

    /**
     *   source:bptbck.0024.script
     */
    @Validate(path="bptbck.wrkfinmod.reptyp")
    public ValidResult checkWrkfinmodReptypN1000(BptbckVo vo){
        List<BptbckgVo> bptbckgs = vo.getBptbckg();
        if (bptbckgs != null && bptbckgs.size() > 0) {
            boolean boolVar = false;
            if(!MdaUtils.isEmpty(vo.getWrkbpdgrp().getRec().getInr())) {
                boolVar = true;
            }
            if(boolVar) {
                if(MdaUtils.isEmpty(vo.getWrkfinmod().getReptyp())) {
                    return errorMandatory();
                }
            }
        }
        return null;
    }

    @Validate(path = "bptbck.setamt")
    public ValidResult checkFinStatus(BptbckVo vo) {
        List<BptbckgVo> bptbckgs = vo.getBptbckg();
        if (bptbckgs != null && bptbckgs.size() > 0) {
            if (StringUtils.isNotEmpty(vo.getWrkbpdgrp().getRec().getInr())) {
                if (!sysmodService.checkFinStatus("BPD", vo.getWrkbpdgrp().getRec().getInr())) {
                    return ValidResult.build(false, "前手融资交易未Release");
                }
            }
        }
        return null;
    }
}
