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

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.xisc.pm.api.dto.PmCompositeBlockDTO;
import com.xisc.pm.api.dto.PmProContYieldDTO;
import com.xisc.pm.api.dto.PmProContYieldRequestDTO;
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 com.xisc.pm.infra.util.Utils;

import io.choerodon.core.exception.CommonException;

/**
 * 全程热处理模式必须相邻匹配（C03）
 * 
 * @author jun.zeng
 * 
 *
 * 
 */
@Component
public class WholeHeatTreatSeqRuleExecStrategy implements RuleExecStrategy {

    @Override
    public void afterPropertiesSet() throws Exception {
        RuleExecStrategyFactory.register(PdRuleItemType.WHOLE_TREAT_ORER_CHECK, 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);

        List<PmContMscTech> pmContMscTechList = (List<PmContMscTech>) bizData.get(RuleSetConstants.PM_CONT_MSC_TECH_LIST);
        PmProContYieldRequestDTO pmProContYield = (PmProContYieldRequestDTO)bizData.get(RuleSetConstants.CALC_REQUEST);

        if (pmContMscTechList == null || pmContMscTechList.isEmpty()) {
            // 读生产合同池没有找到记录，合同号【order_no】，产线号【msc_line_no】
            throw new CommonException("pm.pm_pd_rule_set_001", orderNo, mscLineNo);
        }
        Map<String , String> wholeMap = new  HashMap<String, String>();
        Set<String> wholeSet = new HashSet<>();
        for (PmContMscTech pmContMscTech : pmContMscTechList) {
            if(StringUtils.isNotBlank(pmContMscTech.getWholeHeatTreatmentType())) {
                wholeMap.put(pmContMscTech.getOrderNo(), pmContMscTech.getWholeHeatTreatmentType());
                wholeSet.add(pmContMscTech.getWholeHeatTreatmentType());
            } else {
                wholeMap.put(pmContMscTech.getOrderNo(), "");
                wholeSet.add("");
            }
        }
        if(FormPlateMode.G.getCode().equals(pmProContYield.getPmContFilterRequest().getFormPlateMode()) 
                || FormPlateMode.G1.getCode().equals(pmProContYield.getPmContFilterRequest().getFormPlateMode())) {
            if(wholeSet.size() > 1) {
                throw new CommonException("pm.pm_pd_rule_set_057") ;
            }
        } else {
            if(wholeSet.size() > 2) {
                throw new CommonException("pm.pm_pd_rule_set_059") ;
            }
            if(!wholeSet.contains("") && wholeSet.size() == 2) {
                throw new CommonException("pm.pm_pd_rule_set_059") ;
            }
        }
        wholeSet.clear();
        String wholeHeatTreament = null;
        for (PmProContYieldDTO orderData : pmProContYield.getPmProContYieldList()) {
            if(wholeHeatTreament == null) {
                wholeHeatTreament = wholeMap.get(orderData.getOrderNo());
                wholeSet.add(wholeHeatTreament);
            } else {
                String current = wholeMap.get(orderData.getOrderNo());
                if(!wholeHeatTreament.equals(current)){
                    // 如果当前的热处理类型与上一个热处理类型不一致。则判断当前的热处理类型之前是否出现过
                    if(wholeSet.contains(current)) {
                        // 热处理合同没有组在一起，不能组板
                        throw new CommonException("pm.pm_pd_rule_set_058") ;
                    } else {
                        wholeHeatTreament = current;
                        wholeSet.add(current);
                    }
                }
            }
        }
        return true ;
    }

}
