提交 35610edb authored 作者: huyufan's avatar huyufan

ID生成

上级 1075bda9
...@@ -33,14 +33,13 @@ public class OrderGoodsIdGenerate { ...@@ -33,14 +33,13 @@ public class OrderGoodsIdGenerate {
public long getOrderGoodsId(Integer size) { public long getOrderGoodsId(Integer size) {
long baseStart = Long.parseLong(DateUtils.formatDate(LocalDate.now(), "yyyyMMdd").get() + "00001");
Object o = redisTemplate.opsForValue().get(RedisConstants.ORDER_GOODS_ID_GENERATE);
if (o == null) {
RLock rLock = null; RLock rLock = null;
try { try {
rLock = redissonClient.getLock(RedisConstants.ORDER_NO_BLOCK + type); rLock = redissonClient.getLock(RedisConstants.ORDER_NO_BLOCK + type);
rLock.lock(5, TimeUnit.SECONDS); rLock.lock(5, TimeUnit.SECONDS);
o = redisTemplate.opsForValue().get(RedisConstants.ORDER_GOODS_ID_GENERATE); long baseStart = Long.parseLong(DateUtils.formatDate(LocalDate.now(), "yyyyMMdd").get() + "00001");
Object o = redisTemplate.opsForHash().get(RedisConstants.ORDER_GOODS_ID, type);
if (o == null) { if (o == null) {
Long maxOrderGoodsId = null; Long maxOrderGoodsId = null;
String maxID = orderGoodsDao.getMaxOrderGoodsId(); String maxID = orderGoodsDao.getMaxOrderGoodsId();
...@@ -49,25 +48,25 @@ public class OrderGoodsIdGenerate { ...@@ -49,25 +48,25 @@ public class OrderGoodsIdGenerate {
} }
if (maxOrderGoodsId == null) { if (maxOrderGoodsId == null) {
maxOrderGoodsId = baseStart; maxOrderGoodsId = 0L;
} }
if (baseStart == maxOrderGoodsId) { if (baseStart > maxOrderGoodsId) {
redisTemplate.opsForValue().increment(RedisConstants.ORDER_GOODS_ID_GENERATE, size); redisTemplate.opsForHash().put(RedisConstants.ORDER_GOODS_ID, type, String.valueOf(baseStart + size));
return baseStart; return baseStart;
} else { } else {
redisTemplate.opsForValue().increment(RedisConstants.ORDER_GOODS_ID_GENERATE, size); long orderGoodsId = maxOrderGoodsId + size;
return maxOrderGoodsId; redisTemplate.opsForHash().put(RedisConstants.ORDER_GOODS_ID, type, String.valueOf(orderGoodsId));
return maxOrderGoodsId + 1;
} }
} else { } else {
long redisOrderGoodsId = Long.parseLong(o.toString()); long redisOrderGoodsId = Long.parseLong(o.toString());
if (redisOrderGoodsId < baseStart) { if (redisOrderGoodsId < baseStart) {
redisOrderGoodsId = baseStart; redisOrderGoodsId = baseStart;
} }
long orderGoodsId = redisOrderGoodsId + size;
redisTemplate.opsForValue().increment(RedisConstants.ORDER_GOODS_ID_GENERATE, size); redisTemplate.opsForHash().put(RedisConstants.ORDER_GOODS_ID, type, String.valueOf(orderGoodsId));
return redisOrderGoodsId; return redisOrderGoodsId;
} }
} catch (Exception e) { } catch (Exception e) {
throw new ServiceSystemException(PerformanceResultEnum.ORDER_GOODS_ID_GENERATE_LOCK, e.getMessage()); throw new ServiceSystemException(PerformanceResultEnum.ORDER_GOODS_ID_GENERATE_LOCK, e.getMessage());
...@@ -81,16 +80,6 @@ public class OrderGoodsIdGenerate { ...@@ -81,16 +80,6 @@ public class OrderGoodsIdGenerate {
log.error("redis 货单ID生成 分布式锁释放异常!", e); log.error("redis 货单ID生成 分布式锁释放异常!", e);
} }
} }
} else {
long redisOrderGoodsId = Long.parseLong(o.toString());
if (redisOrderGoodsId < baseStart) {
redisOrderGoodsId = baseStart;
}
redisTemplate.opsForValue().increment(RedisConstants.ORDER_GOODS_ID_GENERATE, size);
return redisOrderGoodsId;
}
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论