Loading

java 生成编号方法


 

    private static final String PACKAGE_CODE_KEY = "key:no:";

    private static final Integer defaultExpireTime = 1;

 

    @Override
    public String generateNo(String prefix) {
        LocalDateTime now = LocalDateTime.now();
        String time = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        BoundValueOperations<String, String> valueOps = this.stringRedisTemplate.boundValueOps(PACKAGE_CODE_KEY + time);
        String number = Optional.of(valueOps)
                .filter(ops -> null != ops.get())
                .map(ops -> ops.increment(1))
                .map(Number::toString)
                .orElseGet(() -> {
                    valueOps.set("1", defaultExpireTime, TimeUnit.DAYS);
                    return "1";
                });
        //不够五位的补0
        String pinEncode = String.format("%05d", Integer.valueOf(number));
        return prefix + time + pinEncode;
    }

 

posted @ 2021-03-30 16:10  路闻man  阅读(543)  评论(0编辑  收藏  举报