package cn.ysk.cashier.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author lyf
 */
public class StringUtil {
    /**
     * 生成指定长度的随机数
     * @param length
     * @return
     */
    public static String genRandomNum(int length) {
        int maxNum = 36;
        int i;
        int count = 0;
        char[] str = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while (count < length) {
            i = Math.abs(r.nextInt(maxNum));
            pwd.append(str[i]);
            count++;
        }
        return pwd.toString();
    }

    /**
     * 生成订单号
     *
     * @return
     */
    public static synchronized String getBillno() {
        StringBuilder billno = new StringBuilder();

        // 日期(格式:20080524)
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        billno.append(format.format(new Date()));
        return billno.toString();
    }
    public static JSONArray stringChangeList(String listString){
        // 使用Fastjson将JSON字符串转换为JSONArray对象
        return JSON.parseArray(listString);

    }

    public static String random(int length) {
        Random random = new Random();
        String result = "";
        for (int i = 0; i < length; i++) {
            result += random.nextInt(10);
        }
        return result;
    }

    private static final String CHINESE_CHARS = "你我他她它们这里那里多少是否好坏快慢上下左右前后高低大小长短方圆胖瘦黑白红绿蓝黄紫粉红桔红橙黄棕灰褐";

    // 生成随机中文昵称
    public static String generateRandomNickname(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(CHINESE_CHARS.length());
            char randomChar = CHINESE_CHARS.charAt(index);
            sb.append(randomChar);
        }
        return desensitizeNickname(sb.toString());
    }

    // 对昵称进行脱敏处理
    public static String desensitizeNickname(String nickname) {
        if (nickname == null || nickname.length() != 5) {
            return nickname;
        }
        return nickname.charAt(0) + "***" + nickname.charAt(4);
    }

}
