package cn.cslg.pas.service.importPatent; import cn.cslg.pas.common.dto.UploadPatentWebDTO; import cn.cslg.pas.common.dto.es.EsCustomFieldDTO; import cn.cslg.pas.common.utils.FormatUtil; import cn.cslg.pas.common.vo.ImportTaskAMVO; import cn.cslg.pas.common.vo.PatentWithIdVO; import cn.cslg.pas.domain.business.ImportTask; import cn.cslg.pas.domain.es.ESImportTask; import cn.cslg.pas.domain.es.Patent; import cn.cslg.pas.domain.es.PatentJoin; import cn.cslg.pas.service.business.CompareLiteratureService; import cn.cslg.pas.service.business.ImportTaskService; import cn.cslg.pas.service.business.es.EsCustomFieldService; import cn.cslg.pas.service.business.es.EsMergePersonService; import cn.cslg.pas.service.business.es.EsProductPatentService; import cn.cslg.pas.service.business.es.EsService; import cn.cslg.pas.service.common.FileManagerService; import cn.cslg.pas.service.common.MessageService; import cn.cslg.pas.service.common.PatentStarApiService; import cn.cslg.pas.service.common.TranslateService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class SavePatentToEsThread extends Thread { private ApplicationContext applicationContext; private List uploadPatentWebDTOS = new CopyOnWriteArrayList<>(); private Lock taskLock = new ReentrantLock(); private Condition taskCondition = taskLock.newCondition(); private TaskThread taskThread; private ImportTaskAMVO importTaskAMVO; private Boolean ifProductAll = false; private Integer i = 0; @Override public void run() { while ((!ifProductAll || uploadPatentWebDTOS.size() > 0) && importTaskAMVO.getState().equals(1)) { try { //判断任务队列是否有任务,若没有则线程等待唤醒 if (uploadPatentWebDTOS.size() == 0) { taskLock.lock(); taskCondition.await(); } } catch (Exception e) { } if (uploadPatentWebDTOS.size() == 0) { break; } UploadPatentWebDTO uploadPatentWebDTO = uploadPatentWebDTOS.remove(0); Patent patent = uploadPatentWebDTO.getPatent(); try { //根据专利号查询专利 EsService esService = applicationContext.getBean(EsService.class); PatentWithIdVO patentWithIdVO = esService.getIdByPatentNo(patent.getPatentNo()); String patentId = null; // 若查出专利则更新 Patent orgPatent = null; //更新专利 if (patentWithIdVO != null) { patentId = patentWithIdVO.getId(); orgPatent = patentWithIdVO.getPatent(); patent = this.formPatent(patent, orgPatent); //更新翻译 if (patent.getClaim() != null && patent.getClaim() != orgPatent.getClaim()) { TranslateService translateService = applicationContext.getBean(TranslateService.class); if (orgPatent.getPatentNo() != null) { translateService.deleteTranslatePatentNo(orgPatent.getPatentNo(), "3"); } } if (patent.getPublicFullText() != null && patent.getPublicFullText() != orgPatent.getPublicFullText()) { TranslateService translateService = applicationContext.getBean(TranslateService.class); if (orgPatent.getPatentNo() != null) { translateService.deleteTranslatePatentNo(orgPatent.getPatentNo(), "4"); } } if (patent.getGrantFullText() != null && patent.getGrantFullText() != orgPatent.getGrantFullText()) { TranslateService translateService = applicationContext.getBean(TranslateService.class); if (orgPatent.getPatentNo() != null) { translateService.deleteTranslatePatentNo(orgPatent.getPatentNo(), "4"); } } BeanUtils.copyProperties(patent, orgPatent, FormatUtil.getNullPropertyNames(patent)); esService.updatePatent(orgPatent, patentWithIdVO.getId()); } else { PatentJoin patentJoin = new PatentJoin(); patentJoin.setName("patent"); patent.setPatentJoin(patentJoin); patentId = esService.addPatent(patent); orgPatent = patent; } //判断是否和专题库或报告关联 ImportTaskAMVO importTaskAMVO = taskThread.getImportTaskAMVO(); //和专题库或报告进行关联 if (importTaskAMVO.getProjectId() != null) { //根据projectId和专利id查询 if (patentId != null) { Boolean ifInproject = esService.searchPatent(patentId, importTaskAMVO.getProjectId()); if (!ifInproject) { System.out.println("多添加的专利:" + patent.getPatentNo()); Patent patentChild = new Patent(); PatentJoin patentJoin = new PatentJoin(); patentJoin.setParent(patentId); patentJoin.setName("project"); patentChild.setPatentJoin(patentJoin); patentChild.setProjectId(importTaskAMVO.getProjectId()); esService.addChildPatent(patentChild, patentId); //当导入专题库时添加默认合并申请人 if (importTaskAMVO.getProjectType() != null && importTaskAMVO.getProjectType().equals(0)) { EsMergePersonService esMergePersonService = applicationContext.getBean(EsMergePersonService.class); esMergePersonService.addDefaultMergePerson(orgPatent, patentId, importTaskAMVO.getProjectId()); } } //添加报告对比文件 if (importTaskAMVO.getProjectType() != null && importTaskAMVO.getProjectType().equals(1)) { if (importTaskAMVO.getReportType()==null||importTaskAMVO.getReportType().equals(7) || importTaskAMVO.getReportType().equals(1) || importTaskAMVO.getReportType().equals(2)) { CompareLiteratureService compareLiteratureService = applicationContext.getBean(CompareLiteratureService.class); compareLiteratureService.addPatentCompareLiterature(patent, importTaskAMVO.getProjectId(), importTaskAMVO.getCreateId()); } } } //和任务关联 if (importTaskAMVO.getId() != null) { if (patentId != null) { Boolean ifInTask = esService.ifInTask(patentId, importTaskAMVO.getProjectId(), importTaskAMVO.getId()); if (!ifInTask) { Patent patentChild = new Patent(); PatentJoin patentJoin = new PatentJoin(); patentJoin.setParent(patentId); patentJoin.setName("import_task"); patentChild.setPatentJoin(patentJoin); ESImportTask esImportTask = new ESImportTask(); esImportTask.setProjectId(importTaskAMVO.getProjectId()); esImportTask.setTaskId(importTaskAMVO.getId()); patentChild.setImportTask(esImportTask); esService.addChildPatent(patentChild, patentId); } } } } else if (importTaskAMVO.getProductId() != null && patentId != null) { EsProductPatentService esProductPatentService = applicationContext.getBean(EsProductPatentService.class); esProductPatentService.addProductPatent(patentId, importTaskAMVO.getProductId()); } //保存和自定义字段关联 if (importTaskAMVO.getFieldDTOS() != null && importTaskAMVO.getFieldDTOS().size() > 0) { for (EsCustomFieldDTO esCustomFieldDTO : importTaskAMVO.getFieldDTOS() ) { esCustomFieldDTO.setPatentId(patentId); esCustomFieldDTO.setOptionType(2); esCustomFieldDTO.setPatentNo(patent.getPatentNo()); EsCustomFieldService esCustomFieldService = applicationContext.getBean(EsCustomFieldService.class); esCustomFieldService.addCustomField(esCustomFieldDTO); } } if (uploadPatentWebDTO.getEsCustomFieldDTOList() != null && uploadPatentWebDTO.getEsCustomFieldDTOList().size() != 0) { for (EsCustomFieldDTO esCustomFieldDTO : uploadPatentWebDTO.getEsCustomFieldDTOList() ) { esCustomFieldDTO.setPatentId(patentId); esCustomFieldDTO.setOptionType(1); esCustomFieldDTO.setPatentNo(patent.getPatentNo()); EsCustomFieldService esCustomFieldService = applicationContext.getBean(EsCustomFieldService.class); esCustomFieldService.addCustomField(esCustomFieldDTO); } } //导入完成,通知前台 taskThread.updateProcess(false, 1, ""); } catch (Exception e) { e.printStackTrace(); taskThread.updateProcess(true, 1, ""); } } taskThread.awakeTaskThread(); System.out.println("专利信息装载完毕"); } public SavePatentToEsThread(TaskThread taskThread, ApplicationContext applicationContext) { this.taskThread = taskThread; this.applicationContext = applicationContext; this.importTaskAMVO = taskThread.getImportTaskAMVO(); } public void awakeTask(UploadPatentWebDTO uploadPatentWebDTO) { synchronized ("") { UploadPatentWebDTO uploadPatentWebDTO1 = new UploadPatentWebDTO(); BeanUtils.copyProperties(uploadPatentWebDTO, uploadPatentWebDTO1); i++; System.out.println("添加了" + i); uploadPatentWebDTOS.add(uploadPatentWebDTO1); if (taskLock.tryLock()) { taskCondition.signalAll(); taskLock.unlock(); } } } public void setIfProductAll(Boolean ifProductAll) { this.ifProductAll = ifProductAll; System.out.println("专利导入全部结束" + this.ifProductAll + importTaskAMVO.getId()); if (taskLock.tryLock()) { System.out.println("专利导入到es解锁" + importTaskAMVO.getId()); taskCondition.signalAll(); taskLock.unlock(); } } public Patent formPatent(Patent patent, Patent orgPatent) { String publicNo = patent.getPublicNo(); String grantNo = patent.getGrantNo(); if (patent.getPatentNo().startsWith("CN")) { if (patent.getPublicNo() != null) { if (patent.getPublicNo().endsWith("B")) { grantNo = patent.getPublicNo(); } } if (patent.getGrantNo() != null) { if (patent.getGrantNo().endsWith("A")) { publicNo = patent.getGrantNo(); } } patent.setPublicNo(publicNo); patent.setGrantNo(grantNo); if (orgPatent.getClaim() != null && orgPatent.getClaim().size() > 0 && orgPatent.getGrantNo() != null && patent.getGrantNo() == null) { patent.setClaim(orgPatent.getClaim()); } if (orgPatent.getGrantNo() != null) { patent.setGrantFullText(patent.getPublicFullText()); patent.setPublicFullText(null); } } return patent; } }