Kaynağa Gözat

成绩查询导出BUG修改

wangliang 3 yıl önce
ebeveyn
işleme
f1b2c67973

+ 164 - 110
themis-business/src/main/java/com/qmth/themis/business/templete/impl/TaskMarkResultStandardExportTemplete.java

@@ -17,18 +17,22 @@ import com.qmth.themis.business.entity.TOeExamAnswer;
 import com.qmth.themis.business.entity.TOeExamRecord;
 import com.qmth.themis.business.entity.TOeExamRecord;
 import com.qmth.themis.business.enums.AnswerTypeEnum;
 import com.qmth.themis.business.enums.AnswerTypeEnum;
 import com.qmth.themis.business.enums.QuestionTypeEnum;
 import com.qmth.themis.business.enums.QuestionTypeEnum;
+import com.qmth.themis.business.enums.TaskStatusEnum;
 import com.qmth.themis.business.service.*;
 import com.qmth.themis.business.service.*;
 import com.qmth.themis.business.templete.TaskExportCommon;
 import com.qmth.themis.business.templete.TaskExportCommon;
 import com.qmth.themis.business.templete.TaskExportTemplete;
 import com.qmth.themis.business.templete.TaskExportTemplete;
 import com.qmth.themis.business.templete.service.TempleteLogicService;
 import com.qmth.themis.business.templete.service.TempleteLogicService;
 import com.qmth.themis.common.contanst.Constants;
 import com.qmth.themis.common.contanst.Constants;
+import com.qmth.themis.common.util.FileUtil;
 import com.qmth.themis.common.util.Result;
 import com.qmth.themis.common.util.Result;
 import com.qmth.themis.common.util.ResultUtil;
 import com.qmth.themis.common.util.ResultUtil;
+import org.apache.commons.io.FileUtils;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
+import org.springframework.util.LinkedMultiValueMap;
 
 
 import javax.annotation.Resource;
 import javax.annotation.Resource;
 import java.io.*;
 import java.io.*;
@@ -80,11 +84,11 @@ public class TaskMarkResultStandardExportTemplete implements TaskExportTemplete
         TaskExportCommon taskExportCommon = new TaskExportCommon(map);
         TaskExportCommon taskExportCommon = new TaskExportCommon(map);
         taskExportCommon.init();
         taskExportCommon.init();
         taskExportCommon.setTxtList(new ArrayList());
         taskExportCommon.setTxtList(new ArrayList());
-        SXSSFWorkbook wb = null;
         StringJoiner stringJoiner = null;
         StringJoiner stringJoiner = null;
-        String excelFilePath = null;
-        InputStream inputStream = null;
-        ByteArrayOutputStream out = null;
+        String zipPath = null;
+        File zip = null;
+        List<File> files = null;
+        int finalSize = 0;
         int min = 0, line = 0;
         int min = 0, line = 0;
         try {
         try {
             taskExportCommon.getTxtList()
             taskExportCommon.getTxtList()
@@ -119,14 +123,13 @@ public class TaskMarkResultStandardExportTemplete implements TaskExportTemplete
                     }
                     }
                 }
                 }
             }
             }
-            List<MarkResultStandardExportDto> markResultStandardExportDtoList = gson
-                    .fromJson(gson.toJson(markResultSimpleExportDtoList),
-                            new TypeToken<List<MarkResultStandardExportDto>>() {
-
-                            }.getType());
+            List<MarkResultStandardExportDto> markResultStandardExportDtoList = gson.fromJson(gson.toJson(markResultSimpleExportDtoList),
+                    new TypeToken<List<MarkResultStandardExportDto>>() {
+                    }.getType());
             Map<Long, List<ExcelDto>> excelDtoMap = null;
             Map<Long, List<ExcelDto>> excelDtoMap = null;
             Map<Long, Map<String, Integer>> paperObjectiveAnswerMap = new HashMap<>();
             Map<Long, Map<String, Integer>> paperObjectiveAnswerMap = new HashMap<>();
-            Set<String> titleSet = new LinkedHashSet<>();
+            LinkedMultiValueMap<Long, ExcelDto> dynamicExcelHead = new LinkedMultiValueMap<>();
+            LinkedMultiValueMap<Long, MarkResultStandardExportDto> markResultStandardExportDtoLinkedMultiValueMap = new LinkedMultiValueMap<>();
             if (Objects.nonNull(markResultStandardExportDtoList) && markResultStandardExportDtoList.size() > 0) {
             if (Objects.nonNull(markResultStandardExportDtoList) && markResultStandardExportDtoList.size() > 0) {
                 for (MarkResultStandardExportDto m : markResultStandardExportDtoList) {
                 for (MarkResultStandardExportDto m : markResultStandardExportDtoList) {
                     excelDtoMap = new HashMap<>();
                     excelDtoMap = new HashMap<>();
@@ -141,11 +144,11 @@ public class TaskMarkResultStandardExportTemplete implements TaskExportTemplete
                                     if (v >= 1 && v < 4) {
                                     if (v >= 1 && v < 4) {
                                         ExcelDto excelAnswerDto = new ExcelDto(title + "作答");
                                         ExcelDto excelAnswerDto = new ExcelDto(title + "作答");
                                         ExcelDto excelScoreDto = new ExcelDto(title + "得分");
                                         ExcelDto excelScoreDto = new ExcelDto(title + "得分");
-                                        titleSet.add(excelAnswerDto.getTitle());
-                                        titleSet.add(excelScoreDto.getTitle());
+                                        dynamicExcelHead.add(m.getPaperId(), excelAnswerDto);
+                                        dynamicExcelHead.add(m.getPaperId(), excelScoreDto);
                                     } else {
                                     } else {
                                         ExcelDto excelScoreDto = new ExcelDto(title + "得分");
                                         ExcelDto excelScoreDto = new ExcelDto(title + "得分");
-                                        titleSet.add(excelScoreDto.getTitle());
+                                        dynamicExcelHead.add(m.getPaperId(), excelScoreDto);
                                     }
                                     }
                                 }
                                 }
                             });
                             });
@@ -206,112 +209,164 @@ public class TaskMarkResultStandardExportTemplete implements TaskExportTemplete
                         }
                         }
                     }
                     }
                     m.setAnswerExcetDto(excelDtoMap);
                     m.setAnswerExcetDto(excelDtoMap);
+                    markResultStandardExportDtoLinkedMultiValueMap.add(m.getPaperId(), m);
                 }
                 }
             }
             }
+            files = Objects.isNull(files) ? new ArrayList<>() : files;
+
             boolean oss = (boolean) taskExportCommon.getOssEnv().get(SystemConstant.OSS);
             boolean oss = (boolean) taskExportCommon.getOssEnv().get(SystemConstant.OSS);
             stringJoiner = new StringJoiner("");
             stringJoiner = new StringJoiner("");
+            StringJoiner excelSj = new StringJoiner("");
             if (!oss) {
             if (!oss) {
                 stringJoiner.add(SystemConstant.FILES_DIR).add(File.separator);
                 stringJoiner.add(SystemConstant.FILES_DIR).add(File.separator);
             }
             }
+            excelSj.add(SystemConstant.FILES_DIR).add(File.separator);
             LocalDateTime nowTime = LocalDateTime.now();
             LocalDateTime nowTime = LocalDateTime.now();
             stringJoiner.add(String.valueOf(nowTime.getYear())).add(File.separator)
             stringJoiner.add(String.valueOf(nowTime.getYear())).add(File.separator)
                     .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
                     .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
                     .add(String.format("%02d", nowTime.getDayOfMonth()));
                     .add(String.format("%02d", nowTime.getDayOfMonth()));
-            //创建excel
-            wb = new SXSSFWorkbook();
-            Sheet sheet = wb.createSheet("成绩统计标准版");
-            CellStyle style = wb.createCellStyle();
-            style.setAlignment(HorizontalAlignment.CENTER); // 水平居中格式
-            style.setVerticalAlignment(VerticalAlignment.CENTER); //垂直居中
-            Row row = sheet.createRow(0);
-            Field[] fields = MarkResultStandardExportDto.class.getDeclaredFields();
-            int num = 0;
-            //绘制表头
-            for (int i = 0; i < fields.length; i++) {
-                Field field = fields[i];
-                field.setAccessible(true);
-                Annotation annotation = field.getAnnotation(ExcelNote.class);
-                if (Objects.nonNull(annotation)) {
-                    Cell cell = row.createCell(i);
-                    cell.setCellValue(field.getAnnotation(ExcelNote.class).value());
-                    cell.setCellStyle(style);
-                    num = i;
-                }
-            }
-            num++;
-            Map<String, Integer> position = new LinkedHashMap<>();
-            //动态表头
-            for (String s : titleSet) {
-                Cell cell = row.createCell(num);
-                cell.setCellValue(s);
-                cell.setCellStyle(style);
-                sheet.setColumnWidth(num, 15 * 256);
-                position.put(s, num);
-                num++;
-            }
-            int rowIndex = 0, cellIndex = 0, max = SystemConstant.MAX_EXPORT_SIZE, size = markResultStandardExportDtoList
-                    .size();
-            if (max >= size) {
-                max = size;
-            }
-            while (max <= size) {
-                List subList = markResultStandardExportDtoList.subList(min, max);
-                //绘制数据
-                for (int y = 0; y < subList.size(); y++) {
-                    line++;
-                    rowIndex++;
-                    MarkResultStandardExportDto markResultStandardExportDto = (MarkResultStandardExportDto) subList
-                            .get(y);
-                    cellIndex = 0;
-                    Row sxssfRow = sheet.createRow(rowIndex);
-                    for (Field field : fields) {
+
+            excelSj.add(String.valueOf(nowTime.getYear())).add(File.separator)
+                    .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
+                    .add(String.format("%02d", nowTime.getDayOfMonth()));
+
+            SXSSFWorkbook wb = null;
+            InputStream inputStream = null;
+            ByteArrayOutputStream out = null;
+            //开始根据卷型生成多个excel
+            for (Map.Entry<Long, List<ExcelDto>> entry : dynamicExcelHead.entrySet()) {
+                try {
+                    //创建excel
+                    wb = new SXSSFWorkbook();
+                    Sheet sheet = wb.createSheet("成绩统计标准版");
+                    CellStyle style = wb.createCellStyle();
+                    style.setAlignment(HorizontalAlignment.CENTER); // 水平居中格式
+                    style.setVerticalAlignment(VerticalAlignment.CENTER); //垂直居中
+                    Row row = sheet.createRow(0);
+                    Field[] fields = MarkResultStandardExportDto.class.getDeclaredFields();
+                    int num = 0;
+                    //绘制表头
+                    for (int i = 0; i < fields.length; i++) {
+                        Field field = fields[i];
                         field.setAccessible(true);
                         field.setAccessible(true);
-                        Annotation annotation = field.getAnnotation(ExcelNotExport.class);
-                        if (Objects.isNull(annotation)) {
-                            Annotation dynamicExport = field.getAnnotation(ExcelDynamicExport.class);
-                            if (Objects.nonNull(dynamicExport)) {
-                                Map<Long, List<ExcelDto>> answerExcetDto = markResultStandardExportDto
-                                        .getAnswerExcetDto();
-                                if (Objects.nonNull(answerExcetDto)) {
-                                    List<ExcelDto> excelDtoList = answerExcetDto
-                                            .get(markResultStandardExportDto.getExamStudentId());
-                                    if (Objects.nonNull(excelDtoList) && excelDtoList.size() > 0) {
-                                        excelDtoList.forEach(s -> {
-                                            if (Objects.nonNull(s) && Objects.nonNull(s.getTitle()) && Objects.nonNull(position.get(s.getTitle()))) {
-                                                taskExportCommon.createExcelCell(sxssfRow, position.get(s.getTitle()),
-                                                        s.getContent(), style);
+                        Annotation annotation = field.getAnnotation(ExcelNote.class);
+                        if (Objects.nonNull(annotation)) {
+                            Cell cell = row.createCell(i);
+                            cell.setCellValue(field.getAnnotation(ExcelNote.class).value());
+                            cell.setCellStyle(style);
+                            num = i;
+                        }
+                    }
+                    num++;
+                    Map<String, Integer> position = new LinkedHashMap<>();
+
+                    //动态表头
+                    for (ExcelDto excelDto : entry.getValue()) {
+                        Cell cell = row.createCell(num);
+                        cell.setCellValue(excelDto.getTitle());
+                        cell.setCellStyle(style);
+                        sheet.setColumnWidth(num, 15 * 256);
+                        position.put(excelDto.getTitle(), num);
+                        num++;
+                    }
+                    List<MarkResultStandardExportDto> markResultStandardExportDtoTempList = markResultStandardExportDtoLinkedMultiValueMap.get(entry.getKey());
+                    int rowIndex = 0, cellIndex = 0, max = SystemConstant.MAX_EXPORT_SIZE, size = markResultStandardExportDtoTempList
+                            .size();
+                    if (max >= size) {
+                        max = size;
+                    }
+                    while (max <= size) {
+                        List subList = markResultStandardExportDtoTempList.subList(min, max);
+                        //绘制数据
+                        for (int y = 0; y < subList.size(); y++) {
+                            finalSize++;
+                            line++;
+                            rowIndex++;
+                            MarkResultStandardExportDto markResultStandardExportDto = (MarkResultStandardExportDto) subList
+                                    .get(y);
+                            cellIndex = 0;
+                            Row sxssfRow = sheet.createRow(rowIndex);
+                            for (Field field : fields) {
+                                field.setAccessible(true);
+                                Annotation annotation = field.getAnnotation(ExcelNotExport.class);
+                                if (Objects.isNull(annotation)) {
+                                    Annotation dynamicExport = field.getAnnotation(ExcelDynamicExport.class);
+                                    if (Objects.nonNull(dynamicExport)) {
+                                        Map<Long, List<ExcelDto>> answerExcetDto = markResultStandardExportDto
+                                                .getAnswerExcetDto();
+                                        if (Objects.nonNull(answerExcetDto)) {
+                                            List<ExcelDto> excelDtoList = answerExcetDto
+                                                    .get(markResultStandardExportDto.getExamStudentId());
+                                            if (Objects.nonNull(excelDtoList) && excelDtoList.size() > 0) {
+                                                excelDtoList.forEach(s -> {
+                                                    if (Objects.nonNull(s) && Objects.nonNull(s.getTitle()) && Objects.nonNull(position.get(s.getTitle()))) {
+                                                        taskExportCommon.createExcelCell(sxssfRow, position.get(s.getTitle()),
+                                                                s.getContent(), style);
+                                                    }
+                                                });
                                             }
                                             }
-                                        });
+                                        }
+                                    } else {
+                                        taskExportCommon
+                                                .createExcelCell(sxssfRow, cellIndex, field.get(markResultStandardExportDto),
+                                                        style);
+                                        cellIndex++;
                                     }
                                     }
                                 }
                                 }
-                            } else {
-                                taskExportCommon
-                                        .createExcelCell(sxssfRow, cellIndex, field.get(markResultStandardExportDto),
-                                                style);
-                                cellIndex++;
                             }
                             }
                         }
                         }
+                        map.put("max", max);
+                        map.put("min", min);
+                        map.put("size", size);
+                        map = templeteLogicService.execExportMarkResultSimpleLogic(map);
+                        if (max == size) {
+                            break;
+                        }
+                        min = max;
+                        max += SystemConstant.MAX_EXPORT_SIZE;
+                        if (max >= size) {
+                            max = size;
+                        }
+                    }
+                    //临时缓冲区
+                    out = new ByteArrayOutputStream();
+                    wb.write(out);
+                    byte[] bookByteAry = out.toByteArray();
+                    inputStream = new ByteArrayInputStream(bookByteAry);
+                    StringBuilder stringBuilder = new StringBuilder(excelSj.toString());
+                    File finalFile = new File(stringBuilder.append(File.separator).append(markResultStandardExportDtoTempList.get(0).getCourseName() + "_" + markResultStandardExportDtoTempList.get(0).getCourseCode()).append(SystemConstant.EXCEL_PREFIX).toString());
+                    if (!finalFile.exists()) {
+                        finalFile.getParentFile().mkdirs();
+                        finalFile.createNewFile();
+                    }
+                    FileUtils.copyInputStreamToFile(inputStream, finalFile);
+                    files.add(finalFile);
+                } catch (Exception e) {
+                    log.error("生成excel报错", e);
+                    e.printStackTrace();
+                } finally {
+                    if (Objects.nonNull(wb)) {
+                        wb.dispose();
+                    }
+                    if (Objects.nonNull(inputStream)) {
+                        inputStream.close();
+                    }
+                    if (Objects.nonNull(out)) {
+                        out.flush();
+                        out.close();
                     }
                     }
                 }
                 }
-                map.put("max", max);
-                map.put("min", min);
-                map.put("size", size);
-                map = templeteLogicService.execExportMarkResultSimpleLogic(map);
-                if (max == size) {
-                    break;
-                }
-                min = max;
-                max += SystemConstant.MAX_EXPORT_SIZE;
-                if (max >= size) {
-                    max = size;
-                }
             }
             }
-            //临时缓冲区
-            out = new ByteArrayOutputStream();
-            wb.write(out);
-            byte[] bookByteAry = out.toByteArray();
-            inputStream = new ByteArrayInputStream(bookByteAry);
-            excelFilePath = taskExportCommon.ossUpload(inputStream, stringJoiner.toString());
+            if (files.size() > 0) {
+                StringBuilder stringBuilder = new StringBuilder(excelSj.toString());
+                String uuid = SystemConstant.getUuid();
+                StringBuilder zipStringBuilder = new StringBuilder(stringJoiner.toString());
+                zipPath = zipStringBuilder.append(File.separator).append(uuid).toString();
+                zip = new File(stringBuilder.append(File.separator).append(uuid).append(".zip").toString());
+                FileUtil.doZip(zip, files);
+                taskExportCommon.getOssUtil().upload(false, zipPath, zip);
+                files.add(zip);
+            }
             long end = System.currentTimeMillis();
             long end = System.currentTimeMillis();
             log.info("导出成绩标准版数据结束,============耗时============:{}秒", (end - start) / 1000);
             log.info("导出成绩标准版数据结束,============耗时============:{}秒", (end - start) / 1000);
         } catch (Exception e) {
         } catch (Exception e) {
@@ -319,17 +374,16 @@ public class TaskMarkResultStandardExportTemplete implements TaskExportTemplete
             taskExportCommon.exception(line, e, taskExportCommon.getTxtList());
             taskExportCommon.exception(line, e, taskExportCommon.getTxtList());
         } finally {
         } finally {
             //这里写入txt文件
             //这里写入txt文件
-            taskExportCommon.writeExportResultTxt(stringJoiner.toString(), taskExportCommon.getTxtList().toString(),
-                    excelFilePath);
-            if (Objects.nonNull(wb)) {
-                wb.dispose();
-            }
-            if (Objects.nonNull(inputStream)) {
-                inputStream.close();
-            }
-            if (Objects.nonNull(out)) {
-                out.flush();
-                out.close();
+            taskExportCommon.getTbTaskHistory().setStatus(TaskStatusEnum.FINISH);
+            taskExportCommon.getTbTaskHistory().setSummary("共导出了" + finalSize + "条数据");
+            taskExportCommon.getTbTaskHistory().setFinishTime(System.currentTimeMillis());
+            taskExportCommon.getTxtList().removeAll(taskExportCommon.getTxtList().subList(1, taskExportCommon.getTxtList().size()));
+            taskExportCommon.getTxtList().add(DateUtil.format(new Date(), Constants.DEFAULT_DATE_PATTERN) + "->数据导出完毕," + taskExportCommon.getTbTaskHistory().getSummary());
+            taskExportCommon.writeExportResultTxt(stringJoiner.toString(), taskExportCommon.getTxtList().toString(), zipPath);
+            if (Objects.nonNull(files)) {
+                for (File file : files) {
+                    file.delete();
+                }
             }
             }
         }
         }
         return ResultUtil.ok(true);
         return ResultUtil.ok(true);