From 7ef4892f9f24f941aca37e6b3991b808a0aca619 Mon Sep 17 00:00:00 2001 From: zhanghua <314079846@qq.com> Date: 星期五, 08 九月 2023 11:16:35 +0800 Subject: [PATCH] 优化 --- ycl-common/src/main/java/com/ycl/utils/common/RandomUtils.java | 78 +++++++++++++++++++++++++++++++++++++++ 1 files changed, 78 insertions(+), 0 deletions(-) diff --git a/ycl-common/src/main/java/com/ycl/utils/common/RandomUtils.java b/ycl-common/src/main/java/com/ycl/utils/common/RandomUtils.java index 34c3268..67b56a5 100644 --- a/ycl-common/src/main/java/com/ycl/utils/common/RandomUtils.java +++ b/ycl-common/src/main/java/com/ycl/utils/common/RandomUtils.java @@ -6,4 +6,82 @@ * @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)); + } } -- Gitblit v1.8.0