package com.bcxin.identify.util;

import com.bcxin.identify.util.common.StringUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class MyConverUtil {


	public static Map<String, String> PO2Map(Object o) throws Exception{
		Map<String, String> map = new HashMap<String, String>();
		Field[] fields = null;
		String clzName = o.getClass().getSimpleName();
		fields = o.getClass().getDeclaredFields();
		for (Field field : fields) {
			
			field.setAccessible(true);
			String proName = field.getName();
			String types = field.getGenericType().toString();

			if(types.indexOf("interface")!=-1){
			}else if("serialVersionUID".equals(proName)){
				
			}else if(Modifier.isFinal(field.getModifiers())){
				System.out.println(proName);
			}else{
				String name = proName.substring(0, 1).toUpperCase() + proName.substring(1);
				Method m = o.getClass().getMethod("get" + name);
	            
				Object proValue = field.get(o);
				String value_obj = "";
				if(proValue == null){
					continue;
				}
				if(!"class java.lang.String".equals(types)){
					if("class java.util.Date".equals(types)){
						// 调用getter方法获取属性值
						Date value = (Date) m.invoke(o);
						if(value != null){
							value_obj = DateUtil.formatDateTime(value);
						}
					}else if("int".equals(types)){
						int value_INT = (int) m.invoke(o);
						if(value_INT == 0){
							value_obj = String.valueOf(value_INT);
						}else{
							value_obj = String.valueOf(proValue);
						}
					}else{
						value_obj = String.valueOf(proValue);
					}
					
				}else{
					value_obj = String.valueOf(proValue);
				}
				if(value_obj == "null"){
					value_obj = "";
				}
				map.put(proName, value_obj);
			}
		}
		return map;
	}
	

	public static Object map2PO(Map<String,String> map,Object o) throws Exception{
		if (!map.isEmpty()) {
			for (String k : map.keySet()) {
				String v = "";
				if (!k.isEmpty()) {
					v = String.valueOf(map.get(k));
				}
				Field[] fields = null;
				fields = o.getClass().getDeclaredFields();
				String clzName = o.getClass().getSimpleName();
				if(StringUtil.isNotBlank(v)){
					for (Field field : fields) {
						int mod = field.getModifiers();
						if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
							continue;
						}
						if (field.getName().equals(k)) {
							String types = field.getGenericType().toString();
							field.setAccessible(true);
							if("class java.lang.String".equals(types)){
								field.set(o, v);
							}
							if("class java.util.Date".equals(types)){
								if(v.length() == 10){
									v = v + " 00:00:00";
								}
								field.set(o, DateUtil.matchFormatByLength(v));
							}
							if("int".equals(types)){
								field.set(o, Integer.parseInt(v));
							}
							if("class java.math.BigDecimal".equals(types)){
								field.set(o, BigDecimal.valueOf(Double.parseDouble(v)));
							}
							if("long".equals(types)){
								field.set(o, Long.parseLong(v));
							}
							if("class java.lang.Integer".equals(types)){
								field.set(o, Integer.parseInt(v));
							}
							if("class java.lang.Boolean".equals(types)){
								field.set(o, Boolean.parseBoolean(v));
							}
						}
					}
				}
			}
		}
		return o;
	}

	public static Object map2PoToString(Map<String,String> map,Object o) throws Exception{
		if (!map.isEmpty()) {
			for (String k : map.keySet()) {
				String v = "";
				if (!k.isEmpty()) {
					v = map.get(k);
				}
				Field[] fields = null;
				fields = o.getClass().getDeclaredFields();
				String clzName = o.getClass().getSimpleName();
				for (Field field : fields) {
					int mod = field.getModifiers();
					if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
						continue;
					}
					if (field.getName().equals(k)) {
						String types = field.getGenericType().toString();
						field.setAccessible(true);
						if("class java.lang.String".equals(types)){
							field.set(o, v);
						}
						if("class java.util.Date".equals(types)&&StringUtil.isNotBlank(v)){
							if(v.length() == 10){
								v = v + " 00:00:00";
							}
							field.set(o, DateUtil.matchFormatByLength(v));
						}
						if("int".equals(types)&&StringUtil.isNotBlank(v)){
							field.set(o, Integer.parseInt(v));
						}
						if("class java.math.BigDecimal".equals(types)&&StringUtil.isNotBlank(v)){
							field.set(o, BigDecimal.valueOf(Double.parseDouble(v)));
						}
						if("long".equals(types)&&StringUtil.isNotBlank(v)){
							field.set(o, Long.parseLong(v));
						}
						if("class java.lang.Integer".equals(types)&&StringUtil.isNotBlank(v)){
							field.set(o, Integer.parseInt(v));
						}
						if("class java.lang.Boolean".equals(types)&&StringUtil.isNotBlank(v)){
							field.set(o, Boolean.parseBoolean(v));
						}
					}
				}
			}
		}
		return o;
	}
}
