|
@@ -14,6 +14,7 @@ import cn.cslg.pas.domain.Patent;
|
|
import cn.cslg.pas.domain.PatentInstruction;
|
|
import cn.cslg.pas.domain.PatentInstruction;
|
|
import cn.cslg.pas.domain.Task;
|
|
import cn.cslg.pas.domain.Task;
|
|
import cn.cslg.pas.mapper.PatentInstructionMapper;
|
|
import cn.cslg.pas.mapper.PatentInstructionMapper;
|
|
|
|
+import cn.cslg.pas.mapper.PatentMapper;
|
|
import cn.cslg.pas.service.upLoadPatent.MessageService;
|
|
import cn.cslg.pas.service.upLoadPatent.MessageService;
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.util.IdUtil;
|
|
import cn.hutool.core.util.IdUtil;
|
|
@@ -37,6 +38,7 @@ import org.apache.pdfbox.pdmodel.font.PDFont;
|
|
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
|
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
|
import org.apache.pdfbox.rendering.ImageType;
|
|
import org.apache.pdfbox.rendering.ImageType;
|
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
import org.springframework.cache.Cache;
|
|
import org.springframework.cache.Cache;
|
|
import org.springframework.cache.CacheManager;
|
|
import org.springframework.cache.CacheManager;
|
|
import org.springframework.context.annotation.Lazy;
|
|
import org.springframework.context.annotation.Lazy;
|
|
@@ -50,7 +52,9 @@ import org.springframework.web.multipart.MultipartFile;
|
|
import javax.imageio.ImageIO;
|
|
import javax.imageio.ImageIO;
|
|
import java.awt.*;
|
|
import java.awt.*;
|
|
import java.awt.image.RenderedImage;
|
|
import java.awt.image.RenderedImage;
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
|
+import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
@@ -69,6 +73,7 @@ import java.util.stream.Collectors;
|
|
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
|
|
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
|
|
public class PatentInstructionService extends ServiceImpl<PatentInstructionMapper, PatentInstruction> {
|
|
public class PatentInstructionService extends ServiceImpl<PatentInstructionMapper, PatentInstruction> {
|
|
private final PatentService patentService;
|
|
private final PatentService patentService;
|
|
|
|
+ private final PatentMapper patentMapper;
|
|
private final TaskService taskService;
|
|
private final TaskService taskService;
|
|
private final MessageService messageService;
|
|
private final MessageService messageService;
|
|
private final FileManagerService fileManagerService;
|
|
private final FileManagerService fileManagerService;
|
|
@@ -324,7 +329,8 @@ public class PatentInstructionService extends ServiceImpl<PatentInstructionMappe
|
|
log.info("开始处理【合并导出专题库专利的pdf首页文件】的业务,业务参数为:{}", params);
|
|
log.info("开始处理【合并导出专题库专利的pdf首页文件】的业务,业务参数为:{}", params);
|
|
|
|
|
|
//根据筛选条件分页查询专利清单(这一次查询目的只是获得专利总数和总页数)
|
|
//根据筛选条件分页查询专利清单(这一次查询目的只是获得专利总数和总页数)
|
|
- IPage<PatentDTO> pageList0 = patentService.getPageList(params);
|
|
|
|
|
|
+ //IPage<PatentDTO> pageList0 = patentService.getPageList(params);
|
|
|
|
+ IPage<Patent> pageList0 = patentMapper.getPageList(new Page<>(params.getCurrent(), params.getSize()), params);
|
|
if (pageList0 == null || pageList0.getRecords() == null || pageList0.getRecords().size() == 0) {
|
|
if (pageList0 == null || pageList0.getRecords() == null || pageList0.getRecords().size() == 0) {
|
|
ThrowException.throwXiaoShiException("合并专利pdf首页文档失败,无专利信息!");
|
|
ThrowException.throwXiaoShiException("合并专利pdf首页文档失败,无专利信息!");
|
|
}
|
|
}
|
|
@@ -353,9 +359,10 @@ public class PatentInstructionService extends ServiceImpl<PatentInstructionMappe
|
|
//一页一页检索
|
|
//一页一页检索
|
|
for (long i = 1; i <= pages; i++) {
|
|
for (long i = 1; i <= pages; i++) {
|
|
params.setCurrent(i);
|
|
params.setCurrent(i);
|
|
- IPage<PatentDTO> pageList = patentService.getPageList(params); //根据筛选条件分页查询专利清单
|
|
|
|
- List<PatentDTO> patents = pageList.getRecords(); //取出专利清单
|
|
|
|
- List<String> patentNos = patents.stream().map(PatentDTO::getPatentNo).collect(Collectors.toList()); //过滤取出专利号
|
|
|
|
|
|
+ //IPage<PatentDTO> pageList = patentService.getPageList(params); //根据筛选条件分页查询专利清单
|
|
|
|
+ IPage<Patent> pageList = patentMapper.getPageList(new Page<>(params.getCurrent(), params.getSize()), params);
|
|
|
|
+ List<Patent> patents = pageList.getRecords(); //取出专利清单
|
|
|
|
+ List<String> patentNos = patents.stream().map(Patent::getPatentNo).collect(Collectors.toList()); //过滤取出专利号
|
|
|
|
|
|
//根据专利号patentNos查询出所有pdf文档数据
|
|
//根据专利号patentNos查询出所有pdf文档数据
|
|
List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos));
|
|
List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos));
|
|
@@ -504,6 +511,12 @@ public class PatentInstructionService extends ServiceImpl<PatentInstructionMappe
|
|
messageService.sendWebsocketMessage(task, total, task.getSuccessNum(), percentage);
|
|
messageService.sendWebsocketMessage(task, total, task.getSuccessNum(), percentage);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * pdf转图片方法Demo
|
|
|
|
+ *
|
|
|
|
+ * @param inputFile
|
|
|
|
+ * @param outputFile
|
|
|
|
+ */
|
|
public void generateBookIamge(File inputFile, File outputFile) {
|
|
public void generateBookIamge(File inputFile, File outputFile) {
|
|
|
|
|
|
try {
|
|
try {
|
|
@@ -531,21 +544,24 @@ public class PatentInstructionService extends ServiceImpl<PatentInstructionMappe
|
|
* @param params 查询条件
|
|
* @param params 查询条件
|
|
*/
|
|
*/
|
|
public IPage<PatentDTO> queryPatentPdfFirstPages(PatentVO params) throws IOException {
|
|
public IPage<PatentDTO> queryPatentPdfFirstPages(PatentVO params) throws IOException {
|
|
- log.info("开始处理【查询专题库专利的pdf首页清单】的业务,业务参数为:{}", params);
|
|
|
|
|
|
+ log.info("开始处理【查询专题库专利的pdf首页图片清单】的业务,业务参数为:{}", params);
|
|
|
|
|
|
|
|
+ IPage<PatentDTO> pageListResult = new Page<>();
|
|
|
|
+ ArrayList<PatentDTO> patentDTOs = new ArrayList<>();
|
|
//根据筛选条件分页查询专利清单(这一次查询目的只是获得专利总数和总页数)
|
|
//根据筛选条件分页查询专利清单(这一次查询目的只是获得专利总数和总页数)
|
|
- IPage<PatentDTO> pageList = patentService.getPageList(params);
|
|
|
|
|
|
+ //IPage<PatentDTO> pageList = patentService.getPageList(params);
|
|
|
|
+ IPage<Patent> pageList = patentMapper.getPageList(new Page<>(params.getCurrent(), params.getSize()), params);
|
|
if (pageList == null || pageList.getRecords() == null || pageList.getRecords().size() == 0) {
|
|
if (pageList == null || pageList.getRecords() == null || pageList.getRecords().size() == 0) {
|
|
- ThrowException.throwXiaoShiException("查询专题库专利的pdf首页清单失败,未获取到任何专利信息!");
|
|
|
|
|
|
+ ThrowException.throwXiaoShiException("查询专题库专利的pdf首页图片清单失败,未获取到任何专利信息!");
|
|
}
|
|
}
|
|
|
|
|
|
- List<PatentDTO> patents = pageList.getRecords(); //取出专利清单
|
|
|
|
- List<String> patentNos = patents.stream().map(PatentDTO::getPatentNo).collect(Collectors.toList()); //过滤取出专利号
|
|
|
|
- //根据专利号patentNos查询出所有pdf文档数据
|
|
|
|
|
|
+ List<Patent> patents = pageList.getRecords(); //取出专利清单
|
|
|
|
+ List<String> patentNos = patents.stream().map(Patent::getPatentNo).collect(Collectors.toList()); //取出专利号
|
|
|
|
+ //查询出所有pdf文档数据
|
|
List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos));
|
|
List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos));
|
|
|
|
|
|
//遍历专利,装载数据
|
|
//遍历专利,装载数据
|
|
- for (PatentDTO patent : patents) {
|
|
|
|
|
|
+ for (Patent patent : patents) {
|
|
String patentNo = patent.getPatentNo();
|
|
String patentNo = patent.getPatentNo();
|
|
List<PatentInstruction> patentInstructionList = patentInstructions.stream().filter(patentInstruction -> patentInstruction.getPatentNo().equals(patentNo)).collect(Collectors.toList());
|
|
List<PatentInstruction> patentInstructionList = patentInstructions.stream().filter(patentInstruction -> patentInstruction.getPatentNo().equals(patentNo)).collect(Collectors.toList());
|
|
PatentInstruction patentInstruction = new PatentInstruction();
|
|
PatentInstruction patentInstruction = new PatentInstruction();
|
|
@@ -561,37 +577,33 @@ public class PatentInstructionService extends ServiceImpl<PatentInstructionMappe
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
+ //判断缓存中是否有当前专利号的说明书pdf首页图片数据
|
|
Cache.ValueWrapper patentNoImgData = cacheManager.getCache("imgData").get(patentNo);
|
|
Cache.ValueWrapper patentNoImgData = cacheManager.getCache("imgData").get(patentNo);
|
|
String imgData;
|
|
String imgData;
|
|
- if (patentNoImgData == null) { //即当前专利号对应的pdf首页图片数据不在缓存里,则先进行缓存,后返回图片数据
|
|
|
|
- imgData = cyCacheUtil.getImgData(patentNo, pdfFile);
|
|
|
|
- } else { //即当前专利号对应的pdf首页图片数据已在缓存里,则先从缓存里取出图片数据并返回后,再删除缓存
|
|
|
|
|
|
+ if (patentNoImgData == null) { //若没有,则手动转化并返回图片数据
|
|
|
|
+ //imgData = cyCacheUtil.getImgData(patentNo, pdfFile); //(此时不能进行缓存,防止缓存过多导致服务器宕机)
|
|
|
|
+ imgData = this.getImgData(pdfFile);
|
|
|
|
+ } else { //若有,则从缓存取出图片数据后,再删除该缓存(防止缓存过多导致服务器宕机)
|
|
imgData = cyCacheUtil.getImgData(patentNo, pdfFile);
|
|
imgData = cyCacheUtil.getImgData(patentNo, pdfFile);
|
|
- cyCacheUtil.deleteImgData(patentNo);
|
|
|
|
|
|
+ cyCacheUtil.deleteImgData(patentNo); //清除缓存
|
|
}
|
|
}
|
|
|
|
|
|
- patent.setPdfFirstPage(imgData);
|
|
|
|
|
|
+ PatentDTO patentDTO = new PatentDTO();
|
|
|
|
+ BeanUtils.copyProperties(patent, patentDTO);
|
|
|
|
+ patentDTO.setPdfFirstPage(imgData);
|
|
|
|
+ patentDTOs.add(patentDTO);
|
|
}
|
|
}
|
|
|
|
|
|
- //开启一个线程去拿当前专利后面5件专利
|
|
|
|
|
|
+ //开启一个线程去拿后面5件专利的pdf首页图片数据进行缓存
|
|
Thread thread = new Thread() {
|
|
Thread thread = new Thread() {
|
|
@Override
|
|
@Override
|
|
public void run() {
|
|
public void run() {
|
|
- //根据当前页和每页条数,计算后面5件专利的位置
|
|
|
|
|
|
+ //计算检索分页信息条件前面所有专利数量 beforeNum,作为接下来检索时的起始位置(sql语句:limit beforeNum-1, 5)
|
|
Long current = params.getCurrent();
|
|
Long current = params.getCurrent();
|
|
Long size = params.getSize();
|
|
Long size = params.getSize();
|
|
long beforeNum = current * size;
|
|
long beforeNum = current * size;
|
|
- long page = beforeNum / 5;
|
|
|
|
- if (page == 0) { //即当前检索到第1 - 4个专利,则接下来按照current=1,size=5继续检索并缓存专利pdf首页图片数据
|
|
|
|
- current = 1L;
|
|
|
|
- } else { //即当前检索到超过5个专利,则接下来按照current = page + 1,size=5继续检索并缓存专利pdf首页图片数据
|
|
|
|
- current = page + 1;
|
|
|
|
- }
|
|
|
|
- params.setCurrent(current);
|
|
|
|
- params.setSize(5L);
|
|
|
|
try {
|
|
try {
|
|
- threadRunTask(params);
|
|
|
|
|
|
+ threadRunTask(params, (int) beforeNum);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
@@ -599,9 +611,12 @@ public class PatentInstructionService extends ServiceImpl<PatentInstructionMappe
|
|
};
|
|
};
|
|
thread.start();
|
|
thread.start();
|
|
|
|
|
|
-
|
|
|
|
- pageList.setRecords(patents);
|
|
|
|
- return pageList;
|
|
|
|
|
|
+ pageListResult.setCurrent(pageList.getCurrent());
|
|
|
|
+ pageListResult.setSize(pageList.getSize());
|
|
|
|
+ pageListResult.setPages(pageList.getPages());
|
|
|
|
+ pageListResult.setTotal(pageList.getTotal());
|
|
|
|
+ pageListResult.setRecords(patentDTOs);
|
|
|
|
+ return pageListResult;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -609,18 +624,17 @@ public class PatentInstructionService extends ServiceImpl<PatentInstructionMappe
|
|
*
|
|
*
|
|
* @param params 检索条件
|
|
* @param params 检索条件
|
|
*/
|
|
*/
|
|
- public void threadRunTask(PatentVO params) throws IOException {
|
|
|
|
|
|
+ public void threadRunTask(PatentVO params, Integer beforeNum) throws IOException {
|
|
//根据筛选条件分页查询专利清单(这一次查询目的只是获得专利总数和总页数)
|
|
//根据筛选条件分页查询专利清单(这一次查询目的只是获得专利总数和总页数)
|
|
- IPage<PatentDTO> pageList = patentService.getPageList(params);
|
|
|
|
- if (pageList == null || pageList.getRecords() == null || pageList.getRecords().size() == 0) {
|
|
|
|
|
|
+ List<Patent> patents = patentMapper.getPageListNext5(params, beforeNum);
|
|
|
|
+ if (patents == null || patents.size() == 0) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- List<PatentDTO> patents = pageList.getRecords(); //取出专利清单
|
|
|
|
- List<String> patentNos = patents.stream().map(PatentDTO::getPatentNo).collect(Collectors.toList()); //过滤取出专利号
|
|
|
|
- //根据专利号patentNos查询出所有pdf文档数据
|
|
|
|
|
|
+ List<String> patentNos = patents.stream().map(Patent::getPatentNo).collect(Collectors.toList()); //取出专利号
|
|
|
|
+ //查询出所有pdf文档数据
|
|
List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos));
|
|
List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos));
|
|
//遍历专利,装载数据
|
|
//遍历专利,装载数据
|
|
- for (PatentDTO patent : patents) {
|
|
|
|
|
|
+ for (Patent patent : patents) {
|
|
String patentNo = patent.getPatentNo();
|
|
String patentNo = patent.getPatentNo();
|
|
List<PatentInstruction> patentInstructionList = patentInstructions.stream().filter(patentInstruction -> patentInstruction.getPatentNo().equals(patentNo)).collect(Collectors.toList());
|
|
List<PatentInstruction> patentInstructionList = patentInstructions.stream().filter(patentInstruction -> patentInstruction.getPatentNo().equals(patentNo)).collect(Collectors.toList());
|
|
PatentInstruction patentInstruction = new PatentInstruction();
|
|
PatentInstruction patentInstruction = new PatentInstruction();
|
|
@@ -636,10 +650,44 @@ public class PatentInstructionService extends ServiceImpl<PatentInstructionMappe
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
|
|
- //解析当前专利号的pdf文件,将pdf首页转换为图片数据并返回,使用名称为"imgData"的缓存进行处理
|
|
|
|
|
|
+ //调用缓存方法,将当前专利号作为key,专利pdf首页文件图片数据作为value放入名称为"imgData"的缓存(key是唯一值,若缓存中已有当前专利号的key,则不会做任何操作)
|
|
cyCacheUtil.getImgData(patentNo, pdfFile);
|
|
cyCacheUtil.getImgData(patentNo, pdfFile);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public String getImgData(File pdfFile) throws IOException {
|
|
|
|
+ //pdf转图片方式二(用依赖pdfbox)
|
|
|
|
+ PDDocument doc = PDDocument.load(pdfFile);
|
|
|
|
+ String newFilePath = StringUtils.getUUID() + ".pdf";
|
|
|
|
+ File outputFile = new File(newFilePath);
|
|
|
|
+ PDFRenderer pdfRenderer = new PDFRenderer(doc);
|
|
|
|
+ // 提取的页码
|
|
|
|
+ int pageNumber = 0;
|
|
|
|
+ // 以300 dpi 读取存入 BufferedImage 对象
|
|
|
|
+ int dpi = 300;
|
|
|
|
+ RenderedImage buffImage = pdfRenderer.renderImageWithDPI(pageNumber, dpi, ImageType.RGB);
|
|
|
|
+ // 将 BufferedImage 写入到 png
|
|
|
|
+ ImageIO.write(buffImage, "png", outputFile);
|
|
|
|
+ doc.close();
|
|
|
|
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
+ FileInputStream in = new FileInputStream(outputFile);
|
|
|
|
+ int bytesRead;
|
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
|
+
|
|
|
|
+ //读取pdf首页临时文件
|
|
|
|
+ while ((bytesRead = in.read(buffer)) != -1) {
|
|
|
|
+ out.write(buffer, 0, bytesRead);
|
|
|
|
+ }
|
|
|
|
+ //关闭读取流
|
|
|
|
+ in.close();
|
|
|
|
+ byte[] bytes = out.toByteArray();
|
|
|
|
+ String imgData = org.apache.commons.codec.binary.Base64.encodeBase64String(bytes);
|
|
|
|
+ //关闭写出流
|
|
|
|
+ out.close();
|
|
|
|
+ //最后记得删除pdf首页的临时文件
|
|
|
|
+ new File(newFilePath).delete();
|
|
|
|
+ return imgData;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
}
|
|
}
|