From d3931a9ddd05f0bd183c90189a8a086e80b6e127 Mon Sep 17 00:00:00 2001 From: ZZX9599 <536509593@qq.com> Date: Mon, 24 Nov 2025 14:21:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../business/controller/MatterController.java | 36 ++++++++++------- .../controller/ModelLibraryController.java | 39 +++++++++++++------ 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/yj/earth/business/controller/MatterController.java b/src/main/java/com/yj/earth/business/controller/MatterController.java index be8e2d4..60fef41 100644 --- a/src/main/java/com/yj/earth/business/controller/MatterController.java +++ b/src/main/java/com/yj/earth/business/controller/MatterController.java @@ -88,14 +88,12 @@ public class MatterController { @Parameter(description = "物资名称") @RequestParam(value = "name", required = false) String name) { Map goodsTotalMap = getGoodCountMap(); - // 分页查询物资表的数据 LambdaQueryWrapper matterWrapper = new LambdaQueryWrapper<>(); if (StringUtils.isNotBlank(name)) { matterWrapper.like(Matter::getName, name); } Page matterPage = matterService.page(new Page<>(pageNum, pageSize), matterWrapper); - // 用统计出的数量替换分页结果中的数量 if (!CollectionUtils.isEmpty(matterPage.getRecords())) { matterPage.getRecords().forEach(matter -> { @@ -117,6 +115,7 @@ public class MatterController { LambdaQueryWrapper sourceWrapper = new LambdaQueryWrapper<>(); sourceWrapper.in(Source::getId, pointIdList); List sourceList = sourceService.list(sourceWrapper); + // 初始化物资统计Map: 用于存储 "物资ID -> 总数量" 的映射关系 Map goodsTotalMap = new HashMap<>(); if (!CollectionUtils.isEmpty(sourceList)) { for (Source source : sourceList) { @@ -129,9 +128,13 @@ public class MatterController { .ifPresent(contentList -> { for (Point.Attribute.Goods.GoodsContent goodsContent : contentList) { if (goodsContent == null) continue; - String goodsName = Optional.ofNullable(goodsContent.getName()).orElse("未知物资"); - int count = Optional.ofNullable(goodsContent.getCnt()).map(Integer::valueOf).orElse(0); - goodsTotalMap.merge(goodsName, count, Integer::sum); + String goodsId = Optional.ofNullable(goodsContent.getId()) + .orElse("未知物资ID"); + int count = Optional.ofNullable(goodsContent.getCnt()) + .map(Integer::valueOf) + .orElse(0); + // 累加数量 + goodsTotalMap.merge(goodsId, count, Integer::sum); } }); } catch (Exception e) { @@ -142,16 +145,21 @@ public class MatterController { // 查询物资列表 List matterList = matterService.list(); - // 以数据库为准、过滤并构建结果 - Map resultMap = matterList.stream() - .map(Matter::getName) // 提取所有标准物资名称 - .filter(goodsTotalMap::containsKey) // 只保留在统计结果中存在的物资 - .collect(Collectors.toMap( - Function.identity(), // Key 为物资名称 - goodsTotalMap::get // Value 为对应的数量 - )); + // 先将 matterList 转换为 ID -> Name 的映射Map、以提高查询效率 + Map matterIdToNameMap = matterList.stream() + .collect(Collectors.toMap(Matter::getId, Matter::getName)); - // 返回与标准物资匹配的统计结果 + // 高效地将 goodsTotalMap 转换为 resultMap + Map 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); } diff --git a/src/main/java/com/yj/earth/business/controller/ModelLibraryController.java b/src/main/java/com/yj/earth/business/controller/ModelLibraryController.java index dda521b..dcc1666 100644 --- a/src/main/java/com/yj/earth/business/controller/ModelLibraryController.java +++ b/src/main/java/com/yj/earth/business/controller/ModelLibraryController.java @@ -174,8 +174,8 @@ public class ModelLibraryController { @Operation(summary = "模型类型列表") @GetMapping("/modelTypeList") - public ApiResponse modelTypeTree() throws SQLException, IllegalAccessException, InstantiationException { - return ApiResponse.success(modelTypeList()); + public ApiResponse modelTypeTree(@Parameter(description = "模型名称") @RequestParam("modelName") String modelName) throws SQLException, IllegalAccessException, InstantiationException { + return ApiResponse.success(modelTypeList(modelName)); } @Operation(summary = "添加模型文件") @@ -440,25 +440,40 @@ public class ModelLibraryController { 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 modelTypeList() throws SQLException, IllegalAccessException, InstantiationException { + private List modelTypeList(String modelName) throws SQLException, IllegalAccessException, InstantiationException { String modelPath = getModelLibrary(); if (modelPath == null) { return null; } - String sql = """ - SELECT id, name, parent_id as parentId, - tree_index as treeIndex, created_at as createdAt, - updated_at as updatedAt FROM model_type ORDER BY tree_index ASC - """; - // 查询所有模型的类型 - List modelTypes = SQLiteUtil.queryForList(modelPath, sql, null, ModelType.class); + + StringBuilder sqlBuilder = new StringBuilder(""" + SELECT id, name, parent_id as parentId, + tree_index as treeIndex, created_at as createdAt, + updated_at as updatedAt + FROM model_type + """); + + if (modelName != null && !modelName.trim().isEmpty()) { + sqlBuilder.append("WHERE name LIKE '%").append(modelName.trim()).append("%' "); + } + + // 为所有查询都加上统一的排序条件 + sqlBuilder.append("ORDER BY tree_index ASC"); + + // 执行查询 + List modelTypes = SQLiteUtil.queryForList( + modelPath, + sqlBuilder.toString(), + null, + ModelType.class + ); + // 转换为树形结构 return buildModelTypeTree(modelTypes); } - private List buildModelTypeTree(List modelTypes) { List treeNodes = modelTypes.stream() .map(modelType -> new ModelTypeVo(modelType))