package com.platform.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class UtilsHelper {
	/**
	 * 
	 * @param clazz
	 * @param req
	 * @return
	 */
	public static Object newObjAndSetAttrsByClass(Class<?> clazz,
			Map<String, String> paramMap) {
		Object object = null;
		try {
			object = clazz.newInstance();
			Field[] fileds = clazz.getDeclaredFields();			
			for (Field fs : fileds) {
				String fieldName = fs.getName();
				String fieldType = fs.getGenericType().toString()
						.replace("class ", ""); // 如果type是类类型，则前面包含"class "，后面跟类名
				if (paramMap.containsKey(fieldName)) {
					Method method = clazz.getDeclaredMethod(
							getAttributeSetName(fs), Class.forName(fieldType));
					String value = paramMap.get(fieldName);					
					if ("java.lang.Integer".equals(fieldType)) {
						method.invoke(object, Integer.valueOf(value));
					} else {
						method.invoke(object, value);
					}
				}
			}
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch blockAQA
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return object;
	};

	/**
	 * 
	 * @param object
	 * @param req
	 * @return
	 */
	public static Object newObjAndSetAttrsByInstance(Object object,
			Map<String, String> mapParam) {
		return newObjAndSetAttrsByClass(object.getClass(), mapParam);
	}

	/**
	 * 生成属性{@fs}的set方法的名字
	 * 
	 * @param fs
	 *            属性对象
	 * @return set方法的名字
	 */
	private static String getAttributeSetName(Field fs) {
		String fieldName = fs.getName();
		char[] cs = fieldName.toCharArray();
		cs[0] -= 32;
		return "set" + String.valueOf(cs);
	}

	public static <K, V> Map<K, V> updateMap(Map<K, V> map1, Map<K, V> map2) {
		if (null == map1 || map1.size() <= 0)
			return map2;
		Iterator<Entry<K, V>> iterator = map2.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry<K, V> entry = iterator.next();
			map1.put(entry.getKey(), entry.getValue());
		}
		return map1;
	}

	public static <K, V> boolean isSubMap(Map<K, V> map1, Map<K, V> map2) {
		boolean flag = true;
		if (null == map2 || map2.size() <= 0 || null == map1)
			return false;
		Iterator<Map.Entry<K, V>> iterator = map1.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry<K, V> entry = iterator.next();
			if (!map2.containsKey(entry.getKey())
					|| !map2.containsValue(entry.getValue()))
				return false;
		}
		return flag;
	}

}
