From b3b17b231e2f2840332ce6eb96f791865fdec6d5 Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期一, 14 四月 2025 16:38:16 +0800
Subject: [PATCH] 代码优化
---
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/IDLongGenerator.java | 288 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 288 insertions(+), 0 deletions(-)
diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/IDLongGenerator.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/IDLongGenerator.java
new file mode 100644
index 0000000..3ab0397
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/IDLongGenerator.java
@@ -0,0 +1,288 @@
+package com.dy.common.util;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import java.util.Calendar;
+
+@Slf4j
+@Component
+public class IDLongGenerator {
+
+ private static final Object synObj = new Object() ;
+
+ private static final int yearLength = 4 ;//4锛氬勾搴﹀彇鍥涗綅锛� 3锛氬勾搴﹀彇涓変綅锛� 2锛氬勾搴﹀彇涓や綅锛� 1锛氬勾搴﹀彇涓�浣嶏紝 0锛氬勾搴﹀彇0浣�
+
+ private static int add = 0 ;
+ private static final int chengShu = 1000 ;//1000锛氫笁浣嶈嚜澧為噺锛屽嵆涓�绉掗挓鍙骇鐢�10000涓狪D
+ private static final int maxAdd = 999 ;
+ private static long last;
+ //鍚庣紑
+ //鍦ㄥ垎甯冨紡绯荤粺涓紝渚嬪澶氫釜鏁版嵁涓棿浠禿ataMw锛屽涓郴缁熼兘浼氬悜鏁版嵁搴撲腑鎻掑叆鏁版嵁锛岀敤鐨勯兘鏄ID鐢熸垚鍣紝
+ //姝D鐢熸垚鍣ㄥ湪鍚勪釜瀛愮郴缁熶腑闅惧厤涓哄悓涓�绫绘暟鎹敓鎴愮浉鍚岀殑ID锛岄�犳垚鏁版嵁搴撴彃鍏ュ洜涓婚敭鐩稿悓鑰屾姤閿�,
+ //鎵�浠ヨ璁℃鍚庣紑锛屾瘡涓瓙绯荤粺鍚庣紑涓嶅悓
+ private static int suffix = 0 ;
+ private static final int noSuffix = 0 ;
+
+ static {
+ last = current() ;
+ }
+
+ /**
+ * 2023 10 28 09 14 40 00000
+ * 闀垮害19鐨処D锛屽勾搴﹀彇涓や綅锛岄暱搴�18鐨処D锛屽勾搴﹀彇鍥涗綅锛�17浣嶆暟瀛楄秴鍑轰簡javascript鐨勮〃鏁拌寖鍥达紝javascript浼氳〃鏁颁笉姝g‘
+ */
+ public Long generate(){
+ synchronized (synObj){
+ //Long id = doGenerate() ;
+ //log.info("浜х敓ID = " + id);
+ //return id ;
+ return doGenerate() ;
+ }
+ }
+ /**
+ * 璁剧疆鍚庣紑锛屼笉鍚屽瓙绯荤粺璁剧疆涓嶅悓鐨勫悗缂�
+ * @param suffix ID鍚庣紑锛�0鍒�99
+ */
+ public static void setSuffix(int suffix) throws Exception{
+ if(suffix < 0 || suffix > 99){
+ throw new Exception("鍚庣紑鍙兘鏄�0鍒�99鐨勬暟瀛�") ;
+ }
+ IDLongGenerator.suffix = suffix ;
+ }
+
+
+ /**
+ * 寰楀埌浠婃棩寮�濮嬬殑id锛堝锛� 230630 00000000000锛�
+ * @return ID
+ */
+ public static Long generateTodayStartId(){
+ return (currentTodayStart() * chengShu) * 100 + noSuffix;
+ }
+
+ /**
+ * 寰楀埌浠婃棩鎴鐨刬d锛堝锛� 230630 24000000000锛�
+ * @return ID
+ */
+ public static Long generateTodayEndId(){
+ return (currentTodayEnd() * chengShu) * 100 + noSuffix;
+ }
+
+
+ /**
+ * 寰楀埌鏌愭棩寮�濮嬬殑id锛堝锛� 230630 00000000000锛�
+ * @return ID
+ */
+ public static Long generateBeforeXDayStartId(int xday){
+ return (beforeXDayStart(xday) * chengShu) * 100 + noSuffix;
+ }
+
+ /**
+ * 寰楀埌鏌愭棩鎴鐨刬d锛堝锛� 230630 24000000000锛�
+ * @return ID
+ */
+ public static Long generateBeforeXDayEndId(int xday){
+ return (beforeXDayEnd(xday) * chengShu) * 100 + noSuffix;
+ }
+
+
+ /**
+ * 寰楀埌鏌愭棩寮�濮嬬殑id锛堝锛� 230630 00000000000锛�
+ * @return ID
+ */
+ public static Long generateOneDayStartId(int year, int month, int day){
+ return (currentOneDayStart(year, month, day) * chengShu) * 100 + suffix;
+ }
+
+ /**
+ * 寰楀埌鏌愭棩鎴鐨刬d锛堝锛� 230630 24000000000锛�
+ * @return ID
+ */
+ public static Long generateOneDayEndId(int year, int month, int day){
+ return (currentOneDayEnd(year, month, day) * chengShu) * 100 + suffix;
+ }
+
+
+
+ /**
+ * 鎵ц鐢熸垚
+ * @return ID 20231218 104504 06900
+ */
+ private Long doGenerate(){
+ long id ;
+ long now = current() ;
+ if(now != last){
+ //涓婃鐢熸垚ID 涓庢湰娆$敓鎴怚D 涓嶅湪鍚屼竴绉掑唴
+ last = now ;
+ add = 0 ;
+ id = last * chengShu + add++;
+ }else{
+ //涓婃鐢熸垚ID 涓庢湰娆$敓鎴怚D 鍦ㄥ悓涓�绉掑唴
+ if(add >= maxAdd){
+ //闄勫姞閲忓凡缁忕敤灏�
+ waitNextSecond(last) ;//绛夊埌涓嬩竴绉�
+ id = last * chengShu + add++ ;//杩斿洖涓婁竴绉掔敓鎴愮殑ID
+ add = 0 ;//闄勫姞閲忓綊闆讹紝涓轰笅涓�绉掑噯澶�
+ }else{
+ //闄勫姞閲忔湭鐢ㄥ敖
+ id = last * chengShu + add++ ;
+ }
+ }
+ return (id * 100) + suffix ;
+ }
+ /**
+ * 绛夊緟涓嬩竴绉掑埌鏉�
+ * @param last 涓婃鏃堕棿锛堢簿纭埌绉掞級
+ */
+ private void waitNextSecond(Long last){
+ try {
+ Thread.sleep(10);
+ } catch (InterruptedException ignored) {
+ }finally{
+ long now = current() ;
+ if(now == last){
+ waitNextSecond(last) ;
+ }
+ }
+ }
+
+
+
+ /**
+ * 鏍煎紡涓� y*yMMddHHmmss
+ * @return ID
+ */
+ private static long current(){
+ Calendar cal = Calendar.getInstance();
+ return (dealYear(cal.get(Calendar.YEAR))) * 10000000000L
+ + (cal.get(Calendar.MONTH) + 1) * 100000000L
+ + cal.get(Calendar.DAY_OF_MONTH) * 1000000L
+ + cal.get(Calendar.HOUR_OF_DAY) * 10000L
+ + cal.get(Calendar.MINUTE) * 100L
+ + cal.get(Calendar.SECOND) ;
+ }
+
+
+ /**
+ * 鏍煎紡涓�
+ * @return ID
+ */
+ private static long currentTodayStart(){
+ Calendar cal = Calendar.getInstance();
+ return (dealYear(cal.get(Calendar.YEAR))) * 10000000000L
+ + (cal.get(Calendar.MONTH) + 1) * 100000000L
+ + cal.get(Calendar.DAY_OF_MONTH) * 1000000L ;
+ }
+
+ /**
+ * 鏍煎紡涓�
+ * @return ID
+ */
+ private static long currentTodayEnd(){
+ Calendar cal = Calendar.getInstance();
+ return (dealYear(cal.get(Calendar.YEAR))) * 10000000000L
+ + (cal.get(Calendar.MONTH) + 1) * 100000000L
+ + cal.get(Calendar.DAY_OF_MONTH) * 1000000L
+ + 24 * 10000L ;
+ }
+
+
+ /**
+ * 鏍煎紡涓�
+ * @return ID
+ */
+ private static long currentOneDayStart(int year, int month, int day){
+ return (dealYear(year)) * 10000000000L
+ + month * 100000000L
+ + day * 1000000L ;
+ }
+
+ /**
+ * 鏍煎紡涓�
+ * @return ID
+ */
+ private static long currentOneDayEnd(int year, int month, int day){
+ return (dealYear(year)) * 10000000000L
+ + month * 100000000L
+ + day * 1000000L
+ + 24 * 10000L ;
+ }
+
+ /**
+ * 鏍煎紡涓�
+ * @return ID
+ */
+ private static long beforeXDayStart(int xday){
+ Calendar cal = Calendar.getInstance();
+ cal.add(Calendar.DATE, xday<=0?xday:-xday);
+ return (dealYear(cal.get(Calendar.YEAR))) * 10000000000L
+ + (cal.get(Calendar.MONTH) + 1) * 100000000L
+ + cal.get(Calendar.DAY_OF_MONTH) * 1000000L ;
+ }
+
+ /**
+ * 鏍煎紡涓�
+ * @return ID
+ */
+ private static long beforeXDayEnd(int xday){
+ Calendar cal = Calendar.getInstance();
+ cal.add(Calendar.DATE, xday<=0?xday:-xday);
+ return (dealYear(cal.get(Calendar.YEAR))) * 10000000000L
+ + (cal.get(Calendar.MONTH) + 1) * 100000000L
+ + cal.get(Calendar.DAY_OF_MONTH) * 1000000L
+ + 24 * 10000L ;
+ }
+
+ /**
+ * 澶勭悊骞村害
+ * @param year 骞村害
+ * @return 澶勭悊鍚庣殑骞村害
+ */
+ private static int dealYear(int year){
+ if(yearLength == 0){
+ return 0 ;
+ }else if(yearLength == 1){
+ return year % 10 ;
+ }else if(yearLength == 2){
+ return year % 100 ;
+ }else if(yearLength == 3){
+ return year % 1000 ;
+ }else if(yearLength == 4){
+ return year ;
+ }else{
+ return year ;
+ }
+ }
+
+// public static void main(String[] args){
+// Calendar cal = Calendar.getInstance();
+// System.out.println(cal.get(Calendar.MONTH) + 1) ;
+//
+// IDLongGenerator o = new IDLongGenerator() ;
+// int total = 1 ;
+// for(int i = 0 ; i < total ; i++){
+// System.out.println(o.generate()) ;
+// }
+// long start = System.currentTimeMillis() ;
+// long end = System.currentTimeMillis() ;
+// System.out.println("浜х敓" + total + "ID鐢ㄦ椂" + (end - start) + "姣");
+//
+// Long todayStartId = IDLongGenerator.generateTodayStartId() ;
+// System.out.println("today start id:\n" + todayStartId);
+// Long todayEndId = IDLongGenerator.generateTodayEndId() ;
+// System.out.println("today end id:\n" + todayEndId);
+//
+//
+// Long beforeXDayStartId = IDLongGenerator.generateBeforeXDayStartId(3) ;
+// System.out.println("before3Day start id:\n" + beforeXDayStartId);
+// Long beforeXDayEndId = IDLongGenerator.generateBeforeXDayEndId(3) ;
+// System.out.println("before3Day end id:\n" + beforeXDayEndId);
+//
+//
+// Long oneDayStartId = IDLongGenerator.generateOneDayStartId(2023, 7, 9) ;
+// System.out.println("OneDay(2023-07-09) start id:\n" + oneDayStartId);
+// Long oneDayEndId = IDLongGenerator.generateOneDayEndId(2023, 7, 11) ;
+// System.out.println("OneDay(2023-07-11) end id:\n" + oneDayEndId);
+// }
+
+}
--
Gitblit v1.8.0