Redis自增编码生成
自增编码方式记录
全局流水号
public String getOrderId(String prefix) {
//生成订单号 redis incr
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String time = sdf.format(new Date());
//当天流水7位
String key = prefix +time;
Long tx = redisTemplate.opsForValue().increment(key, 1);
if (tx < 2) {
//初始化时设置过期时间
redisTemplate.boundValueOps(key).expire(24, TimeUnit.HOURS);
//没值,自动添加
//数据库查询最新数据,如果数据库数据为空,tx不做更改,直接拼接Id
String latestNo = tbContractMapper.getLatestNo(key + "%");
if (!StringUtils.isEmpty(latestNo)){
String txString = latestNo.substring(latestNo.length() - 4);
Integer txValue = Integer.valueOf(txString);
redisTemplate.opsForValue().set(prefix+time, txValue.toString());
tx = redisTemplate.opsForValue().increment(key, 1);
}
}
if (tx > 9999){
ApplicationExceptionUtil.throwParamsException("合同编号生成错误");
}
//自动补0
String end = autoAddZero(String.valueOf(tx));
return prefix + time + end;
}
public String autoAddZero(String liuShuiHao) {
Integer intHao = Integer.parseInt(liuShuiHao);
DecimalFormat df = new DecimalFormat("0000");
return df.format(intHao);
}
根据时间戳,全局编码生成工具和枚举类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;
/**
* CodeGenerateUtil
*/
@Component
public class CodeGenerateUtil {
/**
*
* base redis key
*/
private static final String REDIS_ORDER_NUMBER_KEY_FORMAT = "code-generate:%s";
/**
* 时间戳 线程安全
*/
private static final DateTimeFormatter FORMAT_TO_DAY = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
/**
* redis 缓存
*/
private static StringRedisTemplate stringRedisTemplate;
/**
* 自增数长度 默认4位
*/
private static final String INCREMENT_FORMAT = "%04d";
/**
* 初始化Redis缓存
*/
@Autowired
public void setRedisTemplate(StringRedisTemplate stringRedisTemplate) {
CodeGenerateUtil.stringRedisTemplate = stringRedisTemplate;
}
public static String nextId(CodeEnum orderNoEnum) {
String time = LocalDateTime.now().format(FORMAT_TO_DAY);
// 获取自增数
Long incr = incrementAndGet(orderNoEnum);
String incrString = String.format(INCREMENT_FORMAT, incr);
return orderNoEnum.getCode().concat(time).concat(incrString);
}
private static Long incrementAndGet(CodeEnum orderNoEnum) {
// 加1
Long counter = CodeGenerateUtil.stringRedisTemplate.opsForValue()
.increment(getRedisOrderNumberKey(orderNoEnum), 1L);
// 当计数器等于1的时候,设置过期时间为离第二天0点的时间差
if (Long.valueOf(1L).equals(counter)) {
// 获取当前时间和24点的秒数
long timeout = betweenNowToLastSecond();
CodeGenerateUtil.stringRedisTemplate
.expire(getRedisOrderNumberKey(orderNoEnum), timeout, TimeUnit.SECONDS);
}
return counter;
}
private static String getRedisOrderNumberKey(CodeEnum orderNoEnum) {
return String.format(REDIS_ORDER_NUMBER_KEY_FORMAT, orderNoEnum.getIncrementKey());
}
/**
* 计算当前时间到当天24点的差值
*
* @return 差值
*/
private static long betweenNowToLastSecond() {
LocalDateTime now = LocalDateTime.now();
// 加一天
LocalDateTime tomorrow = now.plusDays(1);
LocalDateTime lastSecond = LocalDateTime.of(tomorrow.getYear(),
tomorrow.getMonth(),
tomorrow.getDayOfMonth(),
0,
0,
0,
1);
return ChronoUnit.SECONDS.between(now, lastSecond);
}
}
/**
* CodeEnum
*
*/
public enum CodeEnum {
/**
* 字典编码
*/
DICTIONARY_CODE("ZD", "dictionary_code_increment"),
;
private final String code;
private final String incrementKey;
CodeEnum(String code, String incrementKey) {
this.code = code;
this.incrementKey = incrementKey;
}
public String getCode() {
return code;
}
public String getIncrementKey() {
return incrementKey;
}
}