This commit is contained in:
2025-11-24 14:21:09 +08:00
parent 907dd0e81c
commit d3931a9ddd
2 changed files with 49 additions and 26 deletions

View File

@ -88,14 +88,12 @@ public class MatterController {
@Parameter(description = "物资名称") @RequestParam(value = "name", required = false) String name) { @Parameter(description = "物资名称") @RequestParam(value = "name", required = false) String name) {
Map<String, Integer> goodsTotalMap = getGoodCountMap(); Map<String, Integer> goodsTotalMap = getGoodCountMap();
// 分页查询物资表的数据 // 分页查询物资表的数据
LambdaQueryWrapper<Matter> matterWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<Matter> matterWrapper = new LambdaQueryWrapper<>();
if (StringUtils.isNotBlank(name)) { if (StringUtils.isNotBlank(name)) {
matterWrapper.like(Matter::getName, name); matterWrapper.like(Matter::getName, name);
} }
Page<Matter> matterPage = matterService.page(new Page<>(pageNum, pageSize), matterWrapper); Page<Matter> matterPage = matterService.page(new Page<>(pageNum, pageSize), matterWrapper);
// 用统计出的数量替换分页结果中的数量 // 用统计出的数量替换分页结果中的数量
if (!CollectionUtils.isEmpty(matterPage.getRecords())) { if (!CollectionUtils.isEmpty(matterPage.getRecords())) {
matterPage.getRecords().forEach(matter -> { matterPage.getRecords().forEach(matter -> {
@ -117,6 +115,7 @@ public class MatterController {
LambdaQueryWrapper<Source> sourceWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<Source> sourceWrapper = new LambdaQueryWrapper<>();
sourceWrapper.in(Source::getId, pointIdList); sourceWrapper.in(Source::getId, pointIdList);
List<Source> sourceList = sourceService.list(sourceWrapper); List<Source> sourceList = sourceService.list(sourceWrapper);
// 初始化物资统计Map: 用于存储 "物资ID -> 总数量" 的映射关系
Map<String, Integer> goodsTotalMap = new HashMap<>(); Map<String, Integer> goodsTotalMap = new HashMap<>();
if (!CollectionUtils.isEmpty(sourceList)) { if (!CollectionUtils.isEmpty(sourceList)) {
for (Source source : sourceList) { for (Source source : sourceList) {
@ -129,9 +128,13 @@ public class MatterController {
.ifPresent(contentList -> { .ifPresent(contentList -> {
for (Point.Attribute.Goods.GoodsContent goodsContent : contentList) { for (Point.Attribute.Goods.GoodsContent goodsContent : contentList) {
if (goodsContent == null) continue; if (goodsContent == null) continue;
String goodsName = Optional.ofNullable(goodsContent.getName()).orElse("未知物资"); String goodsId = Optional.ofNullable(goodsContent.getId())
int count = Optional.ofNullable(goodsContent.getCnt()).map(Integer::valueOf).orElse(0); .orElse("未知物资ID");
goodsTotalMap.merge(goodsName, count, Integer::sum); int count = Optional.ofNullable(goodsContent.getCnt())
.map(Integer::valueOf)
.orElse(0);
// 累加数量
goodsTotalMap.merge(goodsId, count, Integer::sum);
} }
}); });
} catch (Exception e) { } catch (Exception e) {
@ -142,16 +145,21 @@ public class MatterController {
// 查询物资列表 // 查询物资列表
List<Matter> matterList = matterService.list(); List<Matter> matterList = matterService.list();
// 以数据库为准、过滤并构建结果 // 先将 matterList 转换为 ID -> Name 的映射Map、以提高查询效率
Map<String, Integer> resultMap = matterList.stream() Map<String, String> matterIdToNameMap = matterList.stream()
.map(Matter::getName) // 提取所有标准物资名称 .collect(Collectors.toMap(Matter::getId, Matter::getName));
.filter(goodsTotalMap::containsKey) // 只保留在统计结果中存在的物资
.collect(Collectors.toMap(
Function.identity(), // Key 为物资名称
goodsTotalMap::get // Value 为对应的数量
));
// 返回与标准物资匹配的统计结果 // 高效地将 goodsTotalMap 转换为 resultMap
Map<String, Integer> resultMap = new HashMap<>();
goodsTotalMap.forEach((goodsId, count) -> {
// 从映射Map中直接根据ID获取名称、时间复杂度为 O(1)
String goodsName = matterIdToNameMap.get(goodsId);
if (goodsName != null) {
resultMap.put(goodsName, count);
}
});
// 返回结果
return ApiResponse.success(resultMap); return ApiResponse.success(resultMap);
} }

View File

@ -174,8 +174,8 @@ public class ModelLibraryController {
@Operation(summary = "模型类型列表") @Operation(summary = "模型类型列表")
@GetMapping("/modelTypeList") @GetMapping("/modelTypeList")
public ApiResponse modelTypeTree() throws SQLException, IllegalAccessException, InstantiationException { public ApiResponse modelTypeTree(@Parameter(description = "模型名称") @RequestParam("modelName") String modelName) throws SQLException, IllegalAccessException, InstantiationException {
return ApiResponse.success(modelTypeList()); return ApiResponse.success(modelTypeList(modelName));
} }
@Operation(summary = "添加模型文件") @Operation(summary = "添加模型文件")
@ -440,25 +440,40 @@ public class ModelLibraryController {
SQLiteUtil.executeUpdate(modelPath, "UPDATE model_type SET parent_id = ?, tree_index = ? WHERE id = ?", params); SQLiteUtil.executeUpdate(modelPath, "UPDATE model_type SET parent_id = ?, tree_index = ? WHERE id = ?", params);
} }
// 返回树列表 // 返回树列表
return ApiResponse.success(modelTypeList()); return ApiResponse.success(modelTypeList(null));
} }
private List<ModelTypeVo> modelTypeList() throws SQLException, IllegalAccessException, InstantiationException { private List<ModelTypeVo> modelTypeList(String modelName) throws SQLException, IllegalAccessException, InstantiationException {
String modelPath = getModelLibrary(); String modelPath = getModelLibrary();
if (modelPath == null) { if (modelPath == null) {
return null; return null;
} }
String sql = """
StringBuilder sqlBuilder = new StringBuilder("""
SELECT id, name, parent_id as parentId, SELECT id, name, parent_id as parentId,
tree_index as treeIndex, created_at as createdAt, tree_index as treeIndex, created_at as createdAt,
updated_at as updatedAt FROM model_type ORDER BY tree_index ASC updated_at as updatedAt
"""; FROM model_type
// 查询所有模型的类型 """);
List<ModelType> modelTypes = SQLiteUtil.queryForList(modelPath, sql, null, ModelType.class);
if (modelName != null && !modelName.trim().isEmpty()) {
sqlBuilder.append("WHERE name LIKE '%").append(modelName.trim()).append("%' ");
}
// 为所有查询都加上统一的排序条件
sqlBuilder.append("ORDER BY tree_index ASC");
// 执行查询
List<ModelType> modelTypes = SQLiteUtil.queryForList(
modelPath,
sqlBuilder.toString(),
null,
ModelType.class
);
// 转换为树形结构 // 转换为树形结构
return buildModelTypeTree(modelTypes); return buildModelTypeTree(modelTypes);
} }
private List<ModelTypeVo> buildModelTypeTree(List<ModelType> modelTypes) { private List<ModelTypeVo> buildModelTypeTree(List<ModelType> modelTypes) {
List<ModelTypeVo> treeNodes = modelTypes.stream() List<ModelTypeVo> treeNodes = modelTypes.stream()
.map(modelType -> new ModelTypeVo(modelType)) .map(modelType -> new ModelTypeVo(modelType))