package com.sw.mes.outside.util;

import com.sw.mes.core.utils.StringUtils;
import com.sw.mes.outside.dto.ToMESData;

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

public class EAPUtils {
	public static Map<Integer,String> stepMapping = new HashMap<Integer, String>();
	static {
//        stepMapping.put(-1, "离 线");
        stepMapping.put(0, "手动");
        stepMapping.put(1, "抽真空");
        stepMapping.put(2, "检漏");
        stepMapping.put(3, "压力化");
        stepMapping.put(4, "熔料");
        stepMapping.put(5, "熔接");
        stepMapping.put(6, "稳温");
        stepMapping.put(7, "引晶");
        stepMapping.put(8, "手动引晶");
        stepMapping.put(9, "放肩");
        stepMapping.put(10, "转肩");
        stepMapping.put(11, "等径");
        stepMapping.put(12, "收尾");
        stepMapping.put(13, "停炉");
        stepMapping.put(14, "隔离");
        stepMapping.put(15, "副室净化");
        stepMapping.put(16, "煅烧");
        stepMapping.put(17, "氧化");
        stepMapping.put(18, "预热工艺");
        stepMapping.put(19, "冷却保护");
        stepMapping.put(20, "取段");
        stepMapping.put(21, "降籽晶");
        stepMapping.put(22, "结晶");
        stepMapping.put(23, "取肩");
        stepMapping.put(24, "取渣");
        stepMapping.put(25, "料筒提出");
        stepMapping.put(26, "复投");
        stepMapping.put(29, "回熔");
        stepMapping.put(30, "料筒旋回");
        stepMapping.put(31, "合炉盖");
        stepMapping.put(33, "隔离旋开");
        stepMapping.put(34, "拆清");
        stepMapping.put(38, "应急冷却");
        stepMapping.put(39, "炉盖旋开");
        stepMapping.put(40, "主炉室旋回");
        stepMapping.put(41, "主炉室旋开");
        stepMapping.put(42, "装料");
        stepMapping.put(43, "吊料");
        stepMapping.put(46, "预热");
        stepMapping.put(51, "断苞收尾");
        stepMapping.put(52, "取段快速收尾");
        stepMapping.put(53, "快速收尾");
        stepMapping.put(54, "净化熔接稳温");
        stepMapping.put(55, "断线提出");
        stepMapping.put(56, "籽晶旋回");
        stepMapping.put(57, "取段收尾");
        stepMapping.put(58, "断线结晶");
//        stepMapping.put(100, "待 机");
    }


	/**
	 * 在开始搜集数据集 与 结束搜集数据集 中搜索数据点值
	 * @return
	 */
	public static String getEventValue(ToMESData data,String fieldName) {
		String value = null;

		Map<String, String> startData = data.getStartCollectionData();
		Map<String, String> endData = data.getEndCollectionData();

		if(startData != null) {
			value = startData.get(fieldName);
		}

		if(value == null && endData != null) {
			value = endData.get(fieldName);
		}

		return value;
	}


	/**
	 * 在开始搜集数据集 与 结束搜集数据集 中搜索数据点值
	 * @return
	 */
	public static String getEventValue(ToMESData data,String fieldName,int retain) {
		String value = null;

		Map<String, String> startData = data.getStartCollectionData();
		Map<String, String> endData = data.getEndCollectionData();

		if(startData != null) {
			value = startData.get(fieldName);
		}

		if(value == null && endData != null) {
			value = endData.get(fieldName);
		}


		if(value!=null) {
			BigDecimal bd = new BigDecimal(value);
			bd = bd.setScale(retain,BigDecimal.ROUND_HALF_UP);
			value = bd.toString();
		}

		return value;
	}


	/**
	 * 在开始搜集数据集 与 结束搜集数据集 中搜索数据点值
	 * @return
	 */
	public static String getEventValue(ToMESData data,Integer fieldName) {
		String value = null;

		Map<String, String> startData = data.getStartCollectionData();
		Map<String, String> endData = data.getEndCollectionData();

		if(startData != null) {
			value = startData.get(fieldName);
		}

		if(value == null && endData != null) {
			value = endData.get(fieldName);
		}

		return value;
	}


	/**
	 * 在开始搜集数据集 中搜索数据点值
	 * @return
	 */
	public static String getStartEventValue(ToMESData data,String fieldName) {
		String value = null;

		Map<String, String> startData = data.getStartCollectionData();

		if(startData != null) {
			value = startData.get(fieldName);
		}

		return value;
	}


	/**
	 * 在结束搜集数据集 中搜索数据点值
	 * @return
	 */
	public static String getEndEventValue(ToMESData data,String fieldName) {
		String value = null;

		Map<String, String> endData = data.getEndCollectionData();

		if(endData != null) {
			value = endData.get(fieldName);
		}

		return value;
	}



	/**
	 * 在结束搜集数据集 中搜索数据点值
	 * @return
	 */
	public static String getProcessStep(String key) {
		if(StringUtils.isBlank(key)) {
			return null;
		}

		String value = stepMapping.get(new BigDecimal(key).intValue());
		return value;
	}


	/**
	 * 在开始搜集数据集 与 结束搜集数据集 中搜索数据点值
	 * @return
	 */
	public static String getStartCollectionEventValue(ToMESData data,String fieldName,int retain) {
		String value = null;
		try{
			Map<String, String> startData = data.getStartCollectionData();

			if(startData != null) {
				value = startData.get(fieldName);
			}

			if(value.equals("null")){
				value = null;
			}

			if(value!=null) {
				BigDecimal bd = new BigDecimal(value);
				bd = bd.setScale(retain,BigDecimal.ROUND_HALF_UP);
				value = bd.toString();
			}
		}catch (Exception e){
			value = null;
		}
		return value;
	}

	/**
	 * 在开始搜集数据集 与 结束搜集数据集 中搜索数据点值
	 * @return
	 */
	public static String getStartCollectionEventValue(ToMESData data,String fieldName) {
		String value = null;
		try{
			Map<String, String> startData = data.getStartCollectionData();

			if(startData != null) {
				value = startData.get(fieldName);
			}

			if(value == "null"){
				value = null;
			}
		}catch (Exception e){
			value = null;
		}
		return value;
	}

	/**
	 * 在结束搜集数据集 中搜索数据点值
	 * @return
	 */
	public static String getEndCollectionEventValue(ToMESData data,String fieldName) {
		String value = null;
		try{
			Map<String, String> endData = data.getEndCollectionData();

			if(endData != null) {
				value = endData.get(fieldName);
			}

			if(value == "null"){
				value = null;
			}
		}catch (Exception e){
			value = null;
		}
		return value;
	}

	/**
	 * 在结束触发数据集中搜索数据点值
	 * @return
	 * */
	public static String getEndValidateMap(ToMESData data,String fieldName) {
		String value = null;
		try{
			Map<String, Object> endData = data.getEndValidateMap();

			if(endData != null) {
				value = endData.get(fieldName).toString();
			}

			if(value == "null"){
				value = null;
			}
		}catch (Exception e){
			value = null;
		}
		return value;
	}

}
