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

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

import org.apache.commons.lang3.StringUtils;
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.PmContMscTech;
import com.xisc.pm.domain.entity.PmPlateSpecBt;
import com.xisc.pm.domain.entity.PmProContProdTtr;
import com.xisc.pm.domain.entity.PmStCodeBt;
import com.xisc.pm.domain.repository.PmPlateSpecBtRepository;
import com.xisc.pm.domain.repository.PmStCodeBtRepository;
import com.xisc.pm.infra.constant.RuleSetConstants;
import com.xisc.pm.infra.enums.FormPlateMode;
import com.xisc.pm.infra.enums.PdRuleItemType;
import com.xisc.pm.infra.util.Utils;

import io.choerodon.core.exception.CommonException;

/**
 * 特殊钢种组剖分板检查（C05）
 * 
 * @author jun.zeng
 * 
 *
 * 
 */
@Component
public class CombineWidthAndLenLimitRuleExecStrategy implements RuleExecStrategy {

    @Autowired
    protected PmPlateSpecBtRepository pmPlateSpecBtRepository;

    @Autowired
    private PmStCodeBtRepository pmStCodeBtRepository;

    @Override
    public void afterPropertiesSet() throws Exception {
        RuleExecStrategyFactory.register(PdRuleItemType.COMBINE_PLATE_WIDTH_AND_LEN_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);
        BigDecimal combinePlateLen = (BigDecimal) bizData.get(RuleSetConstants.COMBINE_PLATE_LEN);
        BigDecimal combinePlateWidth = (BigDecimal) bizData.get(RuleSetConstants.COMBINE_PLATE_WIDTH);
        Long tenantId = (Long) bizData.get(RuleSetConstants.TENANT_ID);
        String formPlateMode = (String) bizData.get(RuleSetConstants.FORM_PLATE_MODE);
        String stNo = (String) bizData.get(RuleSetConstants.ST_NO);
        PmProContProdTtr axisPmProContProdTtr = (PmProContProdTtr) bizData
                .get(RuleSetConstants.AXIS_PM_PRO_CONT_PROD_ATTR);
        PmContMscTech axisPmContMscTech = (PmContMscTech) bizData.get(RuleSetConstants.AXIS_PM_CONT_MSC_TECH);

        if (axisPmContMscTech == null || axisPmProContProdTtr == null) {
            // 生产合同池没有找到记录，合同号【order_no】，产线号【msc_line_no】。
            throw new CommonException("pm.pm_pd_rule_set_001", orderNo, mscLineNo);
        }

        // 组板的钢板形状代码shp_code不为空，是特殊板业务，校验通过，继续下一规则校验。
        if (StringUtils.isNotBlank(axisPmProContProdTtr.getShpCode())) {
            return true;
        }

        PmStCodeBt record = new PmStCodeBt();
        record.setStNo(StringUtils.isNotBlank(stNo) ? stNo : axisPmContMscTech.getStNo());
        record.setTenantId(tenantId);
        record.setRollLineNo(Utils.mscLineNoToRoolLineNo(mscLineNo));
        PmStCodeBt pmStCodeBt = pmStCodeBtRepository.selectOne(record);

        PmPlateSpecBt pmPlateSpecBt = pmPlateSpecBtRepository.selectLenAndWidthLimit(
                axisPmProContProdTtr.getOrderThick(), combinePlateWidth, axisPmContMscTech.getRollMode(),
                axisPmContMscTech.getAdTypeCode(), FormPlateMode.getByCode(formPlateMode).getType(),
                pmStCodeBt == null ? null : pmStCodeBt.getStCode(), axisPmContMscTech.getWholeHeatTreatmentType(),
                Utils.mscLineNoToRoolLineNo(mscLineNo), tenantId);

        if (combinePlateLen.compareTo(pmPlateSpecBt.getCombinePlateLenMax()) > 0) {
            // 组合长度为【combine_plate_len】，组合板长度上限为【COMBINE_PLATE_LEN_MAX】。
            throw new CommonException("pm.pm_pd_rule_set_016", pmStCodeBt == null ? null : pmStCodeBt.getStCode(), formPlateMode,
                    axisPmContMscTech.getAdTypeCode(), axisPmContMscTech.getRollMode(),
                    axisPmProContProdTtr.getOrderThick().toPlainString(), combinePlateWidth.toPlainString(),
                    axisPmContMscTech.getWholeHeatTreatmentType(), combinePlateLen.toPlainString(),
                    pmPlateSpecBt.getCombinePlateLenMax());
        }
//        if (combinePlateWidth.compareTo(pmPlateSpecBt.getCombinePlateWidthFrom()) < 0
//                || combinePlateWidth.compareTo(pmPlateSpecBt.getCombinePlateWidthTo()) > 0) {
//            // 组合宽度为【combine_plate_width】，组合板宽度上下限分别为【COMBINE_PLATE_WIDTH_TO】、【COMBINE_PLATE_WIDTH_FROM】。
//            throw new CommonException("pm.pm_pd_rule_set_015", combinePlateWidth.toPlainString(),
//                    pmPlateSpecBt.getCombinePlateWidthTo().toPlainString(),
//                    pmPlateSpecBt.getCombinePlateWidthFrom().toPlainString());
//        }
        return true;
    }

}
