提交 47078629 authored 作者: liuhaiquan's avatar liuhaiquan

Merge remote-tracking branch 'origin/v4.9_create_goods_child_20230918' into…

Merge remote-tracking branch 'origin/v4.9_create_goods_child_20230918' into v4.9_create_goods_child_20230918
......@@ -65,6 +65,7 @@ public enum PerformanceResultEnum implements ResultEnum {
ORDER_CHILD_DIRECT_REJECT_TRUCK_ERROR(1381, "非定向车辆无法取消"),
ORDER_CHILD_CANCEL_FORBID(1382, "运单无法取消"),
ORDER_CHILD_CANCEL_FORBID_COUNT(1383, "今日取消运单次数超过上限,暂时无法取消"),
ORDER_GOODS_ID_GENERATE_LOCK(1384, "货单编号ID获取锁出问题"),
......
......@@ -2,15 +2,22 @@ package com.clx.performance.component;
import com.clx.performance.constant.RedisConstants;
import com.clx.performance.dao.OrderGoodsDao;
import com.clx.performance.enums.PerformanceResultEnum;
import com.msl.common.exception.ServiceSystemException;
import com.msl.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.util.concurrent.TimeUnit;
@Component
@Slf4j
public class OrderGoodsIdGenerate {
@Autowired
......@@ -19,38 +26,72 @@ public class OrderGoodsIdGenerate {
@Autowired
private OrderGoodsDao orderGoodsDao;
@Autowired
private RedissonClient redissonClient;
private static final String type = "id";
public long getOrderGoodsId(Integer size) {
long baseStart = Long.parseLong(DateUtils.formatDate(LocalDate.now(), "yyyyMMdd").get() + "00001");
Object o = redisTemplate.opsForHash().get(RedisConstants.ORDER_GOODS_ID, type);
if (o == null ) {
Long maxOrderGoodsId = null;
String maxID = orderGoodsDao.getMaxOrderGoodsId(type);
if (StringUtils.isNotBlank(maxID)) {
maxOrderGoodsId = Long.valueOf(maxID.substring(2));
}
Object o = redisTemplate.opsForValue().get(RedisConstants.ORDER_GOODS_ID_GENERATE);
if (o == null) {
RLock rLock = null;
try {
rLock = redissonClient.getLock(RedisConstants.ORDER_NO_BLOCK + type);
rLock.lock(5, TimeUnit.SECONDS);
o = redisTemplate.opsForValue().get(RedisConstants.ORDER_GOODS_ID_GENERATE);
if (o == null) {
Long maxOrderGoodsId = null;
String maxID = orderGoodsDao.getMaxOrderGoodsId();
if (StringUtils.isNotBlank(maxID)) {
maxOrderGoodsId = Long.valueOf(maxID.substring(2));
}
if (maxOrderGoodsId == null) {
maxOrderGoodsId = 0L;
}
if (baseStart > maxOrderGoodsId) {
redisTemplate.opsForHash().put(RedisConstants.ORDER_GOODS_ID, type, String.valueOf(baseStart + size));
return baseStart;
} else {
long orderGoodsId = maxOrderGoodsId + size;
redisTemplate.opsForHash().put(RedisConstants.ORDER_GOODS_ID, type, String.valueOf(orderGoodsId));
return maxOrderGoodsId + 1;
if (maxOrderGoodsId == null) {
maxOrderGoodsId = baseStart;
}
if (baseStart == maxOrderGoodsId) {
redisTemplate.opsForValue().increment(RedisConstants.ORDER_GOODS_ID_GENERATE, size);
return baseStart;
} else {
redisTemplate.opsForValue().increment(RedisConstants.ORDER_GOODS_ID_GENERATE, size);
return maxOrderGoodsId;
}
} else {
long redisOrderGoodsId = Long.parseLong(o.toString());
if (redisOrderGoodsId < baseStart) {
redisOrderGoodsId = baseStart;
}
redisTemplate.opsForValue().increment(RedisConstants.ORDER_GOODS_ID_GENERATE, size);
return redisOrderGoodsId;
}
} catch (Exception 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;
}
long orderGoodsId = redisOrderGoodsId + size;
redisTemplate.opsForHash().put(RedisConstants.ORDER_GOODS_ID, type, String.valueOf(orderGoodsId));
redisTemplate.opsForValue().increment(RedisConstants.ORDER_GOODS_ID_GENERATE, size);
return redisOrderGoodsId;
}
}
}
......@@ -6,7 +6,7 @@ public class RedisConstants {
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:";
......
......@@ -24,7 +24,7 @@ import java.util.List;
public interface OrderGoodsDao extends BaseDao<OrderGoodsMapper, OrderGoods, Integer> {
boolean updateWeight(@Param("orderId")Integer orderId, @Param("weight") BigDecimal weight);
String getMaxOrderGoodsId(String type);
String getMaxOrderGoodsId();
Optional<OrderGoods> getByOrderGoodsNo(String orderGoodsNo);
......
......@@ -35,8 +35,8 @@ public class OrderGoodsDaoImpl extends BaseDaoImpl<OrderGoodsMapper, OrderGoods,
}
@Override
public String getMaxOrderGoodsId(String type) {
return baseMapper.getMaxOrderGoodsId(type);
public String getMaxOrderGoodsId() {
return baseMapper.getMaxOrderGoodsId();
}
@Override
......
......@@ -29,7 +29,7 @@ public interface OrderGoodsMapper extends BaseMapper<OrderGoods> {
int updateWeight(@Param("orderId") Integer orderId, @Param("weight") BigDecimal weight);
@SelectProvider(type = OrderGoodsSqlProvider.class, method = "getMaxOrderGoodsId")
String getMaxOrderGoodsId(String type);
String getMaxOrderGoodsId();
@SelectProvider(type = OrderGoodsSqlProvider.class, method = "pageOrderGoodsList")
IPage<OrderGoodsVO> pageOrderGoodsList(Page<OrderGoodsVO> page, PageOrderGoodsListParam param);
......
......@@ -16,7 +16,6 @@ public class OrderGoodsSqlProvider {
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 where order_goods_type = '" + type + "')";
}
public String pageOrderGoodsList(@Param("param") PageOrderGoodsListParam param) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论