package com.bonus.canteen.core.utils;

import com.bonus.common.core.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.util.HashSet;
import java.util.Set;

/**
 * @author xliu
 * @date 2025/4/7 17:25
 */
public class BnsUtils {

    public static void copyProperties(Object source, Object target) {
        if (source != null && target != null) {
            BeanUtils.copyProperties(source, target, getNullPropertyNames(source));
        } else {
            throw new ServiceException("复制对象时,源和目标对象均不能为空");
        }
    }

    public static String[] getNullPropertyNames(Object source) {
        BeanWrapper src = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = src.getPropertyDescriptors();
        Set<String> emptyNames = new HashSet();
        PropertyDescriptor[] var4 = pds;
        int var5 = pds.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            PropertyDescriptor pd = var4[var6];
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }
        }

        String[] result = new String[emptyNames.size()];
        return (String[])emptyNames.toArray(result);
    }
}
