Explorar el Código

嵌入式数据库增加service层

欧阳劲驰 hace 2 semanas
padre
commit
8a934c2a9a

+ 11 - 11
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/components/AccessKeyGenerator.java

@@ -1,7 +1,7 @@
 package com.shkpr.service.customgateway.core.components;
 
 import com.shkpr.service.customgateway.core.domain.po.AccessKeys;
-import com.shkpr.service.customgateway.core.repository.embedded.AccessKeysRepository;
+import com.shkpr.service.customgateway.core.service.AccessKeysService;
 import com.shkpr.service.customgateway.core.utils.HexUtil;
 import org.springframework.security.crypto.keygen.BytesKeyGenerator;
 import org.springframework.security.crypto.keygen.KeyGenerators;
@@ -22,12 +22,12 @@ public class AccessKeyGenerator {
      */
     private final static BytesKeyGenerator securityKeyGenerator = KeyGenerators.secureRandom(20);
     /**
-     * 存储库
+     * 存服务
      */
-    private final AccessKeysRepository repository;
+    private final AccessKeysService service;
 
     public AccessKeyGenerator(Builder builder) {
-        this.repository = builder.repository;
+        this.service = builder.service;
     }
 
     /**
@@ -51,7 +51,7 @@ public class AccessKeyGenerator {
         String securityKey = HexUtil.encodeHex(security).toUpperCase();
         //构建并保存
         AccessKeys accessKeys = AccessKeys.of(accessKey, securityKey);
-        repository.save(accessKeys);
+        service.save(accessKeys);
 
         return accessKeys;
     }
@@ -61,18 +61,18 @@ public class AccessKeyGenerator {
      */
     public static class Builder {
         /**
-         * 存储库
+         * 存服务
          */
-        private AccessKeysRepository repository;
+        private AccessKeysService service;
 
         public Builder() {
         }
 
         /**
-         * 配置存储库
+         * 配置存服务
          */
-        public Builder repository(AccessKeysRepository repository) {
-            this.repository = repository;
+        public Builder service(AccessKeysService service) {
+            this.service = service;
             return this;
         }
 
@@ -81,7 +81,7 @@ public class AccessKeyGenerator {
          */
         public AccessKeyGenerator build() {
             // 参数验证
-            if (repository == null) throw new IllegalStateException("存储库不能为空");
+            if (service == null) throw new IllegalStateException("存储服务不能为空");
 
             return new AccessKeyGenerator(this);
         }

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

@@ -4,8 +4,7 @@ import com.shkpr.service.customgateway.core.constants.AreaCode;
 import com.shkpr.service.customgateway.core.constants.DeviceKind;
 import com.shkpr.service.customgateway.core.domain.Device;
 import com.shkpr.service.customgateway.core.domain.DeviceTag;
-import com.shkpr.service.customgateway.core.domain.po.DeviceSequences;
-import com.shkpr.service.customgateway.core.repository.embedded.DeviceSequencesRepository;
+import com.shkpr.service.customgateway.core.service.DeviceSequencesService;
 import org.apache.commons.lang3.StringUtils;
 
 import java.time.LocalDate;
@@ -25,12 +24,12 @@ public class DeviceIdGenerator {
      */
     private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd");
     /**
-     * 存储
+     * 存储服务
      */
-    private final DeviceSequencesRepository repository;
+    private final DeviceSequencesService service;
 
     public DeviceIdGenerator(Builder builder) {
-        this.repository = builder.repository;
+        this.service = builder.service;
     }
 
     /**
@@ -68,54 +67,30 @@ public class DeviceIdGenerator {
         //当前日期
         LocalDate now = LocalDate.now();
         //下一个序列
-        int sequence = getNextSequence(areaCode, kind, now);
+        int sequence = service.getNextSequence(areaCode, kind, now);
         //组装id
         return String.format("%03d%02d%s%04d", areaCode.getCode(), kind.getCode(), now.format(formatter), sequence);
     }
 
-    /**
-     * 获取下一个序号
-     *
-     * @param areaCode 区号
-     * @param kind     种类
-     * @param date     时间
-     * @return 序号
-     */
-    private synchronized int getNextSequence(AreaCode areaCode, DeviceKind kind, LocalDate date) {
-        synchronized (repository) {
-            //自增
-            repository.incrementSeqBYDate(areaCode.getCode(), kind.getCode(), date);
-
-            //获取序号
-            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");
-            return seq;
-        }
-    }
-
 
     /**
      * 构建器
      */
     public static class Builder {
         /**
-         * 存储
+         * 存储服务
          */
-        private DeviceSequencesRepository repository;
+        private DeviceSequencesService service;
 
 
         public Builder() {
         }
 
         /**
-         * 配置存储
+         * 配置存储服务
          */
-        public Builder repository(DeviceSequencesRepository repository) {
-            this.repository = repository;
+        public Builder service(DeviceSequencesService service) {
+            this.service = service;
             return this;
         }
 
@@ -125,7 +100,7 @@ public class DeviceIdGenerator {
          */
         public DeviceIdGenerator build() {
             // 参数验证
-            if (repository == null) throw new IllegalStateException("存储库不能为空");
+            if (service == null) throw new IllegalStateException("存储服务不能为空");
 
             return new DeviceIdGenerator(this);
         }

+ 3 - 3
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/config/DeviceConfig.java

@@ -5,7 +5,7 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
 import com.shkpr.service.customgateway.core.components.DeviceIdGenerator;
 import com.shkpr.service.customgateway.core.components.DeviceRegistry;
 import com.shkpr.service.customgateway.core.properties.DeviceProperties;
-import com.shkpr.service.customgateway.core.repository.embedded.DeviceSequencesRepository;
+import com.shkpr.service.customgateway.core.service.DeviceSequencesService;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -35,9 +35,9 @@ public class DeviceConfig {
      * @return 设备id生成器
      */
     @Bean
-    public DeviceIdGenerator deviceIdGenerator(DeviceSequencesRepository repository) {
+    public DeviceIdGenerator deviceIdGenerator(DeviceSequencesService service) {
         return DeviceIdGenerator.builder()
-                .repository(repository)
+                .service(service)
                 .build();
     }
 }

+ 3 - 3
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/config/SecurityConfig.java

@@ -5,7 +5,7 @@ import com.shkpr.service.customgateway.core.constants.ResponseCode;
 import com.shkpr.service.customgateway.core.filter.TokenFilter;
 import com.shkpr.service.customgateway.core.properties.GlobalProperties;
 import com.shkpr.service.customgateway.core.properties.SecurityProperties;
-import com.shkpr.service.customgateway.core.repository.embedded.AccessKeysRepository;
+import com.shkpr.service.customgateway.core.service.AccessKeysService;
 import com.shkpr.service.customgateway.core.utils.ResponseUtil;
 import com.shkpr.service.customgateway.core.utils.TokenUtil;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -95,9 +95,9 @@ public class SecurityConfig {
      * @return 访问密钥生成器
      */
     @Bean
-    public AccessKeyGenerator accessKeyGenerator(AccessKeysRepository repository) {
+    public AccessKeyGenerator accessKeyGenerator(AccessKeysService service) {
         return AccessKeyGenerator.builder()
-                .repository(repository)
+                .service(service)
                 .build();
     }
 }

+ 1 - 1
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/repository/embedded/AccessKeysRepository.java

@@ -5,7 +5,7 @@ import org.springframework.data.repository.CrudRepository;
 import org.springframework.stereotype.Repository;
 
 /**
- * 设备序列
+ * 访问密钥
  *
  * @author 欧阳劲驰
  * @since 1.0.0

+ 19 - 0
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/service/AccessKeysService.java

@@ -0,0 +1,19 @@
+package com.shkpr.service.customgateway.core.service;
+
+import com.shkpr.service.customgateway.core.domain.po.AccessKeys;
+
+/**
+ * 访问密钥service
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+public interface AccessKeysService {
+    /**
+     * 保存
+     *
+     * @param accessKeys 服务密钥
+     * @return 保存状态
+     */
+    Boolean save(AccessKeys accessKeys);
+}

+ 24 - 0
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/service/DeviceSequencesService.java

@@ -0,0 +1,24 @@
+package com.shkpr.service.customgateway.core.service;
+
+import com.shkpr.service.customgateway.core.constants.AreaCode;
+import com.shkpr.service.customgateway.core.constants.DeviceKind;
+
+import java.time.LocalDate;
+
+/**
+ * 设备序列service
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+public interface DeviceSequencesService {
+    /**
+     * 获取下一个序号
+     *
+     * @param areaCode 区号
+     * @param kind     种类
+     * @param date     时间
+     * @return 序号
+     */
+    Integer getNextSequence(AreaCode areaCode, DeviceKind kind, LocalDate date);
+}

+ 32 - 0
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/service/impl/AccessKeysServiceImpl.java

@@ -0,0 +1,32 @@
+package com.shkpr.service.customgateway.core.service.impl;
+
+import com.shkpr.service.customgateway.core.domain.po.AccessKeys;
+import com.shkpr.service.customgateway.core.repository.embedded.AccessKeysRepository;
+import com.shkpr.service.customgateway.core.service.AccessKeysService;
+import org.springframework.stereotype.Service;
+
+/**
+ * 访问密钥service实现
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+@Service
+public class AccessKeysServiceImpl implements AccessKeysService {
+    final
+    AccessKeysRepository repository;
+
+    public AccessKeysServiceImpl(AccessKeysRepository repository) {
+        this.repository = repository;
+    }
+
+    /**
+     * 保存
+     *
+     * @param accessKeys 服务密钥
+     * @return 保存状态
+     */
+    public Boolean save(AccessKeys accessKeys) {
+        return repository.save(accessKeys).getId() != null;
+    }
+}

+ 51 - 0
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/service/impl/DeviceSequencesServiceImpl.java

@@ -0,0 +1,51 @@
+package com.shkpr.service.customgateway.core.service.impl;
+
+
+import com.shkpr.service.customgateway.core.constants.AreaCode;
+import com.shkpr.service.customgateway.core.constants.DeviceKind;
+import com.shkpr.service.customgateway.core.domain.po.DeviceSequences;
+import com.shkpr.service.customgateway.core.repository.embedded.DeviceSequencesRepository;
+import com.shkpr.service.customgateway.core.service.DeviceSequencesService;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.time.LocalDate;
+
+/**
+ * 设备序列service实现
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+@Service
+public class DeviceSequencesServiceImpl implements DeviceSequencesService {
+    final
+    DeviceSequencesRepository repository;
+
+    public DeviceSequencesServiceImpl(DeviceSequencesRepository repository) {
+        this.repository = repository;
+    }
+
+    /**
+     * 获取下一个序号
+     *
+     * @param areaCode 区号
+     * @param kind     种类
+     * @param date     时间
+     * @return 序号
+     */
+    @Transactional
+    public Integer getNextSequence(AreaCode areaCode, DeviceKind kind, LocalDate date) {
+        //自增
+        repository.incrementSeqBYDate(areaCode.getCode(), kind.getCode(), date);
+
+        //获取序号
+        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");
+        return seq;
+    }
+}