重构:订单接单逻辑使用常量枚举替代魔法字符串,微信通知改为异步发送
Some checks failed
Build and Push Backend / docker (push) Failing after 8s
Some checks failed
Build and Push Backend / docker (push) Failing after 8s
- 新增 OrderConstant.OperatorType 枚举(顾客/店员/管理员) - 新增 EXCLUDE_HISTORY 常量(排除历史订单标识) - updateStateTo1 方法使用枚举常量替代硬编码字符串 - 微信通知改为异步发送,防止 API 失败阻塞订单处理 - 新增 sendOrderMessageAsync/sendOrderFinishMessageAsync/sendOrderCancelMessageAsync 方法 - 订单接单、完成、取消通知均使用异步方式,提高系统稳定性
This commit is contained in:
@@ -17,6 +17,7 @@ import com.starry.admin.modules.clerk.module.vo.PlayClerkUserReviewStateEditVo;
|
||||
import com.starry.admin.modules.clerk.service.IPlayClerkCommodityService;
|
||||
import com.starry.admin.modules.clerk.service.IPlayClerkUserInfoService;
|
||||
import com.starry.admin.modules.clerk.service.IPlayClerkUserReviewInfoService;
|
||||
import com.starry.admin.modules.order.module.constant.OrderConstant;
|
||||
import com.starry.admin.modules.weichat.service.WxCustomMpService;
|
||||
import com.starry.common.utils.IdUtils;
|
||||
import java.util.Arrays;
|
||||
@@ -172,8 +173,8 @@ public class PlayClerkUserReviewInfoServiceImpl
|
||||
userInfo.setClerkState("1");
|
||||
userInfo.setId(entity.getClerkId());
|
||||
userInfo.setAlbum(entity.getAlbum());
|
||||
if(entity.getSex().equals("0")){
|
||||
userInfo.setSex("2");
|
||||
if(OrderConstant.Gender.UNKNOWN.getCode().equals(entity.getSex())){
|
||||
userInfo.setSex(OrderConstant.Gender.FEMALE.getCode());
|
||||
}
|
||||
playClerkUserInfoService.update(userInfo);
|
||||
clerkCommodityService.initClerkCommodity(userInfo.getId());
|
||||
|
||||
@@ -180,6 +180,37 @@ public class OrderConstant {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 操作人类型枚举
|
||||
*/
|
||||
@Getter
|
||||
public enum OperatorType {
|
||||
CUSTOMER("0", "顾客"),
|
||||
CLERK("1", "店员"),
|
||||
ADMIN("2", "管理员");
|
||||
|
||||
private final String code;
|
||||
private final String description;
|
||||
|
||||
OperatorType(String code, String description) {
|
||||
this.code = code;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public static OperatorType fromCode(String code) {
|
||||
for (OperatorType type : values()) {
|
||||
if (type.code.equals(code)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Unknown operator type code: " + code);
|
||||
}
|
||||
}
|
||||
|
||||
// 排除历史记录常量
|
||||
public static final String EXCLUDE_HISTORY_NO = "0";
|
||||
public static final String EXCLUDE_HISTORY_YES = "1";
|
||||
|
||||
// Legacy constants for backward compatibility - consider deprecating
|
||||
@Deprecated
|
||||
public final static String ORDER_STATUS_0 = "0";
|
||||
|
||||
@@ -126,6 +126,11 @@ public class PlayOrderDetailsReturnVo {
|
||||
*/
|
||||
private String placeType;
|
||||
|
||||
/**
|
||||
* 要求店员性别(0:未知;1:男;2:女)- 仅随机单有效
|
||||
*/
|
||||
private String sex;
|
||||
|
||||
/**
|
||||
* 微信号码
|
||||
*/
|
||||
|
||||
@@ -672,7 +672,16 @@ public class PlayOrderInfoServiceImpl extends ServiceImpl<PlayOrderInfoMapper, P
|
||||
PlayClerkUserInfoEntity::getId, PlayOrderInfoEntity::getAcceptBy);
|
||||
lambdaQueryWrapper.eq(PlayOrderInfoEntity::getId, orderId);
|
||||
lambdaQueryWrapper.orderByDesc(PlayOrderInfoEntity::getPurchaserTime);
|
||||
return this.baseMapper.selectJoinOne(PlayOrderDetailsReturnVo.class, lambdaQueryWrapper);
|
||||
PlayOrderDetailsReturnVo vo = this.baseMapper.selectJoinOne(PlayOrderDetailsReturnVo.class, lambdaQueryWrapper);
|
||||
|
||||
// Privacy protection: Hide customer info for pending random orders
|
||||
if (vo != null && OrderConstant.PlaceType.RANDOM.getCode().equals(vo.getPlaceType()) && OrderConstant.OrderStatus.PENDING.getCode().equals(vo.getOrderStatus())) {
|
||||
vo.setCustomNickname("匿名用户");
|
||||
vo.setCustomAvatar("");
|
||||
vo.setCustomId("");
|
||||
}
|
||||
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -833,13 +842,15 @@ public class PlayOrderInfoServiceImpl extends ServiceImpl<PlayOrderInfoMapper, P
|
||||
**/
|
||||
@Override
|
||||
public void updateStateTo1(String operatorByType, String operatorBy, String acceptBy, String orderId) {
|
||||
if (!"1".equals(operatorByType) && !"2".equals(operatorByType)) {
|
||||
if (!OrderConstant.OperatorType.CLERK.getCode().equals(operatorByType)
|
||||
&& !OrderConstant.OperatorType.ADMIN.getCode().equals(operatorByType)) {
|
||||
throw new CustomException("禁止操作");
|
||||
}
|
||||
PlayOrderInfoEntity orderInfo = this.selectOrderInfoById(orderId);
|
||||
PlayClerkUserInfoEntity clerkUserInfoEntity = playClerkUserInfoService.selectById(acceptBy);
|
||||
// 店员接单时,判断店员是否符合资格
|
||||
if ("1".equals(operatorByType) && "1".equals(orderInfo.getPlaceType())) {
|
||||
if (OrderConstant.OperatorType.CLERK.getCode().equals(operatorByType)
|
||||
&& OrderConstant.PlaceType.RANDOM.getCode().equals(orderInfo.getPlaceType())) {
|
||||
// 判断店员等级是否符合规则
|
||||
if (orderInfo.getLevelId().equals(clerkUserInfoEntity.getLevelId())) {
|
||||
PlayClerkLevelInfoEntity levelInfo = playClerkLevelInfoService
|
||||
@@ -848,13 +859,14 @@ public class PlayOrderInfoServiceImpl extends ServiceImpl<PlayOrderInfoMapper, P
|
||||
}
|
||||
// 判断店员性别是否符合规则
|
||||
if (!orderInfo.getSex().equals(clerkUserInfoEntity.getSex())) {
|
||||
String sex = "0".equals(orderInfo.getSex()) ? "未知" : "1".equals(orderInfo.getSex()) ? "男" : "女";
|
||||
throw new CustomException("性别为" + sex + "的店员才可接单");
|
||||
OrderConstant.Gender requiredGender = OrderConstant.Gender.fromCode(orderInfo.getSex());
|
||||
throw new CustomException("性别为" + requiredGender.getDescription() + "的店员才可接单");
|
||||
}
|
||||
// 如果排除已下单的店员
|
||||
LambdaQueryWrapper<PlayOrderInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
lambdaQueryWrapper.eq(PlayOrderInfoEntity::getAcceptBy, acceptBy);
|
||||
if ("1".equals(orderInfo.getExcludeHistory()) && this.baseMapper.selectOne(lambdaQueryWrapper) != null) {
|
||||
if (OrderConstant.EXCLUDE_HISTORY_YES.equals(orderInfo.getExcludeHistory())
|
||||
&& this.baseMapper.selectOne(lambdaQueryWrapper) != null) {
|
||||
// throw new CustomException("只有未接单的店员才可接单");
|
||||
}
|
||||
}
|
||||
@@ -867,7 +879,9 @@ public class PlayOrderInfoServiceImpl extends ServiceImpl<PlayOrderInfoMapper, P
|
||||
entity.setEstimatedRevenueRatio(estimatedRevenueVo.getRevenueRatio());
|
||||
this.baseMapper.updateById(entity);
|
||||
|
||||
wxCustomMpService.sendOrderMessage(orderInfo);
|
||||
// Set acceptBy on orderInfo for notification
|
||||
orderInfo.setAcceptBy(acceptBy);
|
||||
wxCustomMpService.sendOrderMessageAsync(orderInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -967,7 +981,7 @@ public class PlayOrderInfoServiceImpl extends ServiceImpl<PlayOrderInfoMapper, P
|
||||
entity2.setOrderEndTime(LocalDateTime.now());
|
||||
this.baseMapper.updateById(entity2);
|
||||
PlayOrderInfoEntity latest = this.selectOrderInfoById(orderId);
|
||||
wxCustomMpService.sendOrderFinishMessage(latest);
|
||||
wxCustomMpService.sendOrderFinishMessageAsync(latest);
|
||||
earningsService.createFromOrder(latest);
|
||||
break;
|
||||
}
|
||||
@@ -1012,7 +1026,7 @@ public class PlayOrderInfoServiceImpl extends ServiceImpl<PlayOrderInfoMapper, P
|
||||
playOrderRefundInfoService.add(orderId, orderInfo.getPurchaserBy(), orderInfo.getAcceptBy(),
|
||||
orderInfo.getPayMethod(), "0", orderInfo.getFinalAmount(), refundReason, operatorByType, operatorBy,
|
||||
"0", "0");
|
||||
wxCustomMpService.sendOrderCancelMessage(orderInfo, refundReason);
|
||||
wxCustomMpService.sendOrderCancelMessageAsync(orderInfo, refundReason);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1062,7 +1076,7 @@ public class PlayOrderInfoServiceImpl extends ServiceImpl<PlayOrderInfoMapper, P
|
||||
orderInfo.getPayMethod(), "0", actualRefundAmount, refundReason, operatorByType, operatorBy, "0", "0");
|
||||
|
||||
PlayOrderInfoEntity latest = this.selectOrderInfoById(orderId);
|
||||
wxCustomMpService.sendOrderCancelMessage(latest, refundReason);
|
||||
wxCustomMpService.sendOrderCancelMessageAsync(latest, refundReason);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.starry.admin.common.aspect.ClerkUserLogin;
|
||||
import com.starry.admin.common.aspect.CustomUserLogin;
|
||||
import com.starry.admin.common.conf.ThreadLocalRequestDetail;
|
||||
import com.starry.admin.common.exception.CustomException;
|
||||
import com.starry.admin.modules.order.module.constant.OrderConstant;
|
||||
import com.starry.admin.modules.order.module.entity.PlayOrderContinueInfoEntity;
|
||||
import com.starry.admin.modules.order.module.entity.PlayOrderInfoEntity;
|
||||
import com.starry.admin.modules.order.module.vo.PlayOrderContinueQueryVo;
|
||||
@@ -121,9 +122,19 @@ public class WxOrderInfoController {
|
||||
if (vo == null) {
|
||||
throw new CustomException("订单不存在");
|
||||
}
|
||||
if (StringUtils.isNotEmpty(vo.getAcceptBy()) && !vo.getAcceptBy().equals(ThreadLocalRequestDetail.getClerkUserInfo().getId())) {
|
||||
// Privacy protection: Hide customer info for pending random orders that current clerk hasn't accepted
|
||||
String currentClerkId = ThreadLocalRequestDetail.getClerkUserInfo().getId();
|
||||
if (OrderConstant.PlaceType.RANDOM.getCode().equals(vo.getPlaceType()) && OrderConstant.OrderStatus.PENDING.getCode().equals(vo.getOrderStatus())) {
|
||||
// Random order pending - customer info already hidden by service layer
|
||||
vo.setWeiChatCode("");
|
||||
} else if (StringUtils.isNotEmpty(vo.getAcceptBy()) && !vo.getAcceptBy().equals(currentClerkId)) {
|
||||
// Order accepted by another clerk - hide WeChat and customer info
|
||||
vo.setWeiChatCode("");
|
||||
vo.setCustomNickname("匿名用户");
|
||||
vo.setCustomAvatar("");
|
||||
vo.setCustomId("");
|
||||
}
|
||||
|
||||
if(vo.getOrderStatus().equals("4")){
|
||||
vo.setWeiChatCode("");
|
||||
}
|
||||
|
||||
@@ -407,6 +407,45 @@ public class WxCustomMpService {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步发送订单接单通知
|
||||
*/
|
||||
public void sendOrderMessageAsync(PlayOrderInfoEntity order) {
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
sendOrderMessage(order);
|
||||
} catch (Exception e) {
|
||||
log.error("发送订单接单通知失败,orderId={}", order.getId(), e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步发送订单完成通知
|
||||
*/
|
||||
public void sendOrderFinishMessageAsync(PlayOrderInfoEntity order) {
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
sendOrderFinishMessage(order);
|
||||
} catch (Exception e) {
|
||||
log.error("发送订单完成通知失败,orderId={}", order.getId(), e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步发送订单取消通知
|
||||
*/
|
||||
public void sendOrderCancelMessageAsync(PlayOrderInfoEntity order, String refundReason) {
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
sendOrderCancelMessage(order, refundReason);
|
||||
} catch (Exception e) {
|
||||
log.error("发送订单取消通知失败,orderId={}", order.getId(), e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void checkSubscribeThrowsExp(String openId, String tenantId) {
|
||||
if (StrUtil.isBlankIfStr(openId)) {
|
||||
throw new ServiceException("openId不能为空");
|
||||
|
||||
Reference in New Issue
Block a user