|
@@ -2,11 +2,15 @@ package cn.cslg.pas.service.patentPDF;
|
|
|
|
|
|
import cn.cslg.pas.common.model.dto.PatentDTO;
|
|
|
import cn.cslg.pas.common.model.vo.PatentVO;
|
|
|
+import cn.cslg.pas.common.utils.DateUtils;
|
|
|
import cn.cslg.pas.common.utils.FileUtils;
|
|
|
+import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
|
|
|
import cn.cslg.pas.common.utils.StringUtils;
|
|
|
import cn.cslg.pas.domain.PatentInstruction;
|
|
|
+import cn.cslg.pas.domain.Task;
|
|
|
import cn.cslg.pas.mapper.PatentInstructionMapper;
|
|
|
import cn.cslg.pas.service.PatentService;
|
|
|
+import cn.cslg.pas.service.TaskService;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
@@ -34,65 +38,116 @@ import java.util.stream.Collectors;
|
|
|
@Service
|
|
|
public class PatentPDFService extends ServiceImpl<PatentInstructionMapper, PatentInstruction> {
|
|
|
private final PatentService patentService;
|
|
|
+ private final TaskService taskService;
|
|
|
private final FileUtils fileUtils;
|
|
|
+ private final LoginUtils loginUtils;
|
|
|
|
|
|
public void queryPatentPdfFirstPages(PatentVO params, HttpServletResponse response) throws IOException {
|
|
|
log.info("开始处理【导出多件专利pdf首页】的业务,业务参数为:{}", params);
|
|
|
|
|
|
- //根据筛选条件,直接调用查询
|
|
|
- IPage<PatentDTO> pageList = patentService.getPageList(params);
|
|
|
- List<PatentDTO> patents = pageList.getRecords();
|
|
|
- List<String> patentNos = patents.stream().map(PatentDTO::getPatentNo).collect(Collectors.toList());
|
|
|
-
|
|
|
- //根据专利号patentNos查询出所有pdf文档数据
|
|
|
- List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos).groupBy(PatentInstruction::getPatentNo));
|
|
|
+ //根据筛选条件分页查询专利清单(这一次查询目的:只是获得专利总数和总页数)
|
|
|
+ IPage<PatentDTO> pageList0 = patentService.getPageList(params);
|
|
|
+ long total = pageList0.getTotal(); //专利总数(作为任务进度总数)
|
|
|
+ long pages = pageList0.getPages(); //总页数(用来计算要检索几次)
|
|
|
|
|
|
ArrayList<String> filePaths = new ArrayList<>(); //存放所有专利说明书首页pdf的文件路径
|
|
|
- for (PatentInstruction patentInstruction : patentInstructions) { //遍历,将所有专利说明书pdf的首页取出生成新的Pdf文件存在本地
|
|
|
- String filePath = fileUtils.getSystemPath() + patentInstruction.getUrl();
|
|
|
- PDDocument doc = PDDocument.load(new File(filePath));
|
|
|
- Splitter splitter = new Splitter();
|
|
|
- splitter.setStartPage(1);
|
|
|
- splitter.setEndPage(1);
|
|
|
- PDDocument needDoc = splitter.split(doc).get(0);
|
|
|
- String newFilePath = "专利号" + patentInstruction.getPatentNo() + "的说明书pdf首页.pdf";
|
|
|
- needDoc.save(newFilePath);
|
|
|
- filePaths.add(newFilePath);
|
|
|
- needDoc.close();
|
|
|
- doc.close();
|
|
|
- }
|
|
|
-
|
|
|
- //开始合并上面生成的所有专利说明书首页pdf文件
|
|
|
+ String mergedFilePath = StringUtils.getUUID() + ".pdf";
|
|
|
PDFMergerUtility pdfMerger = new PDFMergerUtility();
|
|
|
- for (String filePath : filePaths) {
|
|
|
- pdfMerger.addSource(filePath);
|
|
|
+
|
|
|
+ //上传任务
|
|
|
+ Task task = new Task()
|
|
|
+ .setFileName(mergedFilePath)
|
|
|
+ .setProjectId(params.getProjectId())
|
|
|
+ .setType(2)
|
|
|
+ .setCreateBy(loginUtils.getId())
|
|
|
+ .setStartTime(DateUtils.getDateTime())
|
|
|
+ .setTotal((int) total)
|
|
|
+ .setDefaultNum(0)
|
|
|
+ .setSuccessNum(0)
|
|
|
+ .setTrueSuccessNum(0);
|
|
|
+ taskService.save(task);
|
|
|
+
|
|
|
+ int successNum = 0;
|
|
|
+ //一页一页检索
|
|
|
+ for (long i = 1; i <= pages; 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()); //过滤取出专利号
|
|
|
+
|
|
|
+ //根据专利号patentNos查询出所有pdf文档数据
|
|
|
+ List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos));
|
|
|
+
|
|
|
+ //遍历当前页专利号
|
|
|
+ for (String patentNo : patentNos) {
|
|
|
+ List<PatentInstruction> patentInstructionList = patentInstructions.stream().filter(item -> item.getPatentNo().equals(patentNo)).collect(Collectors.toList());
|
|
|
+ //若没有说明书pdf
|
|
|
+ if (patentInstructionList.size() == 0) {
|
|
|
+ //更新任务表,完成数量+1,发送进度+1
|
|
|
+ task.setSuccessNum(task.getSuccessNum() + 1);
|
|
|
+ Task updateTask = new Task();
|
|
|
+ updateTask.setId(task.getId());
|
|
|
+ updateTask.setSuccessNum(++successNum);
|
|
|
+ taskService.save(updateTask);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //若有说明书pdf
|
|
|
+ PatentInstruction patentInstruction;
|
|
|
+ if (patentInstructionList.size() > 1) { //若有多个,则过滤取出授权文档
|
|
|
+ patentInstruction = patentInstructionList.stream().filter(item -> item.getType() == 2).collect(Collectors.toList()).get(0);
|
|
|
+ } else { //若只有一个pdf,则直接取出
|
|
|
+ patentInstruction = patentInstructionList.get(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ String filePath = fileUtils.getSystemPath() + patentInstruction.getUrl();
|
|
|
+ PDDocument doc = PDDocument.load(new File(filePath));
|
|
|
+ Splitter splitter = new Splitter();
|
|
|
+ splitter.setStartPage(1);
|
|
|
+ splitter.setEndPage(1);
|
|
|
+ PDDocument needDoc = splitter.split(doc).get(0);
|
|
|
+ String newFilePath = "专利号【" + patentNo + "】的说明书pdf首页.pdf";
|
|
|
+ needDoc.save(newFilePath);
|
|
|
+ filePaths.add(newFilePath);
|
|
|
+ pdfMerger.addSource(filePath);
|
|
|
+
|
|
|
+ //更新任务表,完成数量+1,发送进度+1
|
|
|
+ task.setSuccessNum(task.getSuccessNum() + 1);
|
|
|
+ Task updateTask = new Task();
|
|
|
+ updateTask.setId(task.getId());
|
|
|
+ updateTask.setSuccessNum(++successNum);
|
|
|
+ taskService.save(updateTask);
|
|
|
+
|
|
|
+ needDoc.close();
|
|
|
+ doc.close();
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
// 设置合并生成pdf文件名称
|
|
|
- String mergedFilePath = StringUtils.getUUID() + ".pdf";
|
|
|
pdfMerger.setDestinationFileName(mergedFilePath);
|
|
|
// 合并PDF
|
|
|
pdfMerger.mergeDocuments();
|
|
|
|
|
|
- InputStream fis = new BufferedInputStream(new FileInputStream(mergedFilePath));
|
|
|
- byte[] buffer = new byte[fis.available()];
|
|
|
- fis.read(buffer);
|
|
|
- fis.close();
|
|
|
- // 清空response
|
|
|
- response.reset();
|
|
|
- // 设置response的Header
|
|
|
- response.addHeader("Content-Disposition", "attachment;filename=" + new String(mergedFilePath.getBytes()));
|
|
|
- response.addHeader("Content-Length", "" + new File(mergedFilePath).length());
|
|
|
- OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
|
|
|
- response.setContentType("application/octet-stream");
|
|
|
- toClient.write(buffer);
|
|
|
- toClient.flush();
|
|
|
- toClient.close();
|
|
|
+// InputStream fis = new BufferedInputStream(new FileInputStream(mergedFilePath));
|
|
|
+// byte[] buffer = new byte[fis.available()];
|
|
|
+// fis.read(buffer);
|
|
|
+// fis.close();
|
|
|
+// // 清空response
|
|
|
+// response.reset();
|
|
|
+// // 设置response的Header
|
|
|
+// response.addHeader("Content-Disposition", "attachment;filename=" + new String(mergedFilePath.getBytes()));
|
|
|
+// response.addHeader("Content-Length", "" + new File(mergedFilePath).length());
|
|
|
+// OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
|
|
|
+// response.setContentType("application/octet-stream");
|
|
|
+// toClient.write(buffer);
|
|
|
+// toClient.flush();
|
|
|
+// toClient.close();
|
|
|
|
|
|
//最后要记得删除所有文件
|
|
|
for (String filePath : filePaths) { //删除每个pdf首页文件
|
|
|
new File(filePath).delete();
|
|
|
}
|
|
|
- new File(mergedFilePath).delete(); //删除合并的pdf文件
|
|
|
+// new File(mergedFilePath).delete(); //删除合并的pdf文件
|
|
|
|
|
|
}
|
|
|
|