|
@@ -0,0 +1,223 @@
|
|
|
+package cn.cslg.pas.service.business.es;
|
|
|
+
|
|
|
+import cn.cslg.pas.common.dto.ExportDTO;
|
|
|
+import cn.cslg.pas.common.utils.ExcelUtils;
|
|
|
+import cn.cslg.pas.common.utils.FileUtils;
|
|
|
+import cn.cslg.pas.common.utils.ReadExcelUtils;
|
|
|
+import cn.cslg.pas.common.utils.parseQueryToTree.expressManager;
|
|
|
+import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
|
|
|
+import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
|
|
|
+import cn.cslg.pas.common.vo.PatentData;
|
|
|
+import cn.cslg.pas.service.query.FormatQueryService;
|
|
|
+import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.apache.commons.io.output.ByteArrayOutputStream;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.poi.hssf.usermodel.*;
|
|
|
+import org.apache.poi.hssf.util.HSSFColor;
|
|
|
+import org.apache.poi.ss.usermodel.FillPatternType;
|
|
|
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.VerticalAlignment;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor(onConstructor_ = {@Lazy})
|
|
|
+public class EsExportService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FormatQueryService formatQueryService;
|
|
|
+
|
|
|
+ public void exportTree(File file) throws Exception {
|
|
|
+ Sheet sheet = ReadExcelUtils.readExcel(file);
|
|
|
+ int total = sheet.getPhysicalNumberOfRows() - 1;
|
|
|
+ List<ExportDTO> list = new ArrayList<>();
|
|
|
+ for (int i = 0; i < total; i++) {
|
|
|
+ PatentData patentData = ReadExcelUtils.readExcelOneRow(file, sheet, i + 1);
|
|
|
+ Map<Object, Object> patentDataMap = patentData.getMap();
|
|
|
+ String condition = patentDataMap.get("检索条件").toString();
|
|
|
+ String estimatedTree = patentDataMap.get("预期二叉树").toString();
|
|
|
+ String estimatedQuery = patentDataMap.get("预期Query").toString();
|
|
|
+ String outCondition = patentDataMap.get("外部检索条件").toString();
|
|
|
+ String estimatedOutQuery = patentDataMap.get("预期外部检索式").toString();
|
|
|
+ ExportDTO exportDTO = new ExportDTO();
|
|
|
+ exportDTO.setCondition(condition);
|
|
|
+ exportDTO.setOutCondition(outCondition);
|
|
|
+ if (StringUtils.isNotEmpty(estimatedTree)) {
|
|
|
+ exportDTO.setEstimatedTree(estimatedTree);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(estimatedQuery)) {
|
|
|
+ exportDTO.setEstimatedQuery(estimatedQuery);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(estimatedOutQuery)) {
|
|
|
+ exportDTO.setEstimatedOutQuery(estimatedOutQuery);
|
|
|
+ }
|
|
|
+ list.add(exportDTO);
|
|
|
+ }
|
|
|
+ for (ExportDTO exportDTO : list) {
|
|
|
+ if (StringUtils.isNotEmpty(exportDTO.getCondition())) {
|
|
|
+ treeNode tree = null;
|
|
|
+ try {
|
|
|
+ tree = expressManager.getInstance().Parse(exportDTO.getCondition(), false);
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(exportDTO.getCondition() + "转换二叉树表达式错误");
|
|
|
+ }
|
|
|
+ String treeJson = JSONObject.toJSONString(tree, SerializerFeature.DisableCircularReferenceDetect);
|
|
|
+ exportDTO.setActualTree(treeJson);
|
|
|
+ if (treeJson == null || exportDTO.getEstimatedTree() == null || !treeJson.equalsIgnoreCase(exportDTO.getEstimatedTree().trim())) {
|
|
|
+ exportDTO.setTreeFlag("F");
|
|
|
+ } else {
|
|
|
+ exportDTO.setTreeFlag("T");
|
|
|
+ }
|
|
|
+ Query query = null;
|
|
|
+ try {
|
|
|
+ query = formatQueryService.EsQueryToQuery((operateNode) tree, "patent", 331);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ System.out.println(exportDTO.getCondition() + "转换es表达式错误");
|
|
|
+ }
|
|
|
+ String queryStr = query.toString();
|
|
|
+ exportDTO.setActualQuery(queryStr);
|
|
|
+ if (queryStr == null || exportDTO.getEstimatedQuery() == null || !queryStr.equalsIgnoreCase(exportDTO.getEstimatedQuery().trim())) {
|
|
|
+ exportDTO.setQueryFlag("F");
|
|
|
+ } else {
|
|
|
+ exportDTO.setQueryFlag("T");
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(exportDTO.getOutCondition()) && !exportDTO.getOutCondition().contains("+")) {
|
|
|
+ String outQueryStr = null;
|
|
|
+ try {
|
|
|
+ outQueryStr = formatQueryService.reQuery(exportDTO.getOutCondition(), "webSearchConfig");
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(exportDTO.getOutCondition() + "转换外部接口检索表达式错误");
|
|
|
+ }
|
|
|
+ exportDTO.setActualOutQuery(outQueryStr);
|
|
|
+ if (outQueryStr == null || exportDTO.getEstimatedOutQuery() == null || !outQueryStr.equalsIgnoreCase(exportDTO.getEstimatedOutQuery().trim())) {
|
|
|
+ exportDTO.setOutQueryFlag("F");
|
|
|
+ } else {
|
|
|
+ exportDTO.setOutQueryFlag("T");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(list)) {
|
|
|
+ this.loadExportTree(list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void loadExportTree(List<ExportDTO> list) throws FileNotFoundException {
|
|
|
+ try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
|
|
+ List<String> headers = Arrays.asList("检索条件", "预期二叉树", "实际二叉树", "二叉树是否一致", "预期Query", "实际Query","Query是否一致",
|
|
|
+ "外部检索条件","预期外部检索式", "实际外部检索式","外部检索式是否一致");
|
|
|
+ HSSFWorkbook hssfWorkbook = new HSSFWorkbook();//新建工作簿
|
|
|
+ HSSFSheet sheet = hssfWorkbook.createSheet();//新建sheet页
|
|
|
+ sheet.setDefaultColumnWidth(30);
|
|
|
+ HSSFRow headerRow = sheet.createRow(0);//新建标头行
|
|
|
+ headerRow.setHeight((short) 500);
|
|
|
+ HSSFCellStyle headerCellStyle = hssfWorkbook.createCellStyle();//新建标头行格式
|
|
|
+ HSSFCellStyle commonCellStyle = hssfWorkbook.createCellStyle();//新建普通行格式
|
|
|
+ for (int i = 0; i < headers.size(); i++) {//遍历设置标头
|
|
|
+ HSSFCell cell = headerRow.createCell(i);
|
|
|
+ ExcelUtils.setExcelCellStyle(headerCellStyle);
|
|
|
+ headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ headerCellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.SKY_BLUE.getIndex());
|
|
|
+ headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
+ headerCellStyle.setWrapText(true);
|
|
|
+ cell.setCellStyle(headerCellStyle);
|
|
|
+ cell.setCellValue(headers.get(i));
|
|
|
+ }
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ ExportDTO exportDTO = list.get(i);
|
|
|
+ HSSFRow row = sheet.createRow(i + 1);//新建一普通行
|
|
|
+ row.setHeight((short) 800);
|
|
|
+ HSSFCell cell = row.createCell(0);
|
|
|
+ ExcelUtils.setExcelCellStyle(commonCellStyle);
|
|
|
+ commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
|
|
|
+ commonCellStyle.setWrapText(true);
|
|
|
+ cell.setCellStyle(commonCellStyle);
|
|
|
+ cell.setCellValue(exportDTO.getCondition());
|
|
|
+ HSSFCell cell1 = row.createCell(1);
|
|
|
+ ExcelUtils.setExcelCellStyle(commonCellStyle);
|
|
|
+ commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
|
|
|
+ commonCellStyle.setWrapText(true);
|
|
|
+ cell1.setCellStyle(commonCellStyle);
|
|
|
+ cell1.setCellValue(exportDTO.getEstimatedTree());
|
|
|
+ HSSFCell cell2 = row.createCell(2);
|
|
|
+ ExcelUtils.setExcelCellStyle(commonCellStyle);
|
|
|
+ commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
|
|
|
+ commonCellStyle.setWrapText(true);
|
|
|
+ cell2.setCellStyle(commonCellStyle);
|
|
|
+ cell2.setCellValue(exportDTO.getActualTree());
|
|
|
+ HSSFCell cell3 = row.createCell(3);
|
|
|
+ ExcelUtils.setExcelCellStyle(commonCellStyle);
|
|
|
+ commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
|
|
|
+ commonCellStyle.setWrapText(true);
|
|
|
+ cell3.setCellStyle(commonCellStyle);
|
|
|
+ cell3.setCellValue(exportDTO.getTreeFlag());
|
|
|
+ HSSFCell cell4 = row.createCell(4);
|
|
|
+ ExcelUtils.setExcelCellStyle(commonCellStyle);
|
|
|
+ commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
|
|
|
+ commonCellStyle.setWrapText(true);
|
|
|
+ cell4.setCellStyle(commonCellStyle);
|
|
|
+ cell4.setCellValue(exportDTO.getEstimatedQuery());
|
|
|
+ HSSFCell cell5 = row.createCell(5);
|
|
|
+ ExcelUtils.setExcelCellStyle(commonCellStyle);
|
|
|
+ commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
|
|
|
+ commonCellStyle.setWrapText(true);
|
|
|
+ cell5.setCellStyle(commonCellStyle);
|
|
|
+ cell5.setCellValue(exportDTO.getActualQuery());
|
|
|
+ HSSFCell cell6 = row.createCell(6);
|
|
|
+ ExcelUtils.setExcelCellStyle(commonCellStyle);
|
|
|
+ commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
|
|
|
+ commonCellStyle.setWrapText(true);
|
|
|
+ cell6.setCellStyle(commonCellStyle);
|
|
|
+ cell6.setCellValue(exportDTO.getQueryFlag());
|
|
|
+ HSSFCell cell7 = row.createCell(7);
|
|
|
+ ExcelUtils.setExcelCellStyle(commonCellStyle);
|
|
|
+ commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
|
|
|
+ commonCellStyle.setWrapText(true);
|
|
|
+ cell7.setCellStyle(commonCellStyle);
|
|
|
+ cell7.setCellValue(exportDTO.getOutCondition());
|
|
|
+ HSSFCell cell8 = row.createCell(8);
|
|
|
+ ExcelUtils.setExcelCellStyle(commonCellStyle);
|
|
|
+ commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
|
|
|
+ commonCellStyle.setWrapText(true);
|
|
|
+ cell8.setCellStyle(commonCellStyle);
|
|
|
+ cell8.setCellValue(exportDTO.getEstimatedOutQuery());
|
|
|
+ HSSFCell cell9 = row.createCell(9);
|
|
|
+ ExcelUtils.setExcelCellStyle(commonCellStyle);
|
|
|
+ commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
|
|
|
+ commonCellStyle.setWrapText(true);
|
|
|
+ cell9.setCellStyle(commonCellStyle);
|
|
|
+ cell9.setCellValue(exportDTO.getActualOutQuery());
|
|
|
+ HSSFCell cell10 = row.createCell(10);
|
|
|
+ ExcelUtils.setExcelCellStyle(commonCellStyle);
|
|
|
+ commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
|
|
|
+ commonCellStyle.setWrapText(true);
|
|
|
+ cell10.setCellStyle(commonCellStyle);
|
|
|
+ cell10.setCellValue(exportDTO.getOutQueryFlag());
|
|
|
+ }
|
|
|
+ hssfWorkbook.write(out);
|
|
|
+ byte[] bytes = out.toByteArray();
|
|
|
+ FileUtils.getFileByBytes(bytes, "检索条件", ".xlsx");
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ throw new FileNotFoundException();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|