修改取单号逻辑算法
This commit is contained in:
parent
7a26bdec49
commit
f9192c74ff
@ -2,6 +2,7 @@ package com.suisung.mall.shop.number.service;
|
|||||||
|
|
||||||
import com.suisung.mall.common.modules.number.ShopNumberSeq;
|
import com.suisung.mall.common.modules.number.ShopNumberSeq;
|
||||||
import com.suisung.mall.core.web.service.IBaseService;
|
import com.suisung.mall.core.web.service.IBaseService;
|
||||||
|
import org.springframework.data.util.Pair;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -17,6 +18,14 @@ public interface ShopNumberSeqService extends IBaseService<ShopNumberSeq> {
|
|||||||
|
|
||||||
String createNextSeq(String prefix);
|
String createNextSeq(String prefix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建下一个编号(键值对)
|
||||||
|
*
|
||||||
|
* @param prefix 前缀
|
||||||
|
* @return 序号和完整编号
|
||||||
|
*/
|
||||||
|
Pair<Long, String> createNextSeqPair(String prefix);
|
||||||
|
|
||||||
Long createNextNo(String prefix);
|
Long createNextNo(String prefix);
|
||||||
|
|
||||||
List<Long> batchCreateNextNo(String seqName, int batchSize);
|
List<Long> batchCreateNextNo(String seqName, int batchSize);
|
||||||
|
|||||||
@ -27,6 +27,7 @@ import org.redisson.api.RLock;
|
|||||||
import org.redisson.api.RedissonClient;
|
import org.redisson.api.RedissonClient;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Lazy;
|
import org.springframework.context.annotation.Lazy;
|
||||||
|
import org.springframework.data.util.Pair;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Propagation;
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
@ -125,6 +126,44 @@ public class ShopNumberSeqServiceImpl extends BaseServiceImpl<ShopNumberSeqMappe
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到下一个编号键值对
|
||||||
|
* 方法走到这里会产生串行化,集群部署这里不能使用单机锁
|
||||||
|
*
|
||||||
|
* @param prefix
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
|
@DistributedLock(
|
||||||
|
key = "CREATENEXTSEQ_LOCK", // 锁的key
|
||||||
|
waitTime = 3, // 等待3秒
|
||||||
|
leaseTime = 10, // 锁持有10秒
|
||||||
|
errorMsg = "生成ID繁忙,请稍后重试" // 自定义错误消息
|
||||||
|
)
|
||||||
|
public synchronized Pair<Long, String> createNextSeqPair(String prefix) {
|
||||||
|
String ymd = DateUtil.format(new Date(), "yyyyMMdd");
|
||||||
|
String id = String.format("%s_%s_", prefix, ymd);
|
||||||
|
ShopNumberSeq shopNumberSeq = this.baseMapper.selectById(id);
|
||||||
|
if (shopNumberSeq == null) {
|
||||||
|
shopNumberSeq = new ShopNumberSeq();
|
||||||
|
shopNumberSeq.setPrefix(id);
|
||||||
|
shopNumberSeq.setNumber(1L);
|
||||||
|
if (!save(shopNumberSeq)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String order_id = String.format("%s_%s_%s", prefix, ymd, shopNumberSeq.getNumber());
|
||||||
|
shopNumberSeq.setPrefix(id);
|
||||||
|
boolean flag = edit(shopNumberSeq);
|
||||||
|
if (flag) {
|
||||||
|
return Pair.of(shopNumberSeq.getNumber(), order_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 得到下一个Id
|
* 得到下一个Id
|
||||||
*
|
*
|
||||||
@ -537,5 +576,4 @@ public class ShopNumberSeqServiceImpl extends BaseServiceImpl<ShopNumberSeqMappe
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5289,17 +5289,17 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
|
|||||||
throw new ApiException(I18nUtil._("无符合取消条件的订单!"));
|
throw new ApiException(I18nUtil._("无符合取消条件的订单!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
ShopOrderBase shopOrderBase = new ShopOrderBase();
|
ShopOrderBase shopOrderBaseUpd = new ShopOrderBase();
|
||||||
shopOrderBase.setOrder_id(order_id);
|
shopOrderBaseUpd.setOrder_id(order_id);
|
||||||
shopOrderBase.setOrder_state_id(StateCode.ORDER_STATE_CANCEL);
|
shopOrderBaseUpd.setOrder_state_id(StateCode.ORDER_STATE_CANCEL);
|
||||||
if (!shopOrderBaseService.edit(shopOrderBase)) {
|
if (!shopOrderBaseService.edit(shopOrderBaseUpd)) {
|
||||||
throw new ApiException(ResultCode.FAILED);
|
throw new ApiException(ResultCode.FAILED);
|
||||||
}
|
}
|
||||||
|
|
||||||
ShopOrderInfo orderInfo = new ShopOrderInfo();
|
ShopOrderInfo orderInfoUpd = new ShopOrderInfo();
|
||||||
orderInfo.setOrder_id(order_id);
|
orderInfoUpd.setOrder_id(order_id);
|
||||||
orderInfo.setOrder_state_id(StateCode.ORDER_STATE_CANCEL);
|
orderInfoUpd.setOrder_state_id(StateCode.ORDER_STATE_CANCEL);
|
||||||
if (!shopOrderInfoService.edit(orderInfo)) {
|
if (!shopOrderInfoService.edit(orderInfoUpd)) {
|
||||||
throw new ApiException(ResultCode.FAILED);
|
throw new ApiException(ResultCode.FAILED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -5319,7 +5319,7 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
|
|||||||
if (shopOrderInfo.getDelivery_type_id() != null
|
if (shopOrderInfo.getDelivery_type_id() != null
|
||||||
&& StateCode.DELIVERY_TYPE_SAME_CITY == shopOrderInfo.getDelivery_type_id().intValue()) {
|
&& StateCode.DELIVERY_TYPE_SAME_CITY == shopOrderInfo.getDelivery_type_id().intValue()) {
|
||||||
try {
|
try {
|
||||||
logger.info("开始取消顺丰同城配送订单,orderId: {}, delivery_type_id: {}", order_id, shopOrderInfo.getDelivery_type_id());
|
logger.warn("开始取消顺丰同城配送订单,orderId: {}, delivery_type_id: {}", order_id, shopOrderInfo.getDelivery_type_id());
|
||||||
ThirdApiRes sfResult = sfExpressApiService.cancelOrder(order_id, 313, "用户或商家取消订单。");
|
ThirdApiRes sfResult = sfExpressApiService.cancelOrder(order_id, 313, "用户或商家取消订单。");
|
||||||
if (sfResult != null && !sfResult.getError_code().equals(0)) {
|
if (sfResult != null && !sfResult.getError_code().equals(0)) {
|
||||||
log.error("顺丰同城取消订单返回错误,orderId: {}, errorCode: {}, errorMsg: {}",
|
log.error("顺丰同城取消订单返回错误,orderId: {}, errorCode: {}, errorMsg: {}",
|
||||||
@ -6421,7 +6421,9 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
|
|||||||
type_code = StrUtil.isBlank(type_code) ? "DD" : type_code;
|
type_code = StrUtil.isBlank(type_code) ? "DD" : type_code;
|
||||||
String xid = RootContext.getXID();
|
String xid = RootContext.getXID();
|
||||||
RootContext.unbind();
|
RootContext.unbind();
|
||||||
String order_id = shopNumberSeqService.createNextSeq(type_code);
|
Pair<Long, String> seqPair = shopNumberSeqService.createNextSeqPair(type_code);// 位数的序号: DD_20251205_1 2025-12-05 1
|
||||||
|
String order_id = seqPair.getSecond();
|
||||||
|
Long seqNo = seqPair.getFirst(); // 序号: DD_20251205_1 得出 1
|
||||||
RootContext.bind(xid);
|
RootContext.bind(xid);
|
||||||
|
|
||||||
List<ShopOrderItem> item_rows = new ArrayList();
|
List<ShopOrderItem> item_rows = new ArrayList();
|
||||||
@ -6666,6 +6668,7 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
|
|||||||
}
|
}
|
||||||
|
|
||||||
info_row.setOrder_id(order_id);
|
info_row.setOrder_id(order_id);
|
||||||
|
info_row.setOrder_pickup_num(seqNo); //重要:配送员的取单号
|
||||||
info_row.setOrder_title(product_item_name); // 订单标题
|
info_row.setOrder_title(product_item_name); // 订单标题
|
||||||
if (fixOrderVo != null && fixOrderVo.isFix_price() && StrUtil.isNotEmpty(fixOrderVo.getOrder_title())) {
|
if (fixOrderVo != null && fixOrderVo.isFix_price() && StrUtil.isNotEmpty(fixOrderVo.getOrder_title())) {
|
||||||
info_row.setOrder_title(fixOrderVo.getOrder_title()); // 订单标题
|
info_row.setOrder_title(fixOrderVo.getOrder_title()); // 订单标题
|
||||||
|
|||||||
@ -372,8 +372,8 @@ public class ShopOrderInfoServiceImpl extends BaseServiceImpl<ShopOrderInfoMappe
|
|||||||
/**
|
/**
|
||||||
* 已支付的订单,生成取单号,打票机并打印订单
|
* 已支付的订单,生成取单号,打票机并打印订单
|
||||||
*
|
*
|
||||||
* @param storeId
|
* @param storeId 商家ID
|
||||||
* @param orderId
|
* @param orderId 订单ID
|
||||||
* @return 取货单号
|
* @return 取货单号
|
||||||
*/
|
*/
|
||||||
// @Transactional
|
// @Transactional
|
||||||
@ -403,11 +403,22 @@ public class ShopOrderInfoServiceImpl extends BaseServiceImpl<ShopOrderInfoMappe
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 生成取单号
|
// 生成取单号(废弃)
|
||||||
Long orderPickupNum = genTodayPickupNum(storeId);
|
// Long orderPickupNum = genTodayPickupNum(storeId);
|
||||||
if (orderPickupNum == null || orderPickupNum <= 0) {
|
// if (orderPickupNum == null || orderPickupNum <= 0) {
|
||||||
logger.error("生成取单号失败: storeId={}", storeId);
|
// logger.error("生成取单号失败: storeId={}", storeId);
|
||||||
return 0L;
|
// return 0L;
|
||||||
|
// }
|
||||||
|
|
||||||
|
Long orderPickupNum = 0L;
|
||||||
|
// 如果订单中尚未设置取单号,则从订单ID中提取
|
||||||
|
if (CheckUtil.isEmpty(orderInfoOld.getOrder_pickup_num())) {
|
||||||
|
// 从订单ID中提取最后的数字部分作为取单号
|
||||||
|
// 例如订单ID为 DD_20250510_11,则提取出 11 作为取单号
|
||||||
|
orderPickupNum = Convert.toLong(StrUtil.subAfter(orderId, "_", true));
|
||||||
|
} else {
|
||||||
|
// 如果已有取单号,则使用现有的
|
||||||
|
orderPickupNum = orderInfoOld.getOrder_pickup_num();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新订单信息
|
// 更新订单信息
|
||||||
@ -432,6 +443,7 @@ public class ShopOrderInfoServiceImpl extends BaseServiceImpl<ShopOrderInfoMappe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据订单Id、订单状态、配送方式、退款状态 获取订单的数量
|
* 根据订单Id、订单状态、配送方式、退款状态 获取订单的数量
|
||||||
*
|
*
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user