zjh
昨天 2ba86da39808ff8388531cc1f608d69822bee41c
zjh20250731
1个文件已修改
54 ■■■■ 已修改文件
ltkj-hosp/src/main/java/com/ltkj/hosp/idutil/IdUtils.java 54 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ltkj-hosp/src/main/java/com/ltkj/hosp/idutil/IdUtils.java
@@ -1,6 +1,7 @@
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;
@@ -24,10 +25,9 @@
@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;
@@ -36,27 +36,14 @@
    @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)) {
@@ -93,26 +80,6 @@
        }
    }
    /**
     * 生成无限递增条码号
     * @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;
    }
    //生成体检号用
@@ -198,6 +165,11 @@
        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());