|
|
@@ -0,0 +1,129 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.etotem.cfc.entity.DailyFeedback;
|
|
|
+import com.etotem.cfc.entity.HealthCheckin;
|
|
|
+import com.etotem.cfc.entity.MicroActionRecord;
|
|
|
+import com.etotem.cfc.mapper.DailyFeedbackMapper;
|
|
|
+import com.etotem.cfc.mapper.HealthCheckinMapper;
|
|
|
+import com.etotem.cfc.mapper.MicroActionRecordMapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class DailyFeedbackService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private DailyFeedbackMapper dailyFeedbackMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private HealthCheckinMapper healthCheckinMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private MicroActionRecordMapper microActionRecordMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取今日反馈
|
|
|
+ */
|
|
|
+ public DailyFeedback getTodayFeedback(Long childId) {
|
|
|
+ if (childId == null) return null;
|
|
|
+ String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
|
|
+ return dailyFeedbackMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<DailyFeedback>()
|
|
|
+ .eq(DailyFeedback::getChildId, childId)
|
|
|
+ .apply("DATE(feedback_date) = {0}", today)
|
|
|
+ .eq(DailyFeedback::getFeedbackType, "daily")
|
|
|
+ .last("LIMIT 1")
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 标记反馈为已读
|
|
|
+ */
|
|
|
+ public void markAsRead(Long feedbackId) {
|
|
|
+ DailyFeedback fb = dailyFeedbackMapper.selectById(feedbackId);
|
|
|
+ if (fb != null) {
|
|
|
+ fb.setIsRead(1);
|
|
|
+ dailyFeedbackMapper.updateById(fb);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取最近N条反馈
|
|
|
+ */
|
|
|
+ public List<DailyFeedback> getRecentFeedbacks(Long childId, int limit) {
|
|
|
+ if (childId == null) return Collections.emptyList();
|
|
|
+ return dailyFeedbackMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<DailyFeedback>()
|
|
|
+ .eq(DailyFeedback::getChildId, childId)
|
|
|
+ .orderByDesc(DailyFeedback::getFeedbackDate)
|
|
|
+ .last("LIMIT " + limit)
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成预设反馈(基于当日行为)
|
|
|
+ * 目前使用预设文案,后续可对接AI Gateway
|
|
|
+ */
|
|
|
+ public DailyFeedback generatePresetFeedback(Long userId, Long childId) {
|
|
|
+ if (childId == null) return null;
|
|
|
+
|
|
|
+ // Check if already generated today
|
|
|
+ DailyFeedback existing = getTodayFeedback(childId);
|
|
|
+ if (existing != null) return existing;
|
|
|
+
|
|
|
+ // Check today's activities
|
|
|
+ String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
|
|
+
|
|
|
+ boolean hasCheckin = healthCheckinMapper.selectCount(
|
|
|
+ new LambdaQueryWrapper<HealthCheckin>()
|
|
|
+ .eq(HealthCheckin::getChildId, childId)
|
|
|
+ .apply("DATE(checkin_date) = {0}", today)
|
|
|
+ ) > 0;
|
|
|
+
|
|
|
+ boolean hasMicroAction = microActionRecordMapper.selectCount(
|
|
|
+ new LambdaQueryWrapper<MicroActionRecord>()
|
|
|
+ .eq(MicroActionRecord::getChildId, childId)
|
|
|
+ .apply("DATE(completed_date) = {0}", today)
|
|
|
+ ) > 0;
|
|
|
+
|
|
|
+ // Generate feedback text
|
|
|
+ String content;
|
|
|
+ if (hasCheckin || hasMicroAction) {
|
|
|
+ content = getRandomEncouragement();
|
|
|
+ } else {
|
|
|
+ content = "今天还没有记录健康行动,从一个小行动开始吧 ☕️";
|
|
|
+ }
|
|
|
+
|
|
|
+ DailyFeedback feedback = new DailyFeedback();
|
|
|
+ feedback.setUserId(userId);
|
|
|
+ feedback.setChildId(childId);
|
|
|
+ feedback.setFeedbackDate(new Date());
|
|
|
+ feedback.setFeedbackType("daily");
|
|
|
+ feedback.setContent(content);
|
|
|
+ feedback.setIsRead(0);
|
|
|
+ feedback.setSource("preset");
|
|
|
+ feedback.setCreatedAt(new Date());
|
|
|
+ dailyFeedbackMapper.insert(feedback);
|
|
|
+ return feedback;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getRandomEncouragement() {
|
|
|
+ String[] messages = {
|
|
|
+ "太棒了!今天又迈出了健康的一步 🎉",
|
|
|
+ "坚持就是胜利!每天进步一点点 💪",
|
|
|
+ "你的身体正在感谢你的每一个健康选择 🌟",
|
|
|
+ "今天的努力是明天的健康!继续加油 ⭐",
|
|
|
+ "每一个微行动都是对自己的关爱 ❤️",
|
|
|
+ "你今天做得很棒!健康习惯正在养成 🌱",
|
|
|
+ "今天的你已经比昨天更健康了!🔥",
|
|
|
+ "健康是一种生活方式,你今天活出了它!✨"
|
|
|
+ };
|
|
|
+ return messages[new Random().nextInt(messages.length)];
|
|
|
+ }
|
|
|
+}
|