Просмотр исходного кода

3.2.3 题卡选择课程下所有题卡

xiaofei 2 лет назад
Родитель
Сommit
a8de929024

+ 1 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/mapper/ExamCardMapper.java

@@ -29,7 +29,7 @@ public interface ExamCardMapper extends BaseMapper<ExamCard> {
 
     List<ExamCard> listGenericCard(@Param("schoolId") Long schoolId, @Param("type") String type, @Param("cardRuleId") Long cardRuleId);
 
-    List<ExamCard> listCustom(@Param("schoolId") Long schoolId, @Param("orgId") Long orgId, @Param("courseCode") String courseCode, @Param("type") String type, @Param("userId") Long userId);
+    List<ExamCard> listCustom(@Param("schoolId") Long schoolId,@Param("courseCode") String courseCode, @Param("type") String type);
 
     CardDetailDto getCardDetailBySelect(Long cardId);
 

+ 2 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/ExamCardService.java

@@ -56,4 +56,6 @@ public interface ExamCardService extends IService<ExamCard> {
     Long copyCard(Long id, String courseCode);
 
     void convertImage(Long id);
+
+    void cardDownload(HttpServletResponse response, String id);
 }

+ 75 - 7
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/ExamCardServiceImpl.java

@@ -8,10 +8,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.gson.JsonObject;
-import com.qmth.distributed.print.business.bean.dto.CardCustDto;
-import com.qmth.distributed.print.business.bean.dto.CardDetailDto;
-import com.qmth.distributed.print.business.bean.dto.ExamCardPageDto;
-import com.qmth.distributed.print.business.bean.dto.SyncExamCardDto;
+import com.qmth.distributed.print.business.bean.dto.*;
 import com.qmth.distributed.print.business.bean.marking.ConvertJpgStorage;
 import com.qmth.distributed.print.business.bean.params.ExamCardParams;
 import com.qmth.distributed.print.business.bean.params.GenericExamCardParams;
@@ -21,7 +18,9 @@ import com.qmth.distributed.print.business.enums.CardTypeEnum;
 import com.qmth.distributed.print.business.enums.MakeMethodEnum;
 import com.qmth.distributed.print.business.mapper.ExamCardMapper;
 import com.qmth.distributed.print.business.service.*;
+import com.qmth.distributed.print.business.util.CreatePdfUtil;
 import com.qmth.distributed.print.business.util.HtmlToJpgUtil;
+import com.qmth.distributed.print.business.util.HtmlToPdfUtil;
 import com.qmth.teachcloud.common.bean.dto.DataPermissionRule;
 import com.qmth.teachcloud.common.bean.params.ArraysParams;
 import com.qmth.teachcloud.common.config.DictionaryConfig;
@@ -42,10 +41,12 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.FileCopyUtils;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
+import java.nio.charset.StandardCharsets;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -85,7 +86,7 @@ public class ExamCardServiceImpl extends ServiceImpl<ExamCardMapper, ExamCard> i
     HtmlToJpgUtil htmlToJpgUtil;
 
     @Resource
-    DictionaryConfig dictionaryConfig;
+    CreatePdfUtil createPdfUtil;
 
     @Resource
     BasicRoleDataPermissionService basicRoleDataPermissionService;
@@ -273,7 +274,7 @@ public class ExamCardServiceImpl extends ServiceImpl<ExamCardMapper, ExamCard> i
         // 专卡
         if (SystemConstant.ALL_CARD != cardRuleId) {
             // 只查自己创建的专卡(20230405已改为查询课程下所有题卡,允许复制,不能选择)
-            List<ExamCard> customCards = this.baseMapper.listCustom(schoolId, sysUser.getOrgId(), courseCode, CardTypeEnum.CUSTOM.name(), sysUser.getId());
+            List<ExamCard> customCards = this.baseMapper.listCustom(schoolId, courseCode, CardTypeEnum.CUSTOM.name());
             if (!customCards.isEmpty()) {
                 list.addAll(customCards);
             }
@@ -516,7 +517,7 @@ public class ExamCardServiceImpl extends ServiceImpl<ExamCardMapper, ExamCard> i
         if (examCard == null) {
             throw ExceptionResultEnum.ERROR.exception("题卡不存在");
         }
-        if (courseCode.equals(examCard.getCourseCode())) {
+        if (!courseCode.equals(examCard.getCourseCode())) {
             throw ExceptionResultEnum.ERROR.exception("复制的题卡不属于当前课程,无法复制");
         }
 
@@ -571,6 +572,73 @@ public class ExamCardServiceImpl extends ServiceImpl<ExamCardMapper, ExamCard> i
         }
     }
 
+    @Override
+    public void cardDownload(HttpServletResponse response, String id) {
+        File tempFile = null;
+        String rootPath = null;
+        try {
+
+            // 题卡
+            ExamCard examCard = this.getById(id);
+            // 本地保存目录
+            tempFile = SystemConstant.getFileTempDirVar(String.valueOf(System.currentTimeMillis()) + File.separator + SystemConstant.getNanoId(), SystemConstant.TEMP_PREFIX);
+            rootPath = tempFile.getParent();
+            StringJoiner dirPath = new StringJoiner("");
+            dirPath = dirPath.add(rootPath).add(File.separator)
+                    .add(id).add(File.separator).add(SystemConstant.HYPHEN)
+                    .add(examCard.getTitle()).add(File.separator);
+            List<File> fileList = new ArrayList<>();
+
+            String cardHtmlPath = dirPath + examCard.getTitle() + SystemConstant.HTML_PREFIX;
+            String cardPdfPath = dirPath + examCard.getTitle() + SystemConstant.PDF_PREFIX;
+
+            ExamCardDetail examCardDetail = examCardDetailService.getByCardId(examCard.getId());
+            String htmlContent;
+            // 通用模板
+            if (MakeMethodEnum.SELECT.equals(examCard.getMakeMethod())) {
+                htmlContent = createPdfUtil.resetHtmlTemplateBar(examCardDetail.getHtmlContent());
+            } else {
+//                BasicCardRule basicCardRule = basicCardRuleService.getById(examTask.getCardRuleId());
+                htmlContent = createPdfUtil.replaceHtmlCard(examCardDetail, null);
+            }
+
+            // html
+            File htmlFile = new File(cardHtmlPath);
+            if (!htmlFile.exists()) {
+                htmlFile.getParentFile().mkdirs();
+                htmlFile.createNewFile();
+            }
+            // 生成html文件
+            FileCopyUtils.copy(htmlContent.getBytes(StandardCharsets.UTF_8), htmlFile);
+            fileList.add(htmlFile);
+            // 转pdf文件
+            File pdfFile = new File(cardPdfPath);
+            if (!pdfFile.exists()) {
+                pdfFile.getParentFile().mkdirs();
+                pdfFile.createNewFile();
+            }
+            HtmlToPdfUtil.convert(cardHtmlPath, cardPdfPath, PageSizeEnum.A3);
+            fileList.add(pdfFile);
+
+            if (fileList.size() == 0) {
+                throw ExceptionResultEnum.ERROR.exception("没有可导出文件");
+            }
+            if (Objects.nonNull(tempFile)) {
+                tempFile.delete();
+            }
+            teachcloudCommonService.downloadFileAndZip(response, rootPath, rootPath);
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (Objects.nonNull(tempFile)) {
+                tempFile.delete();
+            }
+            if (Objects.nonNull(rootPath)) {
+                ConvertUtil.delFolder(rootPath);
+            }
+        }
+    }
+
     /**
      * 生成新的title
      *

+ 7 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/ExamTaskServiceImpl.java

@@ -1009,13 +1009,20 @@ public class ExamTaskServiceImpl extends ServiceImpl<ExamTaskMapper, ExamTask> i
         String paperAttachmentIds = examTaskDetail.getPaperAttachmentIds();
         if (StringUtils.isNotBlank(paperAttachmentIds)) {
             List<JSONObject> objects = JSON.parseArray(paperAttachmentIds, JSONObject.class);
+            List<Long> cardIds = new ArrayList<>();
             for (JSONObject object : objects) {
+                cardIds.add(object.getLong("cardId"));
                 object.put("cardId", "");
                 object.put("cardType", "");
                 object.put("cardTitle", "");
             }
             examTaskDetail.setPaperAttachmentIds(JSON.toJSONString(objects));
             examTaskDetailService.updateById(examTaskDetail);
+
+            // 更新题卡为未使用
+            UpdateWrapper<ExamCard> updateWrapper = new UpdateWrapper<>();
+            updateWrapper.lambda().set(ExamCard::getUsed, false).in(ExamCard::getId, cardIds);
+            examCardService.update(updateWrapper);
         }
         return true;
     }

+ 2 - 9
distributed-print-business/src/main/resources/mapper/ExamCardMapper.xml

@@ -130,16 +130,9 @@
             exam_card a
         WHERE
             a.school_id = #{schoolId}
-            AND a.type = #{type}
             AND a.course_code = #{courseCode}
-            -- AND a.used = false
-            -- AND a.create_id = #{userId}
-            -- NOT EXISTS( SELECT 1 FROM
-            --     exam_task_detail b
-            --        LEFT JOIN exam_task et on b.exam_task_id = et.id
-            --        LEFT JOIN t_f_flow_approve e ON et.flow_id = e.flow_id
-            -- WHERE
-            --    e.status != 'CANCEL' and FIND_IN_SET(a.id, REPLACE(REPLACE(substring_index(substring_index(CONVERT((b.paper_attachment_ids ->>'$[*].cardId') USING utf8), ']', 1), '[',-1),' "','"'),'"','')))
+            AND a.type = #{type}
+            AND a.create_time >= 1680624000000
         order by a.create_time desc
     </select>
     <select id="getCardDetailBySelect" resultType="com.qmth.distributed.print.business.bean.dto.CardDetailDto">

+ 12 - 0
distributed-print/src/main/java/com/qmth/distributed/print/api/ExamCardController.java

@@ -228,4 +228,16 @@ public class ExamCardController {
         examCardService.convertImage(SystemConstant.convertIdToLong(id));
         return ResultUtil.ok(true, "");
     }
+
+    /**
+     * 导出题卡pdf,html,json,jpg
+     *
+     * @param response
+     * @param id 题卡ID
+     */
+    @ApiOperation(value = "导出题卡文件")
+    @RequestMapping(value = "/download_card", method = RequestMethod.POST)
+    public void cardDownload(HttpServletResponse response, @RequestParam(value = "id") String id) throws Exception {
+        examCardService.cardDownload(response, id);
+    }
 }