deason 6 年之前
父节点
当前提交
c3df383c4f

+ 16 - 0
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/upyun/UpYunClient.java

@@ -16,6 +16,8 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.io.File;
+
 /**
  * @author: fengdesheng
  * @since: 2018/11/1
@@ -43,6 +45,20 @@ public class UpYunClient {
         return Result.error();
     }
 
+    public File download(String filePath) {
+        try {
+            String rootDir = UpYunClient.class.getClassLoader().getResource("").getPath() + "/files";
+            FileUtils.makeDirs(rootDir);
+            final String newFilePath = rootDir + "/" + FileUtils.newFileName(filePath);
+            File file = new File(newFilePath);
+            this.getInstance().readFile(filePath, file);
+            return file;
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+        }
+        return null;
+    }
+
     public UpYun getInstance() {
         UpYun upyun = new UpYun(upYunProperty.getBucket(), upYunProperty.getOperator(), upYunProperty.getPsw());
         upyun.setDebug(true);

+ 31 - 1
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/utils/FileUtils.java

@@ -7,6 +7,7 @@
 
 package cn.com.qmth.examcloud.core.print.common.utils;
 
+import java.io.File;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.UUID;
@@ -32,7 +33,7 @@ public class FileUtils {
      */
     public static String newFileName(String fileName) {
         String uuid = UUID.randomUUID().toString().replaceAll("-", "");
-        if (fileName != null) {
+        if (fileName != null && !"".equals(fileName)) {
             int index = fileName.lastIndexOf(".");
             if (index > 0 && index < fileName.length() - 1) {
                 //获取原来文件的后缀名
@@ -42,4 +43,33 @@ public class FileUtils {
         return uuid;
     }
 
+    /**
+     * 获取文件名(包含后缀名)
+     */
+    public static String getFileName(String filePath) {
+        if (filePath == null || "".equals(filePath)) {
+            return "";
+        }
+        filePath = filePath.replace("\\", "/");
+        int index = filePath.lastIndexOf("/");
+        if (index > -1) {
+            return filePath.substring(index + 1);
+        }
+        return filePath;
+    }
+
+    /**
+     * 创建文件目录
+     */
+    public static boolean makeDirs(String path) {
+        if (path == null || "".equals(path)) {
+            return false;
+        }
+        File folder = new File(path);
+        if (!folder.exists()) {
+            return folder.mkdirs();
+        }
+        return true;
+    }
+
 }

+ 21 - 9
examcloud-core-print-provider/src/main/java/cn/com/qmth/examcloud/core/print/api/controller/UploadController.java → examcloud-core-print-provider/src/main/java/cn/com/qmth/examcloud/core/print/api/controller/CommonController.java

@@ -12,34 +12,46 @@ import cn.com.qmth.examcloud.core.print.common.Result;
 import cn.com.qmth.examcloud.core.print.common.upyun.UpYunClient;
 import cn.com.qmth.examcloud.core.print.common.upyun.UpYunProperty;
 import cn.com.qmth.examcloud.core.print.common.utils.Check;
+import cn.com.qmth.examcloud.core.print.common.utils.FileUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.MultipartHttpServletRequest;
 
 import javax.servlet.http.HttpServletRequest;
+import java.io.File;
 
 /**
- * 文件上传相关接口
+ * 常用相关接口
  *
  * @author: fengdesheng
  * @since: 2018/11/01
  */
 @RestController
-@Api(tags = "文件上传相关接口")
-@RequestMapping("${$rmp.ctrl.print}/upload")
-public class UploadController extends ControllerSupport {
-    private static final Logger log = LoggerFactory.getLogger(UploadController.class);
+@Api(tags = "常用相关接口")
+@RequestMapping("${$rmp.ctrl.print}/common")
+public class CommonController extends ControllerSupport {
+    private static final Logger log = LoggerFactory.getLogger(CommonController.class);
     @Autowired
     private UpYunClient upYunClient;
 
-    @PostMapping
+    @GetMapping("/download")
+    @ApiOperation(value = "下载文件")
+    public void download(@RequestParam String filePath) throws Exception {
+        File file = upYunClient.download(filePath);
+        final String fileName = FileUtils.getFileName(filePath);
+        if (file != null) {
+            super.exportFile(fileName, file);
+        } else {
+            super.exportFile("404", new byte[0]);
+        }
+    }
+
+    @PostMapping("/upload")
     @ApiOperation(value = "上传文件")
     public Result upload(HttpServletRequest request) throws Exception {
         MultipartFile file = null;

+ 3 - 3
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/CommonService.java

@@ -35,12 +35,12 @@ public class CommonService {
      * (正式场景数据极少,一次获取全部)
      */
     public List<ExamInfo> getExamList(String examType) {
-        //todo 暂时直接查库,待“考务”接口提供后改为通过接口获取数据
+        //暂时直接查库,待“考务”接口提供后改为通过接口获取数据
         SqlWrapper sql = new SqlWrapper()
                 .select("em.id examId,em.name examName,em.root_org_id orgId,org.name orgName").from("ec_e_exam em")
                 .innerJoin("ec_b_org org").on("org.id", "em.root_org_id");
         if (StringUtils.isNotBlank(examType)) {
-            //sql.where().eq("em.exam_type", examType); todo 临时注释
+            //sql.where().eq("em.exam_type", examType); todo 仅测试,临时注释
         }
         sql.orderBy("em.id", false);
         return jdbcTemplate.query(sql.build(), new BeanPropertyRowMapper(ExamInfo.class));
@@ -51,7 +51,7 @@ public class CommonService {
      * 含试卷类型、考生数量
      */
     public List<ExamCourseInfo> getExamCourseList(Long orgId, Long examId, Long courseId, String paperType) {
-        //todo 暂时直接查库,待“考务”接口提供后改为通过接口获取数据
+        //暂时直接查库,待“考务”接口提供后改为通过接口获取数据
         SqlWrapper sql = new SqlWrapper()
                 .select("root_org_id orgId,exam_id,course_id,course_code,course_name,paper_type,count(course_code) totalStudent")
                 .from("ec_e_exam_student") //old ecs_exam_student

+ 5 - 3
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/impl/CourseStatisticServiceImpl.java

@@ -12,11 +12,11 @@ import cn.com.qmth.examcloud.core.print.common.jpa.SearchBuilder;
 import cn.com.qmth.examcloud.core.print.common.jpa.SpecUtils;
 import cn.com.qmth.examcloud.core.print.common.utils.Check;
 import cn.com.qmth.examcloud.core.print.entity.CourseStatistic;
-import cn.com.qmth.examcloud.core.print.enums.ExamType;
 import cn.com.qmth.examcloud.core.print.enums.PaperStatus;
 import cn.com.qmth.examcloud.core.print.repository.CourseStatisticRepository;
 import cn.com.qmth.examcloud.core.print.service.CommonService;
 import cn.com.qmth.examcloud.core.print.service.CourseStatisticService;
+import cn.com.qmth.examcloud.core.print.service.PrintingProjectService;
 import cn.com.qmth.examcloud.core.print.service.bean.common.ExamCourseInfo;
 import cn.com.qmth.examcloud.core.print.service.bean.common.ExamInfo;
 import cn.com.qmth.examcloud.core.print.service.bean.coursestatistic.CourseRefreshForm;
@@ -44,6 +44,8 @@ public class CourseStatisticServiceImpl implements CourseStatisticService {
     @Autowired
     private CourseStatisticRepository courseStatisticsRepository;
     @Autowired
+    private PrintingProjectService printingProjectService;
+    @Autowired
     private CommonService commonService;
 
     @Override
@@ -97,8 +99,8 @@ public class CourseStatisticServiceImpl implements CourseStatisticService {
     @Override
     public void initAllCourseStatistic() {
         log.debug("initAllCourseStatistic...");
-        //todo
-        List<ExamInfo> exams = commonService.getExamList(ExamType.TRADITION.name());
+        //只处理已存在印刷项目中的考试
+        List<ExamInfo> exams = printingProjectService.getExamList(null);
         if (exams != null && !exams.isEmpty()) {
             for (ExamInfo info : exams) {
                 List<ExamCourseInfo> examCourses = commonService.getExamCourseList(info.getOrgId(), info.getExamId());

+ 1 - 0
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/impl/PrintingTemplateServiceImpl.java

@@ -63,6 +63,7 @@ public class PrintingTemplateServiceImpl implements PrintingTemplateService {
         Check.isNull(info.getExamId(), "考试ID不能为空!");
         Check.isNull(info.getTemplateType(), "模板类型不能为空!");
         Check.isFalse(TemplateType.isExist(info.getTemplateType()), "模板类型不正确!");
+        Check.isNull(info.getFileName(), "模板文件名不能为空!");
         Check.isNull(info.getFileUrl(), "模板文件地址不能为空!");
 
         SearchBuilder searches = new SearchBuilder()

+ 3 - 2
examcloud-core-print-starter/src/main/resources/security-exclusions.conf

@@ -6,8 +6,9 @@
 [/swagger-resources][/configuration/security][GET]
 [][${springfox.documentation.swagger.v2.path:/v2/api-docs}][GET]
 
-[${$rmp.ctrl.print}/upload][][POST]
-[${$rmp.ctrl.print}/upload][/upyun/info][POST]
+[${$rmp.ctrl.print}/common][/upload][POST]
+[${$rmp.ctrl.print}/common][/download][GET]
+[${$rmp.ctrl.print}/common][/upyun/info][POST]
 
 [${$rmp.ctrl.print}/printing/project][/list][POST]
 [${$rmp.ctrl.print}/printing/project][/{id}][POST]