refactor: 业务序号生成优化

This commit is contained in:
haoxr 2022-12-24 13:20:27 +08:00
parent b2a0dfabb8
commit 4fec2319e1

View File

@ -1,6 +1,6 @@
package com.youlai.common.redis; package com.youlai.common.redis;
import com.youlai.common.constant.RedisConstants; import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
@ -15,22 +15,30 @@ import java.time.format.DateTimeFormatter;
@RequiredArgsConstructor @RequiredArgsConstructor
public class BusinessSnGenerator { public class BusinessSnGenerator {
private final RedisTemplate redisTemplate; private final RedisTemplate redisTemplate;
/** /**
* @param digit 业务序号位数 * @param digit 业务序号位数
* @return * @return
*/ */
public String generateSerialNo(Integer digit) { public String generateSerialNo(String businessType, Integer digit) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); if (StrUtil.isBlank(businessType)) {
String date = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter); businessType = "COMMON";
String key = RedisConstants.BUSINESS_NO_PREFIX +":" + date; }
String date = LocalDateTime.now(ZoneOffset.of("+8"))
.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String key = "SN:" + businessType + ":" + date;
Long increment = redisTemplate.opsForValue().increment(key); Long increment = redisTemplate.opsForValue().increment(key);
return date + String.format("%0" + digit + "d", increment); return date + String.format("%0" + digit + "d", increment);
} }
public String generateSerialNo(){ public String generateSerialNo(Integer digit) {
return this.generateSerialNo(6); return this.generateSerialNo(null,6);
}
public String generateSerialNo(String businessType) {
return this.generateSerialNo(businessType,6);
} }
} }