Ver código fonte

createzip改造

wangliang 2 anos atrás
pai
commit
8259ee074b

+ 215 - 238
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/service/impl/TaskLogicServiceImpl.java

@@ -1,9 +1,7 @@
 package com.qmth.distributed.print.business.templete.service.impl;
 
-import cn.hutool.core.date.DateUtil;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-import com.aliyun.oss.common.utils.BinaryUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.google.common.collect.Lists;
@@ -37,7 +35,6 @@ import com.qmth.teachcloud.common.util.*;
 import com.qmth.teachcloud.common.util.excel.ExcelError;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.Cell;
@@ -1007,7 +1004,9 @@ public class TaskLogicServiceImpl implements TaskLogicService {
             if (Objects.nonNull(zipFile)) {
                 zipFile.delete();
             }
-            ConvertUtil.delFolder(zipLocalRootPath);
+            if (Objects.nonNull(zipLocalRootPath)) {
+                ConvertUtil.delFolder(zipLocalRootPath);
+            }
             if (!CollectionUtils.isEmpty(sourceFileList)) {
                 for (File file : sourceFileList) {
                     file.delete();
@@ -1054,7 +1053,11 @@ public class TaskLogicServiceImpl implements TaskLogicService {
             if (Objects.nonNull(attachmentType) && !Objects.equals(attachmentType, SystemConstant.LOCAL)) {
                 file = fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PATH), file, uploadFileEnum.getFssType());
             } else {
-                FileCopyUtils.copy(new File((String) jsonObject.get(SystemConstant.PATH)), file);
+                if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
+                    FileCopyUtils.copy(new File((String) jsonObject.get(SystemConstant.PATH)), file);
+                } else {
+                    file = fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PATH), file, uploadFileEnum.getFssType());
+                }
             }
         }
         return file;
@@ -1063,172 +1066,182 @@ public class TaskLogicServiceImpl implements TaskLogicService {
     @Override
     @Transactional
     public Map<String, Object> executeExportSampleLogic(Map<String, Object> map) throws Exception {
-        int count = 0;
-        TBTask tbTask = (TBTask) map.get(SystemConstant.TASK);
-        String zipJoiner = null, zipLocalRootPath = null;
-        long time = System.currentTimeMillis();
-        if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
-            zipJoiner = dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator;
-            zipLocalRootPath = dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + time;
-        } else {
-            zipJoiner = dictionaryConfig.fssPublicDomain().getConfig() + File.separator;
-            zipLocalRootPath = dictionaryConfig.fssPublicDomain().getConfig() + File.separator + time;
-        }
-        StringJoiner stringJoine = SystemConstant.getDirName(UploadFileEnum.FILE, true);
-        stringJoine.add(SystemConstant.getNanoId()).add(SystemConstant.ZIP_PREFIX);
-        String dirNameTmp = stringJoine.toString().replaceAll("\\\\", SystemConstant.ORG_SPLIT);
-        File zipFile = new File(zipJoiner + dirNameTmp);
-        if (!zipFile.getParentFile().exists()) {
-            zipFile.getParentFile().mkdirs();
-//            zipFile.createNewFile();
-        }
-
-        boolean oss = dictionaryConfig.sysDomain().isOss();
-
-        // 跟文件路径
-        Long[] ids = (Long[]) map.get("ids");
-        for (Long id : ids) {
-            ExamTask examTask = examTaskService.getById(id);
-            if (Objects.isNull(examTask)) {
-                throw ExceptionResultEnum.ERROR.exception("未找到命题任务");
-            }
-            String examTaskSign = examTask.getCourseName() + SystemConstant.HYPHEN + examTask.getPaperNumber();
-            String firstPath = zipLocalRootPath + File.separator + examTaskSign;
-            ReviewSampleDto reviewSampleDto = examTaskService.findReviewSampleInfoByExamTaskId(id);
-
-            // 收集处理试卷和题卡
-            List<ExamPaperInfoDto> paperInfoDtoList = JSONObject.parseArray(reviewSampleDto.getPaperAttachmentIds(), ExamPaperInfoDto.class);
-            if (Objects.isNull(paperInfoDtoList)) {
-                throw ExceptionResultEnum.ERROR.exception("试卷信息不存在");
+        File zipFile = null;
+        String zipLocalRootPath = null;
+        try {
+            int count = 0;
+            TBTask tbTask = (TBTask) map.get(SystemConstant.TASK);
+            Long time = System.currentTimeMillis();
+            boolean oss = dictionaryConfig.sysDomain().isOss();
+            StringJoiner stringJoiner = new StringJoiner("");
+            if (!oss && Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
+                stringJoiner.add(dictionaryConfig.fssLocalFileDomain().getConfig()).add(File.separator);
             }
-            Set<Long> attPaperIds = new HashSet<>();
-            List<ExamCardDto> examCardDtoList = new ArrayList<>();
-            for (ExamPaperInfoDto paperInfo : paperInfoDtoList) {
-                Long attachmentId = paperInfo.getAttachmentId();
-                Long cardId = paperInfo.getCardId();
-                String paperType = paperInfo.getName();
-                if (SystemConstant.longNotNull(attachmentId)) {
-                    attPaperIds.add(attachmentId);
+            stringJoiner = SystemConstant.getDirName(stringJoiner, UploadFileEnum.FILE, true);
+            stringJoiner.add("导出审核样本_" + time).add(SystemConstant.ZIP_PREFIX);
+
+            String zipDirName = stringJoiner.toString().replaceAll("\\\\", SystemConstant.ORG_SPLIT);
+            zipFile = SystemConstant.getFileTempDirVar(SystemConstant.ZIP_PREFIX);
+            zipLocalRootPath = zipFile.getParent() + File.separator + time;
+
+            // 跟文件路径
+            Long[] ids = (Long[]) map.get("ids");
+            for (Long id : ids) {
+                ExamTask examTask = examTaskService.getById(id);
+                if (Objects.isNull(examTask)) {
+                    throw ExceptionResultEnum.ERROR.exception("未找到命题任务");
                 }
-                if (SystemConstant.longNotNull(cardId)) {
-                    ExamCardDto examCardDto = new ExamCardDto();
-                    examCardDto.setCardId(cardId);
-                    examCardDto.setPaperType(paperType);
-                    examCardDtoList.add(examCardDto);
+                String examTaskSign = examTask.getCourseName() + SystemConstant.HYPHEN + examTask.getPaperNumber();
+                String firstPath = zipLocalRootPath + File.separator + examTaskSign;
+                ReviewSampleDto reviewSampleDto = examTaskService.findReviewSampleInfoByExamTaskId(id);
+
+                // 收集处理试卷和题卡
+                List<ExamPaperInfoDto> paperInfoDtoList = JSONObject.parseArray(reviewSampleDto.getPaperAttachmentIds(), ExamPaperInfoDto.class);
+                if (Objects.isNull(paperInfoDtoList)) {
+                    throw ExceptionResultEnum.ERROR.exception("试卷信息不存在");
                 }
-            }
-            if (attPaperIds.size() > 0) {
-                List<BasicAttachment> paperAttachmentList = basicAttachmentService.listByIds(attPaperIds);
-                for (BasicAttachment paperAttachment : paperAttachmentList) {
-                    JSONObject jsonObject = JSONObject.parseObject(paperAttachment.getPath());
-                    UploadFileEnum uploadFileEnum = UploadFileEnum.valueOf((String) jsonObject.get(SystemConstant.UPLOAD_TYPE));
-                    String paperPath = firstPath + File.separator + "试卷" + File.separator + paperAttachment.getName() + paperAttachment.getType();
-                    if (oss) {
-                        fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PATH), paperPath, uploadFileEnum.getFssType());
-                    } else {
-                        FileUtil.copyFile(jsonObject.getString(SystemConstant.PATH), paperPath);
+                Set<Long> attPaperIds = new HashSet<>();
+                List<ExamCardDto> examCardDtoList = new ArrayList<>();
+                for (ExamPaperInfoDto paperInfo : paperInfoDtoList) {
+                    Long attachmentId = paperInfo.getAttachmentId();
+                    Long cardId = paperInfo.getCardId();
+                    String paperType = paperInfo.getName();
+                    if (SystemConstant.longNotNull(attachmentId)) {
+                        attPaperIds.add(attachmentId);
+                    }
+                    if (SystemConstant.longNotNull(cardId)) {
+                        ExamCardDto examCardDto = new ExamCardDto();
+                        examCardDto.setCardId(cardId);
+                        examCardDto.setPaperType(paperType);
+                        examCardDtoList.add(examCardDto);
                     }
-                    count++;
-                }
-            }
-
-            // 处理审核样品
-            List<Map> confirmInfo = JSONObject.parseArray(reviewSampleDto.getPaperConfirmAttachmentIds(), Map.class);
-            Set<Long> attConfirmIds = new HashSet<>();
-            for (Map confirmMap : confirmInfo) {
-                if (Objects.isNull(confirmMap.get("attachmentId"))) {
-                    throw ExceptionResultEnum.ERROR.exception("未找到附件id");
                 }
-                Long attachmentId = Long.valueOf(String.valueOf(confirmMap.get("attachmentId")));
-                attConfirmIds.add(attachmentId);
-            }
-            if (attConfirmIds.size() > 0) {
-                List<BasicAttachment> confirmAttachmentList = basicAttachmentService.listByIds(attConfirmIds);
-                for (BasicAttachment confirmAttachment : confirmAttachmentList) {
-                    JSONObject jsonObject = JSONObject.parseObject(confirmAttachment.getPath());
-                    UploadFileEnum uploadFileEnum = UploadFileEnum.valueOf((String) jsonObject.get(SystemConstant.UPLOAD_TYPE));
-                    String confirmPath = firstPath + File.separator + "审核样本" + File.separator + confirmAttachment.getName() + confirmAttachment.getType();
-                    if (oss) {
-                        fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PATH), confirmPath, uploadFileEnum.getFssType());
-                    } else {
-                        FileUtil.copyFile(jsonObject.getString(SystemConstant.PATH), confirmPath);
+                if (attPaperIds.size() > 0) {
+                    List<BasicAttachment> paperAttachmentList = basicAttachmentService.listByIds(attPaperIds);
+                    for (BasicAttachment paperAttachment : paperAttachmentList) {
+                        JSONObject jsonObject = JSONObject.parseObject(paperAttachment.getPath());
+                        UploadFileEnum uploadFileEnum = UploadFileEnum.valueOf((String) jsonObject.get(SystemConstant.UPLOAD_TYPE));
+                        String paperPath = firstPath + File.separator + "试卷" + File.separator + paperAttachment.getName() + paperAttachment.getType();
+                        if (oss) {
+                            fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PATH), paperPath, uploadFileEnum.getFssType());
+                        } else {
+                            if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
+                                FileUtil.copyFile(jsonObject.getString(SystemConstant.PATH), paperPath);
+                            } else {
+                                fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PATH), paperPath, uploadFileEnum.getFssType());
+                            }
+                        }
+                        count++;
                     }
-                    count++;
                 }
-            }
 
-            // 处理题卡
-            for (ExamCardDto examCardDto : examCardDtoList) {
-                Long cardId = examCardDto.getCardId();
-                String paperType = examCardDto.getPaperType();
-                ExamCard examCard = examCardService.getById(cardId);
-                if (Objects.isNull(examCard)) {
-                    throw ExceptionResultEnum.ERROR.exception("找不到答题卡");
-                }
-                MakeMethodEnum makeMethodEnum = examCard.getMakeMethod();
-                if (MakeMethodEnum.SELECT.equals(makeMethodEnum) && CardCreateMethodEnum.UPLOAD.equals(examCard.getCreateMethod())) {
-                    Long attachmentId = examCard.getAttachmentId();
-                    if (attachmentId == null || attachmentId == 0) {
-                        throw ExceptionResultEnum.ERROR.exception("找不到模板对应的附件");
+                // 处理审核样品
+                List<Map> confirmInfo = JSONObject.parseArray(reviewSampleDto.getPaperConfirmAttachmentIds(), Map.class);
+                Set<Long> attConfirmIds = new HashSet<>();
+                for (Map confirmMap : confirmInfo) {
+                    if (Objects.isNull(confirmMap.get("attachmentId"))) {
+                        throw ExceptionResultEnum.ERROR.exception("未找到附件id");
                     }
-                    BasicAttachment cardAttachment = basicAttachmentService.getById(attachmentId);
-                    if (Objects.isNull(cardAttachment)) {
-                        throw ExceptionResultEnum.ERROR.exception("找不到附件");
+                    Long attachmentId = Long.valueOf(String.valueOf(confirmMap.get("attachmentId")));
+                    attConfirmIds.add(attachmentId);
+                }
+                if (attConfirmIds.size() > 0) {
+                    List<BasicAttachment> confirmAttachmentList = basicAttachmentService.listByIds(attConfirmIds);
+                    for (BasicAttachment confirmAttachment : confirmAttachmentList) {
+                        JSONObject jsonObject = JSONObject.parseObject(confirmAttachment.getPath());
+                        UploadFileEnum uploadFileEnum = UploadFileEnum.valueOf((String) jsonObject.get(SystemConstant.UPLOAD_TYPE));
+                        String confirmPath = firstPath + File.separator + "审核样本" + File.separator + confirmAttachment.getName() + confirmAttachment.getType();
+                        if (oss) {
+                            fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PATH), confirmPath, uploadFileEnum.getFssType());
+                        } else {
+                            if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
+                                FileUtil.copyFile(jsonObject.getString(SystemConstant.PATH), confirmPath);
+                            } else {
+                                fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PATH), confirmPath, uploadFileEnum.getFssType());
+                            }
+                        }
+                        count++;
                     }
-                    JSONObject jsonObject = JSONObject.parseObject(cardAttachment.getPath());
-                    // 题卡路径
-                    String cardPath = firstPath + File.separator + "题卡" + File.separator + cardAttachment.getName() + cardAttachment.getType();
-                    if (SystemConstant.strNotNull(paperType)) {
-                        cardPath = cardPath + "(" + paperType + ")";
+                }
+
+                // 处理题卡
+                for (ExamCardDto examCardDto : examCardDtoList) {
+                    Long cardId = examCardDto.getCardId();
+                    String paperType = examCardDto.getPaperType();
+                    ExamCard examCard = examCardService.getById(cardId);
+                    if (Objects.isNull(examCard)) {
+                        throw ExceptionResultEnum.ERROR.exception("找不到答题卡");
                     }
-                    UploadFileEnum uploadFileEnum = UploadFileEnum.valueOf((String) jsonObject.get(SystemConstant.UPLOAD_TYPE));
+                    MakeMethodEnum makeMethodEnum = examCard.getMakeMethod();
+                    if (MakeMethodEnum.SELECT.equals(makeMethodEnum) && CardCreateMethodEnum.UPLOAD.equals(examCard.getCreateMethod())) {
+                        Long attachmentId = examCard.getAttachmentId();
+                        if (attachmentId == null || attachmentId == 0) {
+                            throw ExceptionResultEnum.ERROR.exception("找不到模板对应的附件");
+                        }
+                        BasicAttachment cardAttachment = basicAttachmentService.getById(attachmentId);
+                        if (Objects.isNull(cardAttachment)) {
+                            throw ExceptionResultEnum.ERROR.exception("找不到附件");
+                        }
+                        JSONObject jsonObject = JSONObject.parseObject(cardAttachment.getPath());
+                        // 题卡路径
+                        String cardPath = firstPath + File.separator + "题卡" + File.separator + cardAttachment.getName() + cardAttachment.getType();
+                        if (SystemConstant.strNotNull(paperType)) {
+                            cardPath = cardPath + "(" + paperType + ")";
+                        }
+                        UploadFileEnum uploadFileEnum = UploadFileEnum.valueOf((String) jsonObject.get(SystemConstant.UPLOAD_TYPE));
 
-                    if (oss) {
-                        fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PATH), cardPath, uploadFileEnum.getFssType());
+                        if (oss) {
+                            fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PATH), cardPath, uploadFileEnum.getFssType());
+                        } else {
+                            if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
+                                FileUtil.copyFile(jsonObject.getString(SystemConstant.PATH), cardPath);
+                            } else {
+                                fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PATH), cardPath, uploadFileEnum.getFssType());
+                            }
+                        }
                     } else {
-                        FileUtil.copyFile(jsonObject.getString(SystemConstant.PATH), cardPath);
-                    }
-                } else {
-                    List<ExamCardDetail> examCardDetailList = examCardDetailService.list(new QueryWrapper<ExamCardDetail>().lambda().eq(ExamCardDetail::getCardId, cardId));
-                    if (examCardDetailList.size() != 1) {
-                        throw ExceptionResultEnum.ERROR.exception("题卡明细信息异常");
-                    }
-                    ExamCardDetail examCardDetail = examCardDetailService.getByCardId(cardId);
-                    String htmlContent = examCardDetail.getHtmlContent();
-                    byte[] bytes = htmlContent.getBytes();
-
-                    // 题卡路径
-                    String cardPath = firstPath + File.separator + "题卡" + File.separator + examCard.getTitle();
-                    if (SystemConstant.strNotNull(paperType)) {
-                        cardPath = cardPath + "(" + paperType + ")";
-                    }
-                    cardPath = cardPath + SystemConstant.HTML_PREFIX;
+                        List<ExamCardDetail> examCardDetailList = examCardDetailService.list(new QueryWrapper<ExamCardDetail>().lambda().eq(ExamCardDetail::getCardId, cardId));
+                        if (examCardDetailList.size() != 1) {
+                            throw ExceptionResultEnum.ERROR.exception("题卡明细信息异常");
+                        }
+                        ExamCardDetail examCardDetail = examCardDetailService.getByCardId(cardId);
+                        String htmlContent = examCardDetail.getHtmlContent();
+                        byte[] bytes = htmlContent.getBytes();
+
+                        // 题卡路径
+                        String cardPath = firstPath + File.separator + "题卡" + File.separator + examCard.getTitle();
+                        if (SystemConstant.strNotNull(paperType)) {
+                            cardPath = cardPath + "(" + paperType + ")";
+                        }
+                        cardPath = cardPath + SystemConstant.HTML_PREFIX;
 
-                    File localFile = new File(cardPath);
-                    if (!localFile.getParentFile().exists()) {
-                        localFile.getParentFile().mkdirs();
+                        File localFile = new File(cardPath);
+                        if (!localFile.getParentFile().exists()) {
+                            localFile.getParentFile().mkdirs();
+                        }
+                        FileCopyUtils.copy(bytes, localFile);
                     }
-                    FileCopyUtils.copy(bytes, localFile);
                 }
+                count++;
+            }
+            JSONObject jsonObject = SystemConstant.createZip(zipFile, zipLocalRootPath, zipDirName);
+            tbTask.setResultFilePath(jsonObject.toJSONString());
+            map.put("count", count);
+        } catch (Exception e) {
+            log.error(SystemConstant.LOG_ERROR, e);
+            if (e instanceof ApiException) {
+                ResultUtil.error((ApiException) e, e.getMessage());
+            } else {
+                ResultUtil.error(e.getMessage());
+            }
+        } finally {
+            if (Objects.nonNull(zipFile)) {
+                zipFile.delete();
+            }
+            if (Objects.nonNull(zipLocalRootPath)) {
+                ConvertUtil.delFolder(zipLocalRootPath);
             }
-            count++;
-        }
-        zipLocalRootPath = zipLocalRootPath.replaceAll("\\\\", SystemConstant.ORG_SPLIT);
-        Zip4jUtil.zipEncryptFile(zipFile.getPath(), zipLocalRootPath, SystemConstant.ZIP_ENCRYPT_PWD);
-        JSONObject jsonObject = new JSONObject();
-        if (oss) {//上传至oss
-            fileStoreUtil.ossUpload(dirNameTmp, zipFile, BinaryUtil.toBase64String(HexUtils.decodeHex(DigestUtils.md5Hex(new FileInputStream(zipFile)))), fileStoreUtil.getUploadEnumByPath(dirNameTmp).getFssType());
-            jsonObject.put(SystemConstant.PATH, dirNameTmp);
-            jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
-            ConvertUtil.delFolder(zipLocalRootPath);
-            zipFile.delete();
-        } else {
-            jsonObject.put(SystemConstant.TYPE, SystemConstant.LOCAL);
-            jsonObject.put(SystemConstant.PATH, (zipJoiner + dirNameTmp).replaceAll("\\\\", SystemConstant.ORG_SPLIT));
         }
-        jsonObject.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.FILE);
-        tbTask.setResultFilePath(jsonObject.toJSONString());
-        map.put("count", count);
         return map;
     }
 
@@ -1236,36 +1249,29 @@ public class TaskLogicServiceImpl implements TaskLogicService {
     public Map<String, Object> executeExportPaperAndCardLogic(Map<String, Object> map) throws Exception {
         String zipLocalRootPath = null;
         File zipFile = null;
+        List<File> sourceFileList = null;
         try {
+            sourceFileList = new ArrayList<>();
             TBTask tbTask = (TBTask) map.get(SystemConstant.TASK);
-            String yyyyMMddHH24mmss = DateUtil.format(new Date(), SystemConstant.BACK_UP_DATE_PATTERN);
-            BasicSchool basicSchool = commonCacheService.schoolCache(tbTask.getSchoolId());
-            StringJoiner zipJoiner = new StringJoiner("");
+
             Long time = System.currentTimeMillis();
-            if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
-                zipJoiner = zipJoiner.add(dictionaryConfig.fssLocalFileDomain().getConfig()).add(File.separator);
-                zipLocalRootPath = dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + time;
-            } else {
-                zipJoiner = zipJoiner.add(dictionaryConfig.fssPublicDomain().getConfig()).add(File.separator);
-                zipLocalRootPath = dictionaryConfig.fssPublicDomain().getConfig() + File.separator + time;
+            boolean oss = dictionaryConfig.sysDomain().isOss();
+            StringJoiner stringJoiner = new StringJoiner("");
+            if (!oss && Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
+                stringJoiner.add(dictionaryConfig.fssLocalFileDomain().getConfig()).add(File.separator);
             }
-            StringJoiner dirName = SystemConstant.getDirName(UploadFileEnum.FILE, true);
-            dirName.add(yyyyMMddHH24mmss).add("_").add(basicSchool.getName()).add("_").add("试卷导出文件").add(SystemConstant.ZIP_PREFIX);
-            String dirNameTmp = dirName.toString().replaceAll("\\\\", SystemConstant.ORG_SPLIT);
+            stringJoiner = SystemConstant.getDirName(stringJoiner, UploadFileEnum.FILE, true);
+            stringJoiner.add("命题任务审核样本导出_" + time).add(SystemConstant.ZIP_PREFIX);
 
-            // 根文件路径
-            boolean oss = dictionaryConfig.sysDomain().isOss();
+            String zipDirName = stringJoiner.toString().replaceAll("\\\\", SystemConstant.ORG_SPLIT);
+            zipFile = SystemConstant.getFileTempDirVar(SystemConstant.ZIP_PREFIX);
+            zipLocalRootPath = zipFile.getParent() + File.separator + time;
 
             List<ExamTaskDetailDto> examTasks = (List<ExamTaskDetailDto>) map.get("examTasks");
             for (ExamTaskDetailDto examTask : examTasks) {
                 ExamTaskDetailPdfDownloadDto examTaskDetailPdfDownloadDto = examTaskDetailService.findPdfDownload(Long.valueOf(examTask.getId()));
 
                 StringJoiner dirPath = new StringJoiner("");
-                if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
-                    dirPath = dirPath.add(dictionaryConfig.fssLocalFileDomain().getConfig()).add(File.separator);
-                } else {
-                    dirPath = dirPath.add(dictionaryConfig.fssPublicDomain().getConfig()).add(File.separator);
-                }
                 dirPath = dirPath.add(String.valueOf(time)).add(File.separator)
                         .add(examTaskDetailPdfDownloadDto.getSemesterName()).add(File.separator)
                         .add(examTaskDetailPdfDownloadDto.getExamName()).add(File.separator)
@@ -1286,26 +1292,13 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                             Long attachmentId = Long.valueOf(String.valueOf(paperMap.get("attachmentId")));
                             BasicAttachment attachment = basicAttachmentService.getById(attachmentId);
                             if (Objects.nonNull(attachment)) {
-                                JSONObject jsonObject = JSONObject.parseObject(attachment.getPath());
-                                String paperPath = dirPath.toString() + "试卷" + SystemConstant.HYPHEN + name + attachment.getType();
-                                UploadFileEnum uploadFileEnum = UploadFileEnum.valueOf((String) jsonObject.get(SystemConstant.UPLOAD_TYPE));
-                                File file = null;
-                                file = new File(paperPath);
-                                if (!file.exists()) {
-                                    file.getParentFile().mkdirs(); //目标文件目录不存在的话需要创建目录
-//                                        file.createNewFile();
-                                }
-                                if (oss) {
-                                    file = fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PATH), paperPath.toString(), uploadFileEnum.getFssType());
-                                } else {
-                                    IOUtils.copy(new FileInputStream((String) jsonObject.get(SystemConstant.PATH)), new FileOutputStream(file));
-                                }
+                                File paperFile = getAttachmentFile(attachment, dirPath.toString() + "试卷" + SystemConstant.HYPHEN + name + attachment.getType());
+                                sourceFileList.add(paperFile);
                             }
                         }
                         ExamCard examCard = examCardService.getById(Long.parseLong((String) paperMap.get("cardId")));
-                        if (Objects.isNull(examCard)) {
-                            throw ExceptionResultEnum.ERROR.exception("找不到答题卡");
-                        }
+                        Optional.ofNullable(examCard).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("找不到答题卡"));
+
                         MakeMethodEnum makeMethodEnum = examCard.getMakeMethod();
 
                         String cardHtmlPath = dirPath.toString() + "题卡" + SystemConstant.HYPHEN + paperMap.get("name") + SystemConstant.HTML_PREFIX;
@@ -1320,37 +1313,18 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                             htmlContent = createPdfUtil.replaceHtmlCard(examCardDetail, basicCardRule);
                         }
                         // html
-                        File localFile = new File(cardHtmlPath);
-                        if (!localFile.getParentFile().exists()) {
-                            localFile.getParentFile().mkdirs();
-                        }
+                        File localFile = SystemConstant.getFileTempDirVar(cardHtmlPath, null);
                         // 生成html文件
                         FileCopyUtils.copy(htmlContent.getBytes(), localFile);
+                        sourceFileList.add(localFile);
                         // 转pdf文件
-                        File file = new File(cardPdfPath);
-                        if (!file.exists()) {
-                            file.createNewFile();
-                        }
+                        File file = SystemConstant.getFileTempDirVar(cardPdfPath, null);
                         HtmlToPdfUtil.convert(cardHtmlPath, cardPdfPath, PageSizeEnum.A3);
+                        sourceFileList.add(file);
                     }
                 }
             }
-
-            zipFile = new File(zipJoiner.toString() + dirNameTmp);
-            if (!zipFile.getParentFile().exists()) {
-                zipFile.getParentFile().mkdirs();
-            }
-            Zip4jUtil.zipEncryptFile(zipFile.getPath(), zipLocalRootPath, SystemConstant.ZIP_ENCRYPT_PWD);
-            JSONObject jsonObject = new JSONObject();
-            if (oss) {//上传至oss
-                fileStoreUtil.ossUpload(dirNameTmp, zipFile, BinaryUtil.toBase64String(HexUtils.decodeHex(DigestUtils.md5Hex(new FileInputStream(zipFile)))), fileStoreUtil.getUploadEnumByPath(dirNameTmp).getFssType());
-                jsonObject.put(SystemConstant.PATH, dirNameTmp);
-                jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
-            } else {
-                jsonObject.put(SystemConstant.TYPE, SystemConstant.LOCAL);
-                jsonObject.put(SystemConstant.PATH, zipFile.getPath());
-            }
-            jsonObject.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.FILE);
+            JSONObject jsonObject = SystemConstant.createZip(zipFile, zipLocalRootPath, zipDirName);
             tbTask.setResultFilePath(jsonObject.toJSONString());
             map.put(SystemConstant.DATA_COUNT, examTasks.size());
         } catch (Exception e) {
@@ -1361,11 +1335,16 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                 ResultUtil.error(e.getMessage());
             }
         } finally {
+            if (Objects.nonNull(zipFile)) {
+                zipFile.delete();
+            }
             if (Objects.nonNull(zipLocalRootPath)) {
                 ConvertUtil.delFolder(zipLocalRootPath);
             }
-            if (Objects.nonNull(dictionaryConfig.sysDomain()) && dictionaryConfig.sysDomain().isOss() && Objects.nonNull(zipFile)) {
-                zipFile.delete();
+            if (!CollectionUtils.isEmpty(sourceFileList)) {
+                for (File file : sourceFileList) {
+                    file.delete();
+                }
             }
         }
         return map;
@@ -1783,15 +1762,18 @@ public class TaskLogicServiceImpl implements TaskLogicService {
             if (Objects.nonNull(tSyncExamStudentScoreResultList) && tSyncExamStudentScoreResultList.size() > 0) {
                 List<TSyncExamStudentScore> tSyncExamStudentScoreList = new Gson().fromJson(JacksonUtil.parseJson(tSyncExamStudentScoreResultList), new TypeToken<List<TSyncExamStudentScore>>() {
                 }.getType());
+                Long time = System.currentTimeMillis();
+                boolean oss = dictionaryConfig.sysDomain().isOss();
                 StringJoiner stringJoiner = new StringJoiner("");
-                if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
+                if (!oss && Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
                     stringJoiner.add(dictionaryConfig.fssLocalFileDomain().getConfig()).add(File.separator);
-                } else {
-                    stringJoiner.add(dictionaryConfig.fssPublicDomain().getConfig()).add(File.separator);
                 }
-                StringJoiner dirZipName = SystemConstant.getDirName(UploadFileEnum.FILE, true);
-                dirZipName.add(SystemConstant.getNanoId()).add(SystemConstant.ZIP_PREFIX);
-                zipFile = new File(stringJoiner.toString() + dirZipName.toString());
+                stringJoiner = SystemConstant.getDirName(stringJoiner, UploadFileEnum.FILE, true);
+                stringJoiner.add("成绩轨迹下载_" + time).add(SystemConstant.ZIP_PREFIX);
+
+                String zipDirName = stringJoiner.toString().replaceAll("\\\\", SystemConstant.ORG_SPLIT);
+                zipFile = SystemConstant.getFileTempDirVar(SystemConstant.ZIP_PREFIX);
+
                 sourceFiles = new LinkedList<>();
                 List<TSyncExamStudentScore> updateTSyncExamStudentScoreList = new ArrayList<>();
                 errorTSyncExamStudentScoreList = new ArrayList<>();
@@ -1821,18 +1803,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                 }
                 tSyncExamStudentScoreService.saveOrUpdateBatch(updateTSyncExamStudentScoreList);
                 if (!sourceFiles.isEmpty()) {
-                    Zip4jUtil.zipEncryptFile(zipFile.getPath(), sourceFiles, SystemConstant.ZIP_ENCRYPT_PWD);
-                    boolean oss = (boolean) map.get(SystemConstant.OSS);
-                    JSONObject jsonObject = new JSONObject();
-                    if (oss) {//上传至oss
-                        fileStoreUtil.ossUpload(dirZipName.toString(), zipFile, DigestUtils.md5Hex(new FileInputStream(zipFile)), UploadFileEnum.FILE.getFssType());
-                        jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
-                        jsonObject.put(SystemConstant.PATH, dirZipName.toString());
-                    } else {
-                        jsonObject.put(SystemConstant.TYPE, SystemConstant.LOCAL);
-                        jsonObject.put(SystemConstant.PATH, stringJoiner.toString() + dirZipName.toString());
-                    }
-                    jsonObject.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.FILE);
+                    JSONObject jsonObject = SystemConstant.createZip(zipFile, sourceFiles, zipDirName);
                     TBTask tbTask = (TBTask) map.get(SystemConstant.TASK);
                     tbTask.setResultFilePath(jsonObject.toJSONString());
                 } else {
@@ -1852,11 +1823,15 @@ public class TaskLogicServiceImpl implements TaskLogicService {
             }
         } catch (Exception e) {
             log.error(SystemConstant.LOG_ERROR, e);
-            throw ExceptionResultEnum.ERROR.exception(e.getMessage());
+            if (e instanceof ApiException) {
+                ResultUtil.error((ApiException) e, e.getMessage());
+            } else {
+                ResultUtil.error(e.getMessage());
+            }
         } finally {
-//            if (Objects.nonNull(zipFile)) {
-//                zipFile.delete();
-//            }
+            if (Objects.nonNull(zipFile)) {
+                zipFile.delete();
+            }
         }
         return map;
     }
@@ -2136,7 +2111,9 @@ public class TaskLogicServiceImpl implements TaskLogicService {
             if (Objects.nonNull(zipFile)) {
                 zipFile.delete();
             }
-            ConvertUtil.delFolder(zipLocalRootPath);
+            if (Objects.nonNull(zipLocalRootPath)) {
+                ConvertUtil.delFolder(zipLocalRootPath);
+            }
         }
         return map;
     }

+ 33 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/contant/SystemConstant.java

@@ -971,4 +971,37 @@ public class SystemConstant {
         jsonObject.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.FILE);
         return jsonObject;
     }
+
+    /**
+     * 创建加密的zip
+     *
+     * @param zipFile
+     * @param sourceFileList
+     * @param zipDirName
+     * @return
+     * @throws Exception
+     */
+    public static JSONObject createZip(File zipFile, List<File> sourceFileList, String zipDirName) throws Exception {
+        FileStoreUtil fileStoreUtil = SpringContextHolder.getBean(FileStoreUtil.class);
+        DictionaryConfig dictionaryConfig = SpringContextHolder.getBean(DictionaryConfig.class);
+        Zip4jUtil.zipEncryptFile(zipFile.getPath(), sourceFileList, SystemConstant.ZIP_ENCRYPT_PWD);
+
+        boolean oss = dictionaryConfig.sysDomain().isOss();
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put(SystemConstant.PATH, zipDirName);
+        String zipFileMd5 = DigestUtils.md5Hex(new FileInputStream(zipFile));
+        if (oss) {//上传至oss
+            fileStoreUtil.ossUpload(zipDirName, zipFile, zipFileMd5, fileStoreUtil.getUploadEnumByPath(zipDirName).getFssType());
+            jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
+        } else {
+            if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
+                fileStoreUtil.localUpload(zipDirName, new FileInputStream(zipFile), zipFileMd5, LocalCatalogEnum.LOCAL_FILE);
+            } else {
+                fileStoreUtil.ossUpload(zipDirName, zipFile, zipFileMd5, fileStoreUtil.getUploadEnumByPath(zipDirName).getFssType());
+            }
+            jsonObject.put(SystemConstant.TYPE, SystemConstant.LOCAL);
+        }
+        jsonObject.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.FILE);
+        return jsonObject;
+    }
 }