Просмотр исходного кода

feat(S-cfc-383): global.search.query 全局搜索 POST /api/search/global 实现使绿

- SearchController(类级 /api/search + @PostMapping("/global"),统一 Result<T> + @RequestAttribute 取值)
- GlobalSearchService.globalSearch(keyword, userId, familyId, memberId) 聚合四类并分流
  reports/plans/tasks/products;空关键词/无匹配返回空桶不报错;报告/方案按家庭/成员范围过滤越权结果
- 复用既有来源服务:HealthReportService.getFamilyReports / GrowthPlanService.getByMemberId /
  GrowthTaskService.getTaskList / ProductService.list
- 同步 API_REFERENCE §4.59.10 契约缺口登记 + PROJECT-OVERVIEW 对应条目
- 红测试 3 类共 20 用例:18 失败 → 20 全绿
E2E Test Bot 3 дней назад
Родитель
Сommit
8e92400e46

+ 52 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/search/SearchController.java

@@ -0,0 +1,52 @@
+package com.etotem.cfc.controller.search;
+
+import com.etotem.cfc.common.Result;
+import com.etotem.cfc.service.GlobalSearchService;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestAttribute;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+import java.util.Map;
+
+/**
+ * 全局搜索控制器(能力键 {@code global.search.query},SC-P-82 全局搜索)。
+ *
+ * <p>暴露聚合搜索端点 {@code POST /api/search/global}(统一 {@code @PostMapping} 约定),
+ * 聚合 报告/方案/任务/商品 四类结果按类型分流返回;调用者上下文经
+ * {@code @RequestAttribute} 取自 JwtInterceptor(userId/familyId/currentMemberId),
+ * 供越权结果过滤使用(跨家庭结果不返回)。</p>
+ */
+@RestController
+@RequestMapping("/api/search")
+public class SearchController {
+
+    @Resource
+    private GlobalSearchService globalSearchService;
+
+    /**
+     * 全局聚合搜索(报告/方案/任务/商品)。
+     *
+     * <p>请求体(POST):{@code {"keyword": "可选关键词", "page": 1, "pageSize": 10}},
+     * keyword 可空——空关键词返回四类空桶(空结果不报错)。</p>
+     *
+     * @param body            搜索条件(Map 承载 keyword,可空)
+     * @param userId          调用者登录账号(JwtInterceptor 注入)
+     * @param familyId        调用者家庭(越权过滤维度)
+     * @param currentMemberId 调用者成员(越权过滤维度)
+     */
+    @PostMapping("/global")
+    public Result<Map<String, Object>> globalSearch(@RequestBody(required = false) Map<String, Object> body,
+                                                    @RequestAttribute("userId") Long userId,
+                                                    @RequestAttribute(value = "familyId", required = false) Long familyId,
+                                                    @RequestAttribute(value = "currentMemberId", required = false) Long currentMemberId) {
+        String keyword = null;
+        if (body != null && body.get("keyword") != null) {
+            keyword = String.valueOf(body.get("keyword"));
+        }
+        Map<String, Object> data = globalSearchService.globalSearch(keyword, userId, familyId, currentMemberId);
+        return Result.success(data);
+    }
+}

+ 156 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/GlobalSearchService.java

@@ -0,0 +1,156 @@
+package com.etotem.cfc.service;
+
+import com.etotem.cfc.common.Result;
+import com.etotem.cfc.dto.ProductListQueryDTO;
+import com.etotem.cfc.entity.GrowthPlan;
+import com.etotem.cfc.entity.HealthReport;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 全局搜索聚合服务(能力键 {@code global.search.query},SC-P-82 全局搜索)。
+ *
+ * <p>聚合 报告(HealthReportService) / 方案(GrowthPlanService) / 任务(GrowthTaskService) /
+ * 商品(ProductService) 四类来源,结果按类型分流返回(reports/plans/tasks/products 四个桶)。</p>
+ *
+ * <ul>
+ *   <li><b>空结果不报错</b>:关键词为 null / 空串 / 空白或各桶无匹配时,返回空集合而非抛异常;</li>
+ *   <li><b>越权结果过滤</b>:报告/方案按调用者家庭/成员范围过滤,跨家庭结果不返回;</li>
+ *   <li><b>类型分流</b>:返回 Map,四类键各挂其类型结果集合。</li>
+ * </ul>
+ */
+@Service
+public class GlobalSearchService {
+
+    @Resource
+    private HealthReportService healthReportService;
+
+    @Resource
+    private GrowthPlanService growthPlanService;
+
+    @Resource
+    private GrowthTaskService growthTaskService;
+
+    @Resource
+    private ProductService productService;
+
+    /**
+     * 全局聚合搜索入口:报告/方案/任务/商品按类型分流返回。
+     *
+     * @param keyword  搜索关键词(可空,空则返回四类空桶)
+     * @param userId   调用者登录账号
+     * @param familyId 调用者家庭(越权过滤维度;报告桶据此过滤)
+     * @param memberId 调用者成员(越权过滤维度;方案/任务桶据此过滤)
+     */
+    public Map<String, Object> globalSearch(String keyword, Long userId, Long familyId, Long memberId) {
+        Map<String, Object> result = new LinkedHashMap<>();
+        if (keyword == null || keyword.trim().isEmpty()) {
+            // 空结果不报错(SC-P-82):空关键词返回四类空桶
+            result.put("reports", Collections.emptyList());
+            result.put("plans", Collections.emptyList());
+            result.put("tasks", Collections.emptyList());
+            result.put("products", Collections.emptyList());
+            return result;
+        }
+        String kw = keyword.trim().toLowerCase();
+        result.put("reports", searchReports(kw, familyId));
+        result.put("plans", searchPlans(kw, memberId));
+        result.put("tasks", searchTasks(kw, userId));
+        result.put("products", searchProducts(kw, userId));
+        return result;
+    }
+
+    // ------------------------------------------------------------------
+    // 四类来源搜索(复用既有服务,按家庭/成员范围过滤越权结果)
+    // ------------------------------------------------------------------
+
+    private List<HealthReport> searchReports(String kw, Long familyId) {
+        if (familyId == null) {
+            return Collections.emptyList();
+        }
+        List<HealthReport> reports = healthReportService.getFamilyReports(familyId);
+        if (reports == null || reports.isEmpty()) {
+            return Collections.emptyList();
+        }
+        List<HealthReport> matched = new ArrayList<>();
+        for (HealthReport r : reports) {
+            if (containsAny(kw, r.getReportType(), r.getPersonName(), r.getSubjectName(),
+                    r.getReportNumber(), r.getRemark())) {
+                matched.add(r);
+            }
+        }
+        return matched;
+    }
+
+    private List<GrowthPlan> searchPlans(String kw, Long memberId) {
+        if (memberId == null) {
+            return Collections.emptyList();
+        }
+        List<GrowthPlan> plans = growthPlanService.getByMemberId(memberId);
+        if (plans == null || plans.isEmpty()) {
+            return Collections.emptyList();
+        }
+        List<GrowthPlan> matched = new ArrayList<>();
+        for (GrowthPlan p : plans) {
+            if (containsAny(kw, p.getPlanTitle(), p.getPlanContent())) {
+                matched.add(p);
+            }
+        }
+        return matched;
+    }
+
+    private List<Map<String, Object>> searchTasks(String kw, Long userId) {
+        if (userId == null) {
+            return Collections.emptyList();
+        }
+        Map<String, String> params = new HashMap<>();
+        params.put("type", "DAILY");
+        List<Map<String, Object>> tasks = growthTaskService.getTaskList(userId, params);
+        if (tasks == null || tasks.isEmpty()) {
+            return Collections.emptyList();
+        }
+        List<Map<String, Object>> matched = new ArrayList<>();
+        for (Map<String, Object> t : tasks) {
+            for (Object v : t.values()) {
+                if (v != null && v.toString().toLowerCase().contains(kw)) {
+                    matched.add(t);
+                    break;
+                }
+            }
+        }
+        return matched;
+    }
+
+    private List<?> searchProducts(String kw, Long userId) {
+        ProductListQueryDTO query = new ProductListQueryDTO();
+        query.setPage(1);
+        query.setSize(10);
+        query.setKeyword(kw);
+        Result<Map<String, Object>> result = productService.list(query, userId, null);
+        if (result == null || result.getCode() == null || result.getCode() != 200 || result.getData() == null) {
+            return Collections.emptyList();
+        }
+        Object records = result.getData().get("records");
+        if (records instanceof List) {
+            return (List<?>) records;
+        }
+        return Collections.emptyList();
+    }
+
+    /** 大小写不敏感的多字段包含匹配(null 候选自动跳过) */
+    private boolean containsAny(String kw, String... candidates) {
+        for (String c : candidates) {
+            if (c != null && c.toLowerCase().contains(kw)) {
+                return true;
+            }
+        }
+        return false;
+    }
+}

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
docs/superpowers/PROJECT-OVERVIEW.md


+ 3 - 1
docs/superpowers/api/API_REFERENCE.md

@@ -3007,7 +3007,7 @@ W0.2 双配置中心合并:`product_config` 已并入 `service_config`(唯
 
 | 开工包建议路径 | 现状 | 说明 |
 |----------------|------|------|
-| `POST /api/search/global` | **不存在**(无 SearchController) | SC-P-82 `misc.globalSearch` 全局搜索(报告/方案/任务/商品)**未实现**;搜索需求当前分散:`/api/diet/ingredients/search`(食材)、文章/商品搜索各自接口。待实现时在本节登记 |
+| `POST /api/search/global` | ✅ 已实现(2026-11-16,S-cfc-383) | SC-P-82 `misc.globalSearch` 全局搜索落地:`SearchController`(类级 `/api/search` + `@PostMapping("/global")`)→ `GlobalSearchService.globalSearch(keyword, userId, familyId, memberId)` 聚合 报告(`HealthReportService.getFamilyReports` 按关键词过滤)/方案(`GrowthPlanService.getByMemberId`)/任务(`GrowthTaskService.getTaskList`)/商品(`ProductService.list`) 四类按类型分流(reports/plans/tasks/products);空关键词/无匹配返回空桶不报错;报告/方案按家庭/成员范围过滤越权结果。请求体 `{"keyword": "可空"}`,返回 `Result<Map>`(四类键) |
 | `POST /api/provider/report-parse/connect` | **不存在**(无 ProviderController) | SC-S-09 `provider.reportParse.connect` 商品服务商报告解析接入**未实现**;服务商(PROVIDER 角色)当前经 `/api/vendor/*`、`/api/admin/supply-*` 体系管理,无独立报告解析对接端点 |
 | `POST /api/health/report/compare` | ✅ 已实现(2026-11-14,S-cfc-364) | SC-P-24/T-21 `health.report.compare` 报告对比落地:`ReportArchiveCompareService.compareReports(reportAId, reportBId)`(类型一致才可比对,返回报告摘要 + 指标级差异标注 same/different/onlyInA/onlyInB + 总分差异;DAN 两版对比仍走 `/api/dan-report/compare`) |
 | `POST /api/health/report/trend` | **不存在** | SC-P-25 `health.report.trend` 由等价物承载:`/api/health/nutrition/trend` + `/api/food/recommendation/trend`(+ 既有 `/api/indicator/trend`、`/api/cognitive/trend`) |
@@ -3017,6 +3017,8 @@ W0.2 双配置中心合并:`product_config` 已并入 `service_config`(唯
 
 *文档更新:2026-11-06(B-2 报告闭环开工包契约核对补全:4.59.1 报告解析中台 `/api/admin/report-parse/*` + `dan/reparse`;4.59.2 健康报告全链路;4.59.3 代传闭环 `proxy-parse`/`proxy-confirm`;4.59.4 报告权限/分享;4.59.5 DAN 报告模块;4.59.6 趋势与饮食推荐;4.59.7 报告类型链接/检测;4.59.8 报告摘要;4.59.9 LangGraph 依赖登记;4.59.10 契约缺口与等价物映射)*
 
+*文档更新:2026-11-16(S-cfc-383 `global.search.query` SC-P-82:4.59.10 契约缺口 `POST /api/search/global` 由「不存在」更新为「已实现」,登记 `SearchController` + `GlobalSearchService` 四类聚合分流;详见 PROJECT-OVERVIEW 对应条目)*
+
 ---
 
 ### 4.60 平台运营能力(SC-A-01~22、A-25~27,EVD-11 契约核对增补)

Некоторые файлы не были показаны из-за большого количества измененных файлов