基础(二)之创建每天重置的流水号

第一种方式:

 首先,根据时间生成,我们先要格式化一下时间格式

1
2
3
SimpleDateFormat dmfot = new SimpleDateFormat("yyyyMMdd");
//截取当前时间作为流水号
String preCode = dmfot.format(new Date());

 然后我们进行数据库查询,今天最大的流水号是多少? 

1
2
3
4
5
6
7
//获取最大编号
String maxCode = getMaxCode(date);
查询语句如下:
select Max(code)
from table_name
where
declare_time >= STR_TO_DATE(#{date},'%Y-%m-%d')

 这里我们重置一下时间格式,获取当前时间零点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 返回当前时间零点
 * @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

1
2
3
4
5
6
7
8
9
10
11
12
//设置流水号格式,根据实际情况设置
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、不存子:重新生成顺序码

代码实现

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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;
    }
 
} 

自动补零工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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);
    }
}

  

 

posted @   永远爱芒果  阅读(441)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示