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

feat(workbench): 报告手动新增(白名单+固定familyId/userId)

Xiaogang Liao 5 дней назад
Родитель
Сommit
f477075390

+ 14 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/admin/AdminFamilyWorkbenchController.java

@@ -3,6 +3,7 @@ package com.etotem.cfc.controller.admin;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.etotem.cfc.common.Result;
 import com.etotem.cfc.dto.FamilyConversationVO;
+import com.etotem.cfc.dto.FamilyReportCreateDTO;
 import com.etotem.cfc.dto.FamilyMessageVO;
 import com.etotem.cfc.dto.FamilyWorkbenchDTO;
 import com.etotem.cfc.service.FamilyWorkbenchService;
@@ -61,4 +62,17 @@ public class AdminFamilyWorkbenchController {
             return Result.error(e.getMessage());
         }
     }
+
+    @Operation(summary = "报告手动新增")
+    @PostMapping("/families/{id}/reports/create")
+    @PreAuthorize("hasRole('admin')")
+    public Result<Long> createReport(@PathVariable Long id,
+                                      @RequestAttribute(value = "userId", required = false) Long userId,
+                                      @RequestBody FamilyReportCreateDTO dto) {
+        try {
+            return Result.success(service.createFamilyReport(id, userId, dto));
+        } catch (RuntimeException e) {
+            return Result.error(e.getMessage());
+        }
+    }
 }

+ 17 - 0
cfc-backend/src/main/java/com/etotem/cfc/dto/FamilyReportCreateDTO.java

@@ -0,0 +1,17 @@
+package com.etotem.cfc.dto;
+
+import lombok.Data;
+
+import java.util.Date;
+
+/** 单家庭工作台-报告手动新增入参(family_id/user_id 由服务端固定,不取自请求体) */
+@Data
+public class FamilyReportCreateDTO {
+    private String reportType;   // physical_exam / gut_flora(白名单校验)
+    private Date reportDate;
+    private Long subjectId;
+    private String personName;
+    private Integer overallScore;
+    private Integer gutHealthScore;
+    private String remark;
+}

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

@@ -3,6 +3,7 @@ package com.etotem.cfc.service;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.etotem.cfc.dto.FamilyConversationVO;
 import com.etotem.cfc.dto.FamilyMessageVO;
+import com.etotem.cfc.dto.FamilyReportCreateDTO;
 import com.etotem.cfc.dto.FamilyWorkbenchDTO;
 
 /** 单家庭运营工作台-聚合与家庭模块只读查询 */
@@ -16,4 +17,7 @@ public interface FamilyWorkbenchService {
 
     /** 会话消息(分页);会话不属于该家庭抛 RuntimeException("会话不属于该家庭") */
     Page<FamilyMessageVO> pageConversationMessages(Long familyId, Long conversationId, long page, long size);
+
+    /** 报告手动新增:reportType 白名单校验;family_id/user_id 固定;返回报告 id */
+    Long createFamilyReport(Long familyId, Long adminUserId, FamilyReportCreateDTO dto);
 }

+ 28 - 1
cfc-backend/src/main/java/com/etotem/cfc/service/impl/FamilyWorkbenchServiceImpl.java

@@ -3,10 +3,13 @@ package com.etotem.cfc.service.impl;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.etotem.cfc.dto.FamilyConversationVO;
 import com.etotem.cfc.dto.FamilyMessageVO;
+import com.etotem.cfc.dto.FamilyReportCreateDTO;
 import com.etotem.cfc.dto.FamilyWorkbenchCard;
 import com.etotem.cfc.dto.FamilyWorkbenchDTO;
 import com.etotem.cfc.entity.Family;
+import com.etotem.cfc.entity.HealthReport;
 import com.etotem.cfc.mapper.FamilyMapper;
+import com.etotem.cfc.mapper.HealthReportMapper;
 import com.etotem.cfc.mapper.FamilyWorkbenchMapper;
 import com.etotem.cfc.service.FamilyWorkbenchService;
 import com.etotem.cfc.util.FamilyWorkbenchSeriesBuilder;
@@ -32,6 +35,9 @@ public class FamilyWorkbenchServiceImpl implements FamilyWorkbenchService {
     @Resource
     private FamilyWorkbenchMapper workbenchMapper;
 
+    @Resource
+    private HealthReportMapper reportMapper;
+
     @Override
     public FamilyWorkbenchDTO aggregate(Long familyId) {
         Family family = familyMapper.selectById(familyId);
@@ -111,7 +117,28 @@ public class FamilyWorkbenchServiceImpl implements FamilyWorkbenchService {
         return out;
     }
 
-    private FamilyWorkbenchCard card(String key, String label, Long total,
+    @Override
+    public Long createFamilyReport(Long familyId, Long adminUserId, FamilyReportCreateDTO dto) {
+        String rt = dto.getReportType();
+        if (rt == null || !(rt.equals("physical_exam") || rt.equals("gut_flora"))) {
+            throw new RuntimeException("报告类型不合法");
+        }
+        HealthReport r = new HealthReport();
+        r.setUserId(adminUserId);
+        r.setFamilyId(familyId);
+        r.setSubjectId(dto.getSubjectId());
+        r.setPersonName(dto.getPersonName());
+        r.setReportType(rt);
+        r.setReportDate(dto.getReportDate() != null ? dto.getReportDate() : new Date());
+        r.setOverallScore(dto.getOverallScore());
+        r.setGutHealthScore(dto.getGutHealthScore());
+        r.setStatus("active");
+        r.setRemark(dto.getRemark());
+        reportMapper.insert(r);
+        return r.getId();
+    }
+
+        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);

+ 85 - 0
cfc-backend/src/test/java/com/etotem/cfc/service/FamilyWorkbenchReportCreateTest.java

@@ -0,0 +1,85 @@
+package com.etotem.cfc.service;
+
+import com.etotem.cfc.dto.FamilyReportCreateDTO;
+import com.etotem.cfc.entity.HealthReport;
+import com.etotem.cfc.mapper.FamilyMapper;
+import com.etotem.cfc.mapper.FamilyWorkbenchMapper;
+import com.etotem.cfc.mapper.HealthReportMapper;
+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.util.Date;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+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.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * 报告手动新增契约:类型白名单校验、family_id/user_id/status 固定来自服务端(不可注入)、reportDate 默认当天。
+ */
+class FamilyWorkbenchReportCreateTest {
+
+    @Mock private FamilyMapper familyMapper;
+    @Mock private FamilyWorkbenchMapper workbenchMapper;
+    @Mock private HealthReportMapper reportMapper;
+    @InjectMocks private FamilyWorkbenchServiceImpl service;
+
+    @BeforeEach
+    void setUp() {
+        MockitoAnnotations.openMocks(this);
+    }
+
+    @Test
+    void 非法报告类型_拒绝() {
+        FamilyReportCreateDTO dto = new FamilyReportCreateDTO();
+        dto.setReportType("unknown_type");
+        RuntimeException e = assertThrows(RuntimeException.class,
+                () -> service.createFamilyReport(5L, 88L, dto));
+        assertTrue(e.getMessage().contains("报告类型不合法"));
+    }
+
+    @Test
+    void 合法创建_familyId和userId固定来自URL且状态active() {
+        FamilyReportCreateDTO dto = new FamilyReportCreateDTO();
+        dto.setReportType("physical_exam");
+        dto.setReportDate(new Date());
+        dto.setPersonName("小明");
+        dto.setOverallScore(88);
+        when(reportMapper.insert(any(HealthReport.class))).thenReturn(1);
+
+        Long id = service.createFamilyReport(5L, 88L, dto);
+
+        ArgumentCaptor<HealthReport> cap = ArgumentCaptor.forClass(HealthReport.class);
+        verify(reportMapper).insert(cap.capture());
+        HealthReport r = cap.getValue();
+        assertEquals(5L, r.getFamilyId(), "family_id 必须固定为 URL 值");
+        assertEquals(88L, r.getUserId(), "user_id 必须为当前操作人");
+        assertEquals("active", r.getStatus());
+        assertEquals("physical_exam", r.getReportType());
+        assertEquals("小明", r.getPersonName());
+        assertEquals(Integer.valueOf(88), r.getOverallScore());
+        assertNotNull(r.getReportDate());
+    }
+
+    @Test
+    void 报告日期为空_默认当天() {
+        FamilyReportCreateDTO dto = new FamilyReportCreateDTO();
+        dto.setReportType("gut_flora");
+        when(reportMapper.insert(any(HealthReport.class))).thenReturn(1);
+
+        service.createFamilyReport(5L, 88L, dto);
+
+        ArgumentCaptor<HealthReport> cap = ArgumentCaptor.forClass(HealthReport.class);
+        verify(reportMapper).insert(cap.capture());
+        assertNotNull(cap.getValue().getReportDate(), "reportDate 缺省应填当天");
+    }
+}