|
@@ -372,24 +372,26 @@ public class IotOpeationFillServiceImpl implements IotOpeationFillService {
|
|
|
}
|
|
|
|
|
|
// 用于记录每天的序列号,确保当天内不重复
|
|
|
- private static final ConcurrentHashMap<String, AtomicInteger> dailySequence = new ConcurrentHashMap<>();
|
|
|
+ private static final ConcurrentHashMap<String, Integer> dailySequence = new ConcurrentHashMap<>();
|
|
|
// 日期格式化器,用于生成每天的唯一标识
|
|
|
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
|
|
|
+
|
|
|
public static synchronized int generateUniqueNumber() {
|
|
|
// 获取当前日期字符串,如20231005
|
|
|
String today = LocalDate.now().format(DATE_FORMATTER);
|
|
|
+
|
|
|
// 提取年份后两位和月份日期的哈希值作为日期标识(2位)
|
|
|
int dateCode = (Integer.parseInt(today.substring(2, 4)) +
|
|
|
Integer.parseInt(today.substring(4, 6)) +
|
|
|
Integer.parseInt(today.substring(6, 8))) % 99 + 1;
|
|
|
|
|
|
- // 获取当天的序列号生成器,不存在则创建
|
|
|
- AtomicInteger sequence = dailySequence.computeIfAbsent(today, k -> new AtomicInteger(1));
|
|
|
- // 获取下一个序列号
|
|
|
- int seq = sequence.getAndIncrement();
|
|
|
+ // 获取并递增当天的序列号,不存在则初始化为1
|
|
|
+ int seq = dailySequence.merge(today, 1, Integer::sum);
|
|
|
|
|
|
// 检查是否超过每天的最大生成量
|
|
|
if (seq > 9999) {
|
|
|
+ // 回退计数器,因为我们已经递增了它
|
|
|
+ dailySequence.put(today, 9999);
|
|
|
throw new RuntimeException("今日已超过最大生成数量(9999个)");
|
|
|
}
|
|
|
|