| | |
| | | package com.ltkj.common.utils; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.StringRedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.Random; |
| | | |
| | | /** |
| | | * @Company: 西安路泰科技有限公司 |
| | |
| | | public class IdUtils { |
| | | |
| | | private static final String LIS_LAST_ID_KEY = "id:generate:lis:id"; |
| | | private static final String LIS_LAST_ID_INCR_KEY = "id:generate:lis:id:incr"; |
| | | private static final String LIS_CURRENT_DATE_KEY = "id:generate:lis:currentDate"; |
| | | |
| | | // private static final UidGenerator uidGenerator = new DefaultUidGenerator(); // 使用默认的Snowflake生成器 |
| | | |
| | | @Autowired |
| | | private StringRedisTemplate stringRedisTemplate; |
| | |
| | | return String.format(prefix+"%s%05d", yyMMdd, lastId); |
| | | } |
| | | |
| | | /** |
| | | * 生成无限递增条码号 |
| | | * @param prefix |
| | | * @return |
| | | */ |
| | | public synchronized String generateLisNextId(String prefix){ |
| | | String lastIdStr = stringRedisTemplate.opsForValue().get(LIS_LAST_ID_INCR_KEY); |
| | | int current; |
| | | if (StrUtil.isBlank(lastIdStr)) { |
| | | current = 1; |
| | | }else { |
| | | current = Integer.parseInt(lastIdStr); |
| | | } |
| | | int numberLength = String.valueOf(99999).length(); |
| | | String result = prefix + String.format("%0" + numberLength + "d", current); |
| | | current++; |
| | | stringRedisTemplate.opsForValue().set(LIS_LAST_ID_INCR_KEY,String.valueOf(current)); |
| | | return result; |
| | | } |
| | | |
| | | |
| | | //生成体检号用 |
| | | private static long lastTimestamp = -1; |
| | | private static final Random random = new Random(); |
| | | |
| | | public static synchronized String getTjNumber() { |
| | | long timestamp = System.currentTimeMillis(); // 获取当前时间戳(毫秒) |
| | | |
| | | // 如果时间戳和上次生成的时间戳相同,生成一个新的随机数 |
| | | if (timestamp == lastTimestamp) { |
| | | return String.format("%09d", (timestamp % 1000000000) + random.nextInt(900) + 100); |
| | | } else { |
| | | lastTimestamp = timestamp; // 更新最后生成时间戳 |
| | | return String.format("%09d", (timestamp % 1000000000) + random.nextInt(900) + 100); |
| | | } |
| | | } |
| | | |
| | | |
| | | // 使用分布式ID生成器(如Snowflake) |
| | | public static String generateExamNumber() { |
| | | // long id = uidGenerator.getUID(); // 获取生成的唯一ID |
| | | // return String.format("%09d", Math.abs(id) % 1000000000); // 格式化为9位 |
| | | return null; |
| | | } |
| | | |
| | | } |