|
|
@@ -12,6 +12,7 @@ import com.haha.common.enums.OrderStatus;
|
|
|
import com.haha.common.enums.PayStatus;
|
|
|
import com.haha.common.enums.PaymentChannel;
|
|
|
import com.haha.common.config.CommonConfig;
|
|
|
+import com.haha.common.enums.UserTag;
|
|
|
import com.haha.common.utils.EntityLabelUtils;
|
|
|
import com.haha.common.utils.OrderUtils;
|
|
|
import com.haha.common.vo.OrderItemVO;
|
|
|
@@ -22,6 +23,7 @@ import com.haha.entity.Shop;
|
|
|
import com.haha.mapper.DeviceMapper;
|
|
|
import com.haha.mapper.OrderMapper;
|
|
|
import com.haha.mapper.ShopMapper;
|
|
|
+import com.haha.service.UserTagService;
|
|
|
import com.haha.service.OrderGoodsService;
|
|
|
import com.haha.service.OrderService;
|
|
|
import com.haha.service.InviteActivityService;
|
|
|
@@ -46,6 +48,8 @@ import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
@@ -55,6 +59,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
private final DeviceMapper deviceMapper;
|
|
|
private final ShopMapper shopMapper;
|
|
|
private final CommonConfig commonConfig;
|
|
|
+ private final UserTagService userTagService;
|
|
|
|
|
|
@Autowired
|
|
|
@Lazy
|
|
|
@@ -109,8 +114,8 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
// 分页查询
|
|
|
IPage<Order> orderPage = this.page(new Page<>(page, pageSize), wrapper);
|
|
|
|
|
|
- // 填充标签字段和关联信息
|
|
|
- orderPage.getRecords().forEach(this::fillOrderLabels);
|
|
|
+ // 批量填充标签字段和关联信息(含用户消费标签)
|
|
|
+ fillOrderLabels(orderPage.getRecords());
|
|
|
|
|
|
return orderPage;
|
|
|
}
|
|
|
@@ -121,7 +126,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
if (order == null) {
|
|
|
return null;
|
|
|
}
|
|
|
- fillOrderLabels(order);
|
|
|
+ fillOrderLabels(List.of(order));
|
|
|
return order;
|
|
|
}
|
|
|
|
|
|
@@ -154,6 +159,16 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
List<OrderItemVO> products = orderGoodsService.getVOByOrderId(id);
|
|
|
vo.setProducts(products);
|
|
|
|
|
|
+ // 填充用户消费标签
|
|
|
+ if (order.getUserId() != null) {
|
|
|
+ Map<Long, UserTag> tagMap = userTagService.batchComputeUserTags(List.of(order.getUserId()));
|
|
|
+ UserTag tag = tagMap.get(order.getUserId());
|
|
|
+ if (tag != null) {
|
|
|
+ vo.setUserTag(tag.getCode());
|
|
|
+ vo.setUserTagLabel(tag.getDescription());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return vo;
|
|
|
}
|
|
|
|
|
|
@@ -387,50 +402,75 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 填充订单标签字段
|
|
|
+ * 批量填充订单标签字段(含用户消费标签)
|
|
|
+ * 优化:使用批量SQL预计算用户标签,避免N+1查询
|
|
|
*/
|
|
|
- private void fillOrderLabels(Order order) {
|
|
|
- if (order.getPayStatus() != null) {
|
|
|
- var payLabel = EntityLabelUtils.getPayStatusLabel(order.getPayStatus());
|
|
|
- order.setPayStatusLabel(payLabel.getLabel());
|
|
|
- }
|
|
|
- if (order.getStatus() != null) {
|
|
|
- var statusLabel = EntityLabelUtils.getStatusLabel("order", order.getStatus());
|
|
|
- order.setStatusLabel(statusLabel.getLabel());
|
|
|
- }
|
|
|
-
|
|
|
- // 如果 payType 为空但 payChannel 有值,从 payChannel 获取描述
|
|
|
- if (order.getPayType() == null || order.getPayType().isEmpty()) {
|
|
|
- if (order.getPayChannel() != null && !order.getPayChannel().isEmpty()) {
|
|
|
- PaymentChannel channel = PaymentChannel.fromCode(order.getPayChannel());
|
|
|
- if (channel != null) {
|
|
|
- order.setPayType(channel.getDescription());
|
|
|
+ private void fillOrderLabels(List<Order> orders) {
|
|
|
+ if (orders == null || orders.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 批量计算用户消费标签
|
|
|
+ List<Long> userIds = orders.stream()
|
|
|
+ .map(Order::getUserId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ Map<Long, UserTag> userTagMap = userTagService.batchComputeUserTags(userIds);
|
|
|
+
|
|
|
+ // 2. 逐条填充标签
|
|
|
+ for (Order order : orders) {
|
|
|
+ // 用户消费标签
|
|
|
+ if (order.getUserId() != null) {
|
|
|
+ UserTag tag = userTagMap.get(order.getUserId());
|
|
|
+ if (tag != null) {
|
|
|
+ order.setUserTag(tag.getCode());
|
|
|
+ order.setUserTagLabel(tag.getDescription());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 支付状态标签
|
|
|
+ if (order.getPayStatus() != null) {
|
|
|
+ var payLabel = EntityLabelUtils.getPayStatusLabel(order.getPayStatus());
|
|
|
+ order.setPayStatusLabel(payLabel.getLabel());
|
|
|
+ }
|
|
|
+ // 订单状态标签
|
|
|
+ if (order.getStatus() != null) {
|
|
|
+ var statusLabel = EntityLabelUtils.getStatusLabel("order", order.getStatus());
|
|
|
+ order.setStatusLabel(statusLabel.getLabel());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果 payType 为空但 payChannel 有值,从 payChannel 获取描述
|
|
|
+ if (order.getPayType() == null || order.getPayType().isEmpty()) {
|
|
|
+ if (order.getPayChannel() != null && !order.getPayChannel().isEmpty()) {
|
|
|
+ PaymentChannel channel = PaymentChannel.fromCode(order.getPayChannel());
|
|
|
+ if (channel != null) {
|
|
|
+ order.setPayType(channel.getDescription());
|
|
|
+ }
|
|
|
+ } else if (order.getOutTradeNo() != null && !order.getOutTradeNo().isEmpty()) {
|
|
|
+ order.setPayType("微信支付"); // 兼容旧数据
|
|
|
+ } else if (PayStatus.isPaid(order.getPayStatus())) {
|
|
|
+ order.setPayType("免费");
|
|
|
}
|
|
|
- } else if (order.getOutTradeNo() != null && !order.getOutTradeNo().isEmpty()) {
|
|
|
- order.setPayType("微信支付"); // 兼容旧数据
|
|
|
- } else if (PayStatus.isPaid(order.getPayStatus())) {
|
|
|
- order.setPayType("免费");
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- // 根据 deviceId 查询设备信息,获取门店名称
|
|
|
- if (order.getDeviceId() != null && !order.getDeviceId().isEmpty()) {
|
|
|
- try {
|
|
|
- // 通过 deviceId 查询设备
|
|
|
- Device device = deviceMapper.selectByDeviceId(order.getDeviceId());
|
|
|
- if (device != null) {
|
|
|
- order.setShopId(device.getShopId());
|
|
|
- // 通过 shopId 查询门店名称
|
|
|
- if (device.getShopId() != null) {
|
|
|
- Shop shop = shopMapper.selectById(device.getShopId());
|
|
|
- if (shop != null) {
|
|
|
- order.setShopName(shop.getName());
|
|
|
- order.setStoreName(shop.getName());
|
|
|
+ // 根据 deviceId 查询设备信息,获取门店名称
|
|
|
+ if (order.getDeviceId() != null && !order.getDeviceId().isEmpty()) {
|
|
|
+ try {
|
|
|
+ Device device = deviceMapper.selectByDeviceId(order.getDeviceId());
|
|
|
+ if (device != null) {
|
|
|
+ order.setShopId(device.getShopId());
|
|
|
+ if (device.getShopId() != null) {
|
|
|
+ Shop shop = shopMapper.selectById(device.getShopId());
|
|
|
+ if (shop != null) {
|
|
|
+ order.setShopName(shop.getName());
|
|
|
+ order.setStoreName(shop.getName());
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("获取订单关联门店信息失败: orderId={}, deviceId={}", order.getId(), order.getDeviceId(), e);
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- log.warn("获取订单关联门店信息失败: orderId={}, deviceId={}", order.getId(), order.getDeviceId(), e);
|
|
|
}
|
|
|
}
|
|
|
}
|