Kaynağa Gözat

设备id生成增加区号和种类

欧阳劲驰 2 hafta önce
ebeveyn
işleme
98727fa77d

+ 9 - 6
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/components/DeviceIdGenerator.java

@@ -68,7 +68,7 @@ public class DeviceIdGenerator {
         //当前日期
         LocalDate now = LocalDate.now();
         //下一个序列
-        int sequence = getNextSequence(now);
+        int sequence = getNextSequence(areaCode, kind, now);
         //组装id
         return String.format("%03d%02d%s%04d", areaCode.getCode(), kind.getCode(), now.format(formatter), sequence);
     }
@@ -76,17 +76,20 @@ public class DeviceIdGenerator {
     /**
      * 获取下一个序号
      *
-     * @param date 时间
+     * @param areaCode 区号
+     * @param kind     种类
+     * @param date     时间
      * @return 序号
      */
-    private synchronized int getNextSequence(LocalDate date) {
+    private synchronized int getNextSequence(AreaCode areaCode, DeviceKind kind, LocalDate date) {
         synchronized (repository) {
             //自增
-            repository.incrementSeqBYDate(date);
+            repository.incrementSeqBYDate(areaCode.getCode(), kind.getCode(), date);
 
             //获取序号
-            Integer seq = repository.findSeqByDate(date);
-            if (seq == null) seq = repository.save(DeviceSequences.of(date, 1)).getSeq();
+            Integer seq = repository.findSeqByDate(areaCode.getCode(), kind.getCode(), date);
+            if (seq == null)
+                seq = repository.save(DeviceSequences.of(areaCode.getCode(), kind.getCode(), date, 1)).getSeq();
 
             //检查序号是否超出范围
             if (9999 < seq) throw new IllegalStateException("当日设备序号已用尽,无法生成新的设备ID");

+ 10 - 2
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/domain/po/DeviceSequences.java

@@ -25,6 +25,14 @@ public class DeviceSequences {
     @Id
     private Long id;
     /**
+     * 区号
+     */
+    private Integer areaCode;
+    /**
+     * 种类
+     */
+    private Integer kind;
+    /**
      * 时间
      */
     private LocalDate date;
@@ -33,7 +41,7 @@ public class DeviceSequences {
      */
     private Integer seq;
 
-    public static DeviceSequences of(LocalDate date, Integer seq) {
-        return new DeviceSequences(null, date, seq);
+    public static DeviceSequences of(Integer areaCode, Integer kind, LocalDate date, Integer seq) {
+        return new DeviceSequences(null, areaCode, kind, date, seq);
     }
 }

+ 7 - 5
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/repository/embedded/DeviceSequencesRepository.java

@@ -24,9 +24,11 @@ public interface DeviceSequencesRepository extends CrudRepository<DeviceSequence
      * @param date 日期
      */
     @Modifying
-    @Query("merge into device_sequences (date, seq) key (date)" +
-            " values (:date, coalesce((select seq from device_sequences where date = :date), 0) + 1)")
-    void incrementSeqBYDate(@Param("date") LocalDate date);
+    @Query("merge into device_sequences (area_code, kind, date, seq) key (area_code, kind, date)" +
+            " values (:areaCode, :kind, :date, coalesce((" +
+            "select seq from device_sequences where area_code = :areaCode and kind = :kind and date = :date" +
+            "), 0) + 1)")
+    void incrementSeqBYDate(@Param("areaCode") Integer areaCode, @Param("kind") Integer kind, @Param("date") LocalDate date);
 
     /**
      * 查询序列
@@ -34,6 +36,6 @@ public interface DeviceSequencesRepository extends CrudRepository<DeviceSequence
      * @param date 日期
      * @return 序列
      */
-    @Query("select seq from device_sequences where date = :date")
-    Integer findSeqByDate(@Param("date") LocalDate date);
+    @Query("select seq from device_sequences where area_code = :areaCode and kind = :kind and date = :date")
+    Integer findSeqByDate(@Param("areaCode") Integer areaCode, @Param("kind") Integer kind, @Param("date") LocalDate date);
 }

BIN
data.mv.db