From 4fec2319e14b7919ce46077e88bac244f9468a57 Mon Sep 17 00:00:00 2001 From: haoxr <1490493387@qq.com> Date: Sat, 24 Dec 2022 13:20:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=B8=9A=E5=8A=A1=E5=BA=8F?= =?UTF-8?q?=E5=8F=B7=E7=94=9F=E6=88=90=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/redis/BusinessSnGenerator.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/youlai-common/common-redis/src/main/java/com/youlai/common/redis/BusinessSnGenerator.java b/youlai-common/common-redis/src/main/java/com/youlai/common/redis/BusinessSnGenerator.java index 2429a512b..27e37896b 100644 --- a/youlai-common/common-redis/src/main/java/com/youlai/common/redis/BusinessSnGenerator.java +++ b/youlai-common/common-redis/src/main/java/com/youlai/common/redis/BusinessSnGenerator.java @@ -1,6 +1,6 @@ package com.youlai.common.redis; -import com.youlai.common.constant.RedisConstants; +import cn.hutool.core.util.StrUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.core.RedisTemplate; @@ -15,22 +15,30 @@ import java.time.format.DateTimeFormatter; @RequiredArgsConstructor public class BusinessSnGenerator { + private final RedisTemplate redisTemplate; /** - * @param digit 业务序号位数 + * @param digit 业务序号位数 * @return */ - public String generateSerialNo(Integer digit) { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); - String date = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter); - String key = RedisConstants.BUSINESS_NO_PREFIX +":" + date; + public String generateSerialNo(String businessType, Integer digit) { + if (StrUtil.isBlank(businessType)) { + businessType = "COMMON"; + } + String date = LocalDateTime.now(ZoneOffset.of("+8")) + .format(DateTimeFormatter.ofPattern("yyyyMMdd")); + String key = "SN:" + businessType + ":" + date; Long increment = redisTemplate.opsForValue().increment(key); return date + String.format("%0" + digit + "d", increment); } - public String generateSerialNo(){ - return this.generateSerialNo(6); + public String generateSerialNo(Integer digit) { + return this.generateSerialNo(null,6); + } + + public String generateSerialNo(String businessType) { + return this.generateSerialNo(businessType,6); } }