Ver código fonte

feat: 实现行为记录版本化功能

- 新增 BehaviorRecord.recordType 字段,支持 STAGE/SUBMIT 两种类型
- 修改 BehaviorRecordController 接口,新增 recordType 参数(默认 STAGE)
- 优化 BehaviorRecordServiceImpl.findVersions() 逻辑:
  * 只在 recordType 为 SUBMIT 时生成版本快照
  * STAGE 类型记录用于累积事件,不生成版本
  * 版本间时间戳保持连续性
- 更新 BehaviorRecordService 接口签名
- 移除未使用的导入

功能说明:
- STAGE: 自动暂存,累积行为事件但不生成版本
- SUBMIT: 用户主动提交时创建版本快照并重置累积状态
wuzilong 4 meses atrás
pai
commit
9c63e238c0

+ 3 - 2
src/main/java/com/njuzr/eaibackend/controller/BehaviorRecordController.java

@@ -85,9 +85,10 @@ public class BehaviorRecordController {
     public MyResponse addBehaviorRecord(
             @AuthenticationPrincipal(expression = "id") Long studentId,
             @RequestParam Long assignmentId,
-            @RequestParam("file") MultipartFile file
+            @RequestParam("file") MultipartFile file,
+            @RequestParam(value = "recordType", defaultValue = "STAGE") String recordType
     ){
-        return MyResponse.success(behaviorRecordService.upload(assignmentId, studentId, file));
+        return MyResponse.success(behaviorRecordService.upload(assignmentId, studentId, file, recordType));
     }
 
     /**

+ 3 - 0
src/main/java/com/njuzr/eaibackend/po/BehaviorRecord.java

@@ -36,6 +36,9 @@ public class BehaviorRecord {
 //    @Indexed
     private Date createdAt = new Date(); // 记录创建时间
 
+    @Field("record_type")
+    private String recordType = "STAGE"; // 行为记录类型:STAGE/ SUBMIT
+
     private List<BehaviorEvent> events; // 行为事件集合
 
     /**

+ 1 - 1
src/main/java/com/njuzr/eaibackend/service/BehaviorRecordService.java

@@ -15,7 +15,7 @@ import java.util.List;
  */
 public interface BehaviorRecordService {
     List<BehaviorRecordItemVO> find(Long assignmentId, Long studentId);
-    String upload(Long assignmentId, Long studentId, MultipartFile file);
+    String upload(Long assignmentId, Long studentId, MultipartFile file, String recordType);
 
     BehaviorRecordVO findALL(Long assignmentId, Long studentId);
 

+ 23 - 15
src/main/java/com/njuzr/eaibackend/service/impl/BehaviorRecordServiceImpl.java

@@ -125,12 +125,7 @@ public class BehaviorRecordServiceImpl implements BehaviorRecordService {
         int accumulatedLongPauseCount = 0;
         Long lastEndStamp = 0L;
         
-        for (int i = 0; i < records.size(); i++) {
-            BehaviorRecord record = records.get(i);
-            BehaviorRecordVersionsVO.VersionEvents version = new BehaviorRecordVersionsVO.VersionEvents();
-            version.setVersion(i + 1);
-            version.setCreatedAt(record.getCreatedAt());
-            
+        for (BehaviorRecord record : records) {
             List<BehaviorRecord.BehaviorEvent> events = record.getEvents();
             if (events == null) {
                 events = new java.util.ArrayList<>();
@@ -198,14 +193,26 @@ public class BehaviorRecordServiceImpl implements BehaviorRecordService {
             accumulatedCopyCharacterCount += copyCharacterCount;
             accumulatedLongPauseCount += longPauseCount;
             
-            version.setEvents(new java.util.ArrayList<>(accumulatedEvents));
-            version.setInsertCount(accumulatedInsertCount);
-            version.setDeleteCount(accumulatedDeleteCount);
-            version.setCopyCount(accumulatedCopyCount);
-            version.setCopyCharacterCount(accumulatedCopyCharacterCount);
-            version.setLongPauseCount(accumulatedLongPauseCount);
-            
-            versions.add(version);
+            if ("SUBMIT".equalsIgnoreCase(record.getRecordType())) {
+                BehaviorRecordVersionsVO.VersionEvents version = new BehaviorRecordVersionsVO.VersionEvents();
+                version.setVersion(versions.size() + 1);
+                version.setCreatedAt(record.getCreatedAt());
+                version.setEvents(new java.util.ArrayList<>(accumulatedEvents));
+                version.setInsertCount(accumulatedInsertCount);
+                version.setDeleteCount(accumulatedDeleteCount);
+                version.setCopyCount(accumulatedCopyCount);
+                version.setCopyCharacterCount(accumulatedCopyCharacterCount);
+                version.setLongPauseCount(accumulatedLongPauseCount);
+                versions.add(version);
+                
+                accumulatedEvents = new java.util.ArrayList<>();
+                accumulatedInsertCount = 0;
+                accumulatedDeleteCount = 0;
+                accumulatedCopyCount = 0;
+                accumulatedCopyCharacterCount = 0;
+                accumulatedLongPauseCount = 0;
+                lastEndStamp = 0L;
+            }
         }
         
         vo.setVersions(versions);
@@ -302,7 +309,7 @@ public class BehaviorRecordServiceImpl implements BehaviorRecordService {
 //    }
 
 
-    public String upload(Long assignmentId, Long studentId, MultipartFile file) {
+    public String upload(Long assignmentId, Long studentId, MultipartFile file, String recordType) {
         try {
             // 解析JSON文件
             List<BehaviorRecord.BehaviorEvent> events = parseBehaviorEvents(file);
@@ -311,6 +318,7 @@ public class BehaviorRecordServiceImpl implements BehaviorRecordService {
             BehaviorRecord record = new BehaviorRecord();
             record.setStudentId(studentId);
             record.setAssignmentId(assignmentId);
+            record.setRecordType(recordType == null ? "STAGE" : recordType);
             record.setEvents(events);
 
             // 保存到MongoDB (behaviorRecords集合)