package com.jiayue.ipfcst.common.core.util;

import org.springframework.cglib.beans.BeanMap;

import java.util.HashMap;
import java.util.Map;

/**
 * bean和map互转工具
 *
 * @author zzy
 * @version 1.0
 * @since 2019/10/11 14:30
 */
public class BeanMapUtil {
    /**
     * 将对象装换为map
     * @param bean 实体对象
     * @param <T> 实体对象泛型
     * @return map
     */
    public static <T> Map<String, Object> beanToMap(T bean) {
        Map<String, Object> map = new HashMap<>();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                map.put(key+"", beanMap.get(key));
            }
        }
        return map;
    }

    /**
     * 将map装换为javabean对象
     * @param map map
     * @param bean bean 类
     * @return bean对象
     */
    public static <T> T mapToBean(Map<String, Object> map,T bean) {
        BeanMap beanMap = BeanMap.create(bean);
        beanMap.putAll(map);
        return bean;
    }
}
