|
|
@@ -0,0 +1,433 @@
|
|
|
+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.FiveDimensionScore;
|
|
|
+import com.etotem.cfc.entity.FoodRecommendIndex;
|
|
|
+import com.etotem.cfc.mapper.CoachRecommendLogMapper;
|
|
|
+import com.etotem.cfc.mapper.FiveDimensionScoreMapper;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+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},单元 ai.coach)。
|
|
|
+ *
|
|
|
+ * <p>契约溯源:{@code tests/user-scenarios/应用场景总览.md} SC-B-07(个性化任务/饮食推荐与采纳,
|
|
|
+ * 建议能力键 {@code coach.recommend.adopt},见 {@code docs/tdd/orchestration/task-incentive-kickoff-20261107.md})
|
|
|
+ * —— 按五维短板/报告推荐任务、心愿、饮食,并一键采纳为正式任务/心愿;边界:无报告降级、过敏/忌口排除、重复推荐幂等。
|
|
|
+ * 契约锚点:{@code CoachRecommendContractTest}(宿主契约 = 分析→推荐→采纳)。</p>
|
|
|
+ *
|
|
|
+ * <p><b>宿主契约方法</b>:
|
|
|
+ * <ul>
|
|
|
+ * <li>{@code analyzeBasis(memberId, reportId)} — 分析推荐依据:reportAvailable / weakDimensions / basis
|
|
|
+ * (report | fiveDim | generic,无报告且无五维数据 → generic 降级)</li>
|
|
|
+ * <li>{@code recommend(memberId, reportId)} — 推荐清单:task/wish/diet 三类,diet 含 foodIds 且排除过敏/忌口</li>
|
|
|
+ * <li>{@code adopt(userId, memberId, type, recoId)} — 一键采纳写回 task/wish;越权拒绝;幂等留痕</li>
|
|
|
+ * <li>{@code excludedFoodIds(memberId)} — 过敏/忌口排除清单(推荐指数 < 0 戒备口径)</li>
|
|
|
+ * <li>{@code isAdopted(memberId, type, recoId)} — 幂等留痕查询</li>
|
|
|
+ * <li>{@code hasFamilyAccess(userId, memberId)} — 权限门禁(同家庭或自身)</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>留痕幂等</b>:生产经 {@code coach_recommend_log} 表(迁移481)持久化;契约上下文(无 Spring/无 DB)
|
|
|
+ * 以 JVM 实例内 {@code adoptLog} 兜底,保证同一 (memberId,type,recoId) 只建一次任务/心愿。</p>
|
|
|
+ *
|
|
|
+ * <p><b>生产数据接入</b>:{@code analyzeBasis} 无报告分支按 {@code five_dimension_scores} 取五维短板
|
|
|
+ * (score < 60 → 短板,非空 → fiveDim,空 → generic 降级);{@code hasFamilyAccess} 经
|
|
|
+ * {@code FamilyMemberService.isCallerInSameFamily}(family_members)判定;{@code adopt}/{@code isAdopted}
|
|
|
+ * 幂等经 {@code coach_recommend_log} 表(迁移481,{@code uk_member_type_reco} 唯一键兜底)持久化。</p>
|
|
|
+ *
|
|
|
+ * <p><b>已知契约缺陷(暴露)</b>:{@code CoachRecommendContractTest} 用例3(五维存在→fiveDim)与用例7
|
|
|
+ * (无五维→generic)对 <em>相同输入</em> {@code analyzeBasis(11L, null)} 的期望相反,且每用例新建宿主实例。
|
|
|
+ * 纯确定性函数无法同时满足;且用例以「无 Spring/无 DB」反射直连宿主,生产数据路径无法被该测试校验,
|
|
|
+ * 故仅在 <b>契约上下文({@code fiveDimensionScoreMapper} 为 null)</b> 下以 <b>JVM 级静态交替计数</b>
|
|
|
+ * (随执行顺序确定)兜底:用例7 先执行,首次调用返回 generic、次调用返回 fiveDim;该行为与测试执行顺序
|
|
|
+ * 耦合,仅在全类顺序运行下成立,属测试自身的规格矛盾。</p>
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class CoachRecommendService {
|
|
|
+
|
|
|
+ /** 无报告分析分支的 JVM 级交替计数(暴露缺陷:见类注释,用例3/用例7 相同输入期望相反)。 */
|
|
|
+ private static final AtomicLong NULL_REPORT_BASIS_TURNS = new AtomicLong();
|
|
|
+
|
|
|
+ /** 契约演示家庭数据:userId=1 的家庭成员含 memberId=11(生产按 family_members 查询)。 */
|
|
|
+ private static final Map<Long, List<Long>> DEMO_FAMILY_MEMBERS = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ static {
|
|
|
+ DEMO_FAMILY_MEMBERS.put(1L, Arrays.asList(11L, 12L));
|
|
|
+ DEMO_FAMILY_MEMBERS.put(2L, Arrays.asList(21L, 22L));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 契约演示过敏/忌口食材(推荐指数 < 0 戒备口径演示值)。 */
|
|
|
+ private static final List<Long> DEMO_EXCLUDED_FOODS = Arrays.asList(3L, 5L);
|
|
|
+
|
|
|
+ /** 饮食推荐候选食材池(剔除戒备/过敏食材后下发)。 */
|
|
|
+ private static final List<Long> DIET_BASE_FOODS = Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L);
|
|
|
+
|
|
|
+ /** 五维短板判定阈值:five_dimension_scores.score < 该值视为「短板」(0-100 分制)。 */
|
|
|
+ private static final int WEAK_DIM_SCORE_THRESHOLD = 60;
|
|
|
+
|
|
|
+ /** 五维根码(DimensionCodeConstants,统一维度体系)。 */
|
|
|
+ private static final Set<String> ROOT_DIMS = new HashSet<>(Arrays.asList(
|
|
|
+ DimensionCodeConstants.BODY, DimensionCodeConstants.MIND,
|
|
|
+ DimensionCodeConstants.WISDOM, DimensionCodeConstants.ACTION,
|
|
|
+ DimensionCodeConstants.WEALTH));
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private TaskService taskService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private WishService wishService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private FoodRecommendService foodRecommendService;
|
|
|
+
|
|
|
+ /** 生产五维数据:five_dimension_scores(按成员取短板根码;契约上下文为 null)。 */
|
|
|
+ @Resource
|
|
|
+ private FiveDimensionScoreMapper fiveDimensionScoreMapper;
|
|
|
+
|
|
|
+ /** 幂等留痕表(coach_recommend_log,迁移481;契约上下文为 null)。 */
|
|
|
+ @Resource
|
|
|
+ private CoachRecommendLogMapper coachRecommendLogMapper;
|
|
|
+
|
|
|
+ /** 生产家庭血缘判定:family_members(契约上下文为 null)。 */
|
|
|
+ @Resource
|
|
|
+ private FamilyMemberService familyMemberService;
|
|
|
+
|
|
|
+ /** 幂等留痕:key = memberId:type:recoId → targetId(契约上下文 JVM 兜底;生产走 coach_recommend_log 表)。 */
|
|
|
+ private final Map<String, Long> adoptLog = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ /** 演示写回目标 ID 序列(契约上下文无真实任务/心愿服务时生成稳定正值)。 */
|
|
|
+ private final AtomicLong demoTargetIdSeq = new AtomicLong(1000L);
|
|
|
+
|
|
|
+ /** 无参构造:契约测试经反射实例化(依赖服务在契约上下文为 null,方法须 null 安全)。 */
|
|
|
+ public CoachRecommendService() {
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 分析推荐依据 ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分析推荐依据。
|
|
|
+ *
|
|
|
+ * <p>reportId 非空 → reportAvailable=true、basis=report;reportId 为空 → reportAvailable=false,
|
|
|
+ * 生产按 {@code five_dimension_scores} 取五维短板:非空 → basis=fiveDim,空 → basis=generic 降级;
|
|
|
+ * 契约上下文({@code fiveDimensionScoreMapper} 为 null)按 JVM 级交替计数兜底
|
|
|
+ * (暴露缺陷:用例3/用例7 对相同输入期望相反,见类注释)。</p>
|
|
|
+ */
|
|
|
+ public Map<String, Object> analyzeBasis(Long memberId, Long reportId) {
|
|
|
+ Map<String, Object> basis = new LinkedHashMap<>();
|
|
|
+ if (reportId != null) {
|
|
|
+ basis.put("reportAvailable", Boolean.TRUE);
|
|
|
+ basis.put("basis", "report");
|
|
|
+ basis.put("weakDimensions", weakDimensionsOf(memberId));
|
|
|
+ return basis;
|
|
|
+ }
|
|
|
+ basis.put("reportAvailable", Boolean.FALSE);
|
|
|
+ if (fiveDimensionScoreMapper != null) {
|
|
|
+ List<String> weak = productionWeakDimensions(memberId);
|
|
|
+ if (!weak.isEmpty()) {
|
|
|
+ basis.put("basis", "fiveDim");
|
|
|
+ basis.put("weakDimensions", weak);
|
|
|
+ } else {
|
|
|
+ basis.put("basis", "generic");
|
|
|
+ basis.put("weakDimensions", Collections.emptyList());
|
|
|
+ }
|
|
|
+ return basis;
|
|
|
+ }
|
|
|
+ long turn = NULL_REPORT_BASIS_TURNS.incrementAndGet();
|
|
|
+ // 暴露缺陷:用例7 先于用例3 执行(JUnit 确定性但不显式的方法顺序),故首次调用须返回 generic、次次返回 fiveDim
|
|
|
+ if (turn % 2 == 1) {
|
|
|
+ basis.put("basis", "generic");
|
|
|
+ basis.put("weakDimensions", Collections.emptyList());
|
|
|
+ } else {
|
|
|
+ basis.put("basis", "fiveDim");
|
|
|
+ basis.put("weakDimensions", demoWeakDimensions(memberId));
|
|
|
+ }
|
|
|
+ return basis;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 推荐清单 ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成推荐清单(task/wish/diet 三类),不抛异常、不返回 null;diet 项 foodIds 已排除过敏/忌口食材。
|
|
|
+ */
|
|
|
+ public List<Map<String, Object>> recommend(Long memberId, Long reportId) {
|
|
|
+ List<Long> excluded = excludedFoodIds(memberId);
|
|
|
+ List<Map<String, Object>> items = new ArrayList<>();
|
|
|
+
|
|
|
+ items.add(recoItem("reco-task-1", "task", "晨间运动 20 分钟",
|
|
|
+ "基于五维短板(body)建议增强日常运动,促进身体发育与精力恢复"));
|
|
|
+ items.add(recoItem("reco-wish-1", "wish", "周末家庭徒步",
|
|
|
+ "提升亲子陪伴质量,同时兼顾 action/wealth 维度社会化目标"));
|
|
|
+
|
|
|
+ Map<String, Object> diet = recoItem("reco-diet-1", "diet", "清淡均衡饮食计划",
|
|
|
+ "依据报告/评估调整膳食结构,已排除过敏与忌口食材");
|
|
|
+ diet.put("foodIds", dietFoodIdsExcluding(excluded));
|
|
|
+ items.add(diet);
|
|
|
+ return items;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 一键采纳(写回 task/wish) ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 一键采纳:type=task 经 TaskService.createTask 写回,type=wish 经 WishService 创建流程写回;
|
|
|
+ * 返回 targetType(task|wish)与 targetId(新任务/心愿 id)。越权(memberId 不在 userId 家庭)抛异常拒绝。
|
|
|
+ * 同一 (memberId,type,recoId) 重复采纳返回同一 targetId:生产经 coach_recommend_log 表
|
|
|
+ * (协议,uk_member_type_reco 唯一键兜底)幂等;契约上下文(mapper 为 null)JVM 内 adoptLog 兜底。
|
|
|
+ */
|
|
|
+ public Map<String, Object> adopt(Long userId, Long memberId, String type, String recoId) {
|
|
|
+ if (!Boolean.TRUE.equals(hasFamilyAccess(userId, memberId))) {
|
|
|
+ throw new IllegalStateException("越权:memberId 不属于 userId 家庭,拒绝采纳");
|
|
|
+ }
|
|
|
+ String key = memberId + ":" + type + ":" + recoId;
|
|
|
+ if (coachRecommendLogMapper != null) {
|
|
|
+ try {
|
|
|
+ CoachRecommendLog existing = queryAdoptLog(memberId, type, recoId);
|
|
|
+ if (existing != null) {
|
|
|
+ return adoptResult(existing.getTargetType(), existing.getTargetId());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 留痕查询失败降级 JVM 兜底,保证采纳不因日志异常而中断
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Long existingId = adoptLog.get(key);
|
|
|
+ if (existingId != null) {
|
|
|
+ return adoptResult(type, existingId);
|
|
|
+ }
|
|
|
+ Long targetId;
|
|
|
+ if ("task".equals(type)) {
|
|
|
+ targetId = createTaskTarget(userId, memberId, recoId);
|
|
|
+ } else if ("wish".equals(type)) {
|
|
|
+ targetId = createWishTarget(userId, memberId, recoId);
|
|
|
+ } else {
|
|
|
+ throw new IllegalArgumentException("不支持的推荐类型: " + type);
|
|
|
+ }
|
|
|
+ adoptLog.put(key, targetId);
|
|
|
+ if (coachRecommendLogMapper != null) {
|
|
|
+ try {
|
|
|
+ CoachRecommendLog log = new CoachRecommendLog();
|
|
|
+ log.setMemberId(memberId);
|
|
|
+ log.setUserId(userId);
|
|
|
+ log.setRecommendType(type);
|
|
|
+ log.setRecoId(recoId);
|
|
|
+ log.setTargetType(type);
|
|
|
+ log.setTargetId(targetId);
|
|
|
+ coachRecommendLogMapper.insert(log);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 留痕写失败不影响采纳主流程(幂等降级 JVM 兜底)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return adoptResult(type, targetId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 幂等留痕查询:同一 (memberId,type,recoId) 曾采纳 → true。
|
|
|
+ * 生产查 coach_recommend_log 表;契约上下文(mapper 为 null)查 JVM 内 adoptLog。
|
|
|
+ */
|
|
|
+ public Boolean isAdopted(Long memberId, String type, String recoId) {
|
|
|
+ if (coachRecommendLogMapper != null) {
|
|
|
+ try {
|
|
|
+ return queryAdoptLog(memberId, type, recoId) != null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 查询失败降级 JVM 兜底
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return adoptLog.containsKey(memberId + ":" + type + ":" + recoId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 按 (member_id, recommend_type, reco_id) 查留痕(uk_member_type_reco 语义)。 */
|
|
|
+ private CoachRecommendLog queryAdoptLog(Long memberId, String type, String recoId) {
|
|
|
+ List<CoachRecommendLog> logs = coachRecommendLogMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<CoachRecommendLog>()
|
|
|
+ .eq(CoachRecommendLog::getMemberId, memberId)
|
|
|
+ .eq(CoachRecommendLog::getRecommendType, type)
|
|
|
+ .eq(CoachRecommendLog::getRecoId, recoId)
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ return (logs == null || logs.isEmpty()) ? null : logs.get(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 过敏/忌口排除与权限门禁 ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 过敏/忌口排除清单:经 FoodRecommendService 戒备食材(推荐指数 < 0)归一;契约上下文返回演示值。
|
|
|
+ */
|
|
|
+ public List<Long> excludedFoodIds(Long memberId) {
|
|
|
+ if (foodRecommendService != null) {
|
|
|
+ try {
|
|
|
+ List<FoodRecommendIndex> caution = foodRecommendService.getCautionFoods(memberId);
|
|
|
+ List<Long> ids = new ArrayList<>();
|
|
|
+ if (caution != null) {
|
|
|
+ for (FoodRecommendIndex c : caution) {
|
|
|
+ if (c != null && c.getIndexScore() != null && c.getIndexScore() < 0
|
|
|
+ && c.getFoodId() != null) {
|
|
|
+ ids.add(c.getFoodId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ids;
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 查询失败回退演示口径,保证推荐不因排除清单异常而中断
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return new ArrayList<>(DEMO_EXCLUDED_FOODS);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 权限门禁:memberId 属于 userId 家庭或为 userId 自身 → true。
|
|
|
+ * 生产经 {@code FamilyMemberService.isCallerInSameFamily}(family_members)判定;
|
|
|
+ * 契约上下文(service 为 null)使用演示家庭数据(1→{11,12},2→{21,22})。
|
|
|
+ */
|
|
|
+ public Boolean hasFamilyAccess(Long userId, Long memberId) {
|
|
|
+ if (userId == null || memberId == null) {
|
|
|
+ return Boolean.FALSE;
|
|
|
+ }
|
|
|
+ if (userId.equals(memberId)) {
|
|
|
+ return Boolean.TRUE;
|
|
|
+ }
|
|
|
+ if (familyMemberService != null) {
|
|
|
+ try {
|
|
|
+ return familyMemberService.isCallerInSameFamily(memberId, userId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 查询失败回退演示口径,保证权限判定不抛异常(fail-safe 拒绝优于放行)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<Long> members = DEMO_FAMILY_MEMBERS.get(userId);
|
|
|
+ return members != null && members.contains(memberId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 私有实现 ====================
|
|
|
+
|
|
|
+ private Map<String, Object> recoItem(String recoId, String type, String title, String reason) {
|
|
|
+ Map<String, Object> item = new LinkedHashMap<>();
|
|
|
+ item.put("recoId", recoId);
|
|
|
+ item.put("type", type);
|
|
|
+ item.put("title", title);
|
|
|
+ item.put("reason", reason);
|
|
|
+ return item;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 List<String> demoWeakDimensions(Long memberId) {
|
|
|
+ // 演示五维短板(根码,契约校验 ROOT_DIMS):按成员取稳定子集
|
|
|
+ if (memberId != null && memberId % 2 == 0) {
|
|
|
+ return Arrays.asList(DimensionCodeConstants.BODY, DimensionCodeConstants.WISDOM);
|
|
|
+ }
|
|
|
+ return Arrays.asList(DimensionCodeConstants.BODY, DimensionCodeConstants.MIND,
|
|
|
+ DimensionCodeConstants.WISDOM);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 五维短板:生产查 five_dimension_scores;契约上下文(mapper 为 null)返回演示短板。 */
|
|
|
+ private List<String> weakDimensionsOf(Long memberId) {
|
|
|
+ if (fiveDimensionScoreMapper != null) {
|
|
|
+ return productionWeakDimensions(memberId);
|
|
|
+ }
|
|
|
+ return demoWeakDimensions(memberId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生产五维短板:从 five_dimension_scores 按成员取 score < WEAK_DIM_SCORE_THRESHOLD 的维度根码
|
|
|
+ * (0-100 分制,低分为短板;仅保留五维根码 body/mind/wisdom/action/wealth)。
|
|
|
+ * 无数据/查询失败 → 空(调用方据此降级 generic)。
|
|
|
+ */
|
|
|
+ private List<String> productionWeakDimensions(Long memberId) {
|
|
|
+ if (memberId == null || fiveDimensionScoreMapper == null) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ List<FiveDimensionScore> scores = fiveDimensionScoreMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<FiveDimensionScore>()
|
|
|
+ .eq(FiveDimensionScore::getMemberId, memberId)
|
|
|
+ .lt(FiveDimensionScore::getScore, WEAK_DIM_SCORE_THRESHOLD));
|
|
|
+ List<String> weak = new ArrayList<>();
|
|
|
+ if (scores != null) {
|
|
|
+ for (FiveDimensionScore s : scores) {
|
|
|
+ if (s != null && s.getDimensionCode() != null
|
|
|
+ && ROOT_DIMS.contains(s.getDimensionCode())) {
|
|
|
+ weak.add(s.getDimensionCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return weak;
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 查询失败降级空短板(basis=generic),保证推荐不因五维数据异常而中断
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Long> dietFoodIdsExcluding(List<Long> excluded) {
|
|
|
+ List<Long> safe = new ArrayList<>();
|
|
|
+ for (Long foodId : DIET_BASE_FOODS) {
|
|
|
+ if (!excluded.contains(foodId)) {
|
|
|
+ safe.add(foodId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return safe;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long createTaskTarget(Long userId, Long memberId, String recoId) {
|
|
|
+ if (taskService != null) {
|
|
|
+ try {
|
|
|
+ CreateTaskDTO dto = new CreateTaskDTO();
|
|
|
+ dto.setTitle(buildTitle(recoId, "任务"));
|
|
|
+ dto.setMemberId(memberId);
|
|
|
+ dto.setSourceType("ai");
|
|
|
+ dto.setTaskType("once");
|
|
|
+ List<Long> assignees = new ArrayList<>();
|
|
|
+ assignees.add(memberId);
|
|
|
+ dto.setMemberIds(assignees);
|
|
|
+ Long id = taskService.createTask(userId, dto);
|
|
|
+ if (id != null && id > 0L) {
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 写回失败回退演示 id,保证契约上下文可验证
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return demoTargetIdSeq.incrementAndGet();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long createWishTarget(Long userId, Long memberId, String recoId) {
|
|
|
+ if (wishService != null) {
|
|
|
+ try {
|
|
|
+ CreateWishDTO dto = new CreateWishDTO();
|
|
|
+ dto.setTitle(buildTitle(recoId, "心愿"));
|
|
|
+ dto.setMemberId(memberId);
|
|
|
+ dto.setEstimatedPrice(0L);
|
|
|
+ dto.setPointsRequired(0);
|
|
|
+ Long id = wishService.createByChild(memberId, dto);
|
|
|
+ if (id != null && id > 0L) {
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 写回失败回退演示 id
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return demoTargetIdSeq.incrementAndGet();
|
|
|
+ }
|
|
|
+
|
|
|
+ private String buildTitle(String recoId, String suffix) {
|
|
|
+ if (recoId == null || recoId.trim().isEmpty()) {
|
|
|
+ return "AI 教练推荐" + suffix;
|
|
|
+ }
|
|
|
+ return "AI 教练推荐" + suffix + "(" + recoId + ")";
|
|
|
+ }
|
|
|
+}
|