|
|
|
@ -1,16 +1,26 @@
|
|
|
|
|
package com.supervision.util;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
|
import org.springframework.data.redis.core.ValueOperations;
|
|
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class RedisSequenceUtil {
|
|
|
|
|
|
|
|
|
|
private volatile static RedisTemplate<String, String> redisTemplate = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取流程编号
|
|
|
|
|
* @return 流程编号规则 a000001-a999999 b000001-b999999 ...
|
|
|
|
|
*/
|
|
|
|
|
public static String getProcessNo(){
|
|
|
|
|
// 从零开始计算
|
|
|
|
|
long processNoSeq = getIncrement("process_no_seq") -1;
|
|
|
|
@ -21,28 +31,54 @@ public class RedisSequenceUtil {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取复合疾病编号。生成规则:当天日期(YYYMMDD)+自增序列号(3位)20240104001
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String getComplexDiseaseNo(){
|
|
|
|
|
String nowDay = DateUtil.format(new Date(), "YYYYMMdd");
|
|
|
|
|
return nowDay + String.format("%03d", getIncrement("complex_disease_no_seq_"+nowDay));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取自增长值,增长步伐为1
|
|
|
|
|
* @param key redis key
|
|
|
|
|
* @return 自增序列号
|
|
|
|
|
*/
|
|
|
|
|
public static Long getIncrement(String key){
|
|
|
|
|
ValueOperations<String, String> operations = getRedisTemplate().opsForValue();
|
|
|
|
|
return operations.increment( key,1L);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取redisTemplate
|
|
|
|
|
* @return redisTemplate
|
|
|
|
|
*/
|
|
|
|
|
public static RedisTemplate<String, String> getRedisTemplate() {
|
|
|
|
|
if (redisTemplate == null) {
|
|
|
|
|
synchronized (RedisSequenceUtil.class){
|
|
|
|
|
if (null == redisTemplate){
|
|
|
|
|
redisTemplate = createRedisTemplate();
|
|
|
|
|
redisTemplate = getRedisTemplateInContext();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return redisTemplate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static RedisTemplate<String, String> createRedisTemplate() {
|
|
|
|
|
private static RedisTemplate<String, String> getRedisTemplateInContext() {
|
|
|
|
|
ApplicationContext applicationContext = SpringBeanUtil.getApplicationContext();
|
|
|
|
|
if (null == applicationContext){
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return applicationContext.getBean(RedisTemplate.class);
|
|
|
|
|
// 优先选择stringRedisTemplate
|
|
|
|
|
if (applicationContext.containsBean("stringRedisTemplate")){
|
|
|
|
|
return applicationContext.getBean("stringRedisTemplate",StringRedisTemplate.class);
|
|
|
|
|
}
|
|
|
|
|
Map<String, RedisTemplate> redisTemplateMap = applicationContext.getBeansOfType(RedisTemplate.class);
|
|
|
|
|
if (CollUtil.isNotEmpty(redisTemplateMap)){
|
|
|
|
|
return CollUtil.getFirst(redisTemplateMap.values());
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|