浏览代码

审核表

caozixuan 3 年之前
父节点
当前提交
f830100b4b

+ 13 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/entity/ExamTask.java

@@ -120,6 +120,11 @@ public class ExamTask extends BaseEntity implements Serializable {
     @TableField(value = "flow_id", updateStrategy = FieldStrategy.IGNORED)
     private Long flowId;
 
+    @ApiModelProperty(value = "命题任务审批表记录")
+    @JsonSerialize(using = ToStringSerializer.class)
+    @TableField(value = "approve_form_attachment_id")
+    private Long approveFormAttachmentId;
+
     public Long getFlowId() {
         return flowId;
     }
@@ -291,4 +296,12 @@ public class ExamTask extends BaseEntity implements Serializable {
     public void setUsers(List<Map<String, String>> users) {
         this.users = users;
     }
+
+    public Long getApproveFormAttachmentId() {
+        return approveFormAttachmentId;
+    }
+
+    public void setApproveFormAttachmentId(Long approveFormAttachmentId) {
+        this.approveFormAttachmentId = approveFormAttachmentId;
+    }
 }

+ 4 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/ExamTaskService.java

@@ -20,6 +20,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.List;
 import java.util.Map;
 
@@ -219,4 +220,7 @@ public interface ExamTaskService extends IService<ExamTask> {
      * @return 命题任务审核表参数
      */
     ExamTaskApprovalFormDto findExamTaskApprovalForm(Long examTaskId);
+
+
+    void findExamTaskApprovalPdf(HttpServletResponse response,Long examTaskId, String htmlContext) throws Exception;
 }

+ 9 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/PrintCommonService.java

@@ -55,6 +55,15 @@ public interface PrintCommonService {
      */
     public BasicAttachment saveAttachmentHtml(String fileName, String htmlContent, Long userId, List<PdfDto> localFileList) throws IOException;
 
+    /**
+     * 保存html附件和该html转成pdf的附件
+     * @param fileName 文件名称
+     * @param htmlContent html内容
+     * @param userId 创建人
+     * @return 附件对象
+     */
+    public BasicAttachment saveAttachmentHtmlAndPdf(String fileName, String htmlContent,Long userId) throws IOException;
+
     /**
      * 保存附件公用
      *

+ 35 - 6
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/ExamTaskServiceImpl.java

@@ -27,10 +27,7 @@ import com.qmth.teachcloud.common.contant.SystemConstant;
 import com.qmth.teachcloud.common.entity.*;
 import com.qmth.teachcloud.common.enums.*;
 import com.qmth.teachcloud.common.service.*;
-import com.qmth.teachcloud.common.util.ExcelUtil;
-import com.qmth.teachcloud.common.util.RedisUtil;
-import com.qmth.teachcloud.common.util.ResultUtil;
-import com.qmth.teachcloud.common.util.ServletUtil;
+import com.qmth.teachcloud.common.util.*;
 import com.qmth.teachcloud.common.util.excel.ExcelError;
 import org.activiti.engine.ActivitiObjectNotFoundException;
 import org.activiti.engine.TaskService;
@@ -48,7 +45,9 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -154,6 +153,9 @@ public class ExamTaskServiceImpl extends ServiceImpl<ExamTaskMapper, ExamTask> i
     @Resource
     BasicClazzService basicClazzService;
 
+    @Resource
+    FileStoreUtil fileStoreUtil;
+
     @Override
     public List<ExamTask> listByCourseCode(Long schoolId, String code) {
         QueryWrapper<ExamTask> queryWrapper = new QueryWrapper<>();
@@ -1688,8 +1690,6 @@ public class ExamTaskServiceImpl extends ServiceImpl<ExamTaskMapper, ExamTask> i
         // 流程id
         Long flowId = examTask.getFlowId();
 
-
-
         // 2.ExamRoomInfo 部分
         List<ExamRoomInfo> examRoomInfoList = new ArrayList<>();
         List<ExamPackageDetail> examPackageDetailDatasource = this.baseMapper.findExamPackageDetailByPaperNumber(paperNumber);
@@ -1783,6 +1783,35 @@ public class ExamTaskServiceImpl extends ServiceImpl<ExamTaskMapper, ExamTask> i
         return examTaskApprovalFormDto;
     }
 
+    @Transactional
+    @Override
+    public void findExamTaskApprovalPdf(HttpServletResponse response,Long examTaskId, String htmlContext) throws Exception {
+        ExamTask examTask = this.getById(examTaskId);
+        String fileName = examTask.getPaperNumber() + "审批表";
+//        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
+        if (Objects.isNull(examTask)){
+            throw ExceptionResultEnum.ERROR.exception("未找到命题任务");
+        }
+        Long approveFormAttachmentId = examTask.getApproveFormAttachmentId();
+        BasicAttachment basicAttachment = basicAttachmentService.getById(approveFormAttachmentId);
+        if (Objects.isNull(basicAttachment)) {
+            // 命题任务试卷审批表字段为空 -> 保存html并生成pdf附件
+            basicAttachment = printCommonService.saveAttachmentHtmlAndPdf(fileName, htmlContext, 1L);
+            examTask.setApproveFormAttachmentId(basicAttachment.getId());
+            this.updateById(examTask);
+        }
+        JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
+        String pdfPath = (String) jsonObject.get(SystemConstant.PDF_PATH);
+        String type = (String) jsonObject.get(SystemConstant.TYPE);
+        InputStream inputStream = null;
+        if (Objects.equals(type, SystemConstant.OSS)) {
+            inputStream = fileStoreUtil.ossDownloadIs(pdfPath, fileStoreUtil.getUploadEnumByPath(pdfPath).getFssType());
+        } else {
+            inputStream = new FileInputStream(new File(pdfPath));
+        }
+        ConvertUtil.outputFile(response,inputStream,fileName + SystemConstant.PDF_PREFIX);
+    }
+
     /**
      * 简单校验提交参数
      *

+ 98 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/PrintCommonServiceServiceImpl.java

@@ -45,6 +45,7 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.annotation.Resource;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
@@ -332,6 +333,103 @@ public class PrintCommonServiceServiceImpl implements PrintCommonService {
         return basicAttachment;
     }
 
+    /**
+     * 保存html附件和该html转成pdf的附件
+     * @param fileName 文件名称
+     * @param htmlContent html内容
+     * @param userId 创建人
+     * @return 两个附件对象
+     */
+    @Transactional
+    @Override
+    public BasicAttachment saveAttachmentHtmlAndPdf(String fileName, String htmlContent, Long userId) {
+        BasicAttachment basicAttachment = null;
+        try {
+            byte[] bytes = htmlContent.getBytes();
+            int size = bytes.length;
+            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.HTML.getTitle()).add(File.separator)
+                    .add(String.valueOf(nowTime.getYear())).add(File.separator)
+                    .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
+                    .add(String.format("%02d", nowTime.getDayOfMonth()));
+
+            JSONObject jsonObject = new JSONObject();
+            stringJoiner.add(File.separator).add(SystemConstant.getUuid()).add(SystemConstant.HTML_PREFIX);
+            String fileMd5 = null;
+
+            if (oss) {//上传至oss
+                String dirName = stringJoiner.toString().replaceAll("\\\\", "/");
+                fileStoreUtil.ossUpload(dirName, htmlContent, fileStoreUtil.getUploadEnumByPath(dirName).getFssType());
+                jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
+                jsonObject.put(SystemConstant.PATH, dirName);
+                String url = SystemConstant.TEMP_FILES_DIR + File.separator + dirName;
+                File localHtmlFile = fileStoreUtil.ossDownload(dirName, url, fileStoreUtil.getUploadEnumByPath(dirName).getFssType());
+                StringJoiner pdfStringJoiner = new StringJoiner("");
+                pdfStringJoiner.add(UploadFileEnum.PDF.getTitle()).add(File.separator);
+                pdfStringJoiner.add(String.valueOf(nowTime.getYear())).add(File.separator)
+                        .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
+                        .add(String.format("%02d", nowTime.getDayOfMonth()));
+                pdfStringJoiner.add(File.separator).add(SystemConstant.getUuid()).add(SystemConstant.PDF_PREFIX);
+
+                String pdfDirName = pdfStringJoiner.toString();
+                String destUrl = SystemConstant.PDF_TEMP_FILES_DIR + File.separator + pdfDirName;
+//                destUrl = destUrl.replaceAll("\\\\","/");
+
+                HtmlToPdfUtil.convert(localHtmlFile.getPath(), destUrl, PageSizeEnum.A3);
+//                File pdfFile = asposePdfUtil.documentToPdf(localHtmlFile.getPath(), destUrl, PaperSize.A3);
+                File pdfFile = new File(destUrl);
+                if (!pdfFile.exists()) {
+                    pdfFile.getParentFile().mkdirs();
+                    pdfFile.createNewFile();
+                }
+                fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
+//                ossUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
+                pdfDirName = pdfDirName.replaceAll("\\\\", "/");
+                fileStoreUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)), fileStoreUtil.getUploadEnumByPath(pdfDirName).getFssType());
+//                localHtmlFile.delete();
+                jsonObject.put(SystemConstant.PDF_PATH, pdfDirName);
+                // htmlMd5
+                jsonObject.put("htmlMd5", DigestUtils.md5Hex(new FileInputStream(localHtmlFile)));
+                jsonObject.put("pdfMd5", fileMd5);
+            }else {//上传至服务器
+                File finalFile = new File(stringJoiner.toString());
+                if (!finalFile.exists()) {
+                    finalFile.getParentFile().mkdirs();
+                    finalFile.createNewFile();
+                }
+                FileCopyUtils.copy(bytes, new File(stringJoiner.toString()));
+                fileMd5 = DigestUtils.md5Hex(new FileInputStream(stringJoiner.toString()));
+                jsonObject.put(SystemConstant.TYPE, SystemConstant.LOCAL);
+                jsonObject.put(SystemConstant.PATH, stringJoiner.toString());
+                String destUrl = finalFile.getPath().replaceAll(SystemConstant.HTML_PREFIX, SystemConstant.PDF_PREFIX).replaceAll(UploadFileEnum.HTML.name().toLowerCase(), UploadFileEnum.PDF.name().toLowerCase());
+                HtmlToPdfUtil.convert(finalFile.getPath(), destUrl, PageSizeEnum.A3);
+//                File pdfFile = asposePdfUtil.documentToPdf(finalFile.getPath(), destUrl, PaperSize.A3);
+                jsonObject.put(SystemConstant.PDF_PATH, destUrl);
+                jsonObject.put("htmlMd5", DigestUtils.md5Hex(new FileInputStream(finalFile)));
+                jsonObject.put("pdfMd5", fileMd5);
+            }
+            jsonObject.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
+                    UploadFileEnum.HTML, UploadFileEnum.PDF
+            });
+            basicAttachment = new BasicAttachment(jsonObject.toJSONString(), fileName, SystemConstant.HTML_PREFIX, new BigDecimal(size), fileMd5, userId);
+            basicAttachmentService.save(basicAttachment);
+        } catch (Exception e) {
+            log.error("请求出错", e);
+            basicAttachmentService.deleteAttachment(basicAttachment);
+            if (e instanceof ApiException) {
+                ResultUtil.error((ApiException) e, e.getMessage());
+            } else {
+                ResultUtil.error(e.getMessage());
+            }
+        }
+        return basicAttachment;
+    }
+
     /**
      * 保存附件公用
      *

+ 1 - 1
distributed-print/src/main/java/com/qmth/distributed/print/api/BasicClazzController.java

@@ -34,7 +34,7 @@ import java.util.Objects;
 @Api(tags = "班级管理controller")
 @RestController
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.basic}/clazz")
-//@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
+@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
 public class BasicClazzController {
     @Resource
     private BasicClazzService basicClazzService;

+ 19 - 1
distributed-print/src/main/java/com/qmth/distributed/print/api/ExamTaskController.java

@@ -1,5 +1,7 @@
 package com.qmth.distributed.print.api;
 
+import com.qmth.boot.api.annotation.Aac;
+import com.qmth.boot.api.annotation.BOOL;
 import com.qmth.boot.api.constant.ApiConstant;
 import com.qmth.boot.api.exception.ApiException;
 import com.qmth.distributed.print.business.bean.dto.ExamTaskDetailCardDto;
@@ -17,6 +19,7 @@ import com.qmth.distributed.print.business.service.*;
 import com.qmth.distributed.print.business.templete.execute.AsyncCreatePdfTempleteService;
 import com.qmth.distributed.print.business.templete.execute.AsyncTaskReviewSampleExportService;
 import com.qmth.teachcloud.common.bean.params.ArraysParams;
+import com.qmth.teachcloud.common.contant.SpringContextHolder;
 import com.qmth.teachcloud.common.contant.SystemConstant;
 import com.qmth.teachcloud.common.entity.BasicAttachment;
 import com.qmth.teachcloud.common.entity.SysUser;
@@ -56,6 +59,7 @@ import java.util.stream.Collectors;
 @Api(tags = "命题任务Controller")
 @RestController
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.exam}/task")
+//@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
 public class ExamTaskController {
 
     @Autowired
@@ -801,10 +805,24 @@ public class ExamTaskController {
     @ApiOperation(value = "命题任务-审批-获取试卷审批表数据")
     @RequestMapping(value = "/find_approve_form_json", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
-    public Result findApproveFormJson(@ApiParam(value = "题卡规则id", required = true) @RequestParam String examTaskId) {
+    public Result findApproveFormJson(@ApiParam(value = "命题任务id", required = true) @RequestParam String examTaskId) {
         return ResultUtil.ok((Object) examTaskService.findExamTaskApprovalForm(SystemConstant.convertIdToLong(examTaskId)));
     }
 
+    @ApiOperation(value = "命题任务-审批-获取试卷审批表pdf")
+    @RequestMapping(value = "/find_approve_form_pdf", method = RequestMethod.POST)
+    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
+    public void findApproveFormPdf(@ApiParam(value = "命题任务id", required = true) @RequestParam String examTaskId,
+                                   @ApiParam(value = "html", required = true) @RequestParam String htmlContent) throws Exception {
+        // -- 测试用html --
+        Long id = 165147096339447808L;
+        ExamCardDetailService examCardDetailService = SpringContextHolder.getBean(ExamCardDetailService.class);
+        ExamCardDetail examCardDetail = examCardDetailService.getById(id);
+        htmlContent = examCardDetail.getHtmlContent();
+        // --
+        HttpServletResponse response = ServletUtil.getResponse();
+        examTaskService.findExamTaskApprovalPdf(response,SystemConstant.convertIdToLong(examTaskId), htmlContent);
+    }
 
 
 }

+ 17 - 0
distributed-print/src/test/java/com/qmth/distributed/print/ServiceTest.java

@@ -3,20 +3,24 @@ package com.qmth.distributed.print;
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.qmth.distributed.print.business.bean.result.TemplatePrintInfoResult;
+import com.qmth.distributed.print.business.entity.ExamCardDetail;
 import com.qmth.distributed.print.business.entity.ExamPrintPlan;
 import com.qmth.distributed.print.business.entity.ExamTask;
 import com.qmth.distributed.print.business.enums.MessageEnum;
 import com.qmth.distributed.print.business.service.*;
 import com.qmth.teachcloud.common.contant.SystemConstant;
+import com.qmth.teachcloud.common.entity.BasicAttachment;
 import com.qmth.teachcloud.common.entity.BasicCourse;
 import com.qmth.teachcloud.common.service.BasicCourseService;
 import com.qmth.teachcloud.common.util.ConvertUtil;
+import org.activiti.engine.task.Attachment;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
 import javax.annotation.Resource;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -44,6 +48,10 @@ public class ServiceTest {
     private BasicCourseService basicCourseService;
     @Resource
     private ExamTaskService examTaskService;
+    @Resource
+    private PrintCommonService printCommonService;
+    @Resource
+    private ExamCardDetailService examCardDetailService;
 
 
     @Test
@@ -124,4 +132,13 @@ public class ServiceTest {
         System.out.println(JSON.toJSONString(examTaskService.findExamTaskApprovalForm(examTask)));
     }
 
+    @Test
+    public void saveHtmlAndPdf() throws IOException {
+        Long id = 165147096339447808L;
+        ExamCardDetail examCardDetail = examCardDetailService.getById(id);
+        String htmlContent = examCardDetail.getHtmlContent();
+        BasicAttachment basicAttachment = printCommonService.saveAttachmentHtmlAndPdf("那不勒斯",htmlContent,1L);
+        System.out.println(JSON.toJSONString(basicAttachment));
+    }
+
 }

+ 25 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/ConvertUtil.java

@@ -10,6 +10,7 @@ import org.springframework.data.redis.support.atomic.RedisAtomicLong;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
 import java.io.*;
 import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
@@ -299,4 +300,28 @@ public class ConvertUtil {
             return false;
         }
     }
+
+    public static void outputFile(HttpServletResponse response, InputStream inputStream, String fileName) {
+        try {
+            byte[] buf = new byte[1024];
+            int len = 0;
+
+            String fName = new String(fileName.getBytes(), "ISO-8859-1");
+
+            response.reset();
+            response.setContentType("application/x-msdownload");
+            response.setHeader("Content-Disposition", "attachment; filename=" + fName);
+
+            OutputStream outStream = response.getOutputStream();
+
+            while ((len = inputStream.read(buf)) > 0) {
+                outStream.write(buf, 0, len);
+            }
+
+            inputStream.close();
+            outStream.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
 }