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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xisc.pm.api.dto.RuleParamDTO;
import com.xisc.pm.app.service.strategy.RuleExecStrategyFactory;
import com.xisc.pm.domain.entity.PmPdConstBt;
import com.xisc.pm.domain.repository.PmPdConstBtRepository;
import com.xisc.pm.infra.constant.RuleSetConstants;
import com.xisc.pm.infra.enums.PdRuleItemType;

import io.choerodon.core.exception.CommonException;

/**
 * 目标轧制长度限制检查（C05）
 * 
 * @author jun.zeng
 * 
 *
 * 
 */
@Component
public class LaimLimitRuleExecStrategy implements RuleExecStrategy {

    @Autowired
    private PmPdConstBtRepository pmPdConstBtRepository;

    @Override
    public void afterPropertiesSet() throws Exception {
        RuleExecStrategyFactory.register(PdRuleItemType.LAIM_LIMIT, 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 rollLineNo = (String) bizData.get(RuleSetConstants.ROLL_LINE_NO);
        Long tenantId = (Long) bizData.get(RuleSetConstants.TENANT_ID);
        BigDecimal laim = (BigDecimal) bizData.get(RuleSetConstants.LAIM);

        PmPdConstBt record = new PmPdConstBt();
        record.setTenantId(tenantId);
        record.setRollLineNo(rollLineNo);
        //
        // 轧制目标长度为【laim】，厚板设备限制轧制大板最小长度为【HPM_MIN_P_LEN】、厚板设备限制轧制大板最大长度为【HPM_MAX_P_LEN】。
        record = pmPdConstBtRepository.selectOne(record);
        if (record == null) {
            throw new CommonException("pm.pm_pd_rule_set_018", rollLineNo);
        }
        if (laim.compareTo(record.getHpmMinPLen()) < 0 || laim.compareTo(record.getHpmMaxPLen()) > 0) {
            // 组合长度为【combine_plate_len】，组合板长度上下限分别为【COMBINE_PLATE_LEN_MAX】、【COMBINE_PLATE_LEN_MIN】。
            throw new CommonException("pm.pm_pd_rule_set_017", laim.toPlainString(),
                    record.getHpmMaxPLen().toPlainString(), record.getHpmMinPLen().toPlainString());
        }
        return true;
    }

}
