package com.tuoheng.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.*;

/**
 * map工具类
 *
 */
@Slf4j
public class MapUtils {

    /**
     * map的参数组装拼接
     *
     * @param map
     * @return
     */
    public static String MapParamToStr(Map<String, Object> map) {
        if (map == null) return "";

        // 定义初始化
        StringBuilder sb = new StringBuilder();
        int total = 1;
        int size = map.size();

        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();

            if (total >= 0 && total < size) {
                sb.append(key + "=" + value + "&");
            } else if (total == size) {
                sb.append(key + "=" + value);
            }

            total++;
        }
        return sb.toString();
    }


    /**
     * 利用反射将map集合封装成bean对象
     *
     * @param map
     * @param clazz
     * @return
     */
    public static <T> T mapToBean(Map<String, Object> map, Class<?> clazz) throws Exception {
        Object obj = clazz.newInstance();
        if (map != null && !map.isEmpty() && map.size() > 0) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String propertyName = entry.getKey();    // 属性名
                Object value = entry.getValue();        // 属性值
                String setMethodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
                Field field = getClassField(clazz, propertyName);    //获取和map的key匹配的属性名称
                if (field == null) {
                    continue;
                }
                Class<?> fieldTypeClass = field.getType();
                value = convertValType(value, fieldTypeClass);
                try {
                    clazz.getMethod(setMethodName, field.getType()).invoke(obj, value);
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
        }
        return (T) obj;
    }

    /**
     * 对象转map
     *
     * @param obj
     * @return
     */
    public static Map<String, Object> objToMap(Object obj) {

        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        Field[] fields = obj.getClass().getDeclaredFields();    // 获取f对象对应类中的所有属性域
        Field[] superFields = obj.getClass().getSuperclass().getDeclaredFields();

        List<Field> fieldsList = Arrays.asList(fields);
        ArrayList<Field> fieldArrayList = new ArrayList<>(fieldsList);
        for (Field superField : superFields) {
            fieldArrayList.add(superField);
        }
        Collections.reverse(fieldArrayList);

        for (int i = 0; i < fieldArrayList.size(); i++) {
            String varName = fieldArrayList.get(i).getName();
            varName = varName.toLowerCase();                    // 将key置为小写，默认为对象的属性
            try {
                boolean accessFlag = fieldArrayList.get(i).isAccessible();    // 获取原来的访问控制权限
                fieldArrayList.get(i).setAccessible(true);                    // 修改访问控制权限
                Object o = fieldArrayList.get(i).get(obj);                    // 获取在对象f中属性fields[i]对应的对象中的变量
                if (o != null) {
                    map.put(varName, o.toString());
                }
                fieldArrayList.get(i).setAccessible(accessFlag);            // 恢复访问控制权限
            } catch (IllegalArgumentException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            }
        }
        return map;
    }


    /**
     * 根据给定对象类匹配对象中的特定字段
     *
     * @param clazz
     * @param fieldName
     * @return
     */
    private static Field getClassField(Class<?> clazz, String fieldName) {
        if (Object.class.getName().equals(clazz.getName())) {
            return null;
        }
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            if (field.getName().equals(fieldName)) {
                return field;
            }
        }
        Class<?> superClass = clazz.getSuperclass();    //如果该类还有父类，将父类对象中的字段也取出
        if (superClass != null) {                        //递归获取
            return getClassField(superClass, fieldName);
        }
        return null;
    }

    /**
     * 将map的value值转为实体类中字段类型匹配的方法
     *
     * @param value
     * @param fieldTypeClass
     * @return
     */
    private static Object convertValType(Object value, Class<?> fieldTypeClass) {
        Object retVal = null;

        if (Long.class.getName().equals(fieldTypeClass.getName())
                || long.class.getName().equals(fieldTypeClass.getName())) {
            retVal = Long.parseLong(value.toString());
        } else if (Integer.class.getName().equals(fieldTypeClass.getName())
                || int.class.getName().equals(fieldTypeClass.getName())) {
            retVal = Integer.parseInt(value.toString());
        } else if (Float.class.getName().equals(fieldTypeClass.getName())
                || float.class.getName().equals(fieldTypeClass.getName())) {
            retVal = Float.parseFloat(value.toString());
        } else if (Double.class.getName().equals(fieldTypeClass.getName())
                || double.class.getName().equals(fieldTypeClass.getName())) {
            retVal = Double.parseDouble(value.toString());
        } else {
            retVal = value;
        }
        return retVal;
    }


}
