package cn.novalon.gym.manage.member.util;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Entity、Domain、VO、DTO转换工具类
 * 
 * @author 付嘉
 * @date 2026-05-27
 */
@Component
public class BeanConvertUtil {

    /**
     * 单个对象泛型转换
     * @param source 源对象
     * @param targetClass 目标类Class
     * @return 转换后的目标对象
     */
    public static <S, T> T toBean(S source, Class<T> targetClass) {
        if (source == null) {
            return null;
        }
        return BeanUtil.copyProperties(source, targetClass);
    }

    /**
     * 集合批量泛型转换
     * @param sourceList 源对象集合
     * @param targetClass 目标类Class
     * @return 转换后的目标对象集合
     */
    public static <S, T> List<T> toBeanList(List<S> sourceList, Class<T> targetClass) {
        if (sourceList == null || sourceList.isEmpty()) {
            return List.of();
        }

        List<T> targetList = new ArrayList<>();
        for (S source : sourceList) {
            targetList.add(toBean(source, targetClass));
        }
        return targetList;
    }
}
