package com.yzt.airconditionersystem.util;

import java.lang.reflect.Field;
import java.util.*;

/**
 * 工具类
 * @author TzZach
 * @since 2022-04-13 12:31
 */
public class MyUtils {

    /**
     * 生成普通名字
     * @param number 名字字母数
     * @return
     */
    public static String name(int number) {
        String name = String.valueOf(Character.toChars((int) (Math.random() * 26 + 65)));
        for (int i = 0; i < number - 1; i++) {
            String chars = String.valueOf(Character.toChars((int) (Math.random() * 26 + 97)));
            name += chars;
        }
        return name;
    }

    /**
     * 生成公司名字
     * @param number 名字字母数
     * @return
     */
    public static String companyName(int number) {
        String name = String.valueOf(Character.toChars((int) (Math.random() * 26 + 65)));
        for (int i = 0; i < number - 1; i++) {
            String chars = String.valueOf(Character.toChars((int) (Math.random() * 26 + 97)));
            name += chars;
        }
        return name + "公司";
    }

    public static String phone() {
        String phone = "1";

        for (int i = 0; i < 10; i++) {
            phone += (int)(Math.random() * 10);
        }

        return phone;
    }


    public static String decToBin(String data) {
        data = Integer.toBinaryString(Integer.parseInt(data));
        int length = data.length();
        if (length < 16) {
            int numOfZeros = 16 - length;
            for (int j = 0; j < numOfZeros; j++) {
                data = 0 + data;
            }
        }
        return data;
    }

    public static String decToBin(Integer data) {
        String dataBinStr = Integer.toBinaryString(data);
        int length = dataBinStr.length();
        if (length < 16) {
            int numOfZeros = 16 - length;
            for (int j = 0; j < numOfZeros; j++) {
                dataBinStr = 0 + dataBinStr;
            }
        }
        return dataBinStr;
    }


    public static String getUUID() {
        String string = UUID.randomUUID().toString();
        String uuid = string.replace("-", "");
        return uuid;
    }

    /**
     * 判断对象的属性为空的数量
     * @param t
     * @param <T>
     * @return
     */
    public static <T> Integer isFieldNull(T t) throws IllegalAccessException {
        int count = 0;
        Class<?> tClass = t.getClass();
        Field[] declaredFields = tClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            if (declaredField.get(t) == null) {
                count++;
            }
        }

        return count;
    }

    public static <T> Boolean isFieldsAllNull(T t) throws IllegalAccessException {
        int count = 0;
        Class<?> tClass = t.getClass();
        Field[] declaredFields = tClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            if (declaredField.get(t) == null || "serialVersionUID".equals(declaredField.getName())) {
                count++;
            }
        }

        return count == declaredFields.length;
    }

}
