提交 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 { ...@@ -65,6 +65,7 @@ public enum PerformanceResultEnum implements ResultEnum {
ORDER_CHILD_DIRECT_REJECT_TRUCK_ERROR(1381, "非定向车辆无法取消"), ORDER_CHILD_DIRECT_REJECT_TRUCK_ERROR(1381, "非定向车辆无法取消"),
ORDER_CHILD_CANCEL_FORBID(1382, "运单无法取消"), ORDER_CHILD_CANCEL_FORBID(1382, "运单无法取消"),
ORDER_CHILD_CANCEL_FORBID_COUNT(1383, "今日取消运单次数超过上限,暂时无法取消"), ORDER_CHILD_CANCEL_FORBID_COUNT(1383, "今日取消运单次数超过上限,暂时无法取消"),
ORDER_GOODS_ID_GENERATE_LOCK(1384, "货单编号ID获取锁出问题"),
......
...@@ -2,15 +2,22 @@ package com.clx.performance.component; ...@@ -2,15 +2,22 @@ package com.clx.performance.component;
import com.clx.performance.constant.RedisConstants; import com.clx.performance.constant.RedisConstants;
import com.clx.performance.dao.OrderGoodsDao; 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 com.msl.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; 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.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.concurrent.TimeUnit;
@Component @Component
@Slf4j
public class OrderGoodsIdGenerate { public class OrderGoodsIdGenerate {
@Autowired @Autowired
...@@ -19,38 +26,72 @@ public class OrderGoodsIdGenerate { ...@@ -19,38 +26,72 @@ public class OrderGoodsIdGenerate {
@Autowired @Autowired
private OrderGoodsDao orderGoodsDao; private OrderGoodsDao orderGoodsDao;
@Autowired
private RedissonClient redissonClient;
private static final String type = "id"; private static final String type = "id";
public long getOrderGoodsId(Integer size) { public long getOrderGoodsId(Integer size) {
long baseStart = Long.parseLong(DateUtils.formatDate(LocalDate.now(), "yyyyMMdd").get() + "00001"); long baseStart = Long.parseLong(DateUtils.formatDate(LocalDate.now(), "yyyyMMdd").get() + "00001");
Object o = redisTemplate.opsForHash().get(RedisConstants.ORDER_GOODS_ID, type); Object o = redisTemplate.opsForValue().get(RedisConstants.ORDER_GOODS_ID_GENERATE);
if (o == null ) { if (o == null) {
Long maxOrderGoodsId = null; RLock rLock = null;
String maxID = orderGoodsDao.getMaxOrderGoodsId(type); try {
if (StringUtils.isNotBlank(maxID)) { rLock = redissonClient.getLock(RedisConstants.ORDER_NO_BLOCK + type);
maxOrderGoodsId = Long.valueOf(maxID.substring(2)); 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) { 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 { } else {
long orderGoodsId = maxOrderGoodsId + size; redisTemplate.opsForValue().increment(RedisConstants.ORDER_GOODS_ID_GENERATE, size);
redisTemplate.opsForHash().put(RedisConstants.ORDER_GOODS_ID, type, String.valueOf(orderGoodsId)); return maxOrderGoodsId;
return maxOrderGoodsId + 1; }
} 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 { } 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.opsForHash().put(RedisConstants.ORDER_GOODS_ID, type, String.valueOf(orderGoodsId)); redisTemplate.opsForValue().increment(RedisConstants.ORDER_GOODS_ID_GENERATE, size);
return redisOrderGoodsId; 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论