Explorar o código

成绩导出一键下载

wangliang %!s(int64=3) %!d(string=hai) anos
pai
achega
ce119b85d6

+ 5 - 5
distributed-print-business/src/main/java/com/qmth/distributed/print/business/bean/result/TSyncExamStudentScoreResult.java

@@ -41,7 +41,7 @@ public class TSyncExamStudentScoreResult implements Serializable {
 
     @ExcelProperty(name = "姓名", width = 30, index = 2)
     @ApiModelProperty(value = "考生姓名")
-    String examStudentName;
+    String name;
 
     @ExcelProperty(name = "学号", width = 30, index = 3)
     @ApiModelProperty(value = "学号")
@@ -130,12 +130,12 @@ public class TSyncExamStudentScoreResult implements Serializable {
         this.semesterName = semesterName;
     }
 
-    public String getExamStudentName() {
-        return examStudentName;
+    public String getName() {
+        return name;
     }
 
-    public void setExamStudentName(String examStudentName) {
-        this.examStudentName = examStudentName;
+    public void setName(String name) {
+        this.name = name;
     }
 
     public String getStatusStr() {

+ 1 - 7
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/TSyncExamStudentScoreServiceImpl.java

@@ -164,17 +164,11 @@ public class TSyncExamStudentScoreServiceImpl extends ServiceImpl<TSyncExamStude
             }
         } catch (Exception e) {
             e.printStackTrace();
-            if (Objects.nonNull(fileTarget)) {
-                fileTarget.delete();
-            }
-            tSyncExamStudentScore.setPath(null);
+            tSyncExamStudentScore.setTrajectoryUrls(null);
         } finally {
             if (Objects.nonNull(fileSource)) {
                 fileSource.delete();
             }
-            if (Objects.nonNull(fileTarget) && imageTrajectoryEnum == ImageTrajectoryEnum.PREVIEW) {
-                fileTarget.delete();
-            }
         }
         return tSyncExamStudentScore;
     }

+ 3 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/execute/AsyncScoreBatchDownloadService.java

@@ -19,6 +19,7 @@ import org.springframework.stereotype.Service;
 import java.text.MessageFormat;
 import java.util.Date;
 import java.util.Map;
+import java.util.Objects;
 import java.util.StringJoiner;
 
 /**
@@ -32,6 +33,7 @@ import java.util.StringJoiner;
 public class AsyncScoreBatchDownloadService extends AsyncExportTaskTemplete {
 
     public static final String OBJ_TITLE = "成绩轨迹";
+    public static final String BEGIN_TITLE = "->开始准备处理下载的";
     private final static Logger log = LoggerFactory.getLogger(AsyncScoreBatchDownloadService.class);
 
     @Override
@@ -45,7 +47,7 @@ public class AsyncScoreBatchDownloadService extends AsyncExportTaskTemplete {
         try {
             TaskLogicService taskLogicService = SpringContextHolder.getBean(TaskLogicService.class);
             Map<String, Object> result = taskLogicService.executeDownloadScoreLogic(map);
-            stringJoinerSummary.add(MessageFormat.format("{0}{1}{2}{3}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), FINISH_TITLE, Long.valueOf(String.valueOf(result.get("count"))), FINISH_SIZE));
+            stringJoinerSummary.add(MessageFormat.format("{0}{1}{2}{3}{4}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), FINISH_TITLE, Long.valueOf(String.valueOf(result.get("count"))), FINISH_SIZE, Objects.nonNull(result.get("error")) ? (String) result.get("error") : ""));
             tbTask.setResult(TaskResultEnum.SUCCESS);
         } catch (Exception e) {
             log.error("请求出错", e);

+ 1 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/export/AsyncExportTaskTemplete.java

@@ -41,7 +41,7 @@ public abstract class AsyncExportTaskTemplete {
     private final static Logger log = LoggerFactory.getLogger(AsyncImportTaskTemplete.class);
     public static final String BEGIN_TITLE = "->开始准备处理导出的";
     public static final String FINISH_TITLE = "->数据处理结束,共处理了";
-    public static final String FINISH_SIZE = "条数据";
+    public static final String FINISH_SIZE = "条数据";
     public static final String EXCEPTION_TITLE = "->数据处理发生异常!";
     public static final String EXCEPTION_DATA = "错误信息:";
     public static final String TXT_PREFIX = ".txt";

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

@@ -25,7 +25,6 @@ import com.qmth.distributed.print.business.util.CreatePdfUtil;
 import com.qmth.distributed.print.business.util.HtmlToPdfUtil;
 import com.qmth.teachcloud.common.annotation.ExcelDBFieldDesc;
 import com.qmth.teachcloud.common.bean.dto.excel.*;
-import com.qmth.teachcloud.common.bean.params.ApproveUserResult;
 import com.qmth.teachcloud.common.bean.params.ArraysParams;
 import com.qmth.teachcloud.common.contant.SystemConstant;
 import com.qmth.teachcloud.common.entity.*;
@@ -1451,7 +1450,11 @@ public class TaskLogicServiceImpl implements TaskLogicService {
      * @throws Exception
      */
     @Override
+    @Transactional
     public Map<String, Object> executeDownloadScoreLogic(Map<String, Object> map) throws Exception {
+        File zipFile = null;
+        List<File> sourceFiles = null;
+        List<TSyncExamStudentScore> errorTSyncExamStudentScoreList = null;
         try {
             SysUser sysUser = (SysUser) map.get(SystemConstant.USER);
             Long semesterId = null, orgId = null, majorId = null, clazzId = null;
@@ -1464,6 +1467,8 @@ public class TaskLogicServiceImpl implements TaskLogicService {
 
             List<TSyncExamStudentScoreResult> tSyncExamStudentScoreResultList = tSyncExamStudentScoreService.export(sysUser.getSchoolId(), semesterId, orgId, majorId, clazzId, courseCode);
             if (Objects.nonNull(tSyncExamStudentScoreResultList) && tSyncExamStudentScoreResultList.size() > 0) {
+                List<TSyncExamStudentScore> tSyncExamStudentScoreList = new Gson().fromJson(JacksonUtil.parseJson(tSyncExamStudentScoreResultList), new TypeToken<List<TSyncExamStudentScore>>() {
+                }.getType());
                 LocalDateTime nowTime = LocalDateTime.now();
                 StringJoiner stringJoiner = new StringJoiner("");
                 stringJoiner.add(SystemConstant.TEMP_FILES_DIR).add(File.separator);
@@ -1473,54 +1478,61 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                         .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
                         .add(String.format("%02d", nowTime.getDayOfMonth()))
                         .add(File.separator).add(SystemConstant.getUuid()).add(SystemConstant.ZIP_PREFIX);
-                File zipFile = new File(stringJoiner.toString() + dirZipName.toString());
-                if (!zipFile.getParentFile().exists()) {
+                zipFile = new File(stringJoiner.toString() + dirZipName.toString());
+                if (!zipFile.exists()) {
                     zipFile.getParentFile().mkdirs();
                     zipFile.createNewFile();
                 }
-                List<File> sourceFiles = new ArrayList<>();
-
-                for (TSyncExamStudentScoreResult t : tSyncExamStudentScoreResultList) {
+                sourceFiles = new ArrayList<>();
+                List<TSyncExamStudentScore> updateTSyncExamStudentScoreList = new ArrayList<>();
+                errorTSyncExamStudentScoreList = new ArrayList<>();
+                for (TSyncExamStudentScore t : tSyncExamStudentScoreList) {
+                    boolean update = Objects.isNull(t.getTrajectoryUrls()) ? true : false;
+                    t = tSyncExamStudentScoreService.createImageTrajectory(t, ImageTrajectoryEnum.DOWNLOAD);
                     if (Objects.nonNull(t.getTrajectoryUrls())) {
-                        sourceFiles.add(fileStoreUtil.ossDownload(t.getTrajectoryUrls(), stringJoiner.toString() + t.getTrajectoryUrls(), UploadFileEnum.FILE.getFssType()));
+                        sourceFiles.add(t.getTrajectoryFile());
                     } else {
-                        if (Objects.nonNull(t.getSheetUrls())) {
-                            String format = t.getSheetUrls().substring(t.getSheetUrls().lastIndexOf("."), t.getSheetUrls().length());
-                            File file = new File(t.getSheetUrls());
-                            StringJoiner dirImageName = new StringJoiner("");
-                            dirImageName.add(UploadFileEnum.FILE.getTitle()).add(File.separator);
-                            dirImageName.add(String.valueOf(nowTime.getYear())).add(File.separator)
-                                    .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
-                                    .add(String.format("%02d", nowTime.getDayOfMonth()))
-                                    .add(File.separator).add(SystemConstant.getUuid()).add(format);
-                            File fileTarget = new File(stringJoiner.toString() + dirImageName.toString());
-                            ImageTrajectoryUtil.createImage(file, fileTarget, 10, 10);
-                            sourceFiles.add(fileTarget);
-                            fileStoreUtil.ossUpload(dirImageName.toString(), fileTarget, DigestUtils.md5Hex(new FileInputStream(fileTarget)), UploadFileEnum.FILE.getFssType());
-                            t.setTrajectoryUrls(dirImageName.toString());
-                        }
+                        errorTSyncExamStudentScoreList.add(t);
+                    }
+                    if (update || Objects.isNull(t.getTrajectoryUrls())) {
+                        updateTSyncExamStudentScoreList.add(t);
                     }
                 }
-                FileUtil.doZip(zipFile, sourceFiles);
-
-                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());
+                tSyncExamStudentScoreService.saveOrUpdateBatch(updateTSyncExamStudentScoreList);
+                if (Objects.nonNull(sourceFiles) && sourceFiles.size() > 0) {
+                    FileUtil.doZip(zipFile, sourceFiles);
+                    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);
+                    TBTask tbTask = (TBTask) map.get(SystemConstant.TASK);
+                    tbTask.setResultFilePath(jsonObject.toJSONString());
                 }
-                fileStoreUtil.ossUpload(dirZipName.toString(), zipFile, DigestUtils.md5Hex(new FileInputStream(zipFile)), UploadFileEnum.FILE.getFssType());
-                TBTask tbTask = (TBTask) map.get(SystemConstant.TASK);
-                tbTask.setResultFilePath(jsonObject.toJSONString());
             }
-            map.computeIfAbsent("count", v -> Objects.isNull(tSyncExamStudentScoreResultList) ? 0 : tSyncExamStudentScoreResultList.size());
+            List<File> finalSourceFiles = sourceFiles;
+            map.computeIfAbsent("count", v -> Objects.isNull(finalSourceFiles) ? 0 : finalSourceFiles.size());
+            if (Objects.nonNull(errorTSyncExamStudentScoreList) && errorTSyncExamStudentScoreList.size() > 0) {
+                StringJoiner stringJoiner = new StringJoiner("").add("\r\n");
+                for (TSyncExamStudentScore t : errorTSyncExamStudentScoreList) {
+                    stringJoiner.add("[").add(t.getStudentCode()).add(",").add(t.getName()).add("]\r\n");
+                }
+                List<TSyncExamStudentScore> finalErrorTSyncExamStudentScoreList = errorTSyncExamStudentScoreList;
+                map.computeIfAbsent("error", v -> "其中未下载成功数据" + finalErrorTSyncExamStudentScoreList.size() + "条:" + stringJoiner.toString());
+            }
         } catch (Exception e) {
             log.error("请求出错", e);
             e.printStackTrace();
+        } finally {
+            if (Objects.nonNull(zipFile)) {
+                zipFile.delete();
+            }
         }
         return map;
     }

+ 1 - 1
distributed-print-business/src/main/resources/mapper/TSyncExamStudentScoreMapper.xml

@@ -9,7 +9,7 @@
             bs.name as semesterName,
             tsess.exam_id as examId,
             tsess.exam_code as examCode,
-            tsess.name as examStudentName,
+            tsess.name,
             tsess.student_code as studentCode,
             so.id as orgId,
             so.name as orgName,

+ 4 - 11
distributed-print/src/main/java/com/qmth/distributed/print/api/TSyncExamStudentScoreController.java

@@ -23,8 +23,6 @@ import com.qmth.teachcloud.common.entity.TBTask;
 import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
 import com.qmth.teachcloud.common.enums.PushTypeEnum;
 import com.qmth.teachcloud.common.enums.TaskTypeEnum;
-import com.qmth.teachcloud.common.service.TeachcloudCommonService;
-import com.qmth.teachcloud.common.util.FileStoreUtil;
 import com.qmth.teachcloud.common.util.Result;
 import com.qmth.teachcloud.common.util.ResultUtil;
 import com.qmth.teachcloud.common.util.ServletUtil;
@@ -54,7 +52,7 @@ import java.util.Optional;
 @Api(tags = "成绩归档Controller")
 @RestController
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.sync}")
-@Aac(auth = BOOL.FALSE, strict = BOOL.FALSE)
+//@Aac(auth = BOOL.FALSE, strict = BOOL.FALSE)
 @Validated
 public class TSyncExamStudentScoreController {
 
@@ -76,12 +74,6 @@ public class TSyncExamStudentScoreController {
     @Resource
     AsyncScoreBatchDownloadService asyncScoreBatchDownloadService;
 
-    @Resource
-    TeachcloudCommonService teachcloudCommonService;
-
-    @Resource
-    FileStoreUtil fileStoreUtil;
-
     @ApiOperation(value = "成绩归档查询列表")
     @ApiResponses({@ApiResponse(code = 200, message = "成绩查询信息", response = TSyncExamStudentScoreResult.class)})
     @RequestMapping(value = "/score/list", method = RequestMethod.POST)
@@ -144,10 +136,11 @@ public class TSyncExamStudentScoreController {
                 .eq(TSyncExamStudentScore::getStudentCode, studentCode);
         TSyncExamStudentScore tSyncExamStudentScore = tSyncExamStudentScoreService.getOne(tSyncExamStudentScoreQueryWrapper);
         Optional.ofNullable(tSyncExamStudentScore).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("考生不存在"));
+        tSyncExamStudentScore = tSyncExamStudentScoreService.createImageTrajectory(tSyncExamStudentScore, ImageTrajectoryEnum.PREVIEW);
         boolean update = Objects.isNull(tSyncExamStudentScore.getTrajectoryUrls()) ? true : false;
         tSyncExamStudentScore = tSyncExamStudentScoreService.createImageTrajectory(tSyncExamStudentScore, ImageTrajectoryEnum.PREVIEW);
-        Optional.ofNullable(tSyncExamStudentScore.getPath()).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("资源未获取到,请稍候再试"));
-        if (update) {
+        Optional.ofNullable(tSyncExamStudentScore.getTrajectoryUrls()).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("资源未获取到,请稍候再试"));
+        if (update || Objects.isNull(tSyncExamStudentScore.getTrajectoryUrls())) {
             tSyncExamStudentScoreService.updateById(tSyncExamStudentScore);
         }
         return ResultUtil.ok(new EditResult(tSyncExamStudentScore.getPath()));

+ 1 - 1
teachcloud-common/src/main/java/com/qmth/teachcloud/common/enums/TaskTypeEnum.java

@@ -41,7 +41,7 @@ public enum TaskTypeEnum {
 
     SCORE_EXPORT("成绩导出"),
 
-    SCORE_DOWNLOAD("成绩轨迹下载");
+    SCORE_DOWNLOAD("成绩轨迹下载");
 
     private String title;
 

+ 8 - 8
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/FileStoreUtil.java

@@ -5,6 +5,7 @@ import com.qmth.teachcloud.common.config.DictionaryConfig;
 import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
 import com.qmth.teachcloud.common.enums.UploadFileEnum;
 import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -122,7 +123,6 @@ public class FileStoreUtil {
         } else {
             throw ExceptionResultEnum.ERROR.exception("文件存储store类型不存在");
         }
-
         return server + "/" + objectPath;
     }
 
@@ -148,15 +148,15 @@ public class FileStoreUtil {
      */
     private File saveLocal(InputStream inputStream, String dirPath) throws IOException {
         File desFile = new File(dirPath);
-        if (!desFile.getParentFile().exists()) {
+        if (!desFile.exists()) {
             desFile.getParentFile().mkdirs(); //目标文件目录不存在的话需要创建目录
+            desFile.createNewFile();
         }
-        byte[] bytes = new byte[1024]; // 开辟一个拷贝缓冲区
-        try (OutputStream outputStream = new FileOutputStream(desFile)) {
-            int length;
-            while ((length = inputStream.read(bytes)) != -1) { //当读到尽头后,返回值为-1这个时候停止输出,拷贝结束
-                outputStream.write(bytes, 0, length);
-            }
+        if (desFile.length() > 0) {
+            return desFile;
+        }
+        try {
+            FileUtils.copyInputStreamToFile(inputStream, desFile);
             return desFile;
         } finally {
             if (inputStream != null) {