基础(二)之创建每天重置的流水号
第一种方式:
首先,根据时间生成,我们先要格式化一下时间格式
SimpleDateFormat dmfot = new SimpleDateFormat("yyyyMMdd");
//截取当前时间作为流水号
String preCode = dmfot.format(new Date());
然后我们进行数据库查询,今天最大的流水号是多少?
//获取最大编号
String maxCode = getMaxCode(date);
查询语句如下:
select Max(code)
from table_name
where
declare_time >= STR_TO_DATE(#{date},'%Y-%m-%d')
这里我们重置一下时间格式,获取当前时间零点
/**
* 返回当前时间零点
* @return
*/
public static String getCurrentDay() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
Date date = c.getTime();
String currentDay = format.format(date);
return currentDay;
}
查询之后进行判断,如果查出的数据为空,证明今天没有生成流水号,我们直接set
//设置流水号格式,根据实际情况设置
DecimalFormat num=new DecimalFormat("000");
if(maxCode != null && !"null".equals(maxCode)){
//对流水号截取后三位
String code = maxCode.substring(maxCode.length()-3,maxCode.length());
//例如后三位为002,通过以下步骤,将会变为003
int count = Integer.valueOf(code)+1;
String number = num.format(count);
return preCode + "-" + number;
}else {
return preCode + "-" + "001";
}
"001";例如 20191017-001
第二种:
1、使用redis原子自增特性
2、先判断key,是否存在
2.1、存在:顺序码自增
2.2、不存子:重新生成顺序码
代码实现
控制器
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @Author:chenyanbin
*/
@RestController
@RequestMapping("/api/task/v1")
@Api(tags = "任务API 测试")
public class TaskController {
@Autowired
RedisService redisService;
/**
* 自增序号有效期,一天半
*/
public static final Long EXPIRE = 60 * 60 * (24 + 12) * 1L;
/**
* 生成任务规则如下
* 当前年份(省略年份前三位数)+月份+日期+三位顺序码,比如2021年7月15日第3笔。 此编号对应为:10715003
*
* @return
*/
@ApiOperation("生成任务编号")
@GetMapping("generator")
public String generateTaskId() {
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yMMdd");
String key = dtf.format(localDateTime);
long incr;
if (redisService.exists(key)) {
incr = redisService.incr(key, EXPIRE);
} else {
incr = redisService.incr(key, EXPIRE);
}
return key.substring(key.length() - 5) + CommonUtil.automaticFilling((int) incr, 3);
}
}
redis工具类
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
/**
* redis工具类
* @Author:chenyanbin
*/
@Service
@Slf4j
public class RedisService {
@Autowired
private RedisTemplate redisTemplate;
private static double size = Math.pow(2, 32);
/**
* 判断缓存中是否有对应的value
*
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 删除对应的value
*
* @param key
*/
public void remove(final String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
/**
* 写入缓存
*
* @param key 缓存key
* @param value 缓存value
* @param expireTime 过期时间,秒
* @return
*/
public boolean set(final String key, Object value, Long expireTime) {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
return true;
}
/**
* 原子递增
*
* @param key 键
* @param expireTime 过期时间,秒
* @return
*/
public Long incr(final String key, Long expireTime) {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
Long increment = operations.increment(key);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
return increment;
}
/**
* 读取缓存
*
* @param key
* @return
*/
public Object get(final String key) {
Object result = null;
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
}
自动补零工具类
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.util.Random;
import java.util.UUID;
/**
* 公共工具类
*
* @Author:chenyanbin
*/
@Slf4j
public class CommonUtil {
/**
* 自动补位
* @param code 数值
* @param num 保留的位数
* @return
*/
public static String automaticFilling(int code, int num) {
return String.format("%0" + num + "d", code);
}
}

浙公网安备 33010602011771号