package com.zm.order.tool.server.utils;

import com.google.common.collect.Maps;
import org.springframework.cglib.beans.BeanMap;

import java.util.Map;

/**
 * beanMap工具类
 *
 * @author nianchang
 * @ClassName BeanMapUtils
 * @date 2021/08/09 5:42 下午
 */
public class BeanMapUtils {

    /**
     * 将javabean对象转换为map
     *
     * @param bean 数据源
     * @return Map
     * Date: 2021/8/9 5:43 下午
     */
    public static <T> Map<String, Object> beanToMap(T bean) {
        Map<String, Object> map = Maps.newHashMap();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                Object value = beanMap.get(key);
                map.put(key.toString(), value);
            }
        }
        return map;
    }

    /**
     * 将map转换为javabean对象
     *
     * @param map  数据源
     * @param bean 目的数据源
     * @return T
     * Date: 2021/8/9 5:42 下午
     */
    public static <T> T mapToBean(Map<String, Object> map, T bean) {
        if (map != null) {
            BeanMap beanMap = BeanMap.create(bean);
            beanMap.putAll(map);
        }
        return bean;
    }
}
