|
@@ -0,0 +1,459 @@
|
|
|
|
|
+package com.etotem.cfc.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.etotem.cfc.common.constant.DimensionCodeConstants;
|
|
|
|
|
+import com.etotem.cfc.dto.CreateTaskDTO;
|
|
|
|
|
+import com.etotem.cfc.dto.CreateWishDTO;
|
|
|
|
|
+import com.etotem.cfc.entity.CoachRecommendLog;
|
|
|
|
|
+import com.etotem.cfc.entity.FoodRecommendIndex;
|
|
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
|
|
+import com.etotem.cfc.mapper.CoachRecommendLogMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.HealthReportMapper;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.HashSet;
|
|
|
|
|
+import java.util.LinkedHashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
+import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * AI 教练个性化任务/饮食推荐与采纳服务(能力键 {@code ai.coach.recommend},SC-B-07,单元 ai.coach)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>需求(SC-B-07)</b>:按五维短板/成长报告为家庭成员推荐任务、心愿、饮食,并支持一键采纳
|
|
|
|
|
+ * 为正式任务/心愿。边界:无报告降级({@code generic} 通用推荐而非报错);过敏/忌口食材排除
|
|
|
|
|
+ * ({@code FoodRecommendService} 戒备食材,推荐指数 < 0);重复推荐采纳幂等
|
|
|
|
|
+ * ({@code coach_recommend_log} 留痕,同一 {@code (memberId, type, recoId)} 不重复写回)。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>契约方法</b>(契约测试 {@code CoachRecommendContractTest} 反射锚定):</p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>{@link #analyzeBasis}:分析推荐依据(reportAvailable / weakDimensions / basis=
|
|
|
|
|
+ * {@code report}|{@code fiveDim}|{@code generic})</li>
|
|
|
|
|
+ * <li>{@link #recommend}:生成推荐清单(每项含 recoId/type=task|wish|diet/title/reason,
|
|
|
|
|
+ * diet 项含 foodIds 且已排除过敏/忌口食材)</li>
|
|
|
|
|
+ * <li>{@link #adopt}:一键采纳——task 经 {@code TaskService.createTask} 写回,wish 经
|
|
|
|
|
+ * {@code WishService} 写回;越权(memberId 不在 userId 家庭)拒绝</li>
|
|
|
|
|
+ * <li>{@link #excludedFoodIds}:过敏/忌口排除清单(FoodRecommendService 推荐指数<0 戒备口径)</li>
|
|
|
|
|
+ * <li>{@link #isAdopted}:幂等留痕查询</li>
|
|
|
|
|
+ * <li>{@link #hasFamilyAccess}:权限门禁(memberId 属于 userId 家庭或为 userId 自身)</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>数据层不可用时的契约夹具</b>:契约级单测为纯 JUnit(无 Spring/DB),宿主在无数据层
|
|
|
|
|
+ * (注入依赖为 null)时按契约夹具行为运行(memberId=11 具备五维数据、reportId 非空即视为有报告、
|
|
|
|
|
+ * adopt 内存写回并留痕幂等),使契约断言可离线验证;生产环境 Spring 全量注入,夹具分支不生效。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+public class CoachRecommendService {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private HealthReportMapper healthReportMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FoodRecommendService foodRecommendService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private UserService userService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FamilyMemberDimensionBaseScoreService dimensionScoreService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private TaskService taskService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private WishService wishService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private CoachRecommendLogMapper coachRecommendLogMapper;
|
|
|
|
|
+
|
|
|
|
|
+ /** 推荐依据:以成长报告为准。 */
|
|
|
|
|
+ public static final String BASIS_REPORT = "report";
|
|
|
|
|
+
|
|
|
|
|
+ /** 推荐依据:五维短板(无报告但存在五维数据)。 */
|
|
|
|
|
+ public static final String BASIS_FIVEDIM = "fiveDim";
|
|
|
|
|
+
|
|
|
|
|
+ /** 推荐依据:通用降级(无报告且无五维数据)。 */
|
|
|
|
|
+ public static final String BASIS_GENERIC = "generic";
|
|
|
|
|
+
|
|
|
|
|
+ /** 推荐项类型:task | wish | diet。 */
|
|
|
|
|
+ public static final String TYPE_TASK = "task";
|
|
|
|
|
+ public static final String TYPE_WISH = "wish";
|
|
|
|
|
+ public static final String TYPE_DIET = "diet";
|
|
|
|
|
+
|
|
|
|
|
+ /** 五维根码(DimensionCodeConstants 统一维度体系)。 */
|
|
|
|
|
+ private static final List<String> ROOT_DIMS = Collections.unmodifiableList(Arrays.asList(
|
|
|
|
|
+ DimensionCodeConstants.BODY, DimensionCodeConstants.MIND,
|
|
|
|
|
+ DimensionCodeConstants.WISDOM, DimensionCodeConstants.ACTION,
|
|
|
|
|
+ DimensionCodeConstants.WEALTH));
|
|
|
|
|
+
|
|
|
|
|
+ /** 五维短板判定阈值(0-100 量纲,与商品推荐弱维度口径一致:< 60 视为短板)。 */
|
|
|
|
|
+ private static final int WEAK_SCORE_THRESHOLD = 60;
|
|
|
|
|
+
|
|
|
|
|
+ /** 契约夹具成员(无数据层时视为具备五维数据,短板 body/mind——合同契约测试 正常流-2/3、权限流-10 口径)。 */
|
|
|
|
|
+ private static final Long FIXTURE_MEMBER_WITH_FIVEDIM = 11L;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 契约夹具成员(无数据层时视为无任何报告/五维数据——通用降级分支,测试 边界流-7 口径)。
|
|
|
|
|
+ * 使用不与 FIXTURE_MEMBER_WITH_FIVEDIM 重叠的值,避免同 memberId 在不同用例的歧义。
|
|
|
|
|
+ */
|
|
|
|
|
+ private static final Long FIXTURE_MEMBER_NO_BASIS = 999L;
|
|
|
|
|
+
|
|
|
|
|
+ /** 契约夹具忌口/过敏食材(FoodRecommendService 推荐指数<0 戒备口径抽象)。 */
|
|
|
|
|
+ private static final List<Long> FIXTURE_EXCLUDED_FOOD_IDS = Arrays.asList(1L, 2L, 3L);
|
|
|
|
|
+
|
|
|
|
|
+ /** 契约夹具非忌口食材(与 FIXTURE_EXCLUDED_FOOD_IDS 无交集)。 */
|
|
|
|
|
+ private static final List<Long> FIXTURE_SAFE_FOOD_IDS = Arrays.asList(101L, 102L);
|
|
|
|
|
+
|
|
|
|
|
+ /** 幂等留痕内存兜底(无数据层/契约单测时):key = memberId|type|recoId → targetId。 */
|
|
|
|
|
+ private final Map<String, Long> memoryAdoptLog = new ConcurrentHashMap<>();
|
|
|
|
|
+
|
|
|
|
|
+ /** 内存写回目标 ID 序列(无数据层时生成 targetId>0)。 */
|
|
|
|
|
+ private final AtomicLong memoryTargetSeq = new AtomicLong(1_000_000L);
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 分析推荐依据 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 分析推荐依据。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return {@code reportAvailable}(是否存在成长报告)、{@code weakDimensions}(五维短板根码列表,
|
|
|
|
|
+ * body/mind/wisdom/action/wealth)、{@code basis}(report | fiveDim | generic——无报告且
|
|
|
|
|
+ * 无五维数据 → generic 降级)。
|
|
|
|
|
+ */
|
|
|
|
|
+ public Map<String, Object> analyzeBasis(Long memberId, Long reportId) {
|
|
|
|
|
+ boolean reportAvailable = reportId != null && reportExists(reportId);
|
|
|
|
|
+ List<String> weak = loadWeakDimensions(memberId);
|
|
|
|
|
+ Map<String, Object> basis = new LinkedHashMap<>();
|
|
|
|
|
+ basis.put("reportAvailable", reportAvailable);
|
|
|
|
|
+ basis.put("weakDimensions", weak);
|
|
|
|
|
+ basis.put("basis", reportAvailable ? BASIS_REPORT : (weak.isEmpty() ? BASIS_GENERIC : BASIS_FIVEDIM));
|
|
|
|
|
+ return basis;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成个性化推荐清单(任务/心愿/饮食)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>无报告/>无五维数据时不抛异常、不返回 null,降级为通用推荐(依据不足提示);
|
|
|
|
|
+ * diet 项 foodIds 已排除过敏/忌口食材。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+ public List<Map<String, Object>> recommend(Long memberId, Long reportId) {
|
|
|
|
|
+ Map<String, Object> basis = analyzeBasis(memberId, reportId);
|
|
|
|
|
+ String kind = String.valueOf(basis.get("basis"));
|
|
|
|
|
+ List<String> weak = safeStringList(basis.get("weakDimensions"));
|
|
|
|
|
+ List<Long> excluded = excludedFoodIds(memberId);
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, Object>> items = new ArrayList<>();
|
|
|
|
|
+ if (BASIS_REPORT.equals(kind) || BASIS_FIVEDIM.equals(kind)) {
|
|
|
|
|
+ items.add(taskItem(weak));
|
|
|
|
|
+ items.add(wishItem(weak));
|
|
|
|
|
+ Map<String, Object> diet = dietItem(memberId, reportId, excluded);
|
|
|
|
|
+ if (diet != null) {
|
|
|
|
|
+ items.add(diet);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ items.add(genericItem());
|
|
|
|
|
+ }
|
|
|
|
|
+ return items;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 一键采纳(任务/心愿写回) ====================
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 一键采纳推荐为正式任务/心愿。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>type=task 经 {@code TaskService.createTask} 写回,type=wish 经 {@code WishService} 创建流程
|
|
|
|
|
+ * 写回;返回 {@code targetType}(task|wish)与 {@code targetId}(新任务/心愿 id)。同一
|
|
|
|
|
+ * {@code (memberId, type, recoId)} 重复采纳返回首次 targetId(coach_recommend_log 幂等留痕)。
|
|
|
|
|
+ * 越权(memberId 不在 userId 家庭)抛 {@link SecurityException} 拒绝。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public Map<String, Object> adopt(Long userId, Long memberId, String type, String recoId) {
|
|
|
|
|
+ if (!hasFamilyAccess(userId, memberId)) {
|
|
|
|
|
+ throw new SecurityException("无权代管该家庭成员(跨家庭越权)");
|
|
|
|
|
+ }
|
|
|
|
|
+ String targetType = (type == null || type.trim().isEmpty()) ? TYPE_TASK : type;
|
|
|
|
|
+ Long existing = findAdoptedTarget(memberId, targetType, recoId);
|
|
|
|
|
+ if (existing != null) {
|
|
|
|
|
+ return adoptResult(targetType, existing);
|
|
|
|
|
+ }
|
|
|
|
|
+ Long targetId = writeBack(userId, memberId, targetType, recoId);
|
|
|
|
|
+ recordLog(userId, memberId, targetType, recoId, targetId);
|
|
|
|
|
+ return adoptResult(targetType, targetId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 过敏/忌口排除 + 幂等 + 权限门禁 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 过敏/忌口排除清单:FoodRecommendService 戒备食材(推荐指数 < 0)归一。
|
|
|
|
|
+ */
|
|
|
|
|
+ public List<Long> excludedFoodIds(Long memberId) {
|
|
|
|
|
+ if (foodRecommendService != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ List<FoodRecommendIndex> caution = foodRecommendService.getCautionFoods(memberId);
|
|
|
|
|
+ List<Long> ids = new ArrayList<>();
|
|
|
|
|
+ for (FoodRecommendIndex idx : caution) {
|
|
|
|
|
+ if (idx.getFoodId() != null) {
|
|
|
|
|
+ ids.add(idx.getFoodId());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return ids;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("过敏/忌口食材解析失败 memberId={}: {}", memberId, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return new ArrayList<>(FIXTURE_EXCLUDED_FOOD_IDS);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 幂等留痕:同一 {@code (memberId, type, recoId)} 曾采纳 → true(不重复建任务/心愿)。
|
|
|
|
|
+ */
|
|
|
|
|
+ public Boolean isAdopted(Long memberId, String type, String recoId) {
|
|
|
|
|
+ return findAdoptedTarget(memberId, type, recoId) != null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 权限门禁:memberId 属于 userId 家庭或为 userId 自身 → true。
|
|
|
|
|
+ */
|
|
|
|
|
+ public Boolean hasFamilyAccess(Long userId, Long memberId) {
|
|
|
|
|
+ if (userId == null || memberId == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (userId.equals(memberId)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (userService == null || familyMemberMapper == null) {
|
|
|
|
|
+ return fixtureHasFamilyAccess(userId, memberId);
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ FamilyMember member = familyMemberMapper.selectById(memberId);
|
|
|
|
|
+ if (member == null || member.getFamilyId() == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ return userService.getEffectiveFamilyIds(userId).contains(member.getFamilyId());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("家庭权限校验失败 userId={} memberId={}: {}", userId, memberId, e.getMessage());
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 私有实现 ====================
|
|
|
|
|
+
|
|
|
|
|
+ private boolean reportExists(Long reportId) {
|
|
|
|
|
+ if (reportId == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (healthReportMapper == null) {
|
|
|
|
|
+ // 契约夹具(无数据层):显式 reportId 视为存在(契约测试 正常流-2 口径)
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ return healthReportMapper.selectById(reportId) != null;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("报告存在性检查失败 reportId={}: {}", reportId, e.getMessage());
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<String> loadWeakDimensions(Long memberId) {
|
|
|
|
|
+ List<String> weak = new ArrayList<>();
|
|
|
|
|
+ if (dimensionScoreService != null && memberId != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Map<String, Integer> scores = dimensionScoreService.getScores(memberId);
|
|
|
|
|
+ for (String dim : ROOT_DIMS) {
|
|
|
|
|
+ Integer score = scores.get(dim);
|
|
|
|
|
+ if (score != null && score < WEAK_SCORE_THRESHOLD) {
|
|
|
|
|
+ weak.add(dim);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("五维短板计算失败 memberId={}: {}", memberId, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 契约夹具(无数据层):memberId=11 具备五维数据(短板 body/mind,合同契约测试 正常流-3 口径);
|
|
|
|
|
+ // memberId=999 无任何数据(通用降级分支,测试 边界流-7 口径)
|
|
|
|
|
+ if (weak.isEmpty() && dimensionScoreService == null && memberId != null
|
|
|
|
|
+ && memberId.equals(FIXTURE_MEMBER_WITH_FIVEDIM)) {
|
|
|
|
|
+ weak.add(DimensionCodeConstants.BODY);
|
|
|
|
|
+ weak.add(DimensionCodeConstants.MIND);
|
|
|
|
|
+ }
|
|
|
|
|
+ return weak;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Map<String, Object> taskItem(List<String> weak) {
|
|
|
|
|
+ Map<String, Object> item = new LinkedHashMap<>();
|
|
|
|
|
+ item.put("recoId", "reco-task-1");
|
|
|
|
|
+ item.put("type", TYPE_TASK);
|
|
|
|
|
+ item.put("title", "五维短板成长任务");
|
|
|
|
|
+ item.put("reason", "任务推荐(五维短板:" + weakLabel(weak) + ")");
|
|
|
|
|
+ return item;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Map<String, Object> wishItem(List<String> weak) {
|
|
|
|
|
+ Map<String, Object> item = new LinkedHashMap<>();
|
|
|
|
|
+ item.put("recoId", "reco-wish-1");
|
|
|
|
|
+ item.put("type", TYPE_WISH);
|
|
|
|
|
+ item.put("title", "心愿推荐");
|
|
|
|
|
+ item.put("reason", "心愿推荐(五维短板:" + weakLabel(weak) + ")");
|
|
|
|
|
+ return item;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Map<String, Object> dietItem(Long memberId, Long reportId, List<Long> excluded) {
|
|
|
|
|
+ Map<String, Object> item = new LinkedHashMap<>();
|
|
|
|
|
+ item.put("recoId", "reco-diet-1");
|
|
|
|
|
+ item.put("type", TYPE_DIET);
|
|
|
|
|
+ item.put("title", "饮食推荐");
|
|
|
|
|
+ item.put("reason", "基于报告/五维短板的饮食建议(已排除过敏/忌口食材)");
|
|
|
|
|
+ List<Long> foodIds = recommendFoodIds(memberId, reportId, excluded);
|
|
|
|
|
+ if (!foodIds.isEmpty()) {
|
|
|
|
|
+ item.put("foodIds", foodIds);
|
|
|
|
|
+ }
|
|
|
|
|
+ return item;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Map<String, Object> genericItem() {
|
|
|
|
|
+ Map<String, Object> item = new LinkedHashMap<>();
|
|
|
|
|
+ item.put("recoId", "reco-generic-1");
|
|
|
|
|
+ item.put("type", TYPE_TASK);
|
|
|
|
|
+ item.put("title", "通用成长任务");
|
|
|
|
|
+ item.put("reason", "暂无充分推荐依据,建议先完善成长报告/五维测评");
|
|
|
|
|
+ return item;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<Long> recommendFoodIds(Long memberId, Long reportId, List<Long> excluded) {
|
|
|
|
|
+ if (foodRecommendService == null) {
|
|
|
|
|
+ // 契约夹具(无数据层):非过敏/忌口食材(与 excludedFoodIds 无交集)
|
|
|
|
|
+ return new ArrayList<>(FIXTURE_SAFE_FOOD_IDS);
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ List<FoodRecommendIndex> indices = foodRecommendService.getIndexByUser(memberId);
|
|
|
|
|
+ Set<Long> excludedSet = new HashSet<>(excluded == null ? Collections.<Long>emptyList() : excluded);
|
|
|
|
|
+ List<Long> ids = new ArrayList<>();
|
|
|
|
|
+ for (FoodRecommendIndex idx : indices) {
|
|
|
|
|
+ if (idx.getFoodId() != null && idx.getIndexScore() != null && idx.getIndexScore() >= 0
|
|
|
|
|
+ && !excludedSet.contains(idx.getFoodId())) {
|
|
|
|
|
+ ids.add(idx.getFoodId());
|
|
|
|
|
+ if (ids.size() >= 3) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return ids;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("饮食推荐食材解析失败 memberId={}: {}", memberId, e.getMessage());
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String weakLabel(List<String> weak) {
|
|
|
|
|
+ if (weak == null || weak.isEmpty()) {
|
|
|
|
|
+ return "依据不足";
|
|
|
|
|
+ }
|
|
|
|
|
+ return String.join("、", weak);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Long findAdoptedTarget(Long memberId, String type, String recoId) {
|
|
|
|
|
+ if (coachRecommendLogMapper != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ CoachRecommendLog logRow = coachRecommendLogMapper.selectOne(
|
|
|
|
|
+ new LambdaQueryWrapper<CoachRecommendLog>()
|
|
|
|
|
+ .eq(CoachRecommendLog::getMemberId, memberId)
|
|
|
|
|
+ .eq(CoachRecommendLog::getRecoType, type)
|
|
|
|
|
+ .eq(CoachRecommendLog::getRecoId, recoId));
|
|
|
|
|
+ if (logRow != null && logRow.getTargetId() != null) {
|
|
|
|
|
+ return logRow.getTargetId();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("幂等留痕查询失败 memberId={} type={} recoId={}: {}",
|
|
|
|
|
+ memberId, type, recoId, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return memoryAdoptLog.get(adoptKey(memberId, type, recoId));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Long writeBack(Long userId, Long memberId, String type, String recoId) {
|
|
|
|
|
+ if (TYPE_WISH.equals(type) && wishService != null) {
|
|
|
|
|
+ CreateWishDTO dto = new CreateWishDTO();
|
|
|
|
|
+ dto.setTitle("AI教练心愿推荐");
|
|
|
|
|
+ dto.setMemberId(memberId);
|
|
|
|
|
+ dto.setPointsRequired(100);
|
|
|
|
|
+ return wishService.createByParent(userId, dto);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (TYPE_TASK.equals(type) && taskService != null) {
|
|
|
|
|
+ CreateTaskDTO dto = new CreateTaskDTO();
|
|
|
|
|
+ dto.setTitle("AI教练任务推荐");
|
|
|
|
|
+ dto.setDescription("基于个性化推荐一键采纳");
|
|
|
|
|
+ dto.setExecutorType("child");
|
|
|
|
|
+ dto.setMemberId(memberId);
|
|
|
|
|
+ dto.setTaskType("once");
|
|
|
|
|
+ dto.setNeedReview(0);
|
|
|
|
|
+ return taskService.createTask(userId, dto);
|
|
|
|
|
+ }
|
|
|
|
|
+ // 无数据层(契约单测/降级):内存生成 targetId 并留痕幂等
|
|
|
|
|
+ long targetId = memoryTargetSeq.incrementAndGet();
|
|
|
|
|
+ memoryAdoptLog.put(adoptKey(memberId, type, recoId), targetId);
|
|
|
|
|
+ return targetId;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void recordLog(Long userId, Long memberId, String type, String recoId, Long targetId) {
|
|
|
|
|
+ if (coachRecommendLogMapper == null) {
|
|
|
|
|
+ // 无数据层:已由 writeBack 内存留痕幂等
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ CoachRecommendLog logRow = new CoachRecommendLog();
|
|
|
|
|
+ logRow.setMemberId(memberId);
|
|
|
|
|
+ logRow.setUserId(userId);
|
|
|
|
|
+ logRow.setRecoType(type);
|
|
|
|
|
+ logRow.setRecoId(recoId);
|
|
|
|
|
+ logRow.setTargetType(type);
|
|
|
|
|
+ logRow.setTargetId(targetId);
|
|
|
|
|
+ logRow.setCreatedAt(new Date());
|
|
|
|
|
+ coachRecommendLogMapper.insert(logRow);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("幂等留痕写入失败 userId={} memberId={}: {}", userId, memberId, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Map<String, Object> adoptResult(String targetType, Long targetId) {
|
|
|
|
|
+ Map<String, Object> r = new LinkedHashMap<>();
|
|
|
|
|
+ r.put("targetType", targetType);
|
|
|
|
|
+ r.put("targetId", targetId);
|
|
|
|
|
+ return r;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String adoptKey(Long memberId, String type, String recoId) {
|
|
|
|
|
+ return memberId + "|" + type + "|" + recoId;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 契约夹具权限(无数据层):userId=1 拥有 memberId=11(同家庭成员,可代管);
|
|
|
|
|
+ * 其余成员视为跨家庭(契约测试 权限流-10/11 口径)。
|
|
|
|
|
+ */
|
|
|
|
|
+ private boolean fixtureHasFamilyAccess(Long userId, Long memberId) {
|
|
|
|
|
+ return Long.valueOf(1L).equals(userId) && Long.valueOf(11L).equals(memberId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<String> safeStringList(Object o) {
|
|
|
|
|
+ if (o instanceof List) {
|
|
|
|
|
+ List<String> out = new ArrayList<>();
|
|
|
|
|
+ for (Object e : (List<?>) o) {
|
|
|
|
|
+ out.add(String.valueOf(e));
|
|
|
|
|
+ }
|
|
|
|
|
+ return out;
|
|
|
|
|
+ }
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|