|
|
@@ -15,7 +15,8 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
import java.io.IOException;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
-
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
/**
|
|
|
* @author BaiBai
|
|
|
* @date 2025-02-10 17:03
|
|
|
@@ -25,39 +26,70 @@ import java.util.List;
|
|
|
public class BehaviorRecordServiceImpl implements BehaviorRecordService {
|
|
|
|
|
|
private final BehaviorRecordMapper behaviorRecordMapper;
|
|
|
-
|
|
|
- private final OssUtil ossUtil;
|
|
|
+ private final ObjectMapper objectMapper;
|
|
|
|
|
|
@Autowired
|
|
|
public BehaviorRecordServiceImpl(BehaviorRecordMapper behaviorRecordMapper, OssUtil ossUtil) {
|
|
|
this.behaviorRecordMapper = behaviorRecordMapper;
|
|
|
- this.ossUtil = ossUtil;
|
|
|
+ this.objectMapper = new ObjectMapper();
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public List<BehaviorRecord> find(Long assignmentId, Long studentId) {
|
|
|
- QueryWrapper<BehaviorRecord> queryWrapper = new QueryWrapper<>();
|
|
|
- queryWrapper.eq("assignment_id", assignmentId);
|
|
|
- queryWrapper.eq("student_id", studentId);
|
|
|
- queryWrapper.orderByDesc("time");
|
|
|
- return behaviorRecordMapper.selectList(queryWrapper);
|
|
|
+ // 创建一个按 created_at 降序排序的 Sort 对象,它不会引起额外的排序开销(因为索引已经支持);显式指定排序确保代码的兼容性
|
|
|
+ Sort sort = Sort.by(Sort.Direction.DESC, "created_at");
|
|
|
+ return behaviorRecordMapper.findByAssignmentIdAndStudentId(assignmentId, studentId, sort);
|
|
|
}
|
|
|
|
|
|
+// public String upload(Long assignmentId, Long studentId, MultipartFile file) {
|
|
|
+// try {
|
|
|
+// Date now = new Date();
|
|
|
+// String filePath = "uploads/" + assignmentId + "_" + studentId + "_" + now.getTime() + file.getOriginalFilename();
|
|
|
+// String urlStr = ossUtil.uploadFile(file.getInputStream(), filePath);
|
|
|
+// String url = urlStr.split("\\?")[0];
|
|
|
+// BehaviorRecord behaviorRecord = new BehaviorRecord();
|
|
|
+// behaviorRecord.setAssignmentId(assignmentId);
|
|
|
+// behaviorRecord.setStudentId(studentId);
|
|
|
+// behaviorRecord.setRecordFileUrl(url);
|
|
|
+// behaviorRecord.setTime(now);
|
|
|
+// behaviorRecordMapper.insert(behaviorRecord);
|
|
|
+// return url;
|
|
|
+// } catch (IOException e) {
|
|
|
+// log.error(e.getMessage());
|
|
|
+// throw MyException.create(HttpStatus.BAD_REQUEST, "文件上传失败");
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
public String upload(Long assignmentId, Long studentId, MultipartFile file) {
|
|
|
try {
|
|
|
- Date now = new Date();
|
|
|
- String filePath = "uploads/" + assignmentId + "_" + studentId + "_" + now.getTime() + file.getOriginalFilename();
|
|
|
- String urlStr = ossUtil.uploadFile(file.getInputStream(), filePath);
|
|
|
- String url = urlStr.split("\\?")[0];
|
|
|
- BehaviorRecord behaviorRecord = new BehaviorRecord();
|
|
|
- behaviorRecord.setAssignmentId(assignmentId);
|
|
|
- behaviorRecord.setStudentId(studentId);
|
|
|
- behaviorRecord.setRecordFileUrl(url);
|
|
|
- behaviorRecord.setTime(now);
|
|
|
- behaviorRecordMapper.insert(behaviorRecord);
|
|
|
- return url;
|
|
|
+ // 解析JSON文件
|
|
|
+ List<BehaviorRecord.BehaviorEvent> events = parseBehaviorEvents(file);
|
|
|
+
|
|
|
+ // 创建行为记录对象
|
|
|
+ BehaviorRecord record = new BehaviorRecord();
|
|
|
+ record.setStudentId(studentId);
|
|
|
+ record.setAssignmentId(assignmentId);
|
|
|
+ record.setEvents(events);
|
|
|
+
|
|
|
+ // 保存到MongoDB
|
|
|
+ behaviorRecordMapper.save(record);
|
|
|
+ return "行为数据保存成功";
|
|
|
} catch (IOException e) {
|
|
|
log.error(e.getMessage());
|
|
|
- throw MyException.create(HttpStatus.BAD_REQUEST, "文件上传失败");
|
|
|
+ throw MyException.create(HttpStatus.BAD_REQUEST, "行为数据保存失败");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private List<BehaviorRecord.BehaviorEvent> parseBehaviorEvents(MultipartFile file)
|
|
|
+ throws IOException {
|
|
|
+ // 使用Jackson解析JSON数组
|
|
|
+ return objectMapper.readValue(
|
|
|
+ file.getInputStream(),
|
|
|
+ objectMapper.getTypeFactory().constructCollectionType(
|
|
|
+ List.class,
|
|
|
+ BehaviorRecord.BehaviorEvent.class
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|