package com.ruoyi.system.utils;
|
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
import java.util.concurrent.ThreadLocalRandom;
|
|
/**
|
* 订单号生成器
|
*
|
* @author mitao
|
* @since 2025-12-16
|
*/
|
public class OrderCodeGenerator {
|
|
private static final DateTimeFormatter ORDER_CODE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
|
|
/**
|
* 大礼包订单前缀
|
*/
|
public static final String PREFIX_BIG_GIFT = "BG";
|
|
/**
|
* 大礼包合并支付订单前缀
|
*/
|
public static final String PREFIX_BIG_GIFT_PAY = "BGP";
|
|
/**
|
* 实物商品订单前缀
|
*/
|
public static final String PREFIX_PHYSICAL_GOODS = "PG";
|
|
/**
|
* 大礼包尾款支付订单前缀
|
*/
|
public static final String PREFIX_BIG_GIFT_FINAL = "BGF";
|
|
private OrderCodeGenerator() {
|
// 私有构造函数,防止实例化
|
}
|
|
/**
|
* 生成订单号
|
*
|
* @param prefix 订单号前缀
|
* @return 订单号(格式:前缀 + yyyyMMddHHmmss + 4位随机数)
|
*/
|
public static String generate(String prefix) {
|
String timestamp = LocalDateTime.now().format(ORDER_CODE_FORMATTER);
|
int randomNum = ThreadLocalRandom.current().nextInt(1000, 10000);
|
return prefix + timestamp + randomNum;
|
}
|
|
/**
|
* 生成大礼包订单号
|
*
|
* @return 大礼包订单号
|
*/
|
public static String generateBigGiftOrderCode() {
|
return generate(PREFIX_BIG_GIFT);
|
}
|
|
/**
|
* 生成大礼包合并支付订单号
|
*
|
* @return 大礼包合并支付订单号
|
*/
|
public static String generateBigGiftPayOrderCode() {
|
return generate(PREFIX_BIG_GIFT_PAY);
|
}
|
|
/**
|
* 生成实物商品订单号
|
*
|
* @return 实物商品订单号
|
*/
|
public static String generatePhysicalGoodsOrderCode() {
|
return generate(PREFIX_PHYSICAL_GOODS);
|
}
|
|
/**
|
* 生成大礼包尾款支付订单号
|
*
|
* @return 大礼包尾款支付订单号
|
*/
|
public static String generateBigGiftFinalPayOrderCode() {
|
return generate(PREFIX_BIG_GIFT_FINAL);
|
}
|
}
|