Commit 524b139a by 吕明尚

优化订单查询接口

parent ad8ed8a4
...@@ -354,34 +354,38 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme ...@@ -354,34 +354,38 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
public TableDataInfoVo pageList(SOrder sOrder) { public TableDataInfoVo pageList(SOrder sOrder) {
List<SOrder> sOrders = selectSOrderList(sOrder); List<SOrder> sOrders = selectSOrderList(sOrder);
List<SOrderVo> sOrderVos = convertDosToVos(sOrders); List<SOrderVo> sOrderVos = convertDosToVos(sOrders);
BigDecimal totalAmount = new BigDecimal("0.00"); final BigDecimal[] totalAmount = {new BigDecimal("0.00")};
BigDecimal amount = new BigDecimal("0.00"); final BigDecimal[] amount = {new BigDecimal("0.00")};
//优惠卷售卖总金额 //优惠卷售卖总金额
final BigDecimal[] totalSalesAmount = {new BigDecimal("0.00")}; final BigDecimal[] totalSalesAmount = {new BigDecimal("0.00")};
//申请退款总金额 //申请退款总金额
BigDecimal totalRefundAmount = new BigDecimal("0.00"); final BigDecimal[] totalRefundAmount = {new BigDecimal("0.00")};
//实际退款总金额 //实际退款总金额
BigDecimal totalRealRefundAmount = new BigDecimal("0.00"); final BigDecimal[] totalRealRefundAmount = {new BigDecimal("0.00")};
List<SConsumerCoupon> sConsumerCoupons = consumerCouponService.queryList(); Map<Long, SConsumerCoupon> couponMap = consumerCouponService.listByIds(sOrders.stream().map(SOrder::getCouponId).distinct().collect(Collectors.toList()))
.stream().collect(Collectors.toMap(SConsumerCoupon::getId, s -> s));
if (CollectionUtils.isNotEmpty(sOrderVos)) { if (CollectionUtils.isNotEmpty(sOrderVos)) {
for (SOrderVo item : sOrderVos) { sOrderVos.stream().forEach(item -> {
totalAmount = totalAmount.add(item.getTotalPrice()); totalAmount[0] = totalAmount[0].add(item.getTotalPrice());
amount = amount.add(item.getPayPrice()); amount[0] = amount[0].add(item.getPayPrice());
sConsumerCoupons.stream().filter(sConsumerCoupon -> sConsumerCoupon.getId().equals(item.getCouponId())).findFirst().ifPresent(sConsumerCoupon -> { if (Objects.nonNull(item.getCouponId())) {
item.setCouponPayPrice(sConsumerCoupon.getCouponPayPrice()); SConsumerCoupon userCoupon = couponMap.get(item.getCouponId());
item.setPlatformType(sConsumerCoupon.getPlatformType()); if (userCoupon != null) {
if (sConsumerCoupon.getPlatformType().equals(PlatformTypeEnum.MEITUAN.getCode())) { item.setCouponPayPrice(userCoupon.getCouponPayPrice());
item.setCouponSkuId(String.valueOf(sConsumerCoupon.getDealgroupId())); item.setPlatformType(userCoupon.getPlatformType());
} else if (sConsumerCoupon.getPlatformType().equals(PlatformTypeEnum.TIKTOK.getCode())) { if (userCoupon.getPlatformType().equals(PlatformTypeEnum.MEITUAN.getCode())) {
item.setCouponSkuId(String.valueOf(sConsumerCoupon.getTiktokSkuId())); item.setCouponSkuId(String.valueOf(userCoupon.getDealgroupId()));
} else if (userCoupon.getPlatformType().equals(PlatformTypeEnum.TIKTOK.getCode())) {
item.setCouponSkuId(String.valueOf(userCoupon.getTiktokSkuId()));
}
totalSalesAmount[0] = totalSalesAmount[0].add(ObjectUtil.isEmpty(userCoupon.getCouponPayPrice()) ? new BigDecimal("0.00") : userCoupon.getCouponPayPrice());
} }
totalSalesAmount[0] = totalSalesAmount[0].add(ObjectUtil.isEmpty(sConsumerCoupon.getCouponPayPrice()) ? new BigDecimal("0.00") : sConsumerCoupon.getCouponPayPrice());
});
if (item.getPayStatus().equals(YesNoEnum.yes.getIndex()) && item.getRefundStatus().equals(RefundStatusEnum.REFUNDED.getCode())) {
totalRefundAmount = totalRefundAmount.add(ObjectUtil.isEmpty(item.getPayPrice()) ? new BigDecimal("0.00") : item.getPayPrice());
totalRealRefundAmount = totalRealRefundAmount.add(ObjectUtil.isEmpty(item.getRefundPrice()) ? new BigDecimal("0.00") : item.getRefundPrice());
} }
if (item.getPayStatus().equals(YesNoEnum.yes.getIndex()) && item.getRefundStatus().equals(RefundStatusEnum.REFUNDED.getCode())) {
totalRefundAmount[0] = totalRefundAmount[0].add(ObjectUtil.isEmpty(item.getPayPrice()) ? new BigDecimal("0.00") : item.getPayPrice());
totalRealRefundAmount[0] = totalRealRefundAmount[0].add(ObjectUtil.isEmpty(item.getRefundPrice()) ? new BigDecimal("0.00") : item.getRefundPrice());
} }
});
} }
sOrderVos.sort(Comparator.comparing(SOrderVo::getCreateTime).reversed()); sOrderVos.sort(Comparator.comparing(SOrderVo::getCreateTime).reversed());
PageDomain pageDomain = TableSupport.buildPageRequest(); PageDomain pageDomain = TableSupport.buildPageRequest();
...@@ -395,11 +399,11 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme ...@@ -395,11 +399,11 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
tableDataInfo.setTotal(sOrderVos.size()); tableDataInfo.setTotal(sOrderVos.size());
tableDataInfo.setCode(200); tableDataInfo.setCode(200);
tableDataInfo.setMsg("查询成功"); tableDataInfo.setMsg("查询成功");
tableDataInfo.setTotalAmount(totalAmount); tableDataInfo.setTotalAmount(totalAmount[0]);
tableDataInfo.setAmount(amount); tableDataInfo.setAmount(amount[0]);
tableDataInfo.setTotalSalesAmount(totalSalesAmount[0]); tableDataInfo.setTotalSalesAmount(totalSalesAmount[0]);
tableDataInfo.setTotalRefundAmount(totalRefundAmount); tableDataInfo.setTotalRefundAmount(totalRefundAmount[0]);
tableDataInfo.setTotalRealRefundAmount(totalRealRefundAmount); tableDataInfo.setTotalRealRefundAmount(totalRealRefundAmount[0]);
return tableDataInfo; return tableDataInfo;
} }
...@@ -1946,15 +1950,45 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme ...@@ -1946,15 +1950,45 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
private List<SOrderVo> convertDosToVos(List<SOrder> orderList) { private List<SOrderVo> convertDosToVos(List<SOrder> orderList) {
List<SOrderVo> voList = new ArrayList<>(); List<SOrderVo> voList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(orderList)) { if (CollectionUtils.isNotEmpty(orderList)) {
try {
// 使用Map缓存查询结果,减少重复查询 // 使用Map缓存查询结果,减少重复查询
Map<Long, SStore> storeMap = storeService.listByIds(orderList.stream().map(SOrder::getStoreId).collect(Collectors.toSet())) // 合并查询逻辑以减少N+1查询问题
.stream().collect(Collectors.toMap(SStore::getId, s -> s)); Set<Long> storeIds = orderList.stream().map(SOrder::getStoreId).collect(Collectors.toSet());
Map<Long, SRoom> roomMap = roomService.listByIds(orderList.stream().map(SOrder::getRoomId).collect(Collectors.toSet())) Set<Long> roomIds = orderList.stream().map(SOrder::getRoomId).distinct().collect(Collectors.toSet());
.stream().collect(Collectors.toMap(SRoom::getId, s -> s)); Set<Long> couponIds = orderList.stream().map(SOrder::getCouponId).distinct().collect(Collectors.toSet());
Map<Long, SConsumerCoupon> couponMap = consumerCouponService.queryByIds(orderList.stream().map(SOrder::getCouponId).distinct().collect(Collectors.toList())) Set<Long> packIds = orderList.stream().map(SOrder::getPackId).distinct().collect(Collectors.toSet());
.stream().collect(Collectors.toMap(SConsumerCoupon::getId, s -> s));
Map<Long, SPack> packMap = packService.listByIds(orderList.stream().map(SOrder::getPackId).distinct().collect(Collectors.toList())) Map<Long, SStore> storeMap;
.stream().collect(Collectors.toMap(SPack::getId, s -> s)); if (!storeIds.isEmpty()) {
List<SStore> stores = storeService.listByIds(storeIds);
storeMap = stores.stream().collect(Collectors.toMap(SStore::getId, s -> s));
} else {
storeMap = new HashMap<>();
}
Map<Long, SRoom> roomMap;
if (!roomIds.isEmpty()) {
List<SRoom> rooms = roomService.listByIds(roomIds);
roomMap = rooms.stream().collect(Collectors.toMap(SRoom::getId, s -> s));
} else {
roomMap = new HashMap<>();
}
Map<Long, SConsumerCoupon> couponMap;
if (!couponIds.isEmpty()) {
List<SConsumerCoupon> coupons = consumerCouponService.listByIds(couponIds);
couponMap = coupons.stream().collect(Collectors.toMap(SConsumerCoupon::getId, s -> s));
} else {
couponMap = new HashMap<>();
}
Map<Long, SPack> packMap;
if (!packIds.isEmpty()) {
List<SPack> packs = packService.listByIds(packIds);
packMap = packs.stream().collect(Collectors.toMap(SPack::getId, s -> s));
} else {
packMap = new HashMap<>();
}
orderList.forEach(o -> { orderList.forEach(o -> {
SOrderVo vo = new SOrderVo(); SOrderVo vo = new SOrderVo();
...@@ -1974,15 +2008,12 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme ...@@ -1974,15 +2008,12 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
if (Objects.nonNull(vo.getCouponId())) { if (Objects.nonNull(vo.getCouponId())) {
SConsumerCoupon userCoupon = couponMap.get(vo.getCouponId()); SConsumerCoupon userCoupon = couponMap.get(vo.getCouponId());
if (userCoupon != null) { if (userCoupon != null) {
// 判断是否可以退款
vo.setIsRefund(isRefund(vo, userCoupon)); vo.setIsRefund(isRefund(vo, userCoupon));
vo.setCouponName(userCoupon.getName()); vo.setCouponName(userCoupon.getName());
} else { } else {
// 如果优惠券为空,则调用isRefund方法时需要传入null
vo.setIsRefund(isRefund(vo, null)); vo.setIsRefund(isRefund(vo, null));
} }
} else { } else {
// 判断是否可以退款
vo.setIsRefund(isRefund(vo, null)); vo.setIsRefund(isRefund(vo, null));
} }
SPack pack = packMap.get(vo.getPackId()); SPack pack = packMap.get(vo.getPackId());
...@@ -1991,10 +2022,16 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme ...@@ -1991,10 +2022,16 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
} }
voList.add(vo); voList.add(vo);
}); });
} catch (Exception e) {
// 记录日志并处理异常,例如返回错误信息或执行其他错误处理逻辑
// log.error("订单数据转换异常", e);
throw new RuntimeException("订单数据转换异常", e);
}
} }
return voList; return voList;
} }
@Override @Override
public String openDoor(String orderNo) { public String openDoor(String orderNo) {
LambdaQueryWrapper<SOrder> orderLambdaQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<SOrder> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment