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

import com.brilliance.isc.common.component.DyncDaoService;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.liaccv.vo.LiaccvVo;
import com.brilliance.isc.common.liaccv.vo.LiaccvgVo;
import com.brilliance.isc.common.transaction.newcheck.*;
import com.brilliance.isc.common.util.Utils;
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.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class LiaccvValidator extends AbstractModuleValidator<LiaccvVo> {

    @Autowired
    private LiaccvgValidator liaccvgValidator;

    @Autowired
    private DnfmodValidator dnfmodValidator;

    @Autowired
    private DyncDaoService dyncDaoService;

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

    @Override
    public Map<String, String> validate(LiaccvVo vo) {
        Map<String, String> maps = new HashMap<>();
        maps.putAll(super.validate(vo));
        liaccvgValidator.registerParent(vo);
        List<LiaccvgVo> liaccvgs = vo.getLiaccvg();
        for (int i = 0; i < liaccvgs.size(); i++) {
            maps.putAll(liaccvgValidator.validate(liaccvgs.get(i), i));
        }
        liaccvgValidator.removeTl();
        return maps;
    }

    @Validate(path="liaccv.relcshpct")
    public ValidResult checkRelcshpct(LiaccvVo liaccvVo){
        String transName = SettleContext.getTransName();
        if("JSTSET".equalsIgnoreCase(transName)){
            return null;
        }
        //当业务状态为已交割或者是已平仓时,不需要校验保证金实收比例和应收比例比较
        if ("JSTENG".equals(transName)) {
            String objinr = liaccvVo.getRec().getObjinr();
            String objtyp = liaccvVo.getRec().getObjtyp();
            String chlibz1 = dyncDaoService.dyncReadFieldByInr(objtyp, objinr, "chlibz1");
            String picflg = dyncDaoService.dyncReadFieldByInr(objtyp, objinr, "picflg");
            if ("0".equals(chlibz1) || ("2".equals(chlibz1) && "1".equals(picflg))) {
                return null;
            }
        }
        BigDecimal relcrepct = Utils.getFieldValue(liaccvVo.getRec(), "relcrepct", BigDecimal.ZERO);
        if(!MdaUtils.isEmpty(liaccvVo.getRelcshpct())) {
            BigDecimal sum = liaccvVo.getRelcshpct().add(relcrepct);
            if( MdaUtils.compareTo(sum, liaccvVo.getCshpct())<0 ) {
                return ValidResult.build(false,"实收保证金比例不能少于应收保证金比例!");
            }
        } else {
            BigDecimal maxamt = Decimals.add(liaccvVo.getOldamt(),liaccvVo.getChgamt());
            if( MdaUtils.compareTo(maxamt,0)>0 ) {
                BigDecimal sum = BigDecimal.ZERO;
                if (liaccvVo.getRelcshpct() == null) {
                    sum = relcrepct;
                } else {
                    sum = relcrepct.add(liaccvVo.getRelcshpct());
                }
                if( MdaUtils.compareTo(sum, liaccvVo.getCshpct())<0 ) {
                    return ValidResult.build(false,"实收保证金比例不能少于应收保证金比例!");
                }
            }
        }
        return null;
    }

    @Warning(path="liaccv.cshpct")
    public WarningResult warningCshpct1(LiaccvVo liaccvVo) {
        //开立和保证金调整可以修改应收比例
        //当保证金实收比例有值，应收比例为空时增加提醒功能 “保证金应收比例为0，请确认”
        String transName = SettleContext.getTransName();
        if (transName.indexOf("POP") > -1 || transName.indexOf("OPN") > -1 || transName.indexOf("ENG") > -1) {
            BigDecimal cshpct = liaccvVo.getCshpct();
            BigDecimal relcshpct = liaccvVo.getRelcshpct();
            if (cshpct != null && relcshpct != null
                    && cshpct.compareTo(BigDecimal.ZERO) == 0
                    && relcshpct.compareTo(BigDecimal.ZERO) != 0) {
                return WarningResult.build("SUBCHECKCSHPCT1", "保证金应收比例为0，请确认");
            }
        }
        return WarningResult.buildEmpty("SUBCHECKCSHPCT1");
    }

    @Warning(path="liaccv.cshpct")
    public WarningResult warningCshpct(LiaccvVo liaccvVo) {
        BigDecimal cshpct = liaccvVo.getCshpct();
        BigDecimal relcshpct = liaccvVo.getRelcshpct();
        String transName = SettleContext.getTransName();
        if (cshpct.compareTo(relcshpct) != 0 && (transName.indexOf("POP") > -1 || transName.indexOf("OPN") > -1)) {
            return WarningResult.build("SUBCHECKCSHPCT", "保证金实收比例和应收比例不一致");
        }
        return WarningResult.buildEmpty("SUBCHECKCSHPCT");
    }
}
