|
|
@@ -0,0 +1,128 @@
|
|
|
+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;
|
|
|
+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.TemplateException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+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.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class ExportCompositionService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private Configuration freemarkerConfig;
|
|
|
+ @Autowired
|
|
|
+ private EngagementMapper engagementMapper;
|
|
|
+ @Autowired
|
|
|
+ private AssignmentService assignmentService;
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+ @Autowired
|
|
|
+ private WordExporter wordExporter;
|
|
|
+ @Autowired
|
|
|
+ private EngagementMapperServiceImpl engagementMapperServiceImpl;
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ private byte[] exportBatchToZipByStudentIds(List<Long> studentIds, Long assignmentId) {
|
|
|
+ List<ZipExport.ZipItem> zipItems = studentIds.stream()
|
|
|
+ .map(studentId -> {
|
|
|
+ try {
|
|
|
+ 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("exportBatchToZip failed, studentId: {}, reason: {}", studentId, e.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ try {
|
|
|
+ return ZipExport.packToZip(zipItems);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw MyException.create(HttpStatus.INTERNAL_SERVER_ERROR, "ZIP打包失败"+e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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" +
|
|
|
+ "During this season, my family often goes hiking or has picnics. The Mid-Autumn Festival, when we eat mooncakes and admire the full moon, is my favorite holiday. Autumn reminds me of warmth and joy.\n" +
|
|
|
+ "In short, autumn is not only pleasant but also full of happy memories. That’s why I love it most.";
|
|
|
+
|
|
|
+ Map<String, Object> model = new HashMap<>();
|
|
|
+ model.put("title", "title_"+assignmentName);
|
|
|
+ model.put("studentName", "柏琪");
|
|
|
+ model.put("assignmentName", assignmentName);
|
|
|
+ model.put("date", new Date());
|
|
|
+ model.put("content", content.replaceAll("\n", "<br/>"));
|
|
|
+
|
|
|
+ return wordExporter.exportCompositionWord(model);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String processContent(String content) {
|
|
|
+ return content.replaceAll("\n", "<br/>");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|