| | |
| | | package com.ltkj.hosp.idutil; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.ltkj.db.DataSourceContextHolder; |
| | | import com.ltkj.hosp.mapper.OrderNumberMapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.redisson.api.RLock; |
| | |
| | | @Slf4j |
| | | 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 String LIS_LAST_ID_KEY = "id:generate:lis:id"; |
| | | private static String TJH_LAST_ID_KEY = "id:generate:tjhs:tjh"; |
| | | private static String LIS_CURRENT_DATE_KEY = "id:generate:lis:currentDate"; |
| | | |
| | | @Autowired |
| | | private StringRedisTemplate stringRedisTemplate; |
| | |
| | | @Autowired |
| | | private RedissonClient redissonClient; |
| | | |
| | | public synchronized String yuangenerateLisID(String prefix) { |
| | | String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date()); |
| | | String storedDate = stringRedisTemplate.opsForValue().get(LIS_CURRENT_DATE_KEY); |
| | | String lastIdStr = stringRedisTemplate.opsForValue().get(LIS_LAST_ID_KEY); |
| | | int lastId; |
| | | if (storedDate == null || !storedDate.equals(currentDate)) { |
| | | lastId = 1; |
| | | stringRedisTemplate.opsForValue().set(LIS_LAST_ID_KEY, String.valueOf(lastId)); |
| | | stringRedisTemplate.opsForValue().set(LIS_CURRENT_DATE_KEY, currentDate); |
| | | } else { |
| | | lastId = Integer.parseInt(lastIdStr) + 1; |
| | | stringRedisTemplate.opsForValue().set(LIS_LAST_ID_KEY, String.valueOf(lastId)); |
| | | } |
| | | String yyMMdd = currentDate.substring(2); |
| | | return String.format(prefix+"%s%05d", yyMMdd, lastId); |
| | | } |
| | | |
| | | //redis分布式锁和MySQL公用 |
| | | public String generateLisID(String prefix) { |
| | | String lockKey = "lock:tmh:tj_tmh_lock"; |
| | | RLock lock = redissonClient.getLock(lockKey); |
| | | String string = DataSourceContextHolder.getDataSourceKey(); |
| | | |
| | | LIS_LAST_ID_KEY = "id:generate:lis:id:"+string; |
| | | LIS_CURRENT_DATE_KEY = "id:generate:lis:currentDate:"+string; |
| | | |
| | | try { |
| | | if (lock.tryLock(3, 5, TimeUnit.SECONDS)) { |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 生成无限递增条码号 |
| | | * @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; |
| | | } |
| | | |
| | | |
| | | //生成体检号用 |
| | |
| | | String lockKey = "lock:tjh:tj_number_lock"; |
| | | RLock lock = redissonClient.getLock(lockKey); |
| | | |
| | | String string = DataSourceContextHolder.getDataSourceKey(); |
| | | |
| | | LIS_CURRENT_DATE_KEY = "id:generate:lis:currentDate:"+string; |
| | | TJH_LAST_ID_KEY = "id:generate:tjhs:tjh:"+string; |
| | | |
| | | try { |
| | | if (lock.tryLock(5, 10, TimeUnit.SECONDS)) { |
| | | String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date()); |