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

import java.util.List;
import java.util.Map;

import org.hzero.core.base.BaseConstants;
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.infra.constant.RuleSetConstants;
import com.xisc.pm.infra.enums.FormPlateMode;
import com.xisc.pm.infra.enums.PdRuleItemType;

import io.choerodon.core.exception.CommonException;

/**
 * 宽度方向允许组剖分板合同匹配校验（C05）
 * 
 * @author jun.zeng
 * 
 *
 * 
 */
@Component
public class MulitPlateFlagRuleExecStrategy implements RuleExecStrategy {

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

    @SuppressWarnings("unchecked")
    @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 formPlateModeValue = (String) bizData.get(RuleSetConstants.FORM_PLATE_MODE);

        FormPlateMode formPlateMode = FormPlateMode.getByCode(formPlateModeValue);

        PmContMscTech pmContMscTechParam = (PmContMscTech) bizData.get(RuleSetConstants.PM_CONT_MSC_TECH);
        List<PmContMscTech> pmContMscTechs = (List<PmContMscTech>) bizData.get(RuleSetConstants.PM_CONT_MSC_TECH_LIST);

        if (pmContMscTechParam == null && pmContMscTechs == null) {
            // 读生产合同池没有找到记录，合同号【order_no】，产线号【msc_line_no】
            throw new CommonException("pm.pm_pd_rule_set_001", orderNo, mscLineNo);
        }
        
        if (FormPlateMode.S1.getType().equals(formPlateMode.getType())
                || FormPlateMode.G.getType().equals(formPlateMode.getType())) {
            if(pmContMscTechParam != null) {
                if (BaseConstants.Flag.NO.longValue() == pmContMscTechParam.getMultiPlateFlag().longValue()) {
                    // 合同【order_no】的轧制倍尺可否标记MULTI_PLATE_FLAG为否，组板类型为【form_plate_mode】，不能组在一起！
                    throw new CommonException("pm.pm_pd_rule_set_021", orderNo, formPlateModeValue);
                }
            }
            if(pmContMscTechs != null) {
                for (PmContMscTech pmContMscTech : pmContMscTechs) {
                    if (BaseConstants.Flag.NO.longValue() == pmContMscTech.getMultiPlateFlag().longValue()) {
                        // 合同【order_no】的轧制倍尺可否标记MULTI_PLATE_FLAG为否，组板类型为【form_plate_mode】，不能组在一起！
                        throw new CommonException("pm.pm_pd_rule_set_021", pmContMscTech.getOrderNo(), formPlateModeValue);
                    }
                }
            }
        }
        return true;
    }

}
