From 3fc7ca3321fa383e5de6a574083a42aea7c3aa5c Mon Sep 17 00:00:00 2001 From: zhouhy Date: Sat, 7 Oct 2023 18:00:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A1=E5=88=92=E7=BC=96=E5=8F=B7=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils/uuid/PlanCodeUtils.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 aucma-common/src/main/java/com/aucma/common/utils/uuid/PlanCodeUtils.java diff --git a/aucma-common/src/main/java/com/aucma/common/utils/uuid/PlanCodeUtils.java b/aucma-common/src/main/java/com/aucma/common/utils/uuid/PlanCodeUtils.java new file mode 100644 index 0000000..17a1d3f --- /dev/null +++ b/aucma-common/src/main/java/com/aucma/common/utils/uuid/PlanCodeUtils.java @@ -0,0 +1,72 @@ +package com.aucma.common.utils.uuid; + +import com.aucma.common.core.redis.RedisCache; +import com.aucma.common.utils.StringUtils; +import com.aucma.common.utils.spring.SpringUtils; +import org.springframework.beans.factory.annotation.Autowired; + +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Calendar; +import java.util.concurrent.TimeUnit; + +/** + * @ClassName : PlanCodeUtils + * @Description : zhouhy + * @Author :生产计划-计划编号生成 + * @Date: 2023-10-07 09:46 + */ +@Component +public class PlanCodeUtils { + + + + public String getPlanCode(){ + LocalDate date = LocalDate.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd"); + String format = date.format(formatter); + Boolean flag=SpringUtils.getBean(RedisCache.class).hasKey(format); + + System.out.println(flag); + //以当日日期作为key + if (!flag) { + SpringUtils.getBean(RedisCache.class).setCacheObject(format,1); + SpringUtils.getBean(RedisCache.class).expire(format,getSecondsNextEarlyMorning(), TimeUnit.SECONDS); + } + else{ + //获取当前的value在+1 + Object cacheObject = SpringUtils.getBean(RedisCache.class).getCacheObject(format); + String s = String.valueOf(cacheObject); + Integer integer = Integer.valueOf(s); + integer = integer+1; + SpringUtils.getBean(RedisCache.class).setCacheObject(format,integer); + } + Object cacheObject = SpringUtils.getBean(RedisCache.class).getCacheObject(format); + //转成string在转成int + String code = String.format("%04d",Integer.valueOf(String.valueOf(cacheObject))); + + return date.format(formatter)+code; + } + + //判断当前时间距离第二天0点时间的秒数 + public Long getSecondsNextEarlyMorning() { + Calendar cal = Calendar.getInstance(); + cal.add(Calendar.DAY_OF_YEAR, 1); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.MILLISECOND, 0); + return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000; + + } + + + + + + + +}