|
|
@@ -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;
|
|
|
+ }
|
|
|
+}
|