提交 6b040855 authored 作者: huyufan's avatar huyufan

修改bug 货单编号生成

上级 e802a68e
...@@ -33,53 +33,65 @@ public class OrderGoodsIdGenerate { ...@@ -33,53 +33,65 @@ public class OrderGoodsIdGenerate {
public long getOrderGoodsId(Integer size) { public long getOrderGoodsId(Integer size) {
RLock rLock = null; long baseStart = Long.parseLong(DateUtils.formatDate(LocalDate.now(), "yyyyMMdd").get() + "00001");
try { Object o = redisTemplate.opsForValue().get(RedisConstants.ORDER_GOODS_ID_GENERATE);
rLock = redissonClient.getLock(RedisConstants.ORDER_NO_BLOCK + type); if (o == null) {
RLock rLock = null;
rLock.lock(5, TimeUnit.SECONDS); try {
long baseStart = Long.parseLong(DateUtils.formatDate(LocalDate.now(), "yyyyMMdd").get() + "00001"); rLock = redissonClient.getLock(RedisConstants.ORDER_NO_BLOCK + type);
Object o = redisTemplate.opsForHash().get(RedisConstants.ORDER_GOODS_ID, type); rLock.lock(5, TimeUnit.SECONDS);
if (o == null) { o = redisTemplate.opsForValue().get(RedisConstants.ORDER_GOODS_ID_GENERATE);
Long maxOrderGoodsId = null; if (o == null) {
String maxID = orderGoodsDao.getMaxOrderGoodsId(type); Long maxOrderGoodsId = null;
if (StringUtils.isNotBlank(maxID)) { String maxID = orderGoodsDao.getMaxOrderGoodsId();
maxOrderGoodsId = Long.valueOf(maxID.substring(2)); if (StringUtils.isNotBlank(maxID)) {
} maxOrderGoodsId = Long.valueOf(maxID.substring(2));
}
if (maxOrderGoodsId == null) { if (maxOrderGoodsId == null) {
maxOrderGoodsId = 0L; maxOrderGoodsId = baseStart;
} }
if (baseStart > maxOrderGoodsId) { if (baseStart == maxOrderGoodsId) {
redisTemplate.opsForHash().put(RedisConstants.ORDER_GOODS_ID, type, String.valueOf(baseStart + size)); redisTemplate.opsForValue().increment(RedisConstants.ORDER_GOODS_ID_GENERATE, size);
return baseStart; return baseStart;
} else {
redisTemplate.opsForValue().increment(RedisConstants.ORDER_GOODS_ID_GENERATE, size);
return maxOrderGoodsId;
}
} else { } else {
long orderGoodsId = maxOrderGoodsId + size; long redisOrderGoodsId = Long.parseLong(o.toString());
redisTemplate.opsForHash().put(RedisConstants.ORDER_GOODS_ID, type, String.valueOf(orderGoodsId)); if (redisOrderGoodsId < baseStart) {
return maxOrderGoodsId + 1; redisOrderGoodsId = baseStart;
} }
} else {
long redisOrderGoodsId = Long.parseLong(o.toString());
if (redisOrderGoodsId < baseStart) {
redisOrderGoodsId = baseStart;
}
long orderGoodsId = redisOrderGoodsId + size;
redisTemplate.opsForHash().put(RedisConstants.ORDER_GOODS_ID, type, String.valueOf(orderGoodsId));
return redisOrderGoodsId;
}
} catch (Exception e) {
throw new ServiceSystemException(PerformanceResultEnum.ORDER_GOODS_ID_GENERATE_LOCK, e.getMessage());
} finally { redisTemplate.opsForValue().increment(RedisConstants.ORDER_GOODS_ID_GENERATE, size);
try { return redisOrderGoodsId;
if (rLock != null && rLock.isLocked()) {
rLock.unlock();
} }
log.info("锁释放完成货单ID生成");
} catch (Exception e) { } catch (Exception e) {
log.error("redis 货单ID生成 分布式锁释放异常!", e); throw new ServiceSystemException(PerformanceResultEnum.ORDER_GOODS_ID_GENERATE_LOCK, e.getMessage());
} finally {
try {
if (rLock != null && rLock.isLocked()) {
rLock.unlock();
}
log.info("锁释放完成货单ID生成");
} catch (Exception 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;
} }
} }
} }
...@@ -6,7 +6,7 @@ public class RedisConstants { ...@@ -6,7 +6,7 @@ public class RedisConstants {
public static final String ORDER_GOODS_ID ="clx-performance:orderGoodsId:"; public static final String ORDER_GOODS_ID ="clx-performance:orderGoodsId:";
public static final String ORDER_GOODS_ID_GENERATE ="clx-performance:orderGoodsId:generate";
public static final String ZJXL_TRUCK_TRACE_LIST = "clx-performance:zjxl_truck_trace_list:"; public static final String ZJXL_TRUCK_TRACE_LIST = "clx-performance:zjxl_truck_trace_list:";
......
...@@ -24,7 +24,7 @@ import java.util.List; ...@@ -24,7 +24,7 @@ import java.util.List;
public interface OrderGoodsDao extends BaseDao<OrderGoodsMapper, OrderGoods, Integer> { public interface OrderGoodsDao extends BaseDao<OrderGoodsMapper, OrderGoods, Integer> {
boolean updateWeight(@Param("orderId")Integer orderId, @Param("weight") BigDecimal weight); boolean updateWeight(@Param("orderId")Integer orderId, @Param("weight") BigDecimal weight);
String getMaxOrderGoodsId(String type); String getMaxOrderGoodsId();
Optional<OrderGoods> getByOrderGoodsNo(String orderGoodsNo); Optional<OrderGoods> getByOrderGoodsNo(String orderGoodsNo);
......
...@@ -35,8 +35,8 @@ public class OrderGoodsDaoImpl extends BaseDaoImpl<OrderGoodsMapper, OrderGoods, ...@@ -35,8 +35,8 @@ public class OrderGoodsDaoImpl extends BaseDaoImpl<OrderGoodsMapper, OrderGoods,
} }
@Override @Override
public String getMaxOrderGoodsId(String type) { public String getMaxOrderGoodsId() {
return baseMapper.getMaxOrderGoodsId(type); return baseMapper.getMaxOrderGoodsId();
} }
@Override @Override
......
...@@ -29,7 +29,7 @@ public interface OrderGoodsMapper extends BaseMapper<OrderGoods> { ...@@ -29,7 +29,7 @@ public interface OrderGoodsMapper extends BaseMapper<OrderGoods> {
int updateWeight(@Param("orderId") Integer orderId, @Param("weight") BigDecimal weight); int updateWeight(@Param("orderId") Integer orderId, @Param("weight") BigDecimal weight);
@SelectProvider(type = OrderGoodsSqlProvider.class, method = "getMaxOrderGoodsId") @SelectProvider(type = OrderGoodsSqlProvider.class, method = "getMaxOrderGoodsId")
String getMaxOrderGoodsId(String type); String getMaxOrderGoodsId();
@SelectProvider(type = OrderGoodsSqlProvider.class, method = "pageOrderGoodsList") @SelectProvider(type = OrderGoodsSqlProvider.class, method = "pageOrderGoodsList")
IPage<OrderGoodsVO> pageOrderGoodsList(Page<OrderGoodsVO> page, PageOrderGoodsListParam param); IPage<OrderGoodsVO> pageOrderGoodsList(Page<OrderGoodsVO> page, PageOrderGoodsListParam param);
......
...@@ -16,7 +16,6 @@ public class OrderGoodsSqlProvider { ...@@ -16,7 +16,6 @@ public class OrderGoodsSqlProvider {
public String getMaxOrderGoodsId(String type) { public String getMaxOrderGoodsId(String type) {
return "SELECT s.order_goods_no FROM `order_goods` s WHERE s. id = ( SELECT max(id) FROM order_goods )"; return "SELECT s.order_goods_no FROM `order_goods` s WHERE s. id = ( SELECT max(id) FROM order_goods )";
// return "SELECT s.order_goods_no FROM `order_goods` s WHERE s. id = ( SELECT max(id) FROM order_goods where order_goods_type = '" + type + "')";
} }
public String pageOrderGoodsList(@Param("param") PageOrderGoodsListParam param) { public String pageOrderGoodsList(@Param("param") PageOrderGoodsListParam param) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论