|
@@ -0,0 +1,188 @@
|
|
|
|
|
+package com.etotem.cfc.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.etotem.cfc.entity.*;
|
|
|
|
|
+import com.etotem.cfc.mapper.*;
|
|
|
|
|
+import com.etotem.cfc.service.RecommendService;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+public class RecommendServiceImpl implements RecommendService {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired private ProfileSnapshotMapper snapshotMapper;
|
|
|
|
|
+ @Autowired private ArticleMapper articleMapper;
|
|
|
|
|
+ @Autowired private ActivityMapper activityMapper;
|
|
|
|
|
+ @Autowired private TaskMapper taskMapper;
|
|
|
|
|
+ @Autowired private ProductMapper productMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Map<String, Object> getRecommendations(Long memberId) {
|
|
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
|
+
|
|
|
|
|
+ LambdaQueryWrapper<ProfileSnapshot> qw = new LambdaQueryWrapper<>();
|
|
|
|
|
+ qw.eq(ProfileSnapshot::getMemberId, memberId);
|
|
|
|
|
+ ProfileSnapshot snapshot = snapshotMapper.selectOne(qw);
|
|
|
|
|
+ if (snapshot == null) {
|
|
|
|
|
+ result.put("tasks", Collections.emptyList());
|
|
|
|
|
+ result.put("articles", Collections.emptyList());
|
|
|
|
|
+ result.put("activities", Collections.emptyList());
|
|
|
|
|
+ result.put("products", Collections.emptyList());
|
|
|
|
|
+ result.put("matched_tags", Collections.emptyList());
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject body = JSON.parseObject(snapshot.getBodyMetrics());
|
|
|
|
|
+ JSONObject mind = JSON.parseObject(snapshot.getMindMetrics());
|
|
|
|
|
+ JSONObject wisdom = JSON.parseObject(snapshot.getWisdomMetrics());
|
|
|
|
|
+ JSONObject action = JSON.parseObject(snapshot.getActionMetrics());
|
|
|
|
|
+ List<String> domains = JSON.parseArray(snapshot.getProblemDomains(), String.class);
|
|
|
|
|
+ if (domains == null) domains = Collections.emptyList();
|
|
|
|
|
+
|
|
|
|
|
+ Set<String> matchedTags = new LinkedHashSet<>();
|
|
|
|
|
+ List<String> taskTags = new ArrayList<>();
|
|
|
|
|
+ List<String> articleTags = new ArrayList<>();
|
|
|
|
|
+ List<String> activityTags = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ // 规则匹配
|
|
|
|
|
+ if (body.getDoubleValue("sleep_dur_avg") < 8) {
|
|
|
|
|
+ matchedTags.add("sleep_deficit");
|
|
|
|
|
+ taskTags.add("睡眠");
|
|
|
|
|
+ articleTags.add("睡眠");
|
|
|
|
|
+ }
|
|
|
|
|
+ if ((body.getInteger("exercise_count_week") != null ? body.getInteger("exercise_count_week") : 99) < 2) {
|
|
|
|
|
+ matchedTags.add("low_activity");
|
|
|
|
|
+ taskTags.add("运动");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (mind.getDoubleValue("stress_avg") > 6) {
|
|
|
|
|
+ matchedTags.add("high_stress");
|
|
|
|
|
+ articleTags.add("压力");
|
|
|
|
|
+ activityTags.add("冥想");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (mind.getDoubleValue("emotion_joy_ratio") < 0.3) {
|
|
|
|
|
+ matchedTags.add("low_mood");
|
|
|
|
|
+ articleTags.add("情绪");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (wisdom.getInteger("attention_score") != null && wisdom.getInteger("attention_score") < 60) {
|
|
|
|
|
+ matchedTags.add("attention_weak");
|
|
|
|
|
+ taskTags.add("注意力");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (action.getDoubleValue("task_completion_rate") < 0.4) {
|
|
|
|
|
+ matchedTags.add("task_avoidance");
|
|
|
|
|
+ taskTags.add("入门");
|
|
|
|
|
+ }
|
|
|
|
|
+ for (String domain : domains) {
|
|
|
|
|
+ matchedTags.add(domain + "_focus");
|
|
|
|
|
+ if (domain.equals("sleep")) { articleTags.add("睡眠"); }
|
|
|
|
|
+ if (domain.equals("attention")) { taskTags.add("注意力"); }
|
|
|
|
|
+ if (domain.equals("emotion")) { articleTags.add("情绪"); }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ result.put("matched_tags", new ArrayList<>(matchedTags));
|
|
|
|
|
+ result.put("tasks", queryTasks(taskTags));
|
|
|
|
|
+ result.put("articles", queryArticles(articleTags));
|
|
|
|
|
+ result.put("activities", queryActivities(activityTags));
|
|
|
|
|
+ result.put("products", queryProducts());
|
|
|
|
|
+
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<Map<String, Object>> queryTasks(List<String> tags) {
|
|
|
|
|
+ List<Map<String, Object>> list = new ArrayList<>();
|
|
|
|
|
+ for (String tag : tags) {
|
|
|
|
|
+ LambdaQueryWrapper<Task> qw = new LambdaQueryWrapper<>();
|
|
|
|
|
+ qw.like(Task::getCategory, tag).or().like(Task::getDescription, tag)
|
|
|
|
|
+ .eq(Task::getStatus, "pending")
|
|
|
|
|
+ .last("LIMIT 5");
|
|
|
|
|
+ List<Task> tasks = taskMapper.selectList(qw);
|
|
|
|
|
+ for (Task t : tasks) {
|
|
|
|
|
+ Map<String, Object> item = new HashMap<>();
|
|
|
|
|
+ item.put("id", t.getId());
|
|
|
|
|
+ item.put("title", t.getTitle());
|
|
|
|
|
+ item.put("category", t.getCategory());
|
|
|
|
|
+ item.put("type", "task");
|
|
|
|
|
+ list.add(item);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ deduplicate(list);
|
|
|
|
|
+ return list;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<Map<String, Object>> queryArticles(List<String> tags) {
|
|
|
|
|
+ List<Map<String, Object>> list = new ArrayList<>();
|
|
|
|
|
+ for (String tag : tags) {
|
|
|
|
|
+ LambdaQueryWrapper<Article> qw = new LambdaQueryWrapper<>();
|
|
|
|
|
+ qw.and(w -> w.like(Article::getTags, tag).or().like(Article::getTitle, tag))
|
|
|
|
|
+ .eq(Article::getAuditStatus, "approved")
|
|
|
|
|
+ .eq(Article::getStatus, "published")
|
|
|
|
|
+ .orderByDesc(Article::getPublishedAt)
|
|
|
|
|
+ .last("LIMIT 5");
|
|
|
|
|
+ List<Article> articles = articleMapper.selectList(qw);
|
|
|
|
|
+ for (Article a : articles) {
|
|
|
|
|
+ Map<String, Object> item = new HashMap<>();
|
|
|
|
|
+ item.put("id", a.getId());
|
|
|
|
|
+ item.put("title", a.getTitle());
|
|
|
|
|
+ item.put("summary", a.getSummary());
|
|
|
|
|
+ item.put("coverImage", a.getCoverImage());
|
|
|
|
|
+ item.put("type", "article");
|
|
|
|
|
+ list.add(item);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ deduplicate(list);
|
|
|
|
|
+ return list;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<Map<String, Object>> queryActivities(List<String> tags) {
|
|
|
|
|
+ List<Map<String, Object>> list = new ArrayList<>();
|
|
|
|
|
+ for (String tag : tags) {
|
|
|
|
|
+ LambdaQueryWrapper<Activity> qw = new LambdaQueryWrapper<>();
|
|
|
|
|
+ qw.and(w -> w.like(Activity::getTitle, tag).or().like(Activity::getDescription, tag))
|
|
|
|
|
+ .eq(Activity::getAuditStatus, "approved")
|
|
|
|
|
+ .eq(Activity::getStatus, "published")
|
|
|
|
|
+ .orderByAsc(Activity::getStartTime)
|
|
|
|
|
+ .last("LIMIT 3");
|
|
|
|
|
+ List<Activity> acts = activityMapper.selectList(qw);
|
|
|
|
|
+ for (Activity a : acts) {
|
|
|
|
|
+ Map<String, Object> item = new HashMap<>();
|
|
|
|
|
+ item.put("id", a.getId());
|
|
|
|
|
+ item.put("title", a.getTitle());
|
|
|
|
|
+ item.put("dimensionCode", a.getDimensionCode());
|
|
|
|
|
+ item.put("type", "activity");
|
|
|
|
|
+ list.add(item);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ deduplicate(list);
|
|
|
|
|
+ return list;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<Map<String, Object>> queryProducts() {
|
|
|
|
|
+ List<Map<String, Object>> list = new ArrayList<>();
|
|
|
|
|
+ LambdaQueryWrapper<Product> qw = new LambdaQueryWrapper<>();
|
|
|
|
|
+ qw.eq(Product::getStatus, "on_shelf")
|
|
|
|
|
+ .isNotNull(Product::getGrowthCategory)
|
|
|
|
|
+ .last("LIMIT 8");
|
|
|
|
|
+ List<Product> products = productMapper.selectList(qw);
|
|
|
|
|
+ for (Product p : products) {
|
|
|
|
|
+ Map<String, Object> item = new HashMap<>();
|
|
|
|
|
+ item.put("id", p.getId());
|
|
|
|
|
+ item.put("name", p.getName());
|
|
|
|
|
+ item.put("price", p.getPrice());
|
|
|
|
|
+ item.put("growthCategory", p.getGrowthCategory());
|
|
|
|
|
+ item.put("type", "product");
|
|
|
|
|
+ list.add(item);
|
|
|
|
|
+ }
|
|
|
|
|
+ return list;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void deduplicate(List<Map<String, Object>> list) {
|
|
|
|
|
+ Set<Long> seen = new HashSet<>();
|
|
|
|
|
+ list.removeIf(item -> {
|
|
|
|
|
+ Long id = ((Number) item.get("id")).longValue();
|
|
|
|
|
+ return !seen.add(id);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+}
|