package com.ycl.utils.common;
|
|
/**
|
* @author Lyq
|
* @version 1.0
|
* @date 2022/9/7
|
*/
|
public class RandomUtils {
|
/**
|
* 用户名
|
*/
|
private static final String NICK_NAME = "NK";
|
|
/**
|
* 标记长度
|
*/
|
public static final int SIGN_LENGTH = 6;
|
|
/**
|
* 标记格式化格式化字符串
|
*/
|
private static final String FORMT_STRING = "888888";
|
|
private static final String FORMT_STRING_0 = "000000";
|
|
/**
|
* 获取用户名
|
*
|
* @param userId
|
* @return
|
*/
|
public static String getUserId(long userId) {
|
return NICK_NAME + formatSign(userId)
|
+ generateRandomInt(SIGN_LENGTH);
|
|
}
|
|
/**
|
* 格式化标记
|
*
|
* @return4
|
*/
|
public static String formatSign(Integer param, Integer lenght) {
|
String str = Long.toString(param);
|
int length = str.length() - lenght;
|
if (length == 0) {
|
return str;
|
} else if (length < 0) {
|
str = "0" + str;
|
return FORMT_STRING_0.substring(0, Math.abs(length) - 1) + str;
|
} else {
|
return str.substring(length);
|
}
|
}
|
|
/**
|
* 格式化标记
|
*
|
* @return4
|
*/
|
private static String formatSign(long param) {
|
String str = Long.toString(param);
|
int length = str.length() - SIGN_LENGTH;
|
if (length == 0) {
|
return str;
|
} else if (length < 0) {
|
str = "0" + str;
|
return FORMT_STRING.substring(0, Math.abs(length) - 1) + str;
|
} else {
|
return str.substring(length);
|
}
|
}
|
|
/**
|
* 获取指定长度(1位到9位)的随机数字字符串
|
*
|
* @param length 大于1 小于9
|
* @return
|
*/
|
public static String generateRandomInt(int length) {
|
length = length < 1 ? 1 : length;
|
length = length > 9 ? 9 : length;
|
int max = ((int) Math.pow(10, length)) - 1;
|
int min = (int) Math.pow(10, length - 1);
|
return String.valueOf((int) (Math.random() * (max - min) + min));
|
}
|
}
|