Explorar o código

fix: tasks表child_id字段改为可空,支持parent任务

User hai 5 meses
pai
achega
98660fc817

+ 33 - 15
zxyj-backend/src/main/java/com/zxyj/entity/TaskTemplate.java

@@ -12,31 +12,49 @@ import java.util.Date;
 @TableName("admin_task_templates")
 public class TaskTemplate implements Serializable {
 
-  @TableId(type = IdType.AUTO)
-  private Long id;
+    @TableId(type = IdType.AUTO)
+    private Long id;
 
-  private String title;
+    private String title;
 
-  private String description;
+    private String description;
 
-  private Integer points;
+    private Integer points;
 
-  private String category;
+    private String category;
 
-  private Integer needReview;
+    private Integer needReview;
 
-  private Integer reviewByCategory;
+    private Integer reviewByCategory;
 
-  private Integer recommendedAge;
+    private Integer recommendedAge;
 
-  private String difficulty;
+    private String difficulty;
 
-  private Integer isActive;
+    private Integer isActive;
 
-  // 创建该模板的指导师ID
-  private Long teacherId;
+    // 创建该模板的指导师ID
+    private Long teacherId;
 
-  private Date createdAt;
+    // 完成方式(JSON格式): ["checkin","text","image","video"]
+    private String completeTypes;
 
-  private Date updatedAt;
+    // 预计完成时长(分钟)
+    private Integer duration;
+
+    // 审核方式: creator/parent/teacher/ai
+    private String reviewType;
+
+    // 任务性质: onetime/recurring
+    private String taskType;
+
+    // 频率: daily/weekly/monthly
+    private String frequency;
+
+    // 周期内最多完成次数
+    private Integer maxFrequency;
+
+    private Date createdAt;
+
+    private Date updatedAt;
 }

+ 77 - 28
zxyj-backend/src/main/java/com/zxyj/service/TaskService.java

@@ -42,9 +42,6 @@ public class TaskService {
     @Transactional
     public Long createTask(Long userId, CreateTaskDTO dto) {
         User currentUser = userService.getUserInfo(userId);
-        Task task = new Task();
-        task.setFamilyId(currentUser.getFamilyId());
-        task.setCreatorId(userId);
         
         // 处理执行者类型 - 支持孩子任务和家长任务
         String executorType = dto.getExecutorType();
@@ -52,12 +49,53 @@ public class TaskService {
             // 兼容旧版:默认给孩子创建任务
             executorType = "child";
         }
+        
+        // 支持多孩子:为每个孩子创建独立任务实例
+        List<Long> childIds = dto.getChildIds();
+        if (childIds == null || childIds.isEmpty()) {
+            // 兼容旧版:使用单个childId
+            if (dto.getChildId() != null) {
+                childIds = Collections.singletonList(dto.getChildId());
+            } else if (dto.getExecutorId() != null && "child".equals(executorType)) {
+                childIds = Collections.singletonList(dto.getExecutorId());
+            } else {
+                childIds = Collections.emptyList();
+            }
+        }
+        
+        // 如果有多个孩子,为每个孩子创建任务
+        if (!childIds.isEmpty() && childIds.size() > 1) {
+            Long firstChildId = childIds.get(0);
+            Task task = createSingleTask(currentUser, userId, executorType, firstChildId, dto);
+            taskMapper.insert(task);
+            // 为其他孩子创建任务实例
+            for (int i = 1; i < childIds.size(); i++) {
+                Task additionalTask = createSingleTask(currentUser, userId, executorType, childIds.get(i), dto);
+                taskMapper.insert(additionalTask);
+            }
+            return task.getId();
+        } else {
+            // 单个孩子或无孩子
+            Long childId = childIds.isEmpty() ? null : childIds.get(0);
+            Task task = createSingleTask(currentUser, userId, executorType, childId, dto);
+            taskMapper.insert(task);
+            return task.getId();
+        }
+    }
+    
+    /**
+     * 创建单个任务实例
+     */
+    private Task createSingleTask(User currentUser, Long userId, String executorType, Long childId, CreateTaskDTO dto) {
+        Task task = new Task();
+        task.setFamilyId(currentUser.getFamilyId());
+        task.setCreatorId(userId);
         task.setExecutorType(executorType);
         
         if ("child".equals(executorType)) {
             // 给孩子安排的任务
-            task.setExecutorId(dto.getChildId() != null ? dto.getChildId() : dto.getExecutorId());
-            task.setChildId(dto.getChildId() != null ? dto.getChildId() : dto.getExecutorId());
+            task.setExecutorId(childId);
+            task.setChildId(childId);
         } else {
             // 给家长安排的任务
             task.setExecutorId(dto.getExecutorId() != null ? dto.getExecutorId() : userId);
@@ -67,32 +105,43 @@ public class TaskService {
         task.setTitle(dto.getTitle());
         task.setDescription(dto.getDescription());
         task.setPoints(dto.getPoints() != null ? dto.getPoints() : 2);
-task.setDeadline(dto.getDeadline());
-task.setRepeatType(dto.getRepeatType() != null ? dto.getRepeatType() : "none");
-task.setCategory(dto.getCategory());
+        
+        // 如果没有指定截止时间,默认今天23:59
+        if (dto.getDeadline() == null) {
+            Calendar cal = Calendar.getInstance();
+            cal.set(Calendar.HOUR_OF_DAY, 23);
+            cal.set(Calendar.MINUTE, 59);
+            cal.set(Calendar.SECOND, 59);
+            task.setDeadline(cal.getTime());
+        } else {
+            task.setDeadline(dto.getDeadline());
+        }
+        
+        task.setRepeatType(dto.getRepeatType() != null ? dto.getRepeatType() : "none");
+        task.setCategory(dto.getCategory());
 
-// TASK-004: 如果是重复任务,标记为模板
-if (!"none".equals(task.getRepeatType())) {
-task.setIsTemplate(1); // 模板任务
-} else {
-task.setIsTemplate(0);
-}
+        // 如果是重复任务,标记为模板
+        if (!"none".equals(task.getRepeatType())) {
+            task.setIsTemplate(1); // 模板任务
+        } else {
+            task.setIsTemplate(0);
+        }
 
-// 家长给孩子安排任务需要审核,孩子给家长安排任务也需要审核
-if ("child".equals(executorType)) {
-task.setNeedReview(dto.getNeedReview() != null ? dto.getNeedReview() : 0);
-} else {
-// 孩子给家长安排任务,默认需要审核
-task.setNeedReview(dto.getNeedReview() != null ? dto.getNeedReview() : 1);
-}
+        // 家长给孩子安排任务需要审核,孩子给家长安排任务也需要审核
+        if ("child".equals(executorType)) {
+            task.setNeedReview(dto.getNeedReview() != null ? dto.getNeedReview() : 0);
+        } else {
+            // 孩子给家长安排任务,默认需要审核
+            task.setNeedReview(dto.getNeedReview() != null ? dto.getNeedReview() : 1);
+        }
 
-task.setReviewByCategory(dto.getReviewByCategory() != null ? dto.getReviewByCategory() : 0);
-task.setStatus("pending");
-task.setCreatedAt(new Date());
-task.setUpdatedAt(new Date());
-taskMapper.insert(task);
-return task.getId();
-}
+        task.setReviewByCategory(dto.getReviewByCategory() != null ? dto.getReviewByCategory() : 0);
+        task.setStatus("pending");
+        task.setCreatedAt(new Date());
+        task.setUpdatedAt(new Date());
+        
+        return task;
+    }
 
     public List<Task> getTodayTasks(Long childId) {
         Calendar calendar = Calendar.getInstance();

+ 1 - 1
zxyj-backend/src/main/resources/schema.sql

@@ -62,7 +62,7 @@ CREATE TABLE IF NOT EXISTS tasks (
     id BIGINT AUTO_INCREMENT PRIMARY KEY,
     family_id BIGINT NOT NULL,
     creator_id BIGINT NOT NULL,
-    child_id BIGINT NOT NULL,
+    child_id BIGINT COMMENT '执行者ID(child任务时必填,parent任务时可空)',
     executor_type VARCHAR(20) COMMENT '执行者类型: child/parent',
     executor_id BIGINT COMMENT '执行者ID',
     title VARCHAR(200) NOT NULL,