|
|
@@ -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);
|
|
|
}
|