package com.zfire.jiasm.syncdata.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @author yaozx
 * @since 2022-09-16
 */
public class MyBeanUtils {

    private MyBeanUtils() {
    }

    public static void copyProperties(Object src, Object target) {
        BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
    }

    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());
            }
        }

        return (String[])emptyNames.toArray(new String[0]);
    }

    public static String getClientIp(String XForwardedFor, String XRealIP, String PROXYFORWARDEDFOR) {
        String ip = "";
        if (!StringUtils.isEmpty(XForwardedFor)) {
            ip = XForwardedFor.split("\\,")[0];
        } else if (!StringUtils.isEmpty(XRealIP)) {
            ip = XRealIP;
        } else if (!StringUtils.isEmpty(PROXYFORWARDEDFOR)) {
            ip = PROXYFORWARDEDFOR;
        }

        return ip;
    }

    public static String getDateXS(Date date){
        String str = "上午";
        SimpleDateFormat sdf = new SimpleDateFormat("HH");
        Integer sj = Integer.valueOf(sdf.format(date));
        if (sj >= 12){
            str = "下午";
        }

        return str;
    }
}
