package com.diagbot.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: map工具类
 * @author: gaodm
 * @time: 2018/9/4 9:24
 */
public class MapUtil {
    private static final String SP = ";";
    private static final String SSP = ":";

    /**
     * 把Map转换成String。注意以英文分号字符'；'开始和结束
     *
     * @param attrs
     * @return
     */
    public static String toString(Map<String, String> attrs) {
        StringBuilder sb = new StringBuilder();
        if (null != attrs && !attrs.isEmpty()) {
            sb.append(SP);
            for (String key : attrs.keySet()) {
                String val = attrs.get(key);
                if (val != null && !"".equals(val)) {
                    sb.append(key).append(SSP).append(val).append(SP);
                }
            }
        }
        return sb.toString();
    }

    /**
     * 把key:value;key:value格式的String转换成Map
     *
     * @param str
     * @return
     */
    public static Map<String, String> fromString(String str) {
        Map<String, String> attrs = new HashMap<String, String>();
        if (str != null && !"".equals(str)) {
            String[] arr = str.split(SP);
            if (null != arr) {
                for (String kv : arr) {
                    if (kv != null && !"".equals(kv)) {
                        String[] ar = kv.split(SSP);
                        if (null != ar && ar.length == 2) {
                            String key = ar[0];
                            String val = ar[1];
                            if (val != null && !"".equals(val)) {
                                attrs.put(key, val);
                            }
                        }
                    }
                }
            }
        }
        return attrs;
    }

    public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
        if (map == null) {
            return null;
        }

        Object obj = beanClass.newInstance();

        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            Method setter = property.getWriteMethod();
            if (setter != null) {
                setter.invoke(obj, map.get(property.getName()));
            }
        }

        return obj;
    }

    public static Map<String, Object> objectToMap(Object obj) throws Exception {
        if (obj == null) {
            return null;
        }

        Map<String, Object> map = new HashMap<String, Object>();

        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (key.compareToIgnoreCase("class") == 0) {
                continue;
            }
            Method getter = property.getReadMethod();
            Object value = getter != null ? getter.invoke(obj) : null;
            map.put(key, value);
        }
        return map;
    }

    /**
     * 浙江医院用，两MAP合并成一个MAP对象
     * @return
     */
    public static Map<String, Object> objcetKeyConvert(Map<String, String> columnMap, Map<String, Object> entityMap) throws Exception{
        Map<String, Object> wholeMap = new HashMap<>();
        for (String objKey : columnMap.keySet()) {
            wholeMap.put(objKey,entityMap.get(columnMap.get(objKey)));
        }
        return wholeMap;
    }

}
