caozixuan 3 роки тому
батько
коміт
6107b88169

+ 8 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/GradeBatchService.java

@@ -53,4 +53,12 @@ public interface GradeBatchService extends IService<GradeBatch> {
     void downloadFile(Long batchId, HttpServletResponse response) throws IOException;
 
     void uploadFile(Long batchId, MultipartFile file) throws IOException, NoSuchFieldException;
+
+    /**
+     * 创建Txt文件
+     *
+     * @param batchId   批次id
+     * @param exception 异常
+     */
+    void createTxt(Long batchId, String exception);
 }

+ 87 - 5
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/GradeBatchServiceImpl.java

@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.google.common.collect.Lists;
+import com.qmth.boot.api.exception.ApiException;
 import com.qmth.distributed.print.business.bean.dto.ExamStudentCourseDto;
 import com.qmth.distributed.print.business.bean.dto.GradeBatchStudentDto;
 import com.qmth.distributed.print.business.bean.params.analyze.GradeBatchParam;
@@ -19,12 +20,20 @@ import com.qmth.distributed.print.business.mapper.GradeBatchMapper;
 import com.qmth.distributed.print.business.service.GradeBatchPaperService;
 import com.qmth.distributed.print.business.service.GradeBatchService;
 import com.qmth.distributed.print.business.service.GradeBatchStudentService;
+import com.qmth.teachcloud.common.config.DictionaryConfig;
+import com.qmth.teachcloud.common.contant.SpringContextHolder;
 import com.qmth.teachcloud.common.contant.SystemConstant;
 import com.qmth.teachcloud.common.entity.SysUser;
 import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
 import com.qmth.teachcloud.common.enums.GradeAnalyzePaperStatusEnum;
+import com.qmth.teachcloud.common.enums.TaskResultEnum;
+import com.qmth.teachcloud.common.enums.UploadFileEnum;
 import com.qmth.teachcloud.common.util.ExcelUtil;
+import com.qmth.teachcloud.common.util.FileStoreUtil;
+import com.qmth.teachcloud.common.util.ResultUtil;
 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.lang3.StringUtils;
 import org.apache.commons.lang3.time.DateFormatUtils;
 import org.apache.poi.ss.usermodel.HorizontalAlignment;
@@ -36,9 +45,10 @@ import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.io.OutputStream;
+import java.io.*;
 import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.time.LocalDateTime;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -217,7 +227,7 @@ public class GradeBatchServiceImpl extends ServiceImpl<GradeBatchMapper, GradeBa
             for (int i = 0; i < finalExcelList.size(); i++) {
                 LinkedMultiValueMap<Integer, Object> excelMap = finalExcelList.get(i);
                 List<Object> gradeBatchStudentTempList = excelMap.get(i);
-                for (int y = 0; y < gradeBatchStudentTempList.size(); y++) {
+                for (int y = 0; y < Objects.requireNonNull(gradeBatchStudentTempList).size(); y++) {
                     GradeBatchStudentDto examStudentCourseDto = (GradeBatchStudentDto) gradeBatchStudentTempList.get(y);
                     if (StringUtils.isBlank(examStudentCourseDto.getCourseCode())) {
                         excelErrorTemp.add(new ExcelError(y + 1, "excel第" + (i + 1) + "个sheet第" + (y + 1) + "行[课程代码]必填"));
@@ -259,7 +269,7 @@ public class GradeBatchServiceImpl extends ServiceImpl<GradeBatchMapper, GradeBa
             }
 
             if (excelErrorTemp.size() > 0) {
-                List<String> errors = excelErrorTemp.stream().map(m -> m.getExcelErrorType()).collect(Collectors.toList());
+                List<String> errors = excelErrorTemp.stream().map(ExcelError::getExcelErrorType).collect(Collectors.toList());
                 throw ExceptionResultEnum.ERROR.exception(JSONObject.toJSONString(errors));
             }
             return finalExcelList;
@@ -268,6 +278,79 @@ public class GradeBatchServiceImpl extends ServiceImpl<GradeBatchMapper, GradeBa
         executeImportGradeBatchStudent(batchId, list);
     }
 
+    @Override
+    public void createTxt(Long batchId,String exception) {
+        final String TXT_PREFIX = ".txt";
+        GradeBatch gradeBatch = this.getById(batchId);
+        if (Objects.isNull(gradeBatch)){
+            throw ExceptionResultEnum.ERROR.exception("批次不存在");
+        }
+        FileStoreUtil fileStoreUtil = SpringContextHolder.getBean(FileStoreUtil.class);
+        DictionaryConfig dictionaryConfig = SpringContextHolder.getBean(DictionaryConfig.class);
+        ByteArrayOutputStream out = null;
+        InputStream inputStream = null;
+        try {
+            // 有异常才创建txt文件
+            if (SystemConstant.strNotNull(exception)){
+                boolean oss = dictionaryConfig.sysDomain().isOss();
+                LocalDateTime nowTime = LocalDateTime.now();
+                StringJoiner stringJoiner = new StringJoiner("");
+                if (!oss) {
+                    stringJoiner.add(SystemConstant.TEMP_FILES_DIR).add(File.separator);
+                }
+                stringJoiner.add(UploadFileEnum.FILE.getTitle()).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.getDayOfMonth()));
+                stringJoiner.add(File.separator).add(SystemConstant.getUuid()).add(TXT_PREFIX);
+
+                JSONObject jsonObject = new JSONObject();
+                out = new ByteArrayOutputStream();
+                out.write(exception.getBytes(StandardCharsets.UTF_8));
+                byte[] bookByteAry = out.toByteArray();
+                inputStream = new ByteArrayInputStream(bookByteAry);
+
+                if (oss) {//上传至oss\
+                    String dirName = stringJoiner.toString().replaceAll("\\\\", "/");
+                    fileStoreUtil.ossUpload(dirName, inputStream, DigestUtils.md5Hex(inputStream), UploadFileEnum.FILE.getFssType());
+                    jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
+                    jsonObject.put(SystemConstant.PATH, dirName);
+                } else {//上传至服务器
+                    File finalFile = new File(stringJoiner.toString());
+                    if (!finalFile.exists()) {
+                        finalFile.getParentFile().mkdirs();
+                        finalFile.createNewFile();
+                    }
+                    FileUtils.copyInputStreamToFile(inputStream, finalFile);
+                    jsonObject.put(SystemConstant.TYPE, SystemConstant.LOCAL);
+                    jsonObject.put(SystemConstant.PATH, stringJoiner.toString());
+                }
+                jsonObject.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.FILE);
+                gradeBatch.setReportFilePath(jsonObject.toJSONString());
+            }
+        } catch (Exception e) {
+            gradeBatch.setResult(TaskResultEnum.ERROR);
+            if (e instanceof ApiException) {
+                ResultUtil.error((ApiException) e, e.getMessage());
+            } else {
+                ResultUtil.error(e.getMessage());
+            }
+        } finally {
+            try {
+                if (Objects.nonNull(inputStream)) {
+                    inputStream.close();
+                }
+                if (Objects.nonNull(out)) {
+                    out.flush();
+                    out.close();
+                }
+            } catch (IOException e) {
+                log.error(SystemConstant.LOG_ERROR, e);
+            }
+            this.updateById(gradeBatch);
+        }
+    }
+
     @Transactional
     public void executeImportGradeBatchStudent(Long batchId, List<GradeBatchStudentDto> list) {
         UpdateWrapper<GradeBatchStudent> updateWrapper = new UpdateWrapper<>();
@@ -291,5 +374,4 @@ public class GradeBatchServiceImpl extends ServiceImpl<GradeBatchMapper, GradeBa
 
         gradeBatchStudentService.saveBatch(gradeBatchStudents);
     }
-
 }