|
|
@@ -5,16 +5,11 @@ import cn.hutool.core.util.StrUtil;
|
|
|
import cn.tr.plugin.numbering.strategy.core.enums.NumberingStrategyEnum;
|
|
|
import cn.tr.plugin.numbering.strategy.core.properties.NumberingStrategyProperties;
|
|
|
import lombok.Data;
|
|
|
-import org.redisson.api.RMap;
|
|
|
-import org.redisson.api.RedissonClient;
|
|
|
-import org.redisson.client.codec.IntegerCodec;
|
|
|
import org.springframework.cache.Cache;
|
|
|
import org.springframework.cache.CacheManager;
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
-import java.util.Date;
|
|
|
-import java.util.Objects;
|
|
|
-import java.util.Optional;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @ClassName : DefaultSerialStrategyClient
|
|
|
@@ -30,31 +25,34 @@ public class DefaultNumberingStrategyClient implements NumberingStrategyClient {
|
|
|
private static final String CURRENT_NUM="currentNum";
|
|
|
private NumberingStrategyProperties properties;
|
|
|
private NumberingStrategyEnum strategyEnum;
|
|
|
- private Cache cache;
|
|
|
+ private CacheManager cacheManager;
|
|
|
+ private Map<String,Cache> cacheMap=new HashMap<>();
|
|
|
|
|
|
DefaultNumberingStrategyClient(NumberingStrategyProperties properties,CacheManager cacheManager) {
|
|
|
- this.cache = cacheManager.getCache("serialStrategy" + properties.getClientId());
|
|
|
- doInit(properties);
|
|
|
+ this.cacheManager =cacheManager;
|
|
|
+ Assert.notNull(NumberingStrategyEnum.ofValue(properties.getType()),String.format("不支持此生成模式:%s",properties.getType()));
|
|
|
+ this.properties = properties;
|
|
|
+ this.strategyEnum= NumberingStrategyEnum.ofValue(properties.getType());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String getAndIncrementSerialNum() {
|
|
|
- Integer current = Optional.ofNullable(cache.get(CURRENT_NUM, Integer.class)).orElse(0);
|
|
|
+ Integer current = Optional.ofNullable(getCache().get(CURRENT_NUM, Integer.class)).orElse(0);
|
|
|
Integer nextNum=current+1;
|
|
|
- cache.put(CURRENT_NUM,nextNum);
|
|
|
+ getCache().put(CURRENT_NUM,nextNum);
|
|
|
return formatSerialNo(nextNum);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String getNextFormatNum() {
|
|
|
- Integer current = Optional.ofNullable(cache.get(CURRENT_NUM, Integer.class)).orElse(0);
|
|
|
+ Integer current = Optional.ofNullable(getCache().get(CURRENT_NUM, Integer.class)).orElse(0);
|
|
|
Integer nextNum=current+1;
|
|
|
return formatSerialNo(nextNum);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Integer getNextNum() {
|
|
|
- Integer current = Optional.ofNullable(cache.get(CURRENT_NUM, Integer.class)).orElse(0);
|
|
|
+ Integer current = Optional.ofNullable(getCache().get(CURRENT_NUM, Integer.class)).orElse(0);
|
|
|
return current+1;
|
|
|
}
|
|
|
|
|
|
@@ -78,7 +76,7 @@ public class DefaultNumberingStrategyClient implements NumberingStrategyClient {
|
|
|
result=DateUtil.format(new Date(),pattern)+"-"+fillNum;
|
|
|
}
|
|
|
if(StrUtil.isNotEmpty(prefix)){
|
|
|
- result=prefix+"-"+ DateUtil.format(new Date(),pattern)+"-"+fillNum;
|
|
|
+ result=prefix+"-"+ result;
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
@@ -98,19 +96,26 @@ public class DefaultNumberingStrategyClient implements NumberingStrategyClient {
|
|
|
}
|
|
|
|
|
|
private void doInit(NumberingStrategyProperties properties){
|
|
|
- Assert.notNull(NumberingStrategyEnum.ofValue(properties.getType()),String.format("不支持此生成模式:%s",properties.getType()));
|
|
|
- this.properties = properties;
|
|
|
- this.strategyEnum= NumberingStrategyEnum.ofValue(properties.getType());
|
|
|
Integer nextNum = properties.getNextNum();
|
|
|
if (Objects.isNull(nextNum)) {
|
|
|
//更新值为null,缓存值不为null,则不进行更新操作
|
|
|
- nextNum = cache.get(CURRENT_NUM, Integer.class);
|
|
|
+ nextNum = getCache().get(CURRENT_NUM, Integer.class);
|
|
|
if(nextNum!=null){
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
//当nextNum为空或者小于0时,将currentNum设置为0
|
|
|
Integer currentNum=( Objects.isNull(nextNum)||nextNum<=0)?0:nextNum-1;
|
|
|
- cache.put(CURRENT_NUM,currentNum);
|
|
|
+ getCache().put(CURRENT_NUM,currentNum);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Cache getCache(){
|
|
|
+ Cache cache = cacheManager.getCache("serialStrategy" + properties.getClientId());
|
|
|
+ String name = cache.getName();
|
|
|
+ if(!cacheMap.containsKey(name)){
|
|
|
+ cacheMap.put(name,cache);
|
|
|
+ doInit(properties);
|
|
|
+ }
|
|
|
+ return cache;
|
|
|
}
|
|
|
}
|