|
|
@@ -6,6 +6,7 @@ import com.njuzr.eaibackend.mapper.BehaviorRecordMapper;
|
|
|
import com.njuzr.eaibackend.po.BehaviorRecord;
|
|
|
import com.njuzr.eaibackend.service.BehaviorRecordService;
|
|
|
import com.njuzr.eaibackend.utils.OssUtil;
|
|
|
+import com.njuzr.eaibackend.vo.BehaviorRecordVO;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
@@ -15,8 +16,17 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
import java.io.IOException;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
+import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
+import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
+import org.springframework.data.mongodb.core.aggregation.Aggregation;
|
|
|
+import org.springframework.data.mongodb.core.aggregation.AggregationResults;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
/**
|
|
|
* @author BaiBai
|
|
|
* @date 2025-02-10 17:03
|
|
|
@@ -27,20 +37,82 @@ public class BehaviorRecordServiceImpl implements BehaviorRecordService {
|
|
|
|
|
|
private final BehaviorRecordMapper behaviorRecordMapper;
|
|
|
private final ObjectMapper objectMapper;
|
|
|
+ private final MongoTemplate mongoTemplate;
|
|
|
|
|
|
@Autowired
|
|
|
- public BehaviorRecordServiceImpl(BehaviorRecordMapper behaviorRecordMapper, OssUtil ossUtil) {
|
|
|
+ public BehaviorRecordServiceImpl(BehaviorRecordMapper behaviorRecordMapper, OssUtil ossUtil,
|
|
|
+ MongoTemplate mongoTemplate) {
|
|
|
this.behaviorRecordMapper = behaviorRecordMapper;
|
|
|
this.objectMapper = new ObjectMapper();
|
|
|
+ this.mongoTemplate = mongoTemplate;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<BehaviorRecord> find(Long assignmentId, Long studentId) {
|
|
|
// 创建一个按 created_at 降序排序的 Sort 对象,它不会引起额外的排序开销(因为索引已经支持);显式指定排序确保代码的兼容性
|
|
|
- Sort sort = Sort.by(Sort.Direction.DESC, "created_at");
|
|
|
+ Sort sort = Sort.by(Sort.Direction.ASC, "created_at");
|
|
|
return behaviorRecordMapper.findByAssignmentIdAndStudentId(assignmentId, studentId, sort);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public BehaviorRecordVO findALL(Long assignmentId, Long studentId) {
|
|
|
+ // 使用MongoDB聚合管道,直接在数据库层面展开events并投影所需字段
|
|
|
+ Aggregation aggregation = Aggregation.newAggregation(
|
|
|
+ // 匹配条件 - 使用MongoDB字段名
|
|
|
+ Aggregation.match(Criteria.where("assignment_id").is(assignmentId)
|
|
|
+ .and("student_id").is(studentId)),
|
|
|
+ // 按创建时间降序排序 - 使用MongoDB字段名
|
|
|
+ Aggregation.sort(Sort.by(Sort.Direction.ASC, "created_at")),
|
|
|
+ // 展开events数组
|
|
|
+ Aggregation.unwind("events"),
|
|
|
+ // 投影所需字段
|
|
|
+ Aggregation.project()
|
|
|
+ .and("events.type").as("type")
|
|
|
+ .and("events.retain").as("retain")
|
|
|
+ .and("events.insert").as("insert")
|
|
|
+ .and("events.startStamp").as("startStamp")
|
|
|
+ .and("events.endStamp").as("endStamp")
|
|
|
+ .and("events.pauseTime").as("pauseTime")
|
|
|
+ .and("created_at").as("createdAt"));
|
|
|
+
|
|
|
+ AggregationResults<Map> results = mongoTemplate.aggregate(aggregation, "behaviorRecords", Map.class);
|
|
|
+ List<Map> mappedResults = results.getMappedResults();
|
|
|
+
|
|
|
+ BehaviorRecordVO vo = new BehaviorRecordVO();
|
|
|
+ vo.setStudentId(studentId);
|
|
|
+ vo.setAssignmentId(assignmentId);
|
|
|
+
|
|
|
+ if (!mappedResults.isEmpty()) {
|
|
|
+ // 获取第一条记录的创建时间
|
|
|
+ vo.setCreatedAt((Date) mappedResults.get(0).get("createdAt"));
|
|
|
+
|
|
|
+ // 转换为SimplifiedEvent列表
|
|
|
+ List<BehaviorRecordVO.SimplifiedEvent> events = mappedResults.stream()
|
|
|
+ .map(this::mapToSimplifiedEvent)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ vo.setEvents(events);
|
|
|
+ }
|
|
|
+
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为SimplifiedEvent
|
|
|
+ */
|
|
|
+ private BehaviorRecordVO.SimplifiedEvent mapToSimplifiedEvent(Map<String, Object> map) {
|
|
|
+ BehaviorRecordVO.SimplifiedEvent simplified = new BehaviorRecordVO.SimplifiedEvent();
|
|
|
+
|
|
|
+ simplified.setType((String) map.get("type"));
|
|
|
+ simplified.setRetain((Integer) map.get("retain"));
|
|
|
+ simplified.setInsert((String) map.get("insert"));
|
|
|
+ simplified.setStartStamp((Long) map.get("startStamp"));
|
|
|
+ simplified.setEndStamp((Long) map.get("endStamp"));
|
|
|
+ simplified.setPauseTime((Long) map.get("pauseTime"));
|
|
|
+
|
|
|
+ return simplified;
|
|
|
+ }
|
|
|
+
|
|
|
// public String upload(Long assignmentId, Long studentId, MultipartFile file) {
|
|
|
// try {
|
|
|
// Date now = new Date();
|