package com.ruoyi.system.util; import com.ruoyi.common.core.utils.DateUtils; import java.security.SecureRandom; import java.util.Date; import java.util.Random; /** * @author jqs34 * @ClassName CodeFactoryUtil * @description: TODO * @date 2023年02月13日 * @version: 1.0 */ public class CodeFactoryUtil { /** * APP用户编码 */ private static final String APP_USER_PREFIX = "HRT_M"; /** * 商户编号 */ private static final String SHOP_PREFIX = "HRT_S"; /** * 订单前缀 */ private static final String ORDER_PREFIX = "BO"; /** * 退款订单前缀 */ private static final String ORDER_REFUND_PREFIX = "RO"; /** * 用户id和随机数总长度 */ private static final int maxLength = 4; /** * 更具id进行加密+加随机数组成固定长度编码 */ public static void main(String[] args) { Long orderId = 1L; String userNo = getShopNo(orderId); System.out.println(userNo); } /** * 获取商户编号 * * @param shopId * @return */ public static String getShopNo(Long shopId) { String prefix = SHOP_PREFIX; return toFillZeroCode(prefix, maxLength, shopId); } /** * 获取会员编号 * @param userId * @return */ public static String getMemberNo(Long userId) { String prefix = APP_USER_PREFIX; return toFillZeroCode(prefix, maxLength, userId); } /** * 获取订单编号 * * @param userId * @return */ public static String getOrderNo(Long userId) { String prefix = DateUtils.parseDateToStr( "yyyyMMddHHmmss",new Date()); prefix = ORDER_PREFIX + prefix; return toFillZeroCode(prefix, maxLength, userId); } /** * 获取用户编号 * * @param userId * @return */ public static String getAppUserNo(Long userId) { return toFillZeroCode(APP_USER_PREFIX, 9, userId); } /** * 0补位 * * @param prefix * @param totalLength * @param id * @return */ private static String toFillZeroCode(String prefix, int totalLength, Long id) { String idStr = id.toString(); int length = idStr.length(); int fillLength = totalLength - length; StringBuilder idsbs = new StringBuilder(prefix); for (int i = 0; i < fillLength; i++) { idsbs.append("0"); } return idsbs.append(idStr).toString(); } /** * 随机6位数生成 */ public static String getRandStr(int num) { // 默认6位 num = num != 0 ? num : 6; Random r = new SecureRandom(); StringBuffer str = new StringBuffer(); int i = 0; while (i < num) { str.append(r.nextInt(10)); i++; } return str.toString(); } }