Explorar el Código

feat(workbench): 家庭聚合服务(6卡总额+近30天序列)

Xiaogang Liao hace 1 semana
padre
commit
3e89251116

+ 52 - 0
cfc-backend/src/main/java/com/etotem/cfc/mapper/FamilyWorkbenchMapper.java

@@ -0,0 +1,52 @@
+package com.etotem.cfc.mapper;
+
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 单家庭运营工作台-聚合查询(纯只读 @Select)。
+ * 序列统一按 DATE(created_at) 分桶,供 FamilyWorkbenchSeriesBuilder 0 填充。
+ */
+@Mapper
+public interface FamilyWorkbenchMapper {
+
+    @Select("SELECT COUNT(*) FROM health_reports WHERE family_id = #{familyId}")
+    long countReports(@Param("familyId") Long familyId);
+
+    @Select("SELECT COUNT(*) FROM health_plans WHERE family_id = #{familyId}")
+    long countPlans(@Param("familyId") Long familyId);
+
+    @Select("SELECT COUNT(*) FROM tasks WHERE family_id = #{familyId}")
+    long countTasks(@Param("familyId") Long familyId);
+
+    @Select("SELECT COUNT(*) FROM chat_conversations c JOIN family_members fm ON fm.user_id = c.user_id WHERE fm.family_id = #{familyId}")
+    long countConversations(@Param("familyId") Long familyId);
+
+    @Select("SELECT COUNT(*) FROM family_coupon WHERE family_id = #{familyId}")
+    long countCoupons(@Param("familyId") Long familyId);
+
+    @Select("SELECT COALESCE(SUM(total_earned), 0) FROM cf_balance WHERE family_id = #{familyId}")
+    Long cfBalance(@Param("familyId") Long familyId);
+
+    @Select("SELECT DATE(created_at) AS d, COUNT(*) AS c FROM health_reports WHERE family_id = #{familyId} AND created_at >= #{from} GROUP BY DATE(created_at)")
+    List<Map<String, Object>> reportDailySeries(@Param("familyId") Long familyId, @Param("from") String from);
+
+    @Select("SELECT DATE(created_at) AS d, COUNT(*) AS c FROM health_plans WHERE family_id = #{familyId} AND created_at >= #{from} GROUP BY DATE(created_at)")
+    List<Map<String, Object>> planDailySeries(@Param("familyId") Long familyId, @Param("from") String from);
+
+    @Select("SELECT DATE(created_at) AS d, COUNT(*) AS c FROM tasks WHERE family_id = #{familyId} AND created_at >= #{from} GROUP BY DATE(created_at)")
+    List<Map<String, Object>> taskDailySeries(@Param("familyId") Long familyId, @Param("from") String from);
+
+    @Select("SELECT DATE(c.created_at) AS d, COUNT(*) AS c FROM chat_conversations c JOIN family_members fm ON fm.user_id = c.user_id WHERE fm.family_id = #{familyId} AND c.created_at >= #{from} GROUP BY DATE(c.created_at)")
+    List<Map<String, Object>> conversationDailySeries(@Param("familyId") Long familyId, @Param("from") String from);
+
+    @Select("SELECT DATE(received_at) AS d, COUNT(*) AS c FROM family_coupon WHERE family_id = #{familyId} AND received_at >= #{from} GROUP BY DATE(received_at)")
+    List<Map<String, Object>> couponDailySeries(@Param("familyId") Long familyId, @Param("from") String from);
+
+    @Select("SELECT DATE(created_at) AS d, SUM(amount) AS c FROM cf_balance_log WHERE family_id = #{familyId} AND created_at >= #{from} GROUP BY DATE(created_at)")
+    List<Map<String, Object>> cfDailySeries(@Param("familyId") Long familyId, @Param("from") String from);
+}

+ 10 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/FamilyWorkbenchService.java

@@ -0,0 +1,10 @@
+package com.etotem.cfc.service;
+
+import com.etotem.cfc.dto.FamilyWorkbenchDTO;
+
+/** 单家庭运营工作台-聚合与家庭模块只读查询 */
+public interface FamilyWorkbenchService {
+
+    /** 家庭不存在抛 RuntimeException("家庭不存在");返回 family + 6 张指标卡(总额+近30天序列) */
+    FamilyWorkbenchDTO aggregate(Long familyId);
+}

+ 80 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/impl/FamilyWorkbenchServiceImpl.java

@@ -0,0 +1,80 @@
+package com.etotem.cfc.service.impl;
+
+import com.etotem.cfc.dto.FamilyWorkbenchCard;
+import com.etotem.cfc.dto.FamilyWorkbenchDTO;
+import com.etotem.cfc.dto.FamilyWorkbenchSeriesPoint;
+import com.etotem.cfc.entity.Family;
+import com.etotem.cfc.mapper.FamilyMapper;
+import com.etotem.cfc.mapper.FamilyWorkbenchMapper;
+import com.etotem.cfc.service.FamilyWorkbenchService;
+import com.etotem.cfc.util.FamilyWorkbenchSeriesBuilder;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Service
+public class FamilyWorkbenchServiceImpl implements FamilyWorkbenchService {
+
+    @Resource
+    private FamilyMapper familyMapper;
+
+    @Resource
+    private FamilyWorkbenchMapper workbenchMapper;
+
+    @Override
+    public FamilyWorkbenchDTO aggregate(Long familyId) {
+        Family family = familyMapper.selectById(familyId);
+        if (family == null) throw new RuntimeException("家庭不存在");
+
+        FamilyWorkbenchDTO dto = new FamilyWorkbenchDTO();
+        dto.setFamily(familyInfo(family));
+
+        LocalDate to = LocalDate.now();
+        LocalDate from = to.minusDays(29);
+        String fromStr = from.toString();
+
+        List<FamilyWorkbenchCard> cards = new ArrayList<>();
+        cards.add(card("reports", "报告", workbenchMapper.countReports(familyId),
+                workbenchMapper.reportDailySeries(familyId, fromStr), from, to));
+        cards.add(card("plans", "方案", workbenchMapper.countPlans(familyId),
+                workbenchMapper.planDailySeries(familyId, fromStr), from, to));
+        cards.add(card("tasks", "任务", workbenchMapper.countTasks(familyId),
+                workbenchMapper.taskDailySeries(familyId, fromStr), from, to));
+        cards.add(card("messages", "消息", workbenchMapper.countConversations(familyId),
+                workbenchMapper.conversationDailySeries(familyId, fromStr), from, to));
+        cards.add(card("cf", "CF值", workbenchMapper.cfBalance(familyId),
+                workbenchMapper.cfDailySeries(familyId, fromStr), from, to));
+        cards.add(card("coupons", "优惠券", workbenchMapper.countCoupons(familyId),
+                workbenchMapper.couponDailySeries(familyId, fromStr), from, to));
+        dto.setCards(cards);
+        return dto;
+    }
+
+    private FamilyWorkbenchCard card(String key, String label, Long total,
+                                     List<Map<String, Object>> rows, LocalDate from, LocalDate to) {
+        FamilyWorkbenchCard c = new FamilyWorkbenchCard();
+        c.setKey(key);
+        c.setLabel(label);
+        c.setTotal(total == null ? 0L : total);
+        c.setSeries(FamilyWorkbenchSeriesBuilder.buildDailySeries(from, to, rows));
+        return c;
+    }
+
+    private Map<String, Object> familyInfo(Family family) {
+        Map<String, Object> f = new HashMap<>();
+        f.put("id", family.getId());
+        f.put("name", family.getName());
+        f.put("inviteCode", family.getInviteCode());
+        f.put("creatorId", family.getCreatorId());
+        f.put("teacherId", family.getTeacherId());
+        f.put("butlerId", family.getButlerId());
+        f.put("memberCount", family.getMemberCount());
+        f.put("createdAt", family.getCreatedAt());
+        return f;
+    }
+}

+ 124 - 0
cfc-backend/src/test/java/com/etotem/cfc/service/FamilyWorkbenchServiceImplTest.java

@@ -0,0 +1,124 @@
+package com.etotem.cfc.service;
+
+import com.etotem.cfc.dto.FamilyWorkbenchCard;
+import com.etotem.cfc.dto.FamilyWorkbenchDTO;
+import com.etotem.cfc.entity.Family;
+import com.etotem.cfc.mapper.FamilyMapper;
+import com.etotem.cfc.mapper.FamilyWorkbenchMapper;
+import com.etotem.cfc.service.impl.FamilyWorkbenchServiceImpl;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * 单家庭工作台聚合服务契约:家庭不存在报错、6 卡齐全、totals 与序列正确、查询带正确 familyId/起始日。
+ */
+class FamilyWorkbenchServiceImplTest {
+
+    @Mock private FamilyMapper familyMapper;
+    @Mock private FamilyWorkbenchMapper workbenchMapper;
+    @InjectMocks private FamilyWorkbenchServiceImpl service;
+
+    @BeforeEach
+    void setUp() {
+        MockitoAnnotations.openMocks(this);
+    }
+
+    private Map<String, Object> row(String d, long c) {
+        Map<String, Object> m = new HashMap<>();
+        m.put("d", d);
+        m.put("c", c);
+        return m;
+    }
+
+    private Family family(Long id) {
+        Family f = new Family();
+        f.setId(id);
+        f.setName("测试家庭");
+        f.setInviteCode("TEST123");
+        return f;
+    }
+
+    @Test
+    void 家庭不存在_抛业务异常() {
+        when(familyMapper.selectById(999L)).thenReturn(null);
+        RuntimeException e = assertThrows(RuntimeException.class, () -> service.aggregate(999L));
+        assertTrue(e.getMessage().contains("家庭不存在"));
+    }
+
+    @Test
+    void 聚合返回6卡_总额序列正确() {
+        when(familyMapper.selectById(5L)).thenReturn(family(5L));
+        when(workbenchMapper.countReports(5L)).thenReturn(7L);
+        when(workbenchMapper.countPlans(5L)).thenReturn(3L);
+        when(workbenchMapper.countTasks(5L)).thenReturn(9L);
+        when(workbenchMapper.countConversations(5L)).thenReturn(2L);
+        when(workbenchMapper.countCoupons(5L)).thenReturn(4L);
+        when(workbenchMapper.cfBalance(5L)).thenReturn(100L);
+        when(workbenchMapper.reportDailySeries(eq(5L), anyString())).thenReturn(List.of(row("2026-10-20", 2L), row("2026-10-20", 1L)));
+        when(workbenchMapper.planDailySeries(eq(5L), anyString())).thenReturn(new ArrayList<>());
+        when(workbenchMapper.taskDailySeries(eq(5L), anyString())).thenReturn(new ArrayList<>());
+        when(workbenchMapper.conversationDailySeries(eq(5L), anyString())).thenReturn(new ArrayList<>());
+        when(workbenchMapper.couponDailySeries(eq(5L), anyString())).thenReturn(new ArrayList<>());
+        when(workbenchMapper.cfDailySeries(eq(5L), anyString())).thenReturn(new ArrayList<>());
+
+        FamilyWorkbenchDTO dto = service.aggregate(5L);
+
+        assertEquals("测试家庭", dto.getFamily().get("name"));
+        assertEquals(6, dto.getCards().size());
+
+        FamilyWorkbenchCard reports = dto.getCards().get(0);
+        assertEquals("reports", reports.getKey());
+        assertEquals(7L, reports.getTotal());
+        assertEquals(30, reports.getSeries().size());
+        FamilyWorkbenchCard cf = dto.getCards().get(4);
+        assertEquals("cf", cf.getKey());
+        assertEquals(100L, cf.getTotal());
+
+        List<String> keys = dto.getCards().stream().map(FamilyWorkbenchCard::getKey).toList();
+        assertEquals(List.of("reports", "plans", "tasks", "messages", "cf", "coupons"), keys);
+    }
+
+    @Test
+    void 聚合查询携带正确familyId与30天起始日() {
+        when(familyMapper.selectById(5L)).thenReturn(family(5L));
+        when(workbenchMapper.countReports(any())).thenReturn(0L);
+        when(workbenchMapper.countPlans(any())).thenReturn(0L);
+        when(workbenchMapper.countTasks(any())).thenReturn(0L);
+        when(workbenchMapper.countConversations(any())).thenReturn(0L);
+        when(workbenchMapper.countCoupons(any())).thenReturn(0L);
+        when(workbenchMapper.cfBalance(any())).thenReturn(0L);
+        when(workbenchMapper.reportDailySeries(any(), any())).thenReturn(new ArrayList<>());
+        when(workbenchMapper.planDailySeries(any(), any())).thenReturn(new ArrayList<>());
+        when(workbenchMapper.taskDailySeries(any(), any())).thenReturn(new ArrayList<>());
+        when(workbenchMapper.conversationDailySeries(any(), any())).thenReturn(new ArrayList<>());
+        when(workbenchMapper.couponDailySeries(any(), any())).thenReturn(new ArrayList<>());
+        when(workbenchMapper.cfDailySeries(any(), any())).thenReturn(new ArrayList<>());
+
+        service.aggregate(5L);
+
+        verify(workbenchMapper).countReports(5L);
+        verify(workbenchMapper).conversationDailySeries(eq(5L), anyString());
+        ArgumentCaptor<String> from = ArgumentCaptor.forClass(String.class);
+        verify(workbenchMapper).cfDailySeries(eq(5L), from.capture());
+        assertEquals(LocalDate.now().minusDays(29).toString(), from.getValue());
+    }
+}