package com.huigou.topsun.util;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.UUID;

public class CommonUtil {

	/**
	 * @Description TODO(自动生成uuid)
	 */
	public static String createUuid() {
		UUID u = UUID.randomUUID();
		String uuid = u.toString().replace("-", "").toUpperCase();
		return uuid;
	}

	/**
	 * 实体类转Map
	 * @param obj
	 * @return
	 */
	public static HashMap<String, String> entityToMap(Object obj) {
		HashMap<String, String> map = new HashMap();
		for (Field field : obj.getClass().getDeclaredFields()){
			try {
				boolean flag = field.isAccessible();
				field.setAccessible(true);
				Object o = field.get(obj);
				if (o != null){
					Field contField = obj.getClass().getDeclaredField(field.getName());
					JsonProperty voField = contField.getAnnotation(JsonProperty.class);
					String s =  voField.value();
					map.put(s, o.toString());
				}
				field.setAccessible(flag);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return map;
	}
}
