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.PmContMscTech;
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.springframework.stereotype.Component;

import java.util.Map;

/**
 * 加热炉类型匹配校验 规则库执行接口 (C01)
 * @author jun.zeng
 *
 */
@Component
public class HeatingFurnaceTypeRuleExecStrategy implements RuleExecStrategy {


    @Override
    public void afterPropertiesSet() throws Exception {
        RuleExecStrategyFactory.register(PdRuleItemType.HEATING_FURNACE_TYPE , 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);
        // 加热炉类型
        String furType = (String) bizData.get(RuleSetConstants.FUR_TYPE);
        // 产线工艺参数
        PmContMscTech pmContMscTech = (PmContMscTech) bizData.get(RuleSetConstants.PM_CONT_MSC_TECH);
        if (pmContMscTech == null) {
            // 生产合同池没有找到记录，合同号【order_no】，产线号【msc_line_no】。
            throw new CommonException("pm.pm_pd_rule_set_001", orderNo, mscLineNo);
        }
        if (StringUtils.isNotBlank(pmContMscTech.getFurType())
                && !RuleSetConstants.ZERO.equals(pmContMscTech.getFurType())
                && !pmContMscTech.getFurType().equals(furType) && !"S".equals(furType)) {
            // 合同【order_no】的加热炉类型【FUR_TYPE】与指定加热炉类型【fur_type】不匹配。
            throw new CommonException("pm.pm_pd_rule_set_003", orderNo, pmContMscTech.getFurType() ,furType);
        }
        return true ;
    }
    
}
