| | |
| | | public class IdUtils { |
| | | |
| | | private static final String LIS_LAST_ID_KEY = "id:generate:lis:id"; |
| | | private static final String TJH_LAST_ID_KEY = "id:generate:tjhs:tjh"; |
| | | 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 long lastTimestamp = -1; |
| | | private static final Random random = new Random(); |
| | | |
| | | public static synchronized String getTjNumber() { |
| | | long timestamp = System.currentTimeMillis(); // 获取当前时间戳(毫秒) |
| | | // 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); |
| | | // } |
| | | // } |
| | | |
| | | // 如果时间戳和上次生成的时间戳相同,生成一个新的随机数 |
| | | if (timestamp == lastTimestamp) { |
| | | return String.format("%09d", (timestamp % 1000000000) + random.nextInt(900) + 100); |
| | | |
| | | public synchronized String getTjNumber() { |
| | | String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date()); |
| | | String storedDate = stringRedisTemplate.opsForValue().get(LIS_CURRENT_DATE_KEY); |
| | | String lastIdStr = stringRedisTemplate.opsForValue().get(TJH_LAST_ID_KEY); |
| | | if (StrUtil.isBlank(lastIdStr))lastIdStr = "0"; |
| | | int lastId; |
| | | if (storedDate == null || !storedDate.equals(currentDate)) { |
| | | lastId = 1; |
| | | stringRedisTemplate.opsForValue().set(TJH_LAST_ID_KEY, String.valueOf(lastId)); |
| | | stringRedisTemplate.opsForValue().set(LIS_CURRENT_DATE_KEY, currentDate); |
| | | } else { |
| | | lastTimestamp = timestamp; // 更新最后生成时间戳 |
| | | return String.format("%09d", (timestamp % 1000000000) + random.nextInt(900) + 100); |
| | | lastId = Integer.parseInt(lastIdStr) + 1; |
| | | stringRedisTemplate.opsForValue().set(TJH_LAST_ID_KEY, String.valueOf(lastId)); |
| | | } |
| | | String yyMMdd = currentDate.substring(2); |
| | | return String.format("%s%05d", yyMMdd, lastId); |
| | | } |
| | | |
| | | |