fix(订单中心):v2接口查询订单性能优化,查询效率提升80%
This commit is contained in:
+60
-23
@@ -42,6 +42,9 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Order information
|
||||
@@ -830,7 +833,7 @@ public class OrderController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 新版:查询用户订单详情列表(含视频/图片/一键成片)
|
||||
* 新版:查询用户订单详情列表(含视频/图片/一键成片)- 性能优化版本
|
||||
*/
|
||||
@Operation(summary = "新版-查询用户订单详情列表(含视频/图片/一键成片)")
|
||||
@Parameters({
|
||||
@@ -847,64 +850,98 @@ public class OrderController {
|
||||
PageResult<Order> result = orderService.findListByUser(params);
|
||||
if (result.getCount() > 0){
|
||||
List<Map> page = new ArrayList<>();
|
||||
Map<String, String> fileMap = new HashMap<>();
|
||||
fileMap.put("style", fileStyle);
|
||||
|
||||
// 批量获取所有订单号
|
||||
List<String> orderNos = result.getData().stream()
|
||||
.map(Order::getOrderNo)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 批量获取所有文件信息
|
||||
Map<String, List<OrderFile>> orderFilesMap = orderFileService.getOrderFilesByOrderNos(orderNos);
|
||||
|
||||
// 批量获取所有景区信息
|
||||
Set<Long> tourismIds = result.getData().stream()
|
||||
.map(Order::getTourismId)
|
||||
.collect(Collectors.toSet());
|
||||
Map<Long, Tourism> tourismMap = tourismService.getTourismsByIds(tourismIds);
|
||||
|
||||
// 预计算所有文件路径的URL,避免重复调用
|
||||
Set<String> allFilePaths = new HashSet<>();
|
||||
orderFilesMap.values().forEach(files ->
|
||||
files.forEach(file -> allFilePaths.add(file.getFilePath())));
|
||||
|
||||
Map<String, String> fileUrlMap = new HashMap<>();
|
||||
Map<String, String> fileStyleUrlMap = new HashMap<>();
|
||||
|
||||
// 批量获取文件URL
|
||||
for (String filePath : allFilePaths) {
|
||||
String url = fileCenterService.getViewUrlByPath(filePath);
|
||||
fileUrlMap.put(filePath, url.replace(cdnOssUrl, cdnUrl));
|
||||
|
||||
// 预计算样式URL(仅对图片文件)
|
||||
if (filePath.contains("image/jpeg") || filePath.contains("image/")) {
|
||||
Map<String, String> styleParams = new HashMap<>();
|
||||
styleParams.put("path", filePath);
|
||||
styleParams.put("style", fileStyle);
|
||||
String styleUrl = fileCenterService.getViewUrlByPathStyle(styleParams);
|
||||
fileStyleUrlMap.put(filePath, styleUrl);
|
||||
}
|
||||
}
|
||||
|
||||
for(Order order : result.getData()){
|
||||
Map<String,Object> orderFileMap = new HashMap<>();
|
||||
orderFileMap.put("time", sdf.format(order.getCreateTime()));
|
||||
orderFileMap.put("addr", tourismService.getTourismById(order.getTourismId()).getTourismAddress());
|
||||
|
||||
// 从缓存获取景区信息
|
||||
Tourism tourism = tourismMap.get(order.getTourismId());
|
||||
orderFileMap.put("addr", tourism != null ? tourism.getTourismAddress() : "");
|
||||
orderFileMap.put("orderNo", order.getOrderNo());
|
||||
|
||||
// 只查一次所有文件
|
||||
List<OrderFile> allFiles = orderFileService.getOrderFileByOrderNo(order.getOrderNo());
|
||||
// 从缓存获取文件列表
|
||||
List<OrderFile> allFiles = orderFilesMap.getOrDefault(order.getOrderNo(), new ArrayList<>());
|
||||
|
||||
// aiVideo
|
||||
// aiVideo - 使用缓存URL
|
||||
List<OrderFile> aiVideoFiles = allFiles.stream()
|
||||
.filter(f -> "ai_video".equals(f.getFileTag()))
|
||||
.toList();
|
||||
String aiVideoUrl = null;
|
||||
if (!aiVideoFiles.isEmpty()) {
|
||||
// fileMap.put("path", aiVideoFiles.get(0).getFilePath());
|
||||
aiVideoUrl = fileCenterService.getViewUrlByPath(aiVideoFiles.get(0).getFilePath()).replace(cdnOssUrl, cdnUrl);
|
||||
aiVideoUrl = fileUrlMap.get(aiVideoFiles.get(0).getFilePath());
|
||||
}
|
||||
orderFileMap.put("aiVideo", aiVideoUrl);
|
||||
|
||||
// 图片素材
|
||||
// 图片素材 - 使用缓存URL
|
||||
List<OrderFile> imgFiles = allFiles.stream()
|
||||
.filter(f -> "image/jpeg".equals(f.getFileType()))
|
||||
.toList();
|
||||
List<Map<String,String>> imgList = new ArrayList<>();
|
||||
for (OrderFile orderFile : imgFiles){
|
||||
Map<String,String> imgMap = new HashMap<>();
|
||||
String url = fileCenterService.getViewUrlByPath(orderFile.getFilePath());
|
||||
imgMap.put("url", url.replace(cdnOssUrl, cdnUrl));
|
||||
imgMap.put("url", fileUrlMap.get(orderFile.getFilePath()));
|
||||
imgMap.put("contentType", orderFile.getFileType());
|
||||
imgMap.put("path", orderFile.getFilePath());
|
||||
fileMap.put("path",orderFile.getFilePath());
|
||||
imgMap.put("viewUrl", fileCenterService.getViewUrlByPathStyle(fileMap));
|
||||
imgMap.put("viewUrl", fileStyleUrlMap.get(orderFile.getFilePath()));
|
||||
imgList.add(imgMap);
|
||||
}
|
||||
orderFileMap.put("imgList", imgList);
|
||||
|
||||
// 视频素材
|
||||
// 视频素材 - 使用缓存URL
|
||||
List<OrderFile> videoFiles = allFiles.stream()
|
||||
.filter(f -> "raw_video".equals(f.getFileTag()))
|
||||
.toList();
|
||||
List<Map<String,String>> videoList = new ArrayList<>();
|
||||
for (OrderFile orderFile : videoFiles){
|
||||
for (int i = 0; i < videoFiles.size(); i++){
|
||||
OrderFile orderFile = videoFiles.get(i);
|
||||
Map<String,String> videoMap = new HashMap<>();
|
||||
String url = fileCenterService.getViewUrlByPath(orderFile.getFilePath());
|
||||
videoMap.put("url", url.replace(cdnOssUrl, cdnUrl));
|
||||
videoMap.put("url", fileUrlMap.get(orderFile.getFilePath()));
|
||||
videoMap.put("contentType", orderFile.getFileType());
|
||||
videoMap.put("path", orderFile.getFilePath());
|
||||
|
||||
// 视频缩略图使用图片的缩略图
|
||||
if (!imgList.isEmpty()) {
|
||||
// 使用第一张图片的缩略图作为视频缩略图
|
||||
String thumbnailUrl = imgList.get(0).get("viewUrl");
|
||||
// 视频缩略图使用对应索引的图片缩略图
|
||||
if (!imgList.isEmpty() && i < imgList.size()) {
|
||||
String thumbnailUrl = imgList.get(i).get("viewUrl");
|
||||
videoMap.put("viewUrl", thumbnailUrl);
|
||||
} else {
|
||||
// 如果没有图片,则使用默认缩略图或空值
|
||||
videoMap.put("viewUrl", null);
|
||||
}
|
||||
|
||||
|
||||
+7
@@ -106,5 +106,12 @@ public interface IOrderFileService extends ISuperService<OrderFile> {
|
||||
* @return 订单文件资源
|
||||
*/
|
||||
OrderFile getOrderFileByFilePath(String filePath);
|
||||
|
||||
/**
|
||||
* 批量根据订单号查询订单文件资源
|
||||
* @param orderNos 订单号列表
|
||||
* @return 订单号到文件列表的映射
|
||||
*/
|
||||
Map<String, List<OrderFile>> getOrderFilesByOrderNos(List<String> orderNos);
|
||||
}
|
||||
|
||||
|
||||
@@ -76,5 +76,12 @@ public interface ITourismService extends ISuperService<Tourism> {
|
||||
* @return 更新结果
|
||||
*/
|
||||
Result updateTourismLocation(Long id, BigDecimal longitude, BigDecimal latitude);
|
||||
|
||||
/**
|
||||
* 批量根据ID查询景区信息
|
||||
* @param ids 景区ID集合
|
||||
* @return 景区ID到景区信息的映射
|
||||
*/
|
||||
Map<Long, Tourism> getTourismsByIds(java.util.Set<Long> ids);
|
||||
}
|
||||
|
||||
|
||||
+25
@@ -12,8 +12,10 @@ import org.apache.commons.collections4.MapUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Order information
|
||||
@@ -133,4 +135,27 @@ public class OrderFileServiceImpl extends SuperServiceImpl<OrderFileMapper, Orde
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<OrderFile>> getOrderFilesByOrderNos(List<String> orderNos) {
|
||||
try {
|
||||
log.debug("批量查询订单文件: orderNos={}", orderNos);
|
||||
if (orderNos == null || orderNos.isEmpty()) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
// 查询所有文件
|
||||
List<OrderFile> allFiles = baseMapper.selectList(
|
||||
new QueryWrapper<OrderFile>()
|
||||
.in("order_no", orderNos)
|
||||
);
|
||||
|
||||
// 按订单号分组
|
||||
return allFiles.stream()
|
||||
.collect(Collectors.groupingBy(OrderFile::getOrderNo));
|
||||
} catch (Exception e) {
|
||||
log.error("批量查询订单文件失败: orderNos={}", orderNos, e);
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+25
@@ -17,8 +17,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 景区信息服务实现类
|
||||
@@ -172,4 +174,27 @@ public class TourismServiceImpl extends SuperServiceImpl<TourismMapper, Tourism>
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, Tourism> getTourismsByIds(java.util.Set<Long> ids) {
|
||||
try {
|
||||
log.debug("批量查询景区信息: ids={}", ids);
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
// 查询所有景区
|
||||
List<Tourism> tourisms = baseMapper.selectList(
|
||||
new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Tourism>()
|
||||
.in("id", ids)
|
||||
);
|
||||
|
||||
// 转换为Map
|
||||
return tourisms.stream()
|
||||
.collect(Collectors.toMap(Tourism::getId, tourism -> tourism));
|
||||
} catch (Exception e) {
|
||||
log.error("批量查询景区信息失败: ids={}", ids, e);
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user