PatentInstructionService.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. package cn.cslg.pas.service;
  2. import cn.cslg.pas.common.core.base.Constants;
  3. import cn.cslg.pas.common.model.dto.PatentDTO;
  4. import cn.cslg.pas.common.model.dto.UploadFileDTO;
  5. import cn.cslg.pas.common.model.vo.PatentInstructionVO;
  6. import cn.cslg.pas.common.model.vo.PatentVO;
  7. import cn.cslg.pas.common.model.vo.SystemFile;
  8. import cn.cslg.pas.common.utils.*;
  9. import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
  10. import cn.cslg.pas.domain.Patent;
  11. import cn.cslg.pas.domain.PatentInstruction;
  12. import cn.cslg.pas.domain.Task;
  13. import cn.cslg.pas.mapper.PatentInstructionMapper;
  14. import cn.cslg.pas.service.upLoadPatent.MessageService;
  15. import cn.hutool.core.io.FileUtil;
  16. import cn.hutool.core.util.IdUtil;
  17. import cn.hutool.core.util.ZipUtil;
  18. import com.alibaba.fastjson.JSONArray;
  19. import com.alibaba.fastjson2.JSONObject;
  20. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  21. import com.baomidou.mybatisplus.core.metadata.IPage;
  22. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  23. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  24. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  25. import lombok.RequiredArgsConstructor;
  26. import lombok.extern.slf4j.Slf4j;
  27. import org.apache.pdfbox.multipdf.PDFMergerUtility;
  28. import org.apache.pdfbox.multipdf.Splitter;
  29. import org.apache.pdfbox.pdmodel.PDDocument;
  30. import org.apache.pdfbox.pdmodel.PDPage;
  31. import org.apache.pdfbox.pdmodel.PDPageContentStream;
  32. import org.apache.pdfbox.pdmodel.common.PDRectangle;
  33. import org.apache.pdfbox.pdmodel.font.PDFont;
  34. import org.apache.pdfbox.pdmodel.font.PDType1Font;
  35. import org.apache.pdfbox.rendering.ImageType;
  36. import org.apache.pdfbox.rendering.PDFRenderer;
  37. import org.springframework.context.annotation.Lazy;
  38. import org.springframework.scheduling.annotation.Async;
  39. import org.springframework.stereotype.Service;
  40. import org.springframework.transaction.annotation.Transactional;
  41. import org.springframework.transaction.interceptor.TransactionAspectSupport;
  42. import org.springframework.web.multipart.MultipartFile;
  43. import javax.imageio.ImageIO;
  44. import java.awt.*;
  45. import java.awt.image.RenderedImage;
  46. import java.io.File;
  47. import java.io.IOException;
  48. import java.util.List;
  49. import java.util.*;
  50. import java.util.stream.Collectors;
  51. /**
  52. * <p>
  53. * 专利说明书 服务类
  54. * </p>
  55. *
  56. * @author 王岩
  57. * @since 2022-03-02
  58. */
  59. @Slf4j
  60. @Service
  61. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  62. public class PatentInstructionService extends ServiceImpl<PatentInstructionMapper, PatentInstruction> {
  63. private final PatentService patentService;
  64. private final TaskService taskService;
  65. private final MessageService messageService;
  66. private final FileManagerService fileManagerService;
  67. private final FileUtils fileUtils;
  68. private final LoginUtils loginUtils;
  69. public IPage<PatentInstruction> getPageList(PatentInstructionVO params) {
  70. LambdaQueryWrapper<PatentInstruction> queryWrapper = new LambdaQueryWrapper<>();
  71. if (StringUtils.isNotEmpty(params.getPatentNo())) {
  72. queryWrapper.like(PatentInstruction::getPatentNo, params.getPatentNo());
  73. }
  74. if (params.getType() != null) {
  75. queryWrapper.eq(PatentInstruction::getType, params.getType());
  76. }
  77. if (params.getPatentId() != null) {
  78. queryWrapper.eq(PatentInstruction::getPatentId, params.getPatentId());
  79. }
  80. IPage<PatentInstruction> pageList = this.page(new Page<>(params.getCurrent(), params.getSize()), queryWrapper);
  81. return pageList;
  82. }
  83. public List<PatentInstruction> getPatentInstructionByPatentNo(String patentNo) {
  84. LambdaQueryWrapper<PatentInstruction> queryWrapper = new LambdaQueryWrapper<>();
  85. queryWrapper.eq(PatentInstruction::getPatentNo, patentNo);
  86. return this.list(queryWrapper);
  87. }
  88. public List<PatentInstruction> getPatentInstructionByPatentNo(List<String> patentNo) {
  89. if (patentNo == null || patentNo.size() == 0) {
  90. return new ArrayList<>();
  91. }
  92. LambdaQueryWrapper<PatentInstruction> queryWrapper = new LambdaQueryWrapper<>();
  93. queryWrapper.in(PatentInstruction::getPatentNo, patentNo);
  94. return this.list(queryWrapper);
  95. }
  96. public PatentInstruction getPatentInstructionByPatentNoAndType(String patentNo, Integer type) {
  97. LambdaQueryWrapper<PatentInstruction> queryWrapper = new LambdaQueryWrapper<>();
  98. queryWrapper.eq(PatentInstruction::getType, type);
  99. queryWrapper.eq(PatentInstruction::getPatentNo, patentNo).last("Limit 1");
  100. return this.getOne(queryWrapper);
  101. }
  102. @Async("singleThreadAsyncTaskExecutor")
  103. @Transactional(rollbackFor = Exception.class)
  104. public void batchUpload(String url, Integer type, String remark, Integer userId) {
  105. String tempPath = null;
  106. try {
  107. String tempDirectoryName = IdUtil.simpleUUID();
  108. tempPath = fileUtils.getSystemPath(tempDirectoryName);
  109. File tempDirectory = new File(tempPath);
  110. if (!tempDirectory.exists()) {
  111. tempDirectory.mkdir();
  112. }
  113. ZipUtil.unzip(fileUtils.getSystemPath(url), tempPath);
  114. List<File> fileList = FileUtil.loopFiles(tempPath).stream().filter(item -> FileUtil.getType(item).equals("pdf")).collect(Collectors.toList());
  115. for (int i = 0; i < fileList.size(); i++) {
  116. File file = fileList.get(i);
  117. String fileName = file.getName();
  118. String patentNo = FileUtil.getPrefix(file);
  119. String saveName = IdUtil.simpleUUID() + ".pdf";
  120. String saveUrl = fileUtils.getDirectory(saveName);
  121. String savePath = fileUtils.getSystemPath(saveUrl);
  122. FileUtil.copy(file.getPath(), savePath, true);
  123. PatentInstruction temp = this.getPatentInstructionByPatentNoAndType(patentNo, type);
  124. if (temp == null) {
  125. temp = new PatentInstruction();
  126. } else {
  127. FileUtil.del(fileUtils.getSystemPath(temp.getUrl()));
  128. }
  129. temp.setPatentNo(patentNo);
  130. temp.setFileName(saveName);
  131. temp.setSize(FileUtil.size(file));
  132. temp.setUrl(saveUrl);
  133. temp.setRemark(remark);
  134. temp.setCreateBy(userId);
  135. temp.setType(type);
  136. temp.insertOrUpdate();
  137. Map<String, Object> data = new HashMap<>();
  138. data.put("index", i);
  139. data.put("total", fileList.size());
  140. WebSocketServer.sendInfo(Response.websocket(data, ResponseEnum.BATCH_UPLOAD_INSTRUCTION_TASK_SUCCESS), String.valueOf(userId));
  141. }
  142. } catch (Exception e) {
  143. e.printStackTrace();
  144. WebSocketServer.sendInfo(Response.error(ResponseEnum.BATCH_UPLOAD_INSTRUCTION_TASK_ERROR), String.valueOf(userId));
  145. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  146. } finally {
  147. FileUtil.del(tempPath);
  148. }
  149. }
  150. public String add(MultipartFile file, PatentInstruction patentInstruction) {
  151. UploadFileDTO fileDTO = fileUtils.uploadFile(file);
  152. patentInstruction.setCreateBy(loginUtils.getId());
  153. patentInstruction.setUrl(fileDTO.getPath());
  154. patentInstruction.setFileName(fileDTO.getFileName());
  155. patentInstruction.insert();
  156. return Response.success(patentInstruction.getId());
  157. }
  158. @Transactional
  159. public String edit(MultipartFile file, PatentInstruction patentInstruction) {
  160. this.deleteByPatentNoAndType(patentInstruction.getPatentNo(), patentInstruction.getType());
  161. if (StringUtils.isNotEmpty(patentInstruction.getUrl())) {
  162. FileUtil.del(fileUtils.getSystemPath(patentInstruction.getUrl()));
  163. }
  164. UploadFileDTO fileDTO = fileUtils.uploadFile(file);
  165. patentInstruction.setCreateBy(loginUtils.getId());
  166. patentInstruction.setUrl(fileDTO.getPath());
  167. patentInstruction.setFileName(fileDTO.getFileName());
  168. patentInstruction.setSize(fileDTO.getFileSize());
  169. patentInstruction.insert();
  170. return Response.success(true);
  171. }
  172. /**
  173. * 说明书pdf数据入库
  174. *
  175. * @param patentNo 专利号
  176. * @param pdf 装载pdf数据对象
  177. */
  178. @Transactional
  179. public String edit(String patentNo, UploadFileDTO pdf) {
  180. //先把实体类对象赋好值
  181. PatentInstruction patentInstruction = new PatentInstruction();
  182. patentInstruction.setUrl(pdf.getPath());
  183. patentInstruction.setFileName(pdf.getFileName());
  184. patentInstruction.setSize(pdf.getFileSize());
  185. patentInstruction.setPatentNo(patentNo);
  186. patentInstruction.setType(1);
  187. //根据专利号和pdf类型(1公开 2授权)查询该专利号是否已有公开的pdf
  188. List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().eq(PatentInstruction::getPatentNo, patentNo).eq(PatentInstruction::getType, 1));
  189. if (patentInstructions != null && patentInstructions.size() > 0) { //若有则更新
  190. patentInstruction.setId(patentInstructions.get(0).getId());
  191. this.updateById(patentInstruction);
  192. } else { //若没有则新增
  193. this.save(patentInstruction);
  194. }
  195. return Response.success(true);
  196. }
  197. @Transactional
  198. public String edit2(String patentNo, UploadFileDTO pdf2) {
  199. //先把实体类对象赋好值
  200. PatentInstruction patentInstruction = new PatentInstruction();
  201. patentInstruction.setUrl(pdf2.getPath());
  202. patentInstruction.setFileName(pdf2.getFileName());
  203. patentInstruction.setSize(pdf2.getFileSize());
  204. patentInstruction.setPatentNo(patentNo);
  205. patentInstruction.setType(2);
  206. //根据专利号和pdf类型(1公开 2授权)查询该专利号是否已有授权的pdf
  207. List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().eq(PatentInstruction::getPatentNo, patentNo).eq(PatentInstruction::getType, 2));
  208. //若有则更新
  209. if (patentInstructions != null && patentInstructions.size() > 0) {
  210. patentInstruction.setId(patentInstructions.get(0).getId());
  211. this.updateById(patentInstruction);
  212. } else { //若没有则新增
  213. this.save(patentInstruction);
  214. }
  215. return Response.success(true);
  216. }
  217. @Transactional
  218. public String edit12(String patentNo, UploadFileDTO pdf1, UploadFileDTO pdf2) {
  219. //处理公开pdf文档(更新或新增)
  220. PatentInstruction patentInstruction1 = new PatentInstruction();
  221. patentInstruction1.setUrl(pdf1.getPath());
  222. patentInstruction1.setFileName(pdf1.getFileName());
  223. patentInstruction1.setSize(pdf1.getFileSize());
  224. patentInstruction1.setPatentNo(patentNo);
  225. patentInstruction1.setType(1);
  226. //先查询库中是否已有该专利号的公开pdf文档
  227. List<PatentInstruction> patentInstructions1 = this.list(new LambdaQueryWrapper<PatentInstruction>().eq(PatentInstruction::getPatentNo, patentNo).eq(PatentInstruction::getType, 1));
  228. //若有则更新
  229. if (patentInstructions1 != null && patentInstructions1.size() > 0) {
  230. patentInstruction1.setId(patentInstructions1.get(0).getId());
  231. this.updateById(patentInstruction1);
  232. } else { //若没有则新增
  233. this.save(patentInstruction1);
  234. }
  235. //处理授权pdf文档(更新或新增)
  236. PatentInstruction patentInstruction2 = new PatentInstruction();
  237. patentInstruction2.setUrl(pdf2.getPath());
  238. patentInstruction2.setFileName(pdf2.getFileName());
  239. patentInstruction2.setSize(pdf2.getFileSize());
  240. patentInstruction2.setPatentNo(patentNo);
  241. patentInstruction2.setType(2);
  242. //再查询库中是否已有该专利号的授权pdf文档
  243. List<PatentInstruction> patentInstructions2 = this.list(new LambdaQueryWrapper<PatentInstruction>().eq(PatentInstruction::getPatentNo, patentNo).eq(PatentInstruction::getType, 2));
  244. //若有则更新
  245. if (patentInstructions2 != null && patentInstructions2.size() > 0) {
  246. patentInstruction2.setId(patentInstructions2.get(0).getId());
  247. this.updateById(patentInstruction2);
  248. } else { //若没有则新增
  249. this.save(patentInstruction2);
  250. }
  251. return Response.success(true);
  252. }
  253. public String delete(Integer id) {
  254. PatentInstruction temp = this.getById(id);
  255. this.removeById(id);
  256. if (StringUtils.isNotEmpty(temp.getUrl())) {
  257. FileUtil.del(fileUtils.getSystemPath(temp.getUrl()));
  258. }
  259. return Response.success(true);
  260. }
  261. public void deleteByPatentNoAndType(String patentNo, Integer type) {
  262. this.remove(Wrappers.<PatentInstruction>lambdaQuery().eq(PatentInstruction::getPatentNo, patentNo).eq(PatentInstruction::getType, type));
  263. }
  264. public void importPatentInstruction(Integer userId, String tempPath, String patentIdPatentNoJson, String patentInstructionJson) {
  265. List<Patent> importPatentList = JsonUtils.jsonToList(patentIdPatentNoJson, Patent.class);
  266. List<PatentInstruction> importPatentInstructionList = JsonUtils.jsonToList(patentInstructionJson, PatentInstruction.class);
  267. List<PatentInstruction> localPatentInstructionList = this.getPatentInstructionByPatentNo(importPatentList.stream().map(Patent::getPatentNo).collect(Collectors.toList()));
  268. for (PatentInstruction patentInstruction : importPatentInstructionList) {
  269. String fileName = IdUtil.simpleUUID() + "." + FileUtil.extName(patentInstruction.getFileName());
  270. String saveUrl = fileUtils.getDirectory(fileName);
  271. String savePath = fileUtils.getSystemPath(saveUrl);
  272. String tempInstruction = tempPath + FileUtils.FILE_SEPARATOR + Constants.PATENT_INSTRUCTION_DIRECTORY_NAME + FileUtils.FILE_SEPARATOR + patentInstruction.getFileName();
  273. patentInstruction.setId(null);
  274. patentInstruction.setFileName(fileName);
  275. patentInstruction.setUrl(saveUrl);
  276. patentInstruction.setCreateBy(userId);
  277. patentInstruction.setCreateTime(new Date());
  278. if (FileUtil.exist(tempInstruction)) {
  279. patentInstruction.insert();
  280. FileUtil.copy(tempInstruction, savePath, true);
  281. }
  282. }
  283. localPatentInstructionList.forEach(item -> {
  284. item.deleteById();
  285. FileUtil.del(fileUtils.getSystemPath(item.getUrl()));
  286. });
  287. }
  288. public void queryPatentPdfFirstPages(PatentVO params) throws IOException {
  289. log.info("开始处理【合并多个专利pdf首页】的业务,业务参数为:{}", params);
  290. //根据筛选条件分页查询专利清单(这一次查询目的只是获得专利总数和总页数)
  291. IPage<PatentDTO> pageList0 = patentService.getPageList(params);
  292. if (pageList0 == null || pageList0.getRecords() == null || pageList0.getRecords().size() == 0) {
  293. ThrowException.throwXiaoShiException("合并专利pdf首页文档失败,无专利信息!");
  294. }
  295. int total = (int) pageList0.getTotal(); //专利总数(作为任务进度总数)
  296. long pages = pageList0.getPages(); //总页数(用来计算要检索几次)
  297. ArrayList<String> filePaths = new ArrayList<>(); //存放所有临时文件(说明书pdf首页的文件)路径
  298. String mergedFilePath = StringUtils.getUUID() + ".pdf"; //合并后的pdf文件路径
  299. PDFMergerUtility pdfMerger = new PDFMergerUtility(); //pdf合并工具
  300. //上传任务
  301. Task task = new Task()
  302. .setFileName(mergedFilePath)
  303. .setProjectId(params.getProjectId())
  304. .setType(6)
  305. .setCreateBy(loginUtils.getId())
  306. .setStartTime(DateUtils.getDateTime())
  307. .setTotal(total)
  308. .setStatus(6)
  309. .setDefaultNum(0)
  310. .setSuccessNum(0)
  311. .setTrueSuccessNum(0);
  312. taskService.save(task);
  313. //一页一页检索
  314. for (long i = 1; i <= pages; i++) {
  315. params.setCurrent(i);
  316. IPage<PatentDTO> pageList = patentService.getPageList(params); //根据筛选条件分页查询专利清单
  317. List<PatentDTO> patents = pageList.getRecords(); //取出专利清单
  318. List<String> patentNos = patents.stream().map(PatentDTO::getPatentNo).collect(Collectors.toList()); //过滤取出专利号
  319. //根据专利号patentNos查询出所有pdf文档数据
  320. List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos));
  321. //遍历当前专利号清单
  322. for (String patentNo : patentNos) {
  323. List<PatentInstruction> patentInstructionList = patentInstructions.stream().filter(item -> item.getPatentNo().equals(patentNo)).collect(Collectors.toList());
  324. //若有说明书pdf数据
  325. PatentInstruction patentInstruction = new PatentInstruction();
  326. if (patentInstructionList.size() > 1) { //若有多个,则过滤取出授权文档
  327. patentInstruction = patentInstructionList.stream().filter(item -> item.getType() == 2).collect(Collectors.toList()).get(0);
  328. } else if (patentInstructionList.size() == 1) { //若只有一个pdf,则直接取出
  329. patentInstruction = patentInstructionList.get(0);
  330. }
  331. String filePath = fileUtils.getSystemPath() + patentInstruction.getUrl();
  332. File pdfFile = new File(filePath);
  333. //若没有说明书pdf数据,则手写空白页显示该专利没有说明书pdf
  334. if (patentInstructionList.size() == 0 || !pdfFile.exists()) {
  335. //创建文件,设置页码
  336. PDDocument doc = new PDDocument();
  337. PDPage pageOne = new PDPage(PDRectangle.A4);
  338. doc.addPage(pageOne);
  339. //创建页面内容流
  340. PDPageContentStream contents = new PDPageContentStream(doc, pageOne, true, true, true);
  341. //设置要使用的字体类型
  342. //PDFont font = PDType1Font.COURIER_BOLD_OBLIQUE;
  343. PDFont font = PDType1Font.HELVETICA;
  344. //要写入的文本内容
  345. String message = "no pdf of " + patentNo;
  346. float fontSize = 36.0f;
  347. PDRectangle pageSize = pageOne.getMediaBox();
  348. float stringWidth = font.getStringWidth(message) * fontSize / 1000f;
  349. //计算页中心位置
  350. int rotation = pageOne.getRotation();
  351. boolean rotate = rotation == 90 || rotation == 270;
  352. float pageWidth = rotate ? pageSize.getHeight() : pageSize.getWidth();
  353. float pageHeight = rotate ? pageSize.getWidth() : pageSize.getHeight();
  354. double centeredXPosition = rotate ? pageHeight / 2f : (pageWidth - stringWidth) / 2f;
  355. double centeredYPosition = rotate ? (pageWidth - stringWidth) / 2f : pageHeight / 2f;
  356. contents.beginText();
  357. //设置字体和大小
  358. contents.setFont(font, fontSize);
  359. //设置文本颜色
  360. contents.setNonStrokingColor(Color.BLACK);
  361. if (rotate) {
  362. // rotate the text according to the page rotation
  363. contents.setTextRotation(Math.PI / 2, centeredXPosition, centeredYPosition);
  364. } else {
  365. contents.setTextTranslation(centeredXPosition, centeredYPosition);
  366. }
  367. contents.showText(message);
  368. contents.endText();
  369. //记得关闭流对象要不然是无法成功保存pdf文档的
  370. contents.close();
  371. String newFilePath = StringUtils.getUUID() + ".pdf";
  372. doc.save(newFilePath);
  373. filePaths.add(newFilePath);
  374. pdfMerger.addSource(newFilePath);
  375. doc.close();
  376. //更新任务表(完成数量+1)并发送进度
  377. task.setSuccessNum(task.getSuccessNum() + 1);
  378. task.setTrueSuccessNum(task.getTrueSuccessNum() + 1);
  379. sendMessage(total, task);
  380. //continue;
  381. } else {
  382. PDDocument doc = PDDocument.load(pdfFile);
  383. Splitter splitter = new Splitter();
  384. splitter.setStartPage(1);
  385. splitter.setEndPage(1);
  386. PDDocument needDoc = splitter.split(doc).get(0);
  387. String newFilePath = StringUtils.getUUID() + ".pdf";
  388. needDoc.save(newFilePath);
  389. filePaths.add(newFilePath);
  390. pdfMerger.addSource(newFilePath);
  391. needDoc.close();
  392. doc.close();
  393. //更新任务表(完成数量+1)并发送进度
  394. task.setSuccessNum(task.getSuccessNum() + 1);
  395. task.setTrueSuccessNum(task.getTrueSuccessNum() + 1);
  396. sendMessage(total, task);
  397. }
  398. }
  399. }
  400. // 设置合并生成pdf文件名称
  401. pdfMerger.setDestinationFileName(mergedFilePath);
  402. // 合并PDF
  403. pdfMerger.mergeDocuments();
  404. //若最终合并成的pdf文件不存在
  405. if (!new File(mergedFilePath).exists()) {
  406. ThrowException.throwXiaoShiException("当前批次所有专利的说明书文档不存在或已丢失");
  407. }
  408. //File文件转成MultipartFile
  409. MultipartFile multipartFile = FileToMultipartFileUtiles.fileToMultipartFile(mergedFilePath);
  410. ArrayList<MultipartFile> multipartFiles = new ArrayList<>();
  411. multipartFiles.add(multipartFile);
  412. String res = fileManagerService.uploadFile(multipartFiles);
  413. JSONObject jsonObject = JSONObject.parseObject(res);
  414. List<Integer> fileIds = JSONArray.parseArray(jsonObject.get("data").toString(), Integer.class);
  415. res = fileManagerService.getSystemFileFromFMS(fileIds);
  416. List<SystemFile> systemFiles = JSONArray.parseArray(res, SystemFile.class);
  417. SystemFile systemFile = systemFiles.get(0);
  418. Task updateTask = new Task();
  419. updateTask.setId(task.getId());
  420. updateTask.setSystemFileId(fileIds.get(0));
  421. updateTask.setUrl(systemFile.getFilePath().substring(systemFile.getFilePath().indexOf("file") + 4));
  422. updateTask.setFileSize(Long.valueOf(systemFile.getFileLength()));
  423. taskService.updateById(updateTask);
  424. //最后要记得删除所有PDF首页临时文件
  425. for (String filePath : filePaths) { //删除每个pdf首页文件
  426. new File(filePath).delete();
  427. }
  428. //删除最终合并的pdf首页文件
  429. new File(mergedFilePath).delete();
  430. }
  431. /**
  432. * 更新任务表(完成数量+1)、发送websocket进度
  433. *
  434. * @param total 任务专利总数量
  435. * @param task 任务对象
  436. */
  437. private void sendMessage(int total, Task task) {
  438. //更新任务表(完成数量+1),并发送进度+1
  439. Task updateTask = new Task();
  440. updateTask.setId(task.getId());
  441. updateTask.setSuccessNum(task.getSuccessNum());
  442. updateTask.setTrueSuccessNum(task.getTrueSuccessNum());
  443. taskService.updateById(updateTask);
  444. long percentage = (long) Math.floor((task.getSuccessNum() + 0D) / total * 100D);
  445. //当全部完成时
  446. if (task.getSuccessNum().equals(total)) {
  447. percentage = 100L;
  448. //任务表更新最终数据
  449. task.setStatus(2);
  450. updateTask.setStatus(task.getStatus());
  451. updateTask.setEndTime(DateUtils.getDateTime());
  452. taskService.updateById(updateTask);
  453. }
  454. //websocket发送进度
  455. messageService.sendWebsocketMessage(task, total, task.getSuccessNum(), percentage);
  456. }
  457. public void generateBookIamge(File inputFile, File outputFile) {
  458. try {
  459. PDDocument doc = PDDocument.load(inputFile);
  460. PDFRenderer pdfRenderer = new PDFRenderer(doc);
  461. // 提取的页码
  462. int pageNumber = 0;
  463. // 以300 dpi 读取存入 BufferedImage 对象
  464. int dpi = 300;
  465. RenderedImage buffImage = pdfRenderer.renderImageWithDPI(pageNumber, dpi, ImageType.RGB);
  466. // 将 BufferedImage 写入到 png
  467. ImageIO.write(buffImage, "png", outputFile);
  468. // 关闭文档
  469. doc.close();
  470. } catch (Exception e) {
  471. e.printStackTrace();
  472. }
  473. }
  474. }