package com.tofly.common.core.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;

/**
 * @author tuonbed
 * @date 2020/5/8  10:51
 * 实体类与其他类型相互装换
 */
public class BeanUtils {

    private static boolean WRITE_NULL=true;

    public static <T> T mapToBean(Map<String, Object> map, Class<T> clazz) {

        try {
            T t = clazz.newInstance();
            BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                if (map.containsKey(key)) {
                    Object value = map.get(key);
                    Method setter = property.getWriteMethod();
                    Class<?>[] paramTypes = setter.getParameterTypes();
                    if(paramTypes!=null) {
                        Class<?> param = paramTypes[0];
                        // 特殊转换注册
                        try {
                            //                        if ("actual_amt".equalsIgnoreCase(param.getName())) {
                            if (param.getName() == "java.lang.Long") {
                                if (null != value) {
                                    setter.invoke(t, java.lang.Long.valueOf(value.toString()).longValue());
                                } else {
                                    setter.invoke(t, value);
                                }
                            } else {
                                if (value != null && value.getClass().getName() == "java.lang.Integer") {
                                    setter.invoke(t, ((java.lang.Integer) value).intValue());
                                } else {
                                    setter.invoke(t, value);
                                }
                            }
                        } catch (Exception e) {
                            System.err.println(param.getName() + ":" + value.getClass());
                            throw e;
                        }
                    }
                }
            }
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Map<String, Object> beanToMap(Object bean) {
        return beanToMap(bean,WRITE_NULL);
//        if (bean == null) {
//            return null;
//        }
//        Map<String, Object> map = new HashMap<String, Object>();
//        try {
//            BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
//            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
//            for (PropertyDescriptor property : propertyDescriptors) {
//                String key = property.getName();
//                if (!key.equals("class")) {
//                    Method getter = property.getReadMethod();
//                    Object value = getter.invoke(bean);
//                    map.put(key, value);
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return map;
    }

    public static Map<String, Object> beanToMap(Object bean,Boolean writenull) {
        if (bean == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                if (!key.equals("class")) {
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(bean);
                    if(writenull || value!=null){
                        map.put(key, value);
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
