package com.xisc.pm.app.service.strategy.pdrule;

import com.xisc.pm.api.dto.RuleParamDTO;
import com.xisc.pm.app.service.strategy.RuleExecStrategyFactory;
import com.xisc.pm.domain.entity.PmProContProdTtr;
import com.xisc.pm.domain.vo.MmPlateVO;
import com.xisc.pm.infra.constant.RuleSetConstants;
import com.xisc.pm.infra.enums.PdRuleItemType;
import io.choerodon.core.exception.CommonException;
import org.apache.commons.lang3.StringUtils;
import org.hzero.core.base.BaseConstants;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 合同实重交货校验（P01）
 */
@Component
public class WtMethodMatchRuleExecStrategy implements RuleExecStrategy {

    @Override
    public void afterPropertiesSet() throws Exception {
        RuleExecStrategyFactory.register(PdRuleItemType.WT_METHOD_MATCH, this);
    }

    @Override
    public boolean execRule(RuleParamDTO ruleParam) {
        Map<String, Object> bizData = ruleParam.getBizData();
        String orderNo = (String) bizData.get(RuleSetConstants.ORDER_NO);
        String mscLineNo = (String) bizData.get(RuleSetConstants.MSC_LINE_NO);

        PmProContProdTtr pmProContProdTtr = (PmProContProdTtr) bizData.get(RuleSetConstants.PM_PRO_CONT_PROD_ATTR);
        MmPlateVO mmPlateVO = (MmPlateVO) bizData.get(RuleSetConstants.MM_PLATE);
        if (pmProContProdTtr == null || StringUtils.isBlank(pmProContProdTtr.getGiveSampleCode())) {
            // 读生产合同池没有找到记录，合同号【order_no】，产线号【msc_line_no】
            throw new CommonException("pm.pm_pd_rule_set_001", orderNo, mscLineNo);
        }
        if (mmPlateVO == null) {
            throw new CommonException("pm.pm_pd_plate_001");
        }
        String wtMethodCode = pmProContProdTtr.getCheckMethodCode();
        if (StringUtils.isBlank(mmPlateVO.getMatStatus()) || mmPlateVO.getMatGrossWgt() == null) {
            return true;
        }
        if (BaseConstants.Flag.YES.toString().equals(wtMethodCode) && mmPlateVO.getMatStatus().charAt(0) == '3' && BigDecimal.ZERO.compareTo(mmPlateVO.getMatGrossWgt()) == 0) {
            // 材料没有实重，合同要求实重交货，不能进行挂合同操作，合同【ORDER_NO】，材料【mat_no】.
            throw new CommonException("pm.pm_pd_rule_set_036", orderNo, mmPlateVO.getMatNo());
        }
        return true;
    }

}
