ExportController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package com.njuzr.eaibackend.controller;
  2. import com.njuzr.eaibackend.mapper.UserMapper;
  3. import com.njuzr.eaibackend.po.User;
  4. import com.njuzr.eaibackend.service.ExportCompositionService;
  5. import com.njuzr.eaibackend.service.ExportBehaviorOverviewService;
  6. import com.njuzr.eaibackend.service.UserService;
  7. import com.njuzr.eaibackend.service.AssignmentService;
  8. import com.njuzr.eaibackend.service.ClassService;
  9. import com.njuzr.eaibackend.vo.UserVO;
  10. import com.njuzr.eaibackend.vo.AssignmentVO;
  11. import freemarker.template.TemplateException;
  12. import org.apache.commons.lang3.tuple.Pair;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.http.ContentDisposition;
  15. import org.springframework.http.HttpHeaders;
  16. import org.springframework.http.MediaType;
  17. import org.springframework.http.ResponseEntity;
  18. import org.springframework.web.bind.annotation.*;
  19. import java.io.IOException;
  20. import java.net.URLEncoder;
  21. import java.nio.charset.StandardCharsets;
  22. import java.util.Date;
  23. import java.util.List;
  24. @RestController
  25. @RequestMapping("/api/export")
  26. public class ExportController {
  27. @Autowired
  28. private ExportCompositionService exportCompositionService;
  29. @Autowired
  30. private ExportBehaviorOverviewService exportBehaviorOverviewService;
  31. @Autowired
  32. private UserService userService;
  33. @Autowired
  34. private UserMapper userMapper;
  35. @Autowired
  36. private AssignmentService assignmentService;
  37. @Autowired
  38. private ClassService classService;
  39. @GetMapping("/composition/word")
  40. public ResponseEntity<byte[]> exportWord(@RequestParam Long assignmentId, @RequestParam Long studentId) throws TemplateException, IOException {
  41. Pair<UserVO, byte[]> pair = exportCompositionService.exportWord(assignmentId, studentId);
  42. UserVO student = pair.getLeft();
  43. byte[] wordBytes = pair.getRight();
  44. HttpHeaders headers = new HttpHeaders();
  45. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  46. headers.setContentDispositionFormData("attachment",
  47. URLEncoder.encode(String.format("%s_%s.%s", student.getName(), student.getOfficialNumber(), "docx"), StandardCharsets.UTF_8));
  48. return ResponseEntity.ok()
  49. .headers(headers)
  50. .body(wordBytes);
  51. }
  52. @GetMapping("/composition/zip")
  53. public ResponseEntity<byte[]> exportBatch(@RequestParam Long assignmentId, @RequestParam(required = false) List<Long> studentIds) {
  54. byte[] zipBytes = exportCompositionService.exportBatchToZip(studentIds, assignmentId);
  55. return ResponseEntity.ok()
  56. .header(HttpHeaders.CONTENT_DISPOSITION,
  57. "attachment; filename=compositions.zip")
  58. .contentType(MediaType.APPLICATION_OCTET_STREAM)
  59. .body(zipBytes);
  60. }
  61. @GetMapping("/behaviorOverview/excel")
  62. public ResponseEntity<byte[]> exportBehaviorOverviewExcel(@RequestParam Long assignmentId, @RequestParam Long studentId) {
  63. byte[] bytes = exportBehaviorOverviewService.exportBehaviorOverviewExcel(assignmentId, studentId);
  64. HttpHeaders headers = new HttpHeaders();
  65. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  66. // 查询学生信息
  67. String name = null;
  68. String officialNumber = null;
  69. try {
  70. User user = userMapper.selectById(studentId);
  71. if (user != null) {
  72. name = user.getName();
  73. officialNumber = user.getOfficialNumber();
  74. }
  75. } catch (Exception ignored) { }
  76. // 查询作业名称
  77. String assignmentName = null;
  78. try {
  79. AssignmentVO vo = assignmentService.findAssignmentById(assignmentId);
  80. assignmentName = vo != null ? vo.getAssignmentName() : null;
  81. } catch (Exception ignored) { }
  82. String fileName;
  83. if (name == null || name.isEmpty() || officialNumber == null) {
  84. fileName = String.format("行为数据记录_%d_%d.xlsx", studentId, assignmentId);
  85. } else if (assignmentName == null || assignmentName.isEmpty()) {
  86. fileName = String.format("行为数据记录_%s_%s.xlsx", officialNumber, name);
  87. } else {
  88. fileName = String.format("行为数据记录_%s_%s_%s.xlsx", officialNumber, name, assignmentName);
  89. }
  90. headers.setContentDisposition(ContentDisposition.builder("attachment")
  91. .filename(fileName, StandardCharsets.UTF_8)
  92. .build());
  93. return ResponseEntity.ok()
  94. .headers(headers)
  95. .body(bytes);
  96. }
  97. @GetMapping("/behaviorOverview/excel/assignment")
  98. public ResponseEntity<byte[]> exportAssignmentBehaviorOverviewExcel(@RequestParam Long assignmentId) {
  99. byte[] bytes = exportBehaviorOverviewService.exportAssignmentBehaviorOverviewExcel(assignmentId);
  100. HttpHeaders headers = new HttpHeaders();
  101. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  102. String assignmentName = null;
  103. try {
  104. AssignmentVO vo = assignmentService.findAssignmentById(assignmentId);
  105. assignmentName = vo != null ? vo.getAssignmentName() : null;
  106. } catch (Exception ignored) { }
  107. String fileName = assignmentName == null || assignmentName.isEmpty()
  108. ? String.format("行为数据记录_%d.xlsx", assignmentId)
  109. : String.format("行为数据记录_%d_%s.xlsx", assignmentId, assignmentName);
  110. headers.setContentDisposition(ContentDisposition.builder("attachment")
  111. .filename(fileName, StandardCharsets.UTF_8)
  112. .build());
  113. return ResponseEntity.ok()
  114. .headers(headers)
  115. .body(bytes);
  116. }
  117. @GetMapping("/pauseStatistics/excel/assignment")
  118. public ResponseEntity<byte[]> exportAssignmentPauseStatisticsExcel(
  119. @RequestParam Long assignmentId,
  120. @RequestParam(defaultValue = "2000") Integer threshold) {
  121. byte[] bytes = exportBehaviorOverviewService.exportAssignmentPauseStatisticsExcel(assignmentId, threshold);
  122. HttpHeaders headers = new HttpHeaders();
  123. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  124. String assignmentName = null;
  125. try {
  126. AssignmentVO vo = assignmentService.findAssignmentById(assignmentId);
  127. assignmentName = vo != null ? vo.getAssignmentName() : null;
  128. } catch (Exception ignored) { }
  129. String fileName = assignmentName == null || assignmentName.isEmpty()
  130. ? String.format("pause_statistics_assignment_%d_threshold_%d.xlsx", assignmentId, threshold)
  131. : String.format("pause_statistics_assignment_%d_%s_threshold_%d.xlsx", assignmentId, assignmentName, threshold);
  132. headers.setContentDisposition(ContentDisposition.builder("attachment")
  133. .filename(fileName, StandardCharsets.UTF_8)
  134. .build());
  135. return ResponseEntity.ok()
  136. .headers(headers)
  137. .body(bytes);
  138. }
  139. @GetMapping("/behaviorOverview/excel/class")
  140. public ResponseEntity<byte[]> exportClassBehaviorOverviewExcel(@RequestParam Long classId) {
  141. byte[] bytes = exportBehaviorOverviewService.exportClassBehaviorOverviewExcel(classId);
  142. HttpHeaders headers = new HttpHeaders();
  143. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  144. String className = null;
  145. try {
  146. com.njuzr.eaibackend.po.Class cls = classService.getClassById(classId);
  147. className = cls != null ? cls.getClassName() : null;
  148. } catch (Exception ignored) { }
  149. String fileName = className == null || className.isEmpty()
  150. ? String.format("行为数据记录_%d.xlsx", classId)
  151. : String.format("行为数据记录_%d_%s.xlsx", classId, className);
  152. headers.setContentDisposition(ContentDisposition.builder("attachment")
  153. .filename(fileName, StandardCharsets.UTF_8)
  154. .build());
  155. return ResponseEntity.ok()
  156. .headers(headers)
  157. .body(bytes);
  158. }
  159. @GetMapping("/class/download")
  160. public ResponseEntity<byte[]> downloadClassAssignments(@RequestParam Long classId) {
  161. byte[] zipBytes = exportCompositionService.downloadClassAssignments(classId);
  162. HttpHeaders headers = new HttpHeaders();
  163. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  164. String className = null;
  165. try {
  166. com.njuzr.eaibackend.po.Class cls = classService.getClassById(classId);
  167. className = cls != null ? cls.getClassName() : null;
  168. } catch (Exception ignored) { }
  169. String fileName = className == null || className.isEmpty()
  170. ? String.format("class_%d_%s.zip", classId, new java.text.SimpleDateFormat("yyyyMMdd").format(new Date()))
  171. : String.format("%s_%s.zip", className, new java.text.SimpleDateFormat("yyyyMMdd").format(new Date()));
  172. ContentDisposition contentDisposition = ContentDisposition.builder("attachment")
  173. .filename(fileName, StandardCharsets.UTF_8)
  174. .build();
  175. headers.setContentDisposition(contentDisposition);
  176. return ResponseEntity.ok()
  177. .headers(headers)
  178. .body(zipBytes);
  179. }
  180. @GetMapping("/assignment/download")
  181. public ResponseEntity<byte[]> downloadAssignmentSubmissions(@RequestParam Long assignmentId) {
  182. byte[] zipBytes = exportCompositionService.downloadAssignmentSubmissions(assignmentId);
  183. HttpHeaders headers = new HttpHeaders();
  184. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  185. String assignmentName = null;
  186. try {
  187. AssignmentVO vo = assignmentService.findAssignmentById(assignmentId);
  188. assignmentName = vo != null ? vo.getAssignmentName() : null;
  189. } catch (Exception ignored) { }
  190. String fileName = assignmentName == null || assignmentName.isEmpty()
  191. ? String.format("assignment_%d_%s.zip", assignmentId, new java.text.SimpleDateFormat("yyyyMMdd").format(new Date()))
  192. : String.format("%s_%s.zip", assignmentName, new java.text.SimpleDateFormat("yyyyMMdd").format(new Date()));
  193. ContentDisposition contentDisposition = ContentDisposition.builder("attachment")
  194. .filename(fileName, StandardCharsets.UTF_8)
  195. .build();
  196. headers.setContentDisposition(contentDisposition);
  197. return ResponseEntity.ok()
  198. .headers(headers)
  199. .body(zipBytes);
  200. }
  201. }