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.PmIngotSpecTrimBt;
import com.xisc.pm.domain.repository.PmIngotSpecTrimBtRepository;
import com.xisc.pm.domain.repository.PmIngotTypeBtRepository;
import com.xisc.pm.infra.constant.RuleSetConstants;
import com.xisc.pm.infra.enums.PdRuleItemType;
import com.xisc.pm.infra.util.Utils;
import io.choerodon.core.exception.CommonException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 钢锭轧制规格落在最大轧制规格范围内检查
 * 根据锭坯型ingot_type、是否保性能guarantee_quality、轧制产线号在钢锭轧制规格及切边量基准表PM_INGOT_SPEC_TRIM_BT
 * 查询轧制厚度起MIN_ROLL_THICK、轧制厚度止MAX_ROLL_THICK、轧制宽度起MIN_ROLL_WIDTH、轧制宽度止MAX_ROLL_WIDTH、轧制长度起ROLL_LEN_FROM、轧制长度止ROLL_LEN_TO。
 * 若轧制目标厚度taim不在轧制厚度起MIN_ROLL_THICK、轧制厚度止MAX_ROLL_THICK范围内或者轧制目标宽度waim不在轧制宽度起MIN_ROLL_WIDTH、轧制宽度止MAX_ROLL_WIDTH范围内或者轧制目标长度laim不在轧制长度起ROLL_LEN_FROM、轧制长度止ROLL_LEN_TO范围内则校验不通过，
 * 提示信息:轧制规格不满足【ingot_type】轧制范围。【taim】:【MIN_ROLL_THICK】-【MAX_ROLL_THICK】,
 * 【waim】:【MIN_ROLL_WIDTH】-【MAX_ROLL_WIDTH】,
 * 【laim】:【ROLL_LEN_FROM】-【ROLL_LEN_TO】.
 * 
 * 
 * @author jun.zeng
 *
 */
@Component
public class IngotSpecTrimRuleExecStrategy implements RuleExecStrategy {

    @Autowired
    private PmIngotTypeBtRepository pmIngotTypeBtRepository;

    @Autowired
    private PmIngotSpecTrimBtRepository pmIngotSpecTrimBtRepository;

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

    @Override
    public boolean execRule(RuleParamDTO ruleParam) {
        Map<String, Object> bizData = ruleParam.getBizData();
        String ingotType = (String) bizData.get(RuleSetConstants.INGOT_TYPE);
        String mscLineNo = (String) bizData.get(RuleSetConstants.MSC_LINE_NO);
        BigDecimal taim = (BigDecimal) bizData.get(RuleSetConstants.TAIM);
        BigDecimal waim = (BigDecimal) bizData.get(RuleSetConstants.WAIM);
        BigDecimal laim = (BigDecimal) bizData.get(RuleSetConstants.LAIM);
        String guaranteeQuality = bizData.get(RuleSetConstants.INGOT_GUARANTEE_QUALLITY).toString();
        guaranteeQuality = guaranteeQuality == null ? "1" : guaranteeQuality;

        if (StringUtils.isBlank(ingotType)) {
            // 钢锭重不能未空！
            throw new CommonException("pm.pm_pd_rule_set_047");
        }
        PmIngotSpecTrimBt pmIngotSpecTrimBt = pmIngotSpecTrimBtRepository.selectBy(ruleParam.getTenantId(), ingotType,
                guaranteeQuality, mscLineNo);
        if (pmIngotSpecTrimBt == null) {
            // 钢锭轧制规格及切边量基准表,钢锭坯类型代码【{0}】,是否保性能【{1}】，设计产线号【{2}】
            throw new CommonException("pm.pm_ingot_spec_trim_bt_002", ingotType, guaranteeQuality.toString(),
                    mscLineNo);
        }
        // 轧制规格不满足【ingot_type】轧制范围。【{0}】:【{1}】-【{2}】, 【{3}】:【{4}】-【{5}】, 【{6}】:【{7}】-【{8}】.       
        if(!Utils.isRange(taim, pmIngotSpecTrimBt.getMinRollThick(), pmIngotSpecTrimBt.getMaxRollThick())
                || !Utils.isRange(waim, pmIngotSpecTrimBt.getMinRollWidth(), pmIngotSpecTrimBt.getMaxRollWidth())
                || !Utils.isRange(laim, pmIngotSpecTrimBt.getRollLenFrom(), pmIngotSpecTrimBt.getRollLenTo())) {
            throw new CommonException("pm.pm_pd_rule_set_050", ingotType
                    , taim.toPlainString(),pmIngotSpecTrimBt.getMinRollThick().toPlainString(),pmIngotSpecTrimBt.getMaxRollThick().toPlainString()
                    , waim.toPlainString(),pmIngotSpecTrimBt.getMinRollWidth().toPlainString(),pmIngotSpecTrimBt.getMaxRollWidth().toPlainString()
                    , laim.toPlainString(),pmIngotSpecTrimBt.getRollLenFrom().toPlainString(),pmIngotSpecTrimBt.getRollLenTo().toPlainString()
                    );
        }
        return true;
    }

}
