|
|
@@ -0,0 +1,58 @@
|
|
|
+package com.middle.platform.common.utils;
|
|
|
+
|
|
|
+import cn.hutool.core.date.LocalDateTimeUtil;
|
|
|
+import com.google.common.cache.CacheBuilder;
|
|
|
+import com.google.common.cache.CacheLoader;
|
|
|
+import com.google.common.cache.LoadingCache;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
+
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author xucaiqin
|
|
|
+ * @date 2023-12-28 14:28:05
|
|
|
+ */
|
|
|
+public class OrderUtil {
|
|
|
+ private final static String ymd = "yyyyMMdd";
|
|
|
+ private final static LoadingCache<String, Long> cache = CacheBuilder.newBuilder()
|
|
|
+ .expireAfterWrite(24 * 61, TimeUnit.MINUTES) // 设置写入后的过期时间
|
|
|
+ .maximumSize(100) // 设置最大缓存大小
|
|
|
+ .build(new CacheLoader<>() {
|
|
|
+ @NotNull
|
|
|
+ @Override
|
|
|
+ public Long load(String key) {
|
|
|
+ return 1L;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 流水号
|
|
|
+ *
|
|
|
+ * @param prefix 前缀
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @SneakyThrows
|
|
|
+ public static String get(String prefix) {
|
|
|
+ String key = prefix + getYMD();
|
|
|
+ Long aLong = cache.get(key);
|
|
|
+ Optional.of(aLong).ifPresent(v -> cache.put(key, v + 1));
|
|
|
+ return key + String.format("%05d", aLong);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static String getYMD() {
|
|
|
+ return LocalDateTimeUtil.format(LocalDate.now(), DateTimeFormatter.ofPattern(ymd));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ for (int i = 0; i < 10; i++) {
|
|
|
+ System.out.println(get("PRO01"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|