Просмотр исходного кода

feat(health): 心情打卡同步到情绪日记

- EmotionCheckinMapper 新增 selectByChildIdAndDate 方法
- DailyCheckinService.createCheckin 在 moodScore 有值时自动同步到 emotion_checkin 表
- upsert 逻辑:今日已有记录则更新,否则新建
- moodScore 1-10 → moodWeather 映射(sunny/cloudy/rainy/stormy)
- 同步失败不影响主流程
iwt 1 месяц назад
Родитель
Сommit
38751a310a

+ 3 - 0
cfc-backend/src/main/java/com/etotem/cfc/mapper/EmotionCheckinMapper.java

@@ -13,4 +13,7 @@ public interface EmotionCheckinMapper extends BaseMapper<EmotionCheckin> {
 
     @Select("SELECT * FROM emotion_checkin WHERE child_id = #{childId} ORDER BY checkin_date DESC LIMIT #{limit}")
     List<EmotionCheckin> selectLatestByChildId(@Param("childId") Long childId, @Param("limit") int limit);
+
+    @Select("SELECT * FROM emotion_checkin WHERE child_id = #{childId} AND checkin_date = #{checkinDate} LIMIT 1")
+    EmotionCheckin selectByChildIdAndDate(@Param("childId") Long childId, @Param("checkinDate") java.util.Date checkinDate);
 }

+ 57 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/DailyCheckinService.java

@@ -1,7 +1,9 @@
 package com.etotem.cfc.service;
 
 import com.etotem.cfc.entity.DailyHealthCheckin;
+import com.etotem.cfc.entity.EmotionCheckin;
 import com.etotem.cfc.mapper.DailyCheckinMapper;
+import com.etotem.cfc.mapper.EmotionCheckinMapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import org.springframework.stereotype.Service;
 
@@ -21,6 +23,9 @@ public class DailyCheckinService {
     @Resource
     private PointsService pointsService;
 
+    @Resource
+    private EmotionCheckinMapper emotionCheckinMapper;
+
     public List<DailyHealthCheckin> getCheckinsByMember(Long memberId) {
         return dailyCheckinMapper.selectList(new LambdaQueryWrapper<DailyHealthCheckin>()
                 .eq(DailyHealthCheckin::getMemberId, memberId)
@@ -43,9 +48,61 @@ public class DailyCheckinService {
         } catch (Exception e) {
             log.warn("打卡积分发放失败,不影响打卡记录: {}", e.getMessage());
         }
+
+        // 心情打卡同步到情绪日记(如有 moodScore)
+        try {
+            if (checkin.getMoodScore() != null) {
+                syncToEmotionDiary(checkin);
+            }
+        } catch (Exception e) {
+            log.warn("心情打卡同步情绪日记失败,不影响主流程: {}", e.getMessage());
+        }
+
         return checkin;
     }
 
+    /**
+     * 将心情打卡记录同步到情绪日记(emotion_checkin 表)。
+     * 有今日记录则更新,否则新建。
+     */
+    private void syncToEmotionDiary(DailyHealthCheckin checkin) {
+        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
+        String dateStr = sdf.format(checkin.getCheckinDate());
+        Date todayDate = sdf.parse(dateStr);
+
+        EmotionCheckin existing = emotionCheckinMapper.selectByChildIdAndDate(checkin.getMemberId(), todayDate);
+
+        EmotionCheckin ec = new EmotionCheckin();
+        ec.setChildId(checkin.getMemberId());
+        ec.setMoodScore(checkin.getMoodScore());
+        ec.setNote(checkin.getRemark());
+        ec.setMoodWeather(mapMoodToWeather(checkin.getMoodScore()));
+        ec.setCheckinDate(todayDate);
+
+        if (existing != null) {
+            ec.setId(existing.getId());
+            ec.setCreatedAt(existing.getCreatedAt());
+            emotionCheckinMapper.updateById(ec);
+            log.info("心情打卡同步情绪日记(更新): memberId={}, moodScore={}", checkin.getMemberId(), checkin.getMoodScore());
+        } else {
+            ec.setEnergyAwarded(0);
+            emotionCheckinMapper.insert(ec);
+            log.info("心情打卡同步情绪日记(新建): memberId={}, moodScore={}", checkin.getMemberId(), checkin.getMoodScore());
+        }
+    }
+
+    /**
+     * moodScore 1-10 → moodWeather
+     * 8-10: sunny, 5-7: cloudy, 3-4: rainy, 1-2: stormy
+     */
+    private String mapMoodToWeather(Integer moodScore) {
+        if (moodScore == null) return null;
+        if (moodScore >= 8) return "sunny";
+        if (moodScore >= 5) return "cloudy";
+        if (moodScore >= 3) return "rainy";
+        return "stormy";
+    }
+
     public DailyHealthCheckin updateCheckin(Long checkinId, Map<String, Object> updates) {
         DailyHealthCheckin checkin = dailyCheckinMapper.selectById(checkinId);
         if (checkin == null) {