package com.tofly.znb.common.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class ServerUtil {

    private static String PCH = "PCH_";

    public static String getZnbPCH() {
        return PCH + getTimeSequence();
    }

    /**
     *  获取欠费流水编号
     */
    public static String getQflsbh() {
        return String.format("Q%s%05d", getTimeSequence(), (int) ((Math.random() * 9 + 1) * 10000));
    }

    /**
     *  获取收费流水编号
     */
    public static String getSflsbh() {
        return String.format("S%s%05d", getTimeSequence(), (int) ((Math.random() * 9 + 1) * 10000));
    }

    /**
     *  获取 "yyyyMMddHHmmss"格式序号
     */
    public static String getTimeSequence() {
        return DateUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_PATTERN);
    }

    /**
     *  获取对象空字段列表
     */
    public static String[] getNullFields(Object obj) throws IllegalArgumentException {
        if (obj == null) {
            throw new IllegalArgumentException("输入对象不能为null");
        }

        List<String> nullFields = new ArrayList<>();
        Class<?> clazz = obj.getClass();

        // 获取所有声明的字段(包括私有字段)
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            try {
                // 设置可访问私有字段
                field.setAccessible(true);
                // 获取字段值
                Object value = field.get(obj);
                if (value == null) {
                    nullFields.add(field.getName());
                }
            } catch (IllegalAccessException e) {
                // 如果无法访问字段，跳过它
                System.err.println("无法访问字段: " + field.getName());
            }
        }
        return nullFields.toArray(new String[0]);
    }

}
