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

import com.brilliance.isc.bo.model.Dftcre;
import com.brilliance.isc.common.context.SettleContext;
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.vo.component.trtcre.TrtcreVo;
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.util.HashMap;
import java.util.Map;

@Component
public class DftcreValidator extends AbstractModuleValidator<Dftcre> {

    @Autowired
    private DftpValidator dftpValidator;

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

    @Override
    public Map<String, String> validate(Dftcre vo) {
        Map<String, String> maps = new HashMap<>();
        maps.putAll(super.validate(vo));

        //验证Trtp模型
        dftpValidator.registerParent(vo.getDfdgrp());
        maps.putAll(dftpValidator.validate(vo.getDftp()));
        dftpValidator.removeTl();
        return maps;
    }


    /**
     *   source:dftcre.0002.script
     */
    @Validate(path = "dftcre.dfdgrp.rec.fintyp")
    public ValidResult checkDfdgrpRecFintypN1000(Dftcre vo){
        //如选择同业代付则此处为必输项
        if(!MdaUtils.isEmpty(vo.getDfflag())) {
            if(MdaUtils.isEmpty(vo.getDfdgrp().getRec().getFintyp())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:dftcre.0014.script
     */
    @Validate(path = "dftcre.dfdgrp.rec.dfuflg")
    public ValidResult checkDfdgrpRecDfuflgN1000(Dftcre vo){
        //如选择同业代付则此处为必输项
        if(!MdaUtils.isEmpty(vo.getDfflag())) {
            if(MdaUtils.isEmpty(vo.getDfdgrp().getRec().getDfuflg()) ) {
                return errorMandatory();
            } else {
                if( MdaUtils.compareTo(vo.getDfdgrp().getRec().getDfuflg(),"2") == 0
                        && MdaUtils.compareTo(Strings.mid(vo.getDfdgrp().getDff().getExtkey(),5,2),"CN") == 0 ) {
                    return ValidResult.build(false, "所选代付类型与代付行不符");
                }
            }
        }
        return null;
    }

    /**
     *   source:dftcre.0027.script
     */
    @Validate(path = "dftcre.dfdgrp.cbs.max.amt")
    public ValidResult checkDfdgrpCbsMaxAmtN990(Dftcre vo){
        if(MdaUtils.compareTo(vo.getDfdgrp().getCbs().getMax().getAmt(), vo.getOriamt()) > 0) {
            return ValidResult.build(false, "不能大于原始金额");
        } else {
            TrtcreVo trtcre = vo.getRec().getTrtcreVo();
            if (trtcre != null
                    && MdaUtils.compareTo(vo.getDfdgrp().getCbs().getMax().getCur(), trtcre.getOricur()) == 0
                    && MdaUtils.compareTo(vo.getDfdgrp().getCbs().getMax().getAmt(), trtcre.getPctoriamt()) > 0) {
                return ValidResult.build(false, "代付金额不能大于融资金额");
            }
        }
        return null;
    }

    /**
     *   source:dftcre.0032.script
     */
    @Validate(path = "dftcre.dfdgrp.rec.resamt")
    public ValidResult checkDfdgrpRecResamtN1000(Dftcre vo){
        /**
         if GetFramename == "TRTOPN" or GetFramename == "BRTSET" or GetFramename == "BCTSET" then

         **/
        String transName = SettleContext.getDirectTransName();
        if( MdaUtils.compareTo(transName,"TRTOPN") == 0
                || MdaUtils.compareTo(transName,"BRTSET") == 0
                || MdaUtils.compareTo(transName,"BCTSET") == 0
                || MdaUtils.compareTo(transName,"CPTOPN") == 0
                || MdaUtils.compareTo(transName,"CPTATO") == 0 ) {
            if(!MdaUtils.isEmpty(vo.getDfflag())) {
                if(MdaUtils.compareTo(vo.getDfdgrp().getRec().getDfuflg(),"2") == 0 ) {
                    if(MdaUtils.isEmpty(vo.getDfdgrp().getRec().getResamt()) ) {
                       return errorMandatory();
                    }
                }
            }
        }
        return null;
    }

    /**
     *   source:dftcre.0035.script
     */
    @Validate(path = "dftcre.dfdgrp.rec.invtyp")
    public ValidResult checkDfdgrpRecInvtypN1000(Dftcre vo){
        if(!MdaUtils.isEmpty(vo.getDfflag())) {
            if(MdaUtils.isEmpty(vo.getDfdgrp().getRec().getInvtyp())) {
                return errorMandatory();
            }
        }
        return null;
    }
}
