package com.steering.pss.sale.order.craftDesign.tool;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscBiltCEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscGzGgEntity;

public class CraftTool {
	/**
	 * 将对象属性NULL转化为空字符串
	 * 
	 * @param obj
	 */
	public static void nullConverNullString(Object obj) {
		if (obj != null) {
			Class<? extends Object> classz = obj.getClass();
			// 获取所有该对象的属性值
			Field fields[] = classz.getDeclaredFields();
			// 遍历属性值，取得所有属性为 null 值的
			for (Field field : fields) {
				try {
					Type t = field.getGenericType();
					if (!t.toString().equals("boolean")) {
						Method m = classz.getMethod("get"
								+ change(field.getName()));
						Object name = m.invoke(obj);// 调用该字段的get方法
						if (name == null) {

							Method mtd = classz.getMethod(
									"set" + change(field.getName()),
									new Class[] { String.class });// 取得所需类的方法对象
							mtd.invoke(obj, new Object[] { "" });// 执行相应赋值方法
						}
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * @param src
	 *            源字符串
	 * @return 字符串，将src的第一个字母转换为大写，src为空时返回null
	 */
	public static String change(String src) {
		if (src != null) {
			StringBuffer sb = new StringBuffer(src);
			sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
			return sb.toString();
		} else {
			return null;
		}
	}

	/**
	 * 验证字符串 如：1<2
	 * 
	 * @param strA
	 *            数字字符串
	 * @param sign
	 *            符号
	 * @param strB
	 *            数字字符串
	 * @return true Or false
	 */
	public static boolean checkStr(String strA, String sign, String strB) {
		if (strA.length() == 0 || strB.length() == 0) {
			return true;
		}
		double a = Double.parseDouble(strA);
		double b = Double.parseDouble(strB);
		if (sign.equals("=")) {
			if (a == b) {
				return true;
			} else {
				return false;
			}
		}
		if (sign.equals(">")) {
			if (a > b) {
				return true;
			} else {
				return false;
			}
		}
		if (sign.equals(">=")) {
			if (a >= b) {
				return true;
			} else {
				return false;
			}
		}
		if (sign.equals("<")) {
			if (a < b) {
				return true;
			} else {
				return false;
			}
		}
		if (sign.equals("<=")) {
			if (a <= b) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}

	/**
	 * 比较两个数组元素相同
	 * 
	 * @param arrayA
	 *            数组A
	 * @param arrrayB
	 *            数组B
	 * @return boolean
	 */
	public static boolean checkStringArraysCommon(String[] arrayA,
			String[] arrayB) {
		if (arrayA.length == 0 && arrayB.length != 0) {
			return false;
		}
		if (arrayA.length != 0 && arrayB.length == 0) {
			return false;
		}
		if (arrayA.length == 0 && arrayB.length == 0) {
			return true;
		}
		Arrays.sort(arrayA);
		Arrays.sort(arrayB);
		if (Arrays.equals(arrayA, arrayB)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 压缩比计算方法
	 * 
	 * @param rollSpecs
	 * @param biltsSpecs
	 * @return
	 */
	public static boolean compressioRatio(
			List<CraftOrdDesignMscGzGgEntity> rollSpecs,
			List<CraftOrdDesignMscBiltCEntity> biltsSpecs) {
		if (rollSpecs == null || rollSpecs.size() == 0) {
			return true;
		}
		if (biltsSpecs == null || biltsSpecs.size() == 0) {
			return true;
		}
		for (CraftOrdDesignMscGzGgEntity rollSpec : rollSpecs) {
			double dimater = Double.parseDouble(rollSpec.getDimater());
			double height = Double.parseDouble(rollSpec.getHeight());
			for (CraftOrdDesignMscBiltCEntity biltsSpec : biltsSpecs) {
				double specName = Double.parseDouble(biltsSpec.getCSpecName());
				// 坯料断面=πD²/4
				double blank = Math.PI * specName * specName / 4;
				// 轧管断面=πD1²/4-πD2²/4 “D1”为轧管外径；“D2”=轧管外径-轧管壁厚×2
				double roll = Math.PI * dimater * dimater / 4 - Math.PI
						* (dimater - height * 2) * (dimater - height * 2) / 4;
				if (blank / roll <= 3) {
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * 计算字符串公式
	 * 
	 * @param formula
	 *            字符串
	 * @return Object
	 * @throws ScriptException
	 */
	public static double countFormula(String formula) throws ScriptException {
		ScriptEngine jse = new ScriptEngineManager()
				.getEngineByName("JavaScript");
		return (Double) jse.eval(formula);
	}

	/**
	 * 验证是否为DOUBLE
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		try {
			Double.parseDouble(str);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	public static <T> List<T> fiterCommon(List<T> objs, String[] fitParams)
			throws Exception {
		List<String> list = Arrays.asList(fitParams);
		HashMap<String, T> map = new HashMap<String, T>();
		for (T obj : objs) {
			Class<? extends Object> classz = obj.getClass();
			// 获取所有该对象的属性值
			Field[] fields = classz.getDeclaredFields();
			// 取出过滤字段的值组成主键
			String strs = "";
			for (Field field : fields) {
				// 如果过滤的字段组包含某个属性，获取这个属性的值
				if (list.contains(field.getName())) {
					Method m = classz.getMethod("get"
							+ CraftTool.change(field.getName()));
					Object name = m.invoke(obj);
					strs += name.toString() + ",";
				}
			}
			if (!map.containsKey(strs)) {
				map.put(strs, obj);
			}
		}
		// 然后迭代取值
		List<T> fitObj = new ArrayList<T>();
		Iterator<?> iter = map.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<?, ?> entry = (Entry<?, ?>) iter.next();
			fitObj.add((T) entry.getValue());
		}
		return fitObj;
	}

}
