|
@@ -26,6 +26,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
|
|
|
|
|
|
+import java.security.SecureRandom;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
@@ -352,7 +353,7 @@ public class IotOpeationFillServiceImpl implements IotOpeationFillService {
|
|
|
|
|
|
List<IotOpeationFillOrderDO> childList = iotOpeationFillMapper.childList(orderDO);
|
|
|
|
|
|
- int randomNum = generateUniqueNumber();
|
|
|
+ int randomNum = generate8DigitInteger();
|
|
|
|
|
|
//如果是根节点,则证明该部门为小队并且属于瑞恒,创建对应日报虚拟设备
|
|
|
if(childList.size()==0){
|
|
@@ -384,54 +385,25 @@ public class IotOpeationFillServiceImpl implements IotOpeationFillService {
|
|
|
return devList;
|
|
|
}
|
|
|
|
|
|
- // 用于记录每天的序列号,确保当天内不重复
|
|
|
- private static final ConcurrentHashMap<String, Integer> dailySequence = new ConcurrentHashMap<>();
|
|
|
- // 日期格式化器,用于生成每天的唯一标识
|
|
|
- private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
|
|
|
|
|
|
- /**
|
|
|
- * 生成每天唯一的6位数字序列号
|
|
|
- * 格式:两位日期码 + 四位序列号
|
|
|
- *
|
|
|
- * @return 唯一的6位数字
|
|
|
- * @throws RuntimeException 当超过每日最大生成量时抛出
|
|
|
- */
|
|
|
- public static 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;
|
|
|
-
|
|
|
- // 使用循环确保获取到正确的序列号,处理并发情况
|
|
|
- while (true) {
|
|
|
- // 获取当前序列号,不存在则为0
|
|
|
- Integer current = dailySequence.get(today);
|
|
|
- int nextSeq = (current == null) ? 1 : current + 1;
|
|
|
-
|
|
|
- // 检查是否超过每天的最大生成量
|
|
|
- if (nextSeq > 9999) {
|
|
|
- throw new RuntimeException("今日已超过最大生成数量(9999个)");
|
|
|
- }
|
|
|
+ // 使用SecureRandom确保随机数的安全性
|
|
|
+ private static final SecureRandom random = new SecureRandom();
|
|
|
|
|
|
- // 使用putIfAbsent确保原子性操作,如果不存在则设置为1
|
|
|
- if (current == null) {
|
|
|
- if (dailySequence.putIfAbsent(today, 1) == null) {
|
|
|
- return dateCode * 10000 + 1;
|
|
|
- }
|
|
|
- }
|
|
|
- // 使用replace确保只有当前值匹配时才更新,避免覆盖其他线程的更新
|
|
|
- else if (dailySequence.replace(today, current, nextSeq)) {
|
|
|
- return dateCode * 10000 + nextSeq;
|
|
|
- }
|
|
|
+ // 8位数字的最小值和最大值
|
|
|
+ private static final int MIN_8_DIGIT = 10000000; // 最小的8位整数
|
|
|
+ private static final int MAX_8_DIGIT = 99999999; // 最大的8位整数
|
|
|
|
|
|
- // 如果上述操作失败,循环重试
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 生成8位整型随机数
|
|
|
+ * @return 8位整数(范围:10000000 - 99999999)
|
|
|
+ */
|
|
|
+ public static int generate8DigitInteger() {
|
|
|
+ // 生成范围内的随机整数:[min, max]
|
|
|
+ return MIN_8_DIGIT + random.nextInt(MAX_8_DIGIT - MIN_8_DIGIT + 1);
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
public IotOpeationFillDO isReport(IotOpeationFillDO fillDO) {
|
|
|
return iotOpeationFillMapper.isReport(fillDO);
|