|
|
@@ -3,6 +3,7 @@ package com.njuzr.eaibackend.service;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.njuzr.eaibackend.exception.MyException;
|
|
|
import com.njuzr.eaibackend.mapper.EngagementMapper;
|
|
|
+import com.njuzr.eaibackend.mapper.serviceIpml.EngagementMapperServiceImpl;
|
|
|
import com.njuzr.eaibackend.po.Engagement;
|
|
|
import com.njuzr.eaibackend.utils.WordExporter;
|
|
|
import com.njuzr.eaibackend.utils.ZipExport;
|
|
|
@@ -10,22 +11,14 @@ import com.njuzr.eaibackend.vo.AssignmentVO;
|
|
|
import com.njuzr.eaibackend.vo.EngagementVO;
|
|
|
import com.njuzr.eaibackend.vo.UserVO;
|
|
|
import freemarker.template.Configuration;
|
|
|
-import freemarker.template.Template;
|
|
|
import freemarker.template.TemplateException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
-import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
|
|
-import org.apache.poi.xwpf.usermodel.XWPFRun;
|
|
|
-import org.jsoup.Jsoup;
|
|
|
-import org.jsoup.nodes.Element;
|
|
|
-import org.jsoup.nodes.Node;
|
|
|
-import org.jsoup.nodes.TextNode;
|
|
|
+import org.apache.commons.lang3.tuple.Pair;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -44,34 +37,59 @@ public class ExportCompositionService {
|
|
|
private UserService userService;
|
|
|
@Autowired
|
|
|
private WordExporter wordExporter;
|
|
|
+ @Autowired
|
|
|
+ private EngagementMapperServiceImpl engagementMapperServiceImpl;
|
|
|
|
|
|
- public byte[] exportWordReal(Long assignmentId, Long studentId) {
|
|
|
+ public Pair<UserVO, byte[]> exportWord(Long assignmentId, Long studentId) {
|
|
|
AssignmentVO assignment = assignmentService.findAssignmentById(assignmentId);
|
|
|
EngagementVO engagement = assignmentService.getEngagement(studentId, assignmentId);
|
|
|
UserVO user = userService.searchUsers(new Page<>(1,1), studentId, null, null, null).getRecords().get(0);
|
|
|
|
|
|
+ Map<String, Object> model = buildWordInfoModel(assignment, user, engagement);
|
|
|
+
|
|
|
+ return Pair.of(user, wordExporter.exportCompositionWord(model));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> buildWordInfoModel(AssignmentVO assignment, UserVO user, EngagementVO engagement) {
|
|
|
Map<String, Object> model = new HashMap<>();
|
|
|
model.put("title", assignment.getAssignmentName());
|
|
|
model.put("studentName", user.getName());
|
|
|
+ model.put("officialNumber", user.getOfficialNumber());
|
|
|
model.put("assignmentName", assignment.getAssignmentName());
|
|
|
model.put("date", new Date());
|
|
|
model.put("content", processContent(engagement.getTextContent()));
|
|
|
+ return model;
|
|
|
+ }
|
|
|
|
|
|
- return wordExporter.exportCompositionWord(model);
|
|
|
+ public byte[] exportBatchToZip(List<Long> studentIds, Long assignmentId) {
|
|
|
+ if(assignmentId == null){
|
|
|
+ throw MyException.create(HttpStatus.BAD_REQUEST, "assignmentId不能为空");
|
|
|
+ }
|
|
|
|
|
|
+ if (CollectionUtils.isEmpty(studentIds)) {
|
|
|
+ List<Engagement> engagements = engagementMapperServiceImpl.getEngagementByAssignmentId(assignmentId);
|
|
|
+ if (CollectionUtils.isEmpty(engagements)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ studentIds = engagements.stream()
|
|
|
+ .map(Engagement::getStudentId)
|
|
|
+ .toList();
|
|
|
+ }
|
|
|
+ return exportBatchToZipByStudentIds(studentIds, assignmentId);
|
|
|
}
|
|
|
|
|
|
- public byte[] exportBatchToZip(List<Long> studentIds, Long assignmentId) {
|
|
|
+ private byte[] exportBatchToZipByStudentIds(List<Long> studentIds, Long assignmentId) {
|
|
|
List<ZipExport.ZipItem> zipItems = studentIds.stream()
|
|
|
- .map(id -> {
|
|
|
+ .map(studentId -> {
|
|
|
try {
|
|
|
- byte[] wordBytes = exportWord(assignmentId, id);
|
|
|
- String fileName = String.format("%s_%d.docx",
|
|
|
- "作业", id);
|
|
|
- log.info("导出成功 studentId={}", id);
|
|
|
+ Pair<UserVO, byte[]> pair = exportWord(assignmentId, studentId);
|
|
|
+ UserVO student = pair.getLeft();
|
|
|
+ byte[] wordBytes = pair.getRight();
|
|
|
+ String fileName = String.format("%s_%s.%s", student.getName(), student.getOfficialNumber(), "docx");
|
|
|
return new ZipExport.ZipItem(fileName, wordBytes);
|
|
|
} catch (Exception e) {
|
|
|
- log.error("导出失败 studentId={}", id, e);
|
|
|
+ log.error("exportBatchToZip failed, studentId: {}, reason: {}", studentId, e.getMessage());
|
|
|
return null;
|
|
|
}
|
|
|
})
|
|
|
@@ -86,7 +104,7 @@ public class ExportCompositionService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public byte[] exportWord(Long assignmentId, Long studentId) throws IOException, TemplateException {
|
|
|
+ public byte[] exportWordTest(Long assignmentId, Long studentId) throws IOException, TemplateException {
|
|
|
|
|
|
String assignmentName = "My Favorite Season";
|
|
|
String content = "My favorite season is autumn. The weather is cool and comfortable, and the leaves turn golden, making the scenery beautiful. I enjoy walking in the park and listening to the sound of rustling leaves. Autumn is also a harvest season, with fruits like apples and pears ripe for picking.\n" +
|