Przeglądaj źródła

feature 代码提交

xiahan 1 rok temu
rodzic
commit
7437555109

+ 16 - 5
rw-training/training-common/src/main/java/com/rongwei/trainingcommon/sys/service/ExportTrainingDemandService.java

@@ -9,17 +9,20 @@ import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 
 import javax.servlet.http.HttpServletResponse;
+import java.io.InputStream;
 
 
 public interface ExportTrainingDemandService {
     void generatePaper(String surveyId, HttpServletResponse response);
 
-    void  generateUserPaper(String surveyId,String userId, HttpServletResponse response);
+    void generateUserPaper(String surveyId, String userId, HttpServletResponse response);
 
-    default void exportSingleWord(String[] wordKey, String[] wordValue, DataTable dataTable, HttpServletResponse response, String tempPatch) {
-        try {
-            Resource resource = new ClassPathResource(tempPatch);
-            Document doc = new Document(resource.getInputStream());
+    void generateRecord(String surveyId, HttpServletResponse response);
+
+    default Document wordDrawing(String[] wordKey, String[] wordValue, DataTable dataTable, String tempPatch) {
+        Resource resource = new ClassPathResource(tempPatch);
+        try (InputStream inputStream = resource.getInputStream()){
+            Document doc = new Document(inputStream);
             doc.getMailMerge().execute(wordKey, wordValue);
             doc.getMailMerge().executeWithRegions(dataTable);
             //自定义字段
@@ -28,6 +31,14 @@ public interface ExportTrainingDemandService {
             doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS);
             doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS);
             doc.getMailMerge().deleteFields();
+            return doc;
+        } catch (Exception e) {
+            throw new CustomException("word生成异常");
+        }
+    }
+
+    default void exportSingleWord(Document doc, HttpServletResponse response) {
+        try {
             response.setContentType("application/octet-stream;charset=ISO8859-1");
             response.setHeader("Content-Disposition", "attachment;filename=paper.docx");
             doc.save(response.getOutputStream(), SaveFormat.DOCX);

+ 79 - 7
rw-training/training-common/src/main/java/com/rongwei/trainingcommon/sys/service/impl/ExportTrainingDemandServiceImpl.java

@@ -1,12 +1,13 @@
 package com.rongwei.trainingcommon.sys.service.impl;
 
+import com.aspose.words.Document;
+import com.aspose.words.SaveFormat;
 import com.aspose.words.net.System.Data.DataRow;
 import com.aspose.words.net.System.Data.DataTable;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.rongwei.rwadmincommon.system.domain.SysUserDo;
-import com.rongwei.rwadmincommon.system.vo.SysUserOrgVo;
-import com.rongwei.rwadmincommon.system.vo.SysUserVo;
+import com.rongwei.rwcommon.base.BaseDo;
 import com.rongwei.rwcommon.utils.StringUtils;
+import com.rongwei.training.domain.DemandSurveyUserAnswerDo;
 import com.rongwei.training.domain.TrainingDemandSurveyBackupsDo;
 import com.rongwei.training.domain.TrainingDemandSurveyDo;
 import com.rongwei.training.vo.DemandUserVo;
@@ -25,6 +26,8 @@ import org.springframework.stereotype.Service;
 import javax.servlet.http.HttpServletResponse;
 import java.util.*;
 import java.util.stream.Collectors;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
 
 /**
  * ExportTrainingDemandService class
@@ -46,6 +49,8 @@ public class ExportTrainingDemandServiceImpl implements ExportTrainingDemandServ
     private DemandSurveyContentDao demandSurveyContentDao;
     @Autowired
     private TraningCommonDao traningCommonDao;
+    @Autowired
+    private DemandSurveyUserAnswerServiceImpl demandSurveyUserAnswerService;
     // 单选 多选
     public static final String RADIO = "1";
     // 单选 多选
@@ -97,8 +102,9 @@ public class ExportTrainingDemandServiceImpl implements ExportTrainingDemandServ
         // 获取问卷调查试卷
         List<TrainingDemandVo> surveyContent = demandSurveyContentDao.getSurveyContent(trainingDemandSurveyBackupsDos.get(0).getId(), "");
         DataTable dataTable = createDataTable(surveyContent, false);
+        Document document = wordDrawing(WORD_KEY, createWordValue(trainingDemandSurveyDo, null, ""), dataTable, DOCX_FILE_PATH);
         // 文件导出
-        exportSingleWord(WORD_KEY, createWordValue(trainingDemandSurveyDo, null, ""), dataTable, response, DOCX_FILE_PATH);
+        exportSingleWord(document, response);
     }
 
     /**
@@ -113,18 +119,84 @@ public class ExportTrainingDemandServiceImpl implements ExportTrainingDemandServ
             throw new RuntimeException("参数异常");
         }
         TrainingDemandSurveyBackupsDo trainingDemandSurveyBackupsDo = trainingDemandSurveyBackupsDao.selectById(surveyId);
-        if(trainingDemandSurveyBackupsDo==null){
+        if (trainingDemandSurveyBackupsDo == null) {
             throw new RuntimeException("当前计划已删除");
         }
         TrainingDemandSurveyDo trainingDemandSurveyDo = trainingDemandSurveyDao.selectById(trainingDemandSurveyBackupsDo.getOldSurveyId());
-        if(trainingDemandSurveyDo==null){
+        if (trainingDemandSurveyDo == null) {
             throw new RuntimeException("当前计划已删除");
         }
         List<TrainingDemandVo> surveyContent = demandSurveyContentDao.getSurveyContent(surveyId, userId);
         List<DemandUserVo> demandUserInfo = traningCommonDao.getDemandUserInfo(Arrays.asList(userId));
         DataTable dataTable = createDataTable(surveyContent, true);
+        // 绘制word
+        Document document = wordDrawing(WORD_KEY, createWordValue(trainingDemandSurveyDo, demandUserInfo, userId), dataTable, DOCX_FILE_PATH);
         // 文件导出
-        exportSingleWord(WORD_KEY, createWordValue(trainingDemandSurveyDo, demandUserInfo, userId), dataTable, response, DOCX_FILE_PATH);
+        exportSingleWord(document, response);
+    }
+
+
+    /**
+     * 生成所有已填写问卷调查记录的数据
+     *
+     * @param surveyId
+     * @param response
+     */
+    @Override
+    public void generateRecord(String surveyId, HttpServletResponse response) {
+        log.info("开始生成问卷调查试卷");
+        if (StringUtils.isBlank(surveyId)) {
+            log.error("参数异常");
+            return;
+        }
+        // 判断问卷调查是否生成
+        TrainingDemandSurveyDo trainingDemandSurveyDo = trainingDemandSurveyDao.selectById(surveyId);
+        if (trainingDemandSurveyDo == null) {
+            log.error("问卷调查已删除");
+            return;
+        }
+        List<TrainingDemandSurveyBackupsDo> trainingDemandSurveyBackupsDos = trainingDemandSurveyBackupsDao.selectList(new LambdaQueryWrapper<TrainingDemandSurveyBackupsDo>()
+                .eq(TrainingDemandSurveyBackupsDo::getOldSurveyId, surveyId));
+        if (trainingDemandSurveyBackupsDos.isEmpty()) {
+            log.error("问卷调查暂未发布");
+            return;
+        }
+        String backupId = trainingDemandSurveyBackupsDos.get(0).getId();
+        // 获取问卷调查题目信息
+        List<TrainingDemandVo> surveyContent = demandSurveyContentDao.getSurveyContent(backupId, "");
+        // 获取用户填报记录
+        List<DemandSurveyUserAnswerDo> userAnswer = demandSurveyUserAnswerService.list(new LambdaQueryWrapper<DemandSurveyUserAnswerDo>().eq(DemandSurveyUserAnswerDo::getSurverid, backupId));
+        if (userAnswer.isEmpty()) {
+            log.error("参数异常");
+            throw new RuntimeException("当前调查暂无人填写问卷信息");
+        }
+        // 对问卷答案按照填写人进行分组
+        Map<String, List<DemandSurveyUserAnswerDo>> collect = userAnswer.stream().collect(Collectors.groupingBy(BaseDo::getCreateuserid));
+
+        List<String> userIds = userAnswer.stream().map(BaseDo::getCreateuserid).distinct().collect(Collectors.toList());
+        List<DemandUserVo> demandUserInfo = traningCommonDao.getDemandUserInfo(userIds);
+
+        Map<String, Document> documentMap = new HashMap<>();
+        for (Map.Entry<String, List<DemandSurveyUserAnswerDo>> entry : collect.entrySet()) {
+            List<DemandSurveyUserAnswerDo> userAnswerList = entry.getValue();
+            surveyContent.forEach(info -> {
+                Optional<DemandSurveyUserAnswerDo> first = userAnswerList.stream().filter(answer -> answer.getSurvercontentid().equals(info.getContentid())).findFirst();
+                info.setUserAnswer(first.isPresent() ? first.get().getAnswer() : "");
+            });
+            DataTable dataTable = createDataTable(surveyContent, true);
+            // 绘制word
+            Document document = wordDrawing(WORD_KEY, createWordValue(trainingDemandSurveyDo, demandUserInfo, entry.getKey()), dataTable, DOCX_FILE_PATH);
+            documentMap.put(userAnswerList.get(0).getCreateusername(), document);
+        }
+        try (ZipOutputStream outputStream = new ZipOutputStream(response.getOutputStream());) {
+            for (Map.Entry<String, Document> entry : documentMap.entrySet()) {
+                ZipEntry zipEntry = new ZipEntry(entry.getKey() + ".docx");
+                outputStream.putNextEntry(zipEntry);
+                entry.getValue().save(outputStream, SaveFormat.DOCX);
+            }
+        } catch (Exception e) {
+            log.error("压缩包生成异常");
+        }
     }
 
 

+ 6 - 0
rw-training/training-server/src/main/java/com/rongwei/training/controller/TrainingDemandController.java

@@ -96,4 +96,10 @@ public class TrainingDemandController {
         exportTrainingDemandService.generateUserPaper(surveyId, userId, response);
     }
 
+    @PostMapping("/records")
+    public void generateRecord(@RequestParam(name = "surveyId") String surveyId,
+                              HttpServletResponse response) {
+        log.info("开始生成培训问卷");
+        exportTrainingDemandService.generateRecord(surveyId, response);
+    }
 }