package nc.util.fa.httputil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;


import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pub.lang.UFDouble;
import uap.json.JSONObject;

public class BeanUtils {
	/**
	 * 将源Bean转换为目标Bean 要求:需要两个互转的VO字段名一致
	 * 
	 * @param srcPropety    源
	 * @param targetPropety 目标
	 * @return
	 */
	public static Object copy(Object srcPropety, Class targetPropety) {
		Object resultValue = null;
		try {
			resultValue = targetPropety.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		for (Field targetField : targetPropety.getDeclaredFields()) {// 获取目标字段
			try {
				targetField.setAccessible(true);
				if (java.lang.reflect.Modifier.isFinal(targetField.getModifiers()))
					continue;
				targetField.set(resultValue, getFieldValueByName(targetField.getName(), srcPropety));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return resultValue;
	}

	/**
	 * 根据JSON字段 将值Set入VO中,需要保证VO拥有该字段
	 * 
	 * @param <T>
	 * @param propety  需要转换的目标VO
	 * @param jsonData 需要转换的JSON数据
	 * @return
	 */
	public static <T> T JSONObjectToVO(Class clazz, String strDate) {
		Object resultValue = null;
		JSONObject jsonData = null;
		Iterator keys ;
		try {
			jsonData =new JSONObject(strDate);
			keys = jsonData.keys();
			resultValue = clazz.newInstance();
		} catch (Exception e1) {
			e1.printStackTrace();
			return null;
		}
		Field fields[] = clazz.getDeclaredFields();
		Set<String> set = new HashSet<String>();
		for (Field fieldItem : fields) {
			set.add(fieldItem.getName());
		}
		while (keys.hasNext()) {
			String jsonField = (String)keys.next();
			try {
				if (!set.contains(jsonField))
					continue;// 此处对JSON内有但VO内没有的字段进行过滤
				Field field = clazz.getDeclaredField(jsonField);
				field.setAccessible(true);
				if (java.lang.reflect.Modifier.isFinal(field.getModifiers()))
					continue;
				if (UFDate.class == field.getType()) {// 此处if判断,因暂时遇见该三种类型,若后续有其它类型,请自行添加
					field.set(resultValue, new UFDate(jsonData.getString(jsonField)));
					continue;
				}
				if (UFDouble.class == field.getType()) {
					field.set(resultValue, new UFDouble(jsonData.getString(jsonField)));
					continue;
				}
				if (UFDateTime.class == field.getType()) {
					field.set(resultValue, new UFDateTime(jsonData.getString(jsonField)));
					continue;
				}
				if (Integer.class == field.getType()) {
					field.set(resultValue, new Integer(jsonData.getString(jsonField)));
					continue;
				}
				if (UFBoolean.class == field.getType()) {
					String str = jsonData.getString(jsonField);
					if (str != null && "Y".equals(str.toUpperCase()))
						field.set(resultValue, new UFBoolean(jsonData.getString(jsonField)));
					continue;
				}
				field.set(resultValue, jsonData.getString(jsonField));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return (T) resultValue;
	}

	/**
	 * 根据属性名获取值
	 * 
	 * @param fieldName
	 * @param o
	 * @return
	 */
	private static Object getFieldValueByName(String fieldName, Object o) {
		try {
			String firstLetter = fieldName.substring(0, 1).toUpperCase();
			String getter = "get" + firstLetter + fieldName.substring(1);
			Method method = o.getClass().getMethod(getter, new Class[] {});
			Object value = method.invoke(o, new Object[] {});
			return value;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 校验放入的数据是否有null，如果有则 抛出异常不储存数据
	 * 
	 * @param jsonDataByInteface
	 * @param type
	 * @throws BusinessException
	 * @throws ParseException 
	 */
	public void checkNeedItem(String jsonData, String[] checkItem) throws BusinessException, ParseException {
		JSONObject object = new JSONObject(jsonData);
		for (int i = 0; i < checkItem.length; i++) {
			if (object.get(checkItem[i]) == null || "".equals(object.get(checkItem[i]))) {
				throw new BusinessException(checkItem[i] + "是null ,请插入数据");
			}
		}
	}

}
