package com.ylcm.sys.common;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.ylcm.sys.domain.BasePojo;

import net.sf.json.JSONObject;

public class ObjectUtil {

	/**
	 * map转java类
	 * 
	 * @param pojo
	 * @param map
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public static Object mapToObject(BasePojo pojo, Map<String, Object> map)
			throws InstantiationException, IllegalAccessException {
		Class c = pojo.getClass();
		Object o = c.newInstance();
		Field[] fs = c.getDeclaredFields();
		for (String key : map.keySet()) {
			for (Field field : fs) {
				if (key.equals(field.getName())) {
					field.setAccessible(true);
					Object obj = map.get(key);
					if (obj instanceof String) {
						field.set(o, String.valueOf(obj));
						break;
					} else if (obj instanceof Long) {
						field.set(o, Long.valueOf(obj + ""));
						break;
					} else if (obj instanceof Integer || obj instanceof BigInteger) {
						field.set(o, Integer.valueOf(obj + ""));
						break;
					} else if (obj instanceof Double) {
						field.set(o, Double.valueOf(obj + ""));
						break;
					} else if (obj == null) {
						break;
					} else {
						field.set(o, String.valueOf(obj));
						break;
					}
				}
			}
		}

		return o;
	}
	
	/**
	 * map转java类
	 * 
	 * @param pojo
	 * @param map
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public static Object mapToObject(Object ob, Map<String, Object> map)
			throws InstantiationException, IllegalAccessException {
		Class c = ob.getClass();
		Object o = c.newInstance();
		Field[] fs = c.getDeclaredFields();
		for (String key : map.keySet()) {
			for (Field field : fs) {
				if (key.equals(field.getName())) {
					field.setAccessible(true);
					Object obj = map.get(key);
					if (obj instanceof String) {
						field.set(o, String.valueOf(obj));
						break;
					} else if (obj instanceof Long) {
						field.set(o, Long.valueOf(obj + ""));
						break;
					} else if (obj instanceof Integer || obj instanceof BigInteger) {
						field.set(o, Integer.valueOf(obj + ""));
						break;
					} else if (obj instanceof Double) {
						field.set(o, Double.valueOf(obj + ""));
						break;
					} else if (obj == null) {
						break;
					} else {
						field.set(o, String.valueOf(obj));
						break;
					}
				}
			}
		}

		return o;
	}

	/**
	 * json转java类
	 * 
	 * @param pojo
	 * @param map
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public static Object jsonToObject(Object pojo, JSONObject map)
			throws InstantiationException, IllegalAccessException {
		Class c = pojo.getClass();
		Object o = c.newInstance();
		Field[] fs = c.getDeclaredFields();

		Set<String> keys = map.keySet();
		for (Object key : keys) {
			for (Field field : fs) {
				if (key.equals(field.getName())) {
					field.setAccessible(true);
					Object obj = map.get(key);
					if (obj instanceof String) {
						field.set(o, String.valueOf(obj));
						break;
					} else if (obj instanceof Long) {
						field.set(o, Long.valueOf(obj + ""));
						break;
					} else if (obj instanceof Integer || obj instanceof BigInteger) {
						field.set(o, Integer.valueOf(obj + ""));
						break;
					} else if (obj instanceof Double) {
						field.set(o, Double.valueOf(obj + ""));
						break;
					} else if (obj == null) {
						break;
					} else {
						field.set(o, String.valueOf(obj));
						break;
					}
				}
			}
		}

		return o;
	}

	/**
	 * javabean转map
	 * 
	 * @date 2018年1月12日
	 * @author zjm
	 */
	public static Map java2Map(Object javaBean) {
		Map map = new HashMap();

		try {
			// 获取javaBean属性
			BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());

			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			if (propertyDescriptors != null && propertyDescriptors.length > 0) {
				String propertyName = null; // javaBean属性名
				Object propertyValue = null; // javaBean属性值
				for (PropertyDescriptor pd : propertyDescriptors) {
					propertyName = pd.getName();
					if (!propertyName.equals("class")) {
						Method readMethod = pd.getReadMethod();
						propertyValue = readMethod.invoke(javaBean, new Object[0]);
						map.put(propertyName, propertyValue);
					}
				}
			}

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

		return map;
	}
	
	
	/**
	 * 
	 * json字符串转map
	 * @param object
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static Map<String, Object> jsonToMap(Object object) {
	    Map<String, Object> data = new HashMap<String, Object>();
	    // 将json字符串转换成jsonObject
	    JSONObject jsonObject = JSONObject.fromObject(object);
	    Iterator ite = jsonObject.keys();
	    // 遍历jsonObject数据,添加到Map对象
	    while (ite.hasNext()) {
	        String key = ite.next().toString();
	        Object value = jsonObject.get(key);
	        data.put(key, value);
	    }
	    return data;
	}

}
