package com.yuchen.saas.park.api.util;

import net.logstash.logback.encoder.org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @Author: baichaojie
 * @CreateTime: 2024/10/30 14:37
 * @Description: TODO
 * @Version: 1.0
 */
public class StringUtil {


    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 固定位数不足补位
     * @param str 操作字符串
     * @param length 位数
     * @param c 要补位的字符
     * @return
     */
    public static String supplyLength(String str, int length, char c) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = str.length(); i < length; i++) {
            stringBuilder.append(c);
        }
        stringBuilder.append(str);
        return stringBuilder.toString();
    }

    /**
     * This method is used for checking string start with prefix
     *
     * @param str
     * @param prefix
     * @return
     */
    public static boolean startWith(String str, String prefix) {
        return StringUtils.startsWith(str, prefix);
    }

    /**
     * This method is used for checking whether string is empty
     *
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        return StringUtils.isEmpty(str);
    }

    /**
     * This method is used for splitting string by separator
     * @param str
     * @param separator
     * @return
     */
    public static String[] split(String str, String separator) {
        return StringUtils.split(str, separator);
    }

    /**
     * This method is used for converting object to string
     * @param obj
     * @return
     */
    public static String objectToString(Object obj) {
        return ReflectionToStringBuilder.toString(obj);
    }

    /**
     *
     * This method is used for replace required character with new new character
     * @param inputStr
     * @param requiredStr
     * @param replacementStr
     * @return String
     */
    public static String replaceAll(String inputStr, String requiredStr, String replacementStr) {
        String outputStr = null;
        if (null != inputStr && null != requiredStr && null != replacementStr) {
            outputStr = inputStr.replaceAll(requiredStr, replacementStr);
        }
        return outputStr;
    }

    /**
     * 字符串数组转int数组
     *
     * @param strArr 字符数组
     * @param count  截取的长度
     * @return
     */
    public static int[] strArrayToIntArray(String[] strArr, int count) {
        int[] b = new int[count];
        for (int i = 0; i < count; i++) {
            b[i] = Integer.parseInt(strArr[i]);
        }

        return b;
    }

    /**
     * 用英文逗号隔开的String转为list
     *
     * @param str
     * @return
     */
    public static List<String> switchToList(String str) {
        List<String> resultList = new ArrayList<String>();
        if (StringUtils.isBlank(str)) {
            return resultList;
        }
        String[] split = str.split(",");
        Arrays.asList(split).stream().forEach(item -> {
            resultList.add(item.trim());
        });
        return resultList;
    }

    /**
     * 分割list集合
     *
     * @param lists 被分割的list集合
     * @param num   list的size大小
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> splitList(List<T> lists, Integer num) {
        int maxCount;
        if (num == null || num == 0) {
            maxCount = 1000;
        } else {
            maxCount = num.intValue();
        }
        List<List<T>> result = new ArrayList<>();
        if (lists != null) {
            //记录数量
            int length = lists.size();
            int time = length / num;
            //计算拆分几条
            time = length % num > 0 ? time + 1 : time;
            int end;
            List<T> tmpList;
            for (int i = 1, start = 0; i <= time; i++) {
                if (i == time) {
                    end = length;
                } else {
                    end = i * maxCount;
                }
                tmpList = lists.subList(start, end);
                start = end;
                result.add(tmpList);
            }
        }
        return result;
    }

    /**
     * 验证字符串是否为纯数字
     *
     * @param str 需要校验的字符串
     * @return true：纯数字  false：非纯数字
     */
    public static boolean validateStrIsNumber(String str) {
        return str.matches("[0-9]+");
    }

}
