123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- package cn.cslg.pas.service.importPatent;
- import cn.cslg.pas.common.dto.UploadPatentWebDTO;
- import cn.cslg.pas.common.model.cronModel.SystemFile;
- import cn.cslg.pas.common.utils.ReadExcelUtils;
- import cn.cslg.pas.common.vo.ImportTaskAMVO;
- import cn.cslg.pas.common.vo.PatentData;
- import cn.cslg.pas.common.vo.UploadParamsVO;
- import cn.cslg.pas.common.vo.UploadSettingVO;
- import cn.cslg.pas.domain.es.Patent;
- import cn.cslg.pas.domain.es.Text;
- import cn.cslg.pas.service.common.ExcuteDataToVOService;
- import cn.cslg.pas.service.common.ExcuteUploadSettingService;
- import cn.cslg.pas.service.common.FileManagerService;
- import com.alibaba.fastjson.JSONArray;
- import org.apache.commons.compress.utils.IOUtils;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.springframework.beans.factory.annotation.Configurable;
- import org.springframework.context.ApplicationContext;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- @Configurable
- public class GetPatentFromExcelThread extends Thread {
- private ImportTaskAMVO importTaskAMVO;
- private ApplicationContext applicationContext;
- private SavePatentToEsThread savePatentToEsThread;
- private GetPatentPictureFromExcelThread getPatentPictureFromExcelThread;
- @Override
- public void run() {
- try {
- FileManagerService fileManagerService = applicationContext.getBean(FileManagerService.class);
- String res = fileManagerService.getSystemFileFromFMS(Arrays.asList(importTaskAMVO.getFileGuid()));
- List<SystemFile> systemFiles = JSONArray.parseArray(res, SystemFile.class);
- SystemFile systemFile = systemFiles.get(0);
- String suffix = systemFile.getFileName().substring(systemFile.getFileName().lastIndexOf("."));
- //调用文件系统取出文件接口,获得文件流
- byte[] bytes = fileManagerService.downloadSystemFileFromFMS(importTaskAMVO.getFileGuid());
- //创建临时文件tempFile,并将文件读取到tempFile
- File tempFile = File.createTempFile(systemFile.getFileName() + "temp", suffix);
- try (
- InputStream inputStream = new ByteArrayInputStream(bytes);
- FileOutputStream outputStream = new FileOutputStream(tempFile)
- ) {
- IOUtils.copy(inputStream, outputStream); // 将输入流复制到临时文件
- }
- Integer total = importTaskAMVO.getAllNum();
- Integer lastIndex = importTaskAMVO.getDoneNum();
- Integer sourceId = importTaskAMVO.getSourceId();
- //解析数据源类,通过数据来源id(如1:智慧芽)解析数据源配置文件,获得数据源配置文件对象jsonData
- ExcuteDataToVOService excuteDataToVOService = applicationContext.getBean(ExcuteDataToVOService.class);
- ExcuteUploadSettingService excuteUploadSettingService = applicationContext.getBean(ExcuteUploadSettingService.class);
- List<UploadSettingVO.Column> jsonData = excuteUploadSettingService.ExcuteUploadSetting(sourceId.toString());
- //解析Excel文件获得工作簿
- Sheet sheet = ReadExcelUtils.readExcel(tempFile);
- //遍历专利总数量,在循环中将专利一个一个存入消费者专利队列
- for (int i = lastIndex; i < total; i++) {
- if(!importTaskAMVO.getState().equals(1)){
- System.out.println(importTaskAMVO.getId());
- break;
- }
- PatentData patentData = ReadExcelUtils.readExcelOneRow(tempFile, sheet, i + 1);
- //调用装载数据类(根据数据源配置文件对象和专利源数据生成专利数据)
- UploadParamsVO uploadParamsVO = excuteDataToVOService.fileToPatentVO(patentData, jsonData);
- this.loadPatent(uploadParamsVO);
- UploadPatentWebDTO uploadPatentWebDTO =new UploadPatentWebDTO();
- uploadPatentWebDTO.setHaveSaveCounter(0);
- uploadPatentWebDTO.setTotalCounter(importTaskAMVO.getShouldSaveCounter());
- uploadPatentWebDTO.setPictureData(uploadParamsVO.getPictureData());
- uploadPatentWebDTO.setPatent(uploadParamsVO.getPatent());
- //专利丢入消费者队列,并唤醒消费者线程
- savePatentToEsThread.awakeTask(uploadPatentWebDTO.getPatent());
- getPatentPictureFromExcelThread.awakeTask(uploadPatentWebDTO);
- }
- //专利取完通知消费者线程
- savePatentToEsThread.setIfProductAll(true);
- getPatentPictureFromExcelThread.setIfProductAll(true);
- //删除临时文件tempFile
- new File(tempFile.getPath()).delete();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public GetPatentFromExcelThread(TaskThread taskThread, SavePatentToEsThread savePatentToEsThread, GetPatentPictureFromExcelThread getPatentPictureFromExcelThread) {
- this.importTaskAMVO = taskThread.getImportTaskAMVO();
- this.savePatentToEsThread = savePatentToEsThread;
- this.applicationContext = taskThread.getApplicationContext();
- this.getPatentPictureFromExcelThread=getPatentPictureFromExcelThread;
- }
- public void loadPatent(UploadParamsVO uploadParamsVO) {
- Patent patent = uploadParamsVO.getPatent();
- String patentNo =patent.getPatentNo();
- String contry =patentNo.substring(0,2);
- //装载标题
- if (uploadParamsVO.getTitle() != null) {
- List<Text> texts = new ArrayList<>();
- if (uploadParamsVO.getTitle().getName() != null) {
- Text text = new Text();
- text.setTextContent(uploadParamsVO.getTitle().getName());
- text.setIfOrigin(true);
- text.setLanguage(contry);
- texts.add(text);
- }
- if (uploadParamsVO.getTitle().getNameOut() != null) {
- Text text = new Text();
- text.setTextContent(uploadParamsVO.getTitle().getNameOut());
- text.setIfOrigin(false);
- text.setLanguage("CN");
- texts.add(text);
- }
- if (texts.size() != 0) {
- patent.setTitle(texts);
- }
- }
- //装载摘要
- if (uploadParamsVO.getAbstractStr() != null) {
- List<Text> texts = new ArrayList<>();
- if (uploadParamsVO.getAbstractStr().getName() != null) {
- Text text = new Text();
- text.setTextContent(uploadParamsVO.getAbstractStr().getName());
- text.setIfOrigin(true);
- text.setLanguage(contry);
- texts.add(text);
- }
- if (uploadParamsVO.getAbstractStr().getNameOut() != null) {
- Text text = new Text();
- text.setTextContent(uploadParamsVO.getAbstractStr().getNameOut());
- text.setIfOrigin(false);
- text.setLanguage("CN");
- texts.add(text);
- }
- if (texts.size() != 0) {
- patent.setAbstractStr(texts);
- }
- }
- //装载权利要求
- if (uploadParamsVO.getPatentRight() != null) {
- List<Text> texts = new ArrayList<>();
- if (uploadParamsVO.getPatentRight().getName() != null) {
- Text text = new Text();
- text.setTextContent(uploadParamsVO.getPatentRight().getName());
- text.setIfOrigin(true);
- text.setLanguage(contry);
- texts.add(text);
- }
- if (uploadParamsVO.getPatentRight().getNameOut() != null) {
- Text text = new Text();
- text.setTextContent(uploadParamsVO.getPatentRight().getNameOut());
- text.setIfOrigin(false);
- text.setLanguage("CN");
- texts.add(text);
- }
- if (texts.size() != 0) {
- patent.setClaim(texts);
- }
- }
- }
- }
|