package com.ruoyi.platform.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;


/**
 * @Author ifredom
 * @Description 类型转换: Entity - Vo转换
 * @Date 2022/5/10 15:59
 * @Param [params]
 **/
public class ConvertUtil {
    public static final Logger logger = LoggerFactory.getLogger(ConvertUtil.class);


        /**
     * 将实体对象转换为VO对象
     * @param source 源实体对象
     * @param target 目标VO对象的类
     * @return 转换后的VO对象
     */
    public static <T> T entityToVo(Object source, Class<T> target) {
        if (source == null) {
            return null;
        }
        T targetObject = null;
        try {
            targetObject = target.newInstance();
            BeanUtils.copyProperties(source, targetObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return targetObject;
    }


    // 将实体对象列表转换为目标类型的列表
    public static <T> List<T> entityToVoList(Collection<?> sourceList, Class<T> target) {
        if (sourceList == null) {
            return null;
        }
        List<T> targetList = new ArrayList<>(sourceList.size());

        try {
            for (Object source : sourceList) {
                T targetObject = target.newInstance();
                BeanUtils.copyProperties(source, targetObject);
                targetList.add(targetObject);
            }
        } catch (Exception e) {
            logger.error("convert error ", e);
        }
        return targetList;
    }

    public static <T> List<T> convertListMapToObjectList(List<Map<String, Object>> listMap, Class<T> targetClass) {
        List<T> resultList = new ArrayList<>();

        if (listMap != null) {
            for (Map<String, Object> map : listMap) {
                T targetObject = convertMapToObject(map, targetClass);
                if (targetObject != null) {
                    resultList.add(targetObject);
                }
            }
        }

        return resultList;
    }

    private static <T> T convertMapToObject(Map<String, Object> map, Class<T> targetClass) {
        try {
            T targetObject = targetClass.newInstance();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                String camelCaseKey = toCamelCase(key);
                if (hasProperty(targetClass, camelCaseKey)) {
                    PropertyUtils.setProperty(targetObject, camelCaseKey, value);
                }
            }
            return targetObject;
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static boolean hasProperty(Class<?> clazz, String propertyName) {
        try {
            Field field = clazz.getDeclaredField(propertyName);
            return field != null;
        } catch (NoSuchFieldException e) {
            return false;
        }
    }

    private static String toCamelCase(String key) {
        StringBuilder sb = new StringBuilder();
        boolean nextUpperCase = false;
        for (char c : key.toCharArray()) {
            if (c == '_') {
                nextUpperCase = true;
            } else {
                if (nextUpperCase) {
                    sb.append(Character.toUpperCase(c));
                    nextUpperCase = false;
                } else {
                    sb.append(c);
                }
            }
        }
        return sb.toString();
    }
}

