PatentExportService.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. package cn.cslg.pas.service.business;
  2. import cn.cslg.pas.common.dto.ExportTaskDTO;
  3. import cn.cslg.pas.common.dto.PatentColumnDTO;
  4. import cn.cslg.pas.common.dto.PatentExport.PatentExportVO;
  5. import cn.cslg.pas.common.dto.es.EsQueryPatentFieldsDTO;
  6. import cn.cslg.pas.common.dto.es.InnerFields;
  7. import cn.cslg.pas.common.model.cronModel.Records;
  8. import cn.cslg.pas.common.model.request.QueryRequest;
  9. import cn.cslg.pas.common.model.request.StringRequest;
  10. import cn.cslg.pas.common.utils.*;
  11. import cn.cslg.pas.common.vo.ConfigVOS.PatentConfigVO;
  12. import cn.cslg.pas.common.vo.FieldValueVO;
  13. import cn.cslg.pas.common.vo.ImportTaskAMVO;
  14. import cn.cslg.pas.common.vo.PatentPageMessageVO;
  15. import cn.cslg.pas.common.vo.WebSocketMessageVO;
  16. import cn.cslg.pas.common.vo.business.PatentNoVO;
  17. import cn.cslg.pas.common.vo.es.EsCustomFieldBatchVO;
  18. import cn.cslg.pas.common.vo.es.EsPatentCommonVO;
  19. import cn.cslg.pas.common.vo.es.EsPatentFieldsVO;
  20. import cn.cslg.pas.common.vo.es.InnerPatentFieldsVO;
  21. import cn.cslg.pas.domain.business.ImportTask;
  22. import cn.cslg.pas.domain.es.Patent;
  23. import cn.cslg.pas.exception.ExceptionEnum;
  24. import cn.cslg.pas.exception.XiaoShiException;
  25. import cn.cslg.pas.factorys.PatentExportFactory.GetValueImp;
  26. import cn.cslg.pas.factorys.PatentExportFactory.PatentExportFactory;
  27. import cn.cslg.pas.service.business.es.EsCustomFieldService;
  28. import cn.cslg.pas.service.business.es.EsPatentService;
  29. import cn.cslg.pas.service.business.es.EsService;
  30. import cn.cslg.pas.service.common.MessageService;
  31. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  32. import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
  33. import co.elastic.clients.elasticsearch.core.SearchRequest;
  34. import co.elastic.clients.elasticsearch.core.SearchResponse;
  35. import co.elastic.clients.elasticsearch.core.search.Hit;
  36. import com.alibaba.fastjson.JSON;
  37. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  38. import lombok.extern.slf4j.Slf4j;
  39. import org.apache.poi.hssf.usermodel.*;
  40. import org.apache.poi.hssf.util.HSSFColor;
  41. import org.apache.poi.ss.usermodel.FillPatternType;
  42. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  43. import org.apache.poi.ss.usermodel.VerticalAlignment;
  44. import org.springframework.beans.BeanUtils;
  45. import org.springframework.beans.factory.annotation.Autowired;
  46. import org.springframework.scheduling.annotation.Async;
  47. import org.springframework.stereotype.Service;
  48. import org.springframework.util.CollectionUtils;
  49. import java.io.ByteArrayOutputStream;
  50. import java.io.FileNotFoundException;
  51. import java.io.IOException;
  52. import java.util.*;
  53. import java.util.stream.Collectors;
  54. /**
  55. * Excel导出专利
  56. *
  57. * @Author xiexiang
  58. * @Date 2024/1/3
  59. */
  60. @Slf4j
  61. @Service
  62. public class PatentExportService {
  63. @Autowired
  64. private PDFExportFirstPageService pdfExportFirstPageService;
  65. @Autowired
  66. private EsPatentService patentService;
  67. @Autowired
  68. private MessageService messageService;
  69. @Autowired
  70. private PatentExportFactory patentExportFactory;
  71. @Autowired
  72. private ParseByteToFileUtils parseByteToFileUtils;
  73. @Autowired
  74. private ImportTaskService importTaskService;
  75. @Autowired
  76. private EsCustomFieldService esCustomFieldService;
  77. @Autowired
  78. private EsService esService;
  79. @Autowired
  80. private ElasticsearchClient client;
  81. @Autowired
  82. private EsPatentService esPatentService;
  83. /**
  84. * 导出专利
  85. *
  86. * @param exportTask
  87. * @return
  88. * @throws IOException
  89. */
  90. @Async
  91. public void exportPatent(ExportTaskDTO exportTask) throws IOException {
  92. EsCustomFieldBatchVO EsVO = exportTask.getEsVO();
  93. try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
  94. //字段是否被选择
  95. String esSelected = JsonUtils.objectToJson(EsVO.getSelected());
  96. //专利字段的集合
  97. List<PatentExportVO> selected = Optional.ofNullable(JsonUtils.jsonToList(esSelected, PatentExportVO.class))
  98. .orElse(new ArrayList<>());
  99. if (selected.isEmpty()) {
  100. throw new IllegalArgumentException("没有选择要导出的字段数据");
  101. }
  102. //key的集合(标头:例如patentNo)
  103. //过滤出被选择的字段
  104. List<PatentExportVO> patentExportVOS = selected.stream()
  105. .filter(PatentExportVO::getSelected)
  106. .collect(Collectors.toList());
  107. List<String> types = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
  108. List<InnerFields> innerFieldsList = new ArrayList<>();
  109. for (PatentExportVO patentExportVO : patentExportVOS) {
  110. if (types.contains(patentExportVO.getType())) {
  111. InnerFields innerFields = new InnerFields();
  112. innerFields.setFieldId(patentExportVO.getValue());
  113. innerFields.setFieldType(Integer.parseInt(patentExportVO.getType()));
  114. innerFieldsList.add(innerFields);
  115. }
  116. }
  117. //name的集合
  118. List<String> headers = selected.stream().filter(PatentExportVO::getSelected).map(PatentExportVO::getName).distinct().collect(Collectors.toList());
  119. long startTime = System.currentTimeMillis();
  120. //新建工作簿
  121. HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
  122. //新建sheet页
  123. HSSFSheet sheet = hssfWorkbook.createSheet();
  124. sheet.setDefaultColumnWidth(30);
  125. //新建标头行
  126. HSSFRow headerRow = sheet.createRow(0);
  127. headerRow.setHeight((short) 500);
  128. //新建标头行格式
  129. HSSFCellStyle headerCellStyle = hssfWorkbook.createCellStyle();
  130. //新建普通行格式
  131. HSSFCellStyle commonCellStyle = hssfWorkbook.createCellStyle();
  132. //遍历设置标头
  133. for (int i = 0; i < headers.size(); i++) {
  134. HSSFCell cell = headerRow.createCell(i);
  135. ExcelUtils.setExcelCellStyle(headerCellStyle);
  136. headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
  137. headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  138. headerCellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.SKY_BLUE.getIndex());
  139. headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  140. headerCellStyle.setWrapText(true);
  141. cell.setCellStyle(headerCellStyle);
  142. cell.setCellValue(headers.get(i));
  143. }
  144. //根据projectId查询专利信息
  145. List<String> patentNos = pdfExportFirstPageService.getPatentNo(EsVO);
  146. if (patentNos.isEmpty()) {
  147. throw new XiaoShiException("未获取到专利信息");
  148. }
  149. Integer total = patentNos.size();
  150. Integer defaultNum = 0;
  151. for (int i = 0; i < patentNos.size(); i++) {
  152. String patentNo = patentNos.get(i);
  153. Map<String, Object> map = new LinkedHashMap<>();
  154. PatentNoVO patentNoVO = new PatentNoVO();
  155. patentNoVO.setPatentNo(patentNo);
  156. patentNoVO.setProjectId(exportTask.getProjectId());
  157. PatentColumnDTO patent = patentService.selectPatentDetail(patentNoVO);
  158. if (patent == null) {
  159. defaultNum++;
  160. continue;
  161. }
  162. EsQueryPatentFieldsDTO esQueryPatentFieldsDTO = new EsQueryPatentFieldsDTO();
  163. esQueryPatentFieldsDTO.setProjectId(exportTask.getProjectId());
  164. esQueryPatentFieldsDTO.setTaskId(EsVO.getTaskId());
  165. esQueryPatentFieldsDTO.setPatentNos(patentNos);
  166. esQueryPatentFieldsDTO.setInnerFields(innerFieldsList);
  167. List<EsPatentFieldsVO> list = esCustomFieldService.getPatentFields(esQueryPatentFieldsDTO);
  168. for (int j = 0; j < patentExportVOS.size(); j++) {
  169. String column = patentExportVOS.get(j).getValue();
  170. String name = patentExportVOS.get(j).getName();
  171. String type = patentExportVOS.get(j).getType();
  172. if (types.contains(type)) {
  173. EsPatentFieldsVO esPatentFieldsVO = list.stream()
  174. .filter(item -> item.getPatentNo().equals(patentNo))
  175. .findFirst()
  176. .orElse(null);
  177. if (esPatentFieldsVO != null) {
  178. List<InnerPatentFieldsVO> innerClassFields = esPatentFieldsVO.getInnerClassFields();
  179. if (!innerClassFields.isEmpty()) {
  180. InnerPatentFieldsVO innerPatentFieldsVO = innerClassFields.stream()
  181. .filter(item -> item.getField().equals(column) && item.getFieldType().equals(Integer.parseInt(type)))
  182. .findFirst()
  183. .orElse(null);
  184. if (innerPatentFieldsVO != null) {
  185. List<FieldValueVO> fieldValueVOS = innerPatentFieldsVO.getFieldValueVOS();
  186. if (!fieldValueVOS.isEmpty()) {
  187. StringBuffer sb = new StringBuffer();
  188. for (FieldValueVO fieldValueVO : fieldValueVOS) {
  189. String valueStr = fieldValueVO.getValue();
  190. sb.append(valueStr);
  191. }
  192. map.put(name, sb.toString());
  193. }
  194. }
  195. }
  196. }
  197. } else {
  198. // 使用反射获取属性值
  199. Object value = GenerateObjectUtil.getPropertyValue(patent, column);
  200. if (value != null) {
  201. String json = CommonService.readJsonFile("patent.json");
  202. List<PatentConfigVO> patentConfigVOS = JSON.parseArray(json, PatentConfigVO.class);
  203. PatentConfigVO patentConfigVO = patentConfigVOS.stream().filter(item -> item.getValue().equals(column)).findFirst().orElse(null);
  204. String exportClass = patentConfigVO.getExportClass();
  205. GetValueImp getValueImp = patentExportFactory.getClass(exportClass);
  206. if (getValueImp != null) {
  207. String reValue = getValueImp.getValue(value);
  208. map.put(name, reValue);
  209. }
  210. }
  211. }
  212. }
  213. //新建一普通行
  214. HSSFRow row = sheet.createRow(i + 1);
  215. row.setHeight((short) 800);
  216. for (String key : map.keySet()) {
  217. int index = headers.indexOf(key);
  218. if (index != -1) {
  219. HSSFCell cell = row.createCell(index);
  220. ExcelUtils.setExcelCellStyle(commonCellStyle);
  221. commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
  222. commonCellStyle.setWrapText(true);
  223. cell.setCellStyle(commonCellStyle);
  224. if (StringUtils.isNotNull(map.get(key))) {
  225. cell.setCellValue(String.valueOf(map.get(key)));
  226. }
  227. }
  228. }
  229. WebSocketMessageVO webSocketMessageVO = new WebSocketMessageVO();
  230. webSocketMessageVO.setProjectId(EsVO.getProjectId());
  231. webSocketMessageVO.setCreateId(exportTask.getCreateId());
  232. webSocketMessageVO.setCode(602);
  233. webSocketMessageVO.setAllNum(total);
  234. webSocketMessageVO.setCurrentNum(i + 1);
  235. webSocketMessageVO.setTaskId(exportTask.getTaskId());
  236. webSocketMessageVO.setState(1);
  237. messageService.sendPatentExportMessage(webSocketMessageVO);
  238. }
  239. hssfWorkbook.write(out);
  240. log.info("导出excel结束,总数据量={},耗时={}ms", total, System.currentTimeMillis() - startTime);
  241. WebSocketMessageVO webSocketMessageVO = new WebSocketMessageVO();
  242. webSocketMessageVO.setProjectId(EsVO.getProjectId());
  243. webSocketMessageVO.setCreateId(exportTask.getCreateId());
  244. webSocketMessageVO.setCode(603);
  245. webSocketMessageVO.setAllNum(total);
  246. webSocketMessageVO.setCurrentNum(total);
  247. webSocketMessageVO.setTaskId(exportTask.getTaskId());
  248. webSocketMessageVO.setState(2);
  249. messageService.sendPatentExportMessage(webSocketMessageVO);
  250. String fileGuid = "";
  251. if (out.toByteArray() != null && out.toByteArray().length != 0) {
  252. fileGuid = parseByteToFileUtils.uploadFile(out.toByteArray(), 1);
  253. }
  254. exportTask.setFileGuid(fileGuid);
  255. exportTask.setAllNum(total);
  256. exportTask.setDefaultNum(defaultNum);
  257. exportTask.setDoneNum(total - defaultNum);
  258. exportTask.setState(2);
  259. Integer taskId = importTaskService.updateExportTask(exportTask);
  260. if (taskId == null) {
  261. throw new XiaoShiException("导出记录失败");
  262. }
  263. } catch (FileNotFoundException e) {
  264. throw new FileNotFoundException();
  265. } catch (Exception e) {
  266. }
  267. }
  268. public HSSFWorkbook getExportHssWorkbook(List<String> headers) {
  269. //新建工作簿
  270. HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
  271. //新建sheet页
  272. HSSFSheet sheet = hssfWorkbook.createSheet();
  273. sheet.setDefaultColumnWidth(30);
  274. //新建标头行
  275. HSSFRow headerRow = sheet.createRow(0);
  276. headerRow.setHeight((short) 500);
  277. //新建标头行格式
  278. HSSFCellStyle headerCellStyle = hssfWorkbook.createCellStyle();
  279. //新建普通行格式
  280. HSSFCellStyle commonCellStyle = hssfWorkbook.createCellStyle();
  281. //遍历设置标头
  282. for (int i = 0; i < headers.size(); i++) {
  283. HSSFCell cell = headerRow.createCell(i);
  284. ExcelUtils.setExcelCellStyle(headerCellStyle);
  285. headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
  286. headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  287. headerCellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.SKY_BLUE.getIndex());
  288. headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  289. headerCellStyle.setWrapText(true);
  290. cell.setCellStyle(headerCellStyle);
  291. cell.setCellValue(headers.get(i));
  292. }
  293. return hssfWorkbook;
  294. }
  295. public Integer updateTaskStateToDoing(Integer taskId,Long total) {
  296. ImportTask importTask = importTaskService.getById(taskId);
  297. if (importTask == null) {
  298. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "导入任务不存在");
  299. } else {
  300. importTask.setState(1);
  301. importTask.setAllNum(total.intValue());
  302. importTask.updateById();
  303. }
  304. return importTask.getId();
  305. }
  306. public Records queryPatents(EsPatentCommonVO esPatentCommonVO) throws Exception {
  307. List<Patent> patentList = new ArrayList<>();
  308. Records records = new Records();
  309. SearchRequest.Builder builder = esService.getCommonPatent(esPatentCommonVO);
  310. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  311. List<Hit<Patent>> hits = response.hits().hits();
  312. Long total = 0L;
  313. if (org.apache.commons.lang3.StringUtils.isNotEmpty(esPatentCommonVO.getEsField())) {
  314. Aggregate aggregate = response.aggregations().get("count");
  315. total = aggregate.cardinality().value();
  316. } else {
  317. total = response.hits().total().value();
  318. }
  319. records.setTotal(total);
  320. for (Hit<Patent> hit : hits) {
  321. patentList.add(hit.source());
  322. }
  323. records.setData(patentList);
  324. return records;
  325. }
  326. @Async
  327. public void exportPatent2(ExportTaskDTO exportTask, ImportTaskAMVO importTaskAMVO) throws Exception {
  328. EsCustomFieldBatchVO esVO = exportTask.getEsVO();
  329. List<PatentExportVO> selectedFields = esVO.getSelected();
  330. selectedFields=selectedFields.stream().filter(PatentExportVO::getSelected).collect(Collectors.toList());
  331. Integer taskId = exportTask.getTaskId();
  332. Integer projectId = exportTask.getProjectId();
  333. String createId = exportTask.getCreateId();
  334. if (taskId == null) {
  335. throw new XiaoShiException(ExceptionEnum.BUSINESS_CHECK, "任务id不可为空");
  336. }
  337. if (CollectionUtils.isEmpty(selectedFields)) {
  338. throw new IllegalArgumentException("没有选择要导出的字段数据");
  339. }
  340. List<String> headers = selectedFields.stream().filter(PatentExportVO::getSelected).map(PatentExportVO::getName).distinct().collect(Collectors.toList());
  341. List<String> notInNos = esVO.getIsDelete();
  342. List<String> inNos = esVO.getIsAdd();
  343. List<String> types = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
  344. List<InnerFields> innerFieldsList = new ArrayList<>();
  345. for (PatentExportVO patentExportVO : selectedFields) {
  346. if (types.contains(patentExportVO.getType())) {
  347. InnerFields innerFields = new InnerFields();
  348. innerFields.setFieldId(patentExportVO.getValue());
  349. innerFields.setFieldType(Integer.parseInt(patentExportVO.getType()));
  350. innerFieldsList.add(innerFields);
  351. }
  352. }
  353. HSSFWorkbook hssfWorkbook = this.getExportHssWorkbook(headers);
  354. HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
  355. StringRequest queryRequest = new StringRequest();
  356. BeanUtils.copyProperties(esVO, queryRequest);
  357. EsQueryPatentFieldsDTO esQueryPatentFieldsDTO = new EsQueryPatentFieldsDTO();
  358. esQueryPatentFieldsDTO.setProjectId(exportTask.getProjectId());
  359. esQueryPatentFieldsDTO.setTaskId(esVO.getTaskId());
  360. esQueryPatentFieldsDTO.setInnerFields(innerFieldsList);
  361. HSSFCellStyle commonCellStyle = this.getComCellStyle(hssfWorkbook);
  362. Long startNum = null;
  363. Long endNum = null;
  364. if (esVO.getStartNumber() != null) {
  365. startNum =0l+esVO.getStartNumber();
  366. }
  367. if (esVO.getEndNumber() != null) {
  368. endNum = 0l+esVO.getEndNumber();
  369. }
  370. if (startNum == null) {
  371. startNum = 1l;
  372. }
  373. Long toNum = startNum+9l;
  374. if (endNum != null && endNum < toNum) {
  375. toNum = endNum;
  376. }
  377. Integer index = 0;
  378. String json = CommonService.readJsonFile("patent.json");
  379. List<PatentConfigVO> patentConfigVOS = JSON.parseArray(json, PatentConfigVO.class);
  380. EsPatentCommonVO esPatentCommonVO = esService.tranPatentRequestToComVO(queryRequest);
  381. esPatentCommonVO.setStartNum(startNum);
  382. esPatentCommonVO.setEndNum(toNum);
  383. Records firstRecords = this.queryPatents(esPatentCommonVO);
  384. Long total = 0l;
  385. if (startNum != null && endNum != null) {
  386. total = (endNum - startNum) + 1;
  387. if (notInNos != null) {
  388. total = total - notInNos.size();
  389. }
  390. if (inNos != null) {
  391. total += inNos.size();
  392. }
  393. } else if (inNos != null) {
  394. total = 0L + inNos.size();
  395. } else {
  396. firstRecords.getTotal();
  397. }
  398. this.updateTaskStateToDoing(taskId,total);
  399. List<Patent> firstPatentList = (List<Patent>) firstRecords.getData();
  400. List<String> firstPatentNos = firstPatentList.stream().map(Patent::getPatentNo).collect(Collectors.toList());
  401. //查询自定义字段
  402. esQueryPatentFieldsDTO.setPatentNos(firstPatentNos);
  403. List<EsPatentFieldsVO> firstEsPatentFieldsVOS = esCustomFieldService.getPatentFields(esQueryPatentFieldsDTO);
  404. for (int i = 0; i < firstPatentList.size(); i++) {
  405. Patent patent = firstPatentList.get(i);
  406. if (notInNos != null && notInNos.contains(patent.getPatentNo())) {
  407. continue;
  408. }
  409. this.loadPatentCell(patent, selectedFields, patentConfigVOS, firstEsPatentFieldsVOS, hssfSheet, commonCellStyle, index);
  410. index++;
  411. importTaskAMVO.setDoneNum(index);
  412. this.sendWebSocketMessage(projectId, taskId, createId, total.intValue(), index, false);
  413. }
  414. if (endNum == null) {
  415. endNum = total;
  416. }
  417. while (toNum < endNum) {
  418. startNum = toNum + 1;
  419. toNum = toNum + 10L;
  420. if (toNum > endNum) {
  421. toNum = endNum;
  422. }
  423. esPatentCommonVO.setStartNum(startNum);
  424. esPatentCommonVO.setEndNum(toNum);
  425. Records records = this.queryPatents(esPatentCommonVO);
  426. List<Patent> patentList = (List<Patent>) records.getData();
  427. List<String> patentNos = patentList.stream().map(Patent::getPatentNo).collect(Collectors.toList());
  428. ;
  429. //查询自定义字段
  430. esQueryPatentFieldsDTO.setPatentNos(patentNos);
  431. List<EsPatentFieldsVO> esPatentFieldsVOS = esCustomFieldService.getPatentFields(esQueryPatentFieldsDTO);
  432. for (int i = 0; i < patentList.size(); i++) {
  433. Patent patent = patentList.get(i);
  434. if (notInNos != null && notInNos.contains(patent.getPatentNo())) {
  435. continue;
  436. }
  437. this.loadPatentCell(patent, selectedFields, patentConfigVOS, esPatentFieldsVOS, hssfSheet, commonCellStyle, index);
  438. index++;
  439. importTaskAMVO.setDoneNum(index);
  440. this.sendWebSocketMessage(projectId, taskId, createId, total.intValue(), index, true);
  441. }
  442. }
  443. esQueryPatentFieldsDTO.setPatentNos(inNos);
  444. List<EsPatentFieldsVO> esPatentFieldsVOS = esCustomFieldService.getPatentFields(esQueryPatentFieldsDTO);
  445. List<Patent> patentList = esPatentService.getPatentsByNo(inNos, true, null, null);
  446. for (int i = 0; i < patentList.size(); i++) {
  447. Patent patent = patentList.get(i);
  448. if (notInNos != null && notInNos.contains(patent.getPatentNo())) {
  449. continue;
  450. }
  451. this.loadPatentCell(patent, selectedFields, patentConfigVOS, esPatentFieldsVOS, hssfSheet, commonCellStyle, index);
  452. index++;
  453. importTaskAMVO.setDoneNum(index);
  454. importTaskAMVO.setState(2);
  455. this.sendWebSocketMessage(projectId, taskId, createId, total.intValue(), index, false);
  456. }
  457. try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
  458. hssfWorkbook.write(out);
  459. String fileGuid = "";
  460. if (out.toByteArray() != null && out.toByteArray().length != 0) {
  461. fileGuid = parseByteToFileUtils.uploadFile(out.toByteArray(), 1);
  462. }
  463. exportTask.setFileGuid(fileGuid);
  464. exportTask.setState(2);
  465. Integer reTaskId = importTaskService.updateExportTask(exportTask);
  466. if (reTaskId == null) {
  467. throw new XiaoShiException("导出记录失败");
  468. }
  469. } catch (FileNotFoundException e) {
  470. e.printStackTrace();
  471. throw new FileNotFoundException();
  472. } catch (Exception e) {
  473. e.printStackTrace();
  474. }
  475. }
  476. public void sendWebSocketMessage(Integer projectId, Integer taskId, String createId, Integer total, Integer index, Boolean ifDone) {
  477. WebSocketMessageVO webSocketMessageVO = new WebSocketMessageVO();
  478. webSocketMessageVO.setProjectId(projectId);
  479. webSocketMessageVO.setCreateId(createId);
  480. if (ifDone) {
  481. webSocketMessageVO.setCode(603);
  482. } else {
  483. webSocketMessageVO.setCode(602);
  484. }
  485. webSocketMessageVO.setAllNum(total);
  486. webSocketMessageVO.setCurrentNum(index);
  487. webSocketMessageVO.setTaskId(taskId);
  488. if (ifDone) {
  489. webSocketMessageVO.setState(2);
  490. } else {
  491. webSocketMessageVO.setState(1);
  492. }
  493. messageService.sendPatentExportMessage(webSocketMessageVO);
  494. }
  495. public HSSFCellStyle getComCellStyle(HSSFWorkbook hssfWorkbook) {
  496. HSSFCellStyle commonCellStyle = hssfWorkbook.createCellStyle();
  497. ExcelUtils.setExcelCellStyle(commonCellStyle);
  498. commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
  499. commonCellStyle.setWrapText(true);
  500. return commonCellStyle;
  501. }
  502. public void loadPatentCell(Patent patent, List<PatentExportVO> patentExportVOS, List<PatentConfigVO> patentConfigVOS, List<EsPatentFieldsVO> esPatentFieldsVOs, HSSFSheet sheet, HSSFCellStyle commonCellStyle, Integer index) {
  503. HSSFRow row = sheet.createRow(index + 1);
  504. row.setHeight((short) 800);
  505. for (int j = 0; j < patentExportVOS.size(); j++) {
  506. String column = patentExportVOS.get(j).getValue();
  507. String name = patentExportVOS.get(j).getName();
  508. String type = patentExportVOS.get(j).getType();
  509. List<String> types = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
  510. if (types.contains(type)) {
  511. EsPatentFieldsVO esPatentFieldsVO = esPatentFieldsVOs.stream()
  512. .filter(item -> item.getPatentNo().equals(patent.getPatentNo()))
  513. .findFirst()
  514. .orElse(null);
  515. if (esPatentFieldsVO != null) {
  516. List<InnerPatentFieldsVO> innerClassFields = esPatentFieldsVO.getInnerClassFields();
  517. if (!innerClassFields.isEmpty()) {
  518. InnerPatentFieldsVO innerPatentFieldsVO = innerClassFields.stream()
  519. .filter(item -> item.getField().equals(column) && item.getFieldType().equals(Integer.parseInt(type)))
  520. .findFirst()
  521. .orElse(null);
  522. if (innerPatentFieldsVO != null) {
  523. List<FieldValueVO> fieldValueVOS = innerPatentFieldsVO.getFieldValueVOS();
  524. if (!fieldValueVOS.isEmpty()) {
  525. StringBuffer sb = new StringBuffer();
  526. for (FieldValueVO fieldValueVO : fieldValueVOS) {
  527. String valueStr = fieldValueVO.getValue();
  528. sb.append(valueStr);
  529. }
  530. HSSFCell cell = row.createCell(j);
  531. ExcelUtils.setExcelCellStyle(commonCellStyle);
  532. commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
  533. commonCellStyle.setWrapText(true);
  534. cell.setCellStyle(commonCellStyle);
  535. cell.setCellValue(sb.toString());
  536. }
  537. }
  538. }
  539. }
  540. } else {
  541. // 使用反射获取属性值
  542. Object value = GenerateObjectUtil.getPropertyValue(patent, column);
  543. if (value != null) {
  544. PatentConfigVO patentConfigVO = patentConfigVOS.stream().filter(item -> item.getValue().equals(column)).findFirst().orElse(null);
  545. String exportClass = patentConfigVO.getExportClass();
  546. GetValueImp getValueImp = patentExportFactory.getClass(exportClass);
  547. if (getValueImp != null) {
  548. String reValue = getValueImp.getValue(value);
  549. HSSFCell cell = row.createCell(j);
  550. cell.setCellStyle(commonCellStyle);
  551. cell.setCellValue(reValue);
  552. }
  553. }
  554. }
  555. }
  556. }
  557. }