杜洪波
2024-08-30 e27c43f254793a834f4713637494b088bdb34680
src/main/java/com/product/lucene/service/LuceneService.java
@@ -32,6 +32,8 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * 文件检索
@@ -118,13 +120,13 @@
   public JSONObject searchProduct(FieldSetEntity fse) throws Exception {
      // 刷新IndexSearcher实例
      searcherManager.maybeRefresh();
      // 获取安全的IndexSearcher实例
      // 创建IndexSearcher实例
      IndexSearcher indexSearcher = null;
      // 文档信息汇总
      List<JSONObject> jsonTotal = new ArrayList<>();
      List<JSONObject> listJsonFile = new ArrayList<>();
      Long totalCount = 0L;
      try {
         // 获取安全的IndexSearcher实例
         indexSearcher = searcherManager.acquire();
         
         // 获取条件查询结果
@@ -132,7 +134,6 @@
   
         // 获取高亮匹配器
         Highlighter highlighter = highlighterParse(finalQuery);
         
         // 分页查询文档
         TopDocs topDocs = searchByPage(fse.getInteger(CoreConst.CPAGE), fse.getInteger(CoreConst.PAGESIZE),
@@ -143,42 +144,42 @@
         for (int i = 0; i < hits.length; i++) {
            // 获取检索命中的文档信息
            Document doc = indexSearcher.doc(hits[i].doc);
            String fileUUID = doc.get(CoreConst.UUID);
            String functionUUID = doc.get(CoreConst.FUNCTION_UUID);
            String fileName = doc.get(CmnConst.FILE_NAME);
            String fileContent = doc.get(CmnConst.FILE_CONTENT);
            JSONObject json = new JSONObject();
            if (!StringUtils.isEmpty(functionUUID)) {
               getConfig(json, functionUUID, fileUUID);
            }
            json.put(CoreConst.FUNCTION_UUID, doc.get(CoreConst.FUNCTION_UUID));
            JSONObject jsonFile = new JSONObject();
            jsonFile.put(CmnConst.FILE_UUID, doc.get(CoreConst.UUID));
            jsonFile.put(CoreConst.FUNCTION_UUID, doc.get(CoreConst.FUNCTION_UUID));
   
            // 文件名增加高亮显示
            String fileName = doc.get(CmnConst.FILE_NAME);
            String file_name = highlighter.getBestFragment(new SmartChineseAnalyzer(), CmnConst.FILE_NAME,
                  CmnConst.FILE_NAME);
                  fileName);
            if (StringUtils.isEmpty(file_name)) {
               file_name = fileName;
            }
            // 文件内容增加高亮显示
            String fileContent = doc.get(CmnConst.FILE_CONTENT);
            String file_content = highlighter.getBestFragment(new SmartChineseAnalyzer(), CmnConst.FILE_CONTENT,
                  fileContent);
            if (StringUtils.isEmpty(file_content)) {
               file_content = fileContent;
            }
            json.put("file_uuid", fileUUID);
            json.put("function_uuid", functionUUID);
            json.put(CmnConst.FILE_NAME, file_name);
            json.put(CmnConst.FILE_CONTENT, file_content);
            jsonTotal.add(json);
            jsonFile.put(CmnConst.FILE_NAME, file_name);
            jsonFile.put(CmnConst.FILE_CONTENT, file_content);
            listJsonFile.add(jsonFile);
         }
      } finally {
         // 关闭实例资源
         searcherManager.release(indexSearcher);
      }
      // 创建返回信息对象
      JSONObject jsonReturn = new JSONObject();
      getFileInfo(jsonTotal);
      jsonReturn.put(CmnConst.TOTALCOUNT, totalCount);
      jsonReturn.put(CmnConst.DATA, jsonTotal);
      if (listJsonFile.size() > 0) {
         // 获取文档展示信息
         getFileInfo(listJsonFile);
         jsonReturn.put(CmnConst.DATA, listJsonFile);
      }
      return jsonReturn;
   }
   
@@ -243,7 +244,10 @@
      return highlighter;
   }
   /**
    *    获取文档展示信息
    * @param listJsonFile
    */
   public void getFileInfo(List<JSONObject> listJsonFile) {
        StringBuilder sql = new StringBuilder();
      for (int i = 0; i < listJsonFile.size(); i++) {
@@ -252,14 +256,16 @@
            sql.append("UNION ALL \n");
         }
         sql.append("SELECT \n");
         sql.append(" A.uuid,A.attachment_data_table,A.attachment_data_field,D.user_name AS upload_user,A.created_utc_datetime AS upload_date, \n");
         sql.append(" B.org_fields,B.user_fields,B.title_field,B.time_field \n");
         sql.append(" A.uuid,A.attachment_data_table,A.attachment_data_field,DATE_FORMAT(A.created_utc_datetime,'%Y-%m-%d') AS upload_date, \n");
         sql.append(" B.org_fields,B.user_fields,B.title_field,B.time_field, \n");
         sql.append(" C.uuid AS function_uuid,C.function_name,D.user_name AS upload_user ");
         sql.append("FROM product_sys_attachments A \n");
         sql.append("LEFT JOIN product_sys_document_search B ON B.table_name = A.attachment_data_table \n");
         sql.append("LEFT JOIN product_sys_functions C ON C.uuid = B.function_uuid \n");
         sql.append("LEFT JOIN product_sys_users D ON D.user_id = A.created_by \n");
         sql.append("WHERE A.uuid = '").append(jsonFile.get("file_uuid")).append("' \n");
      }
      // 获取文档附件、配置信息
      DataTableEntity dtConfig = baseDao.listTable(sql.toString(), new Object[] {});
      if (!BaseUtil.dataTableIsEmpty(dtConfig)) {
         StringBuilder sqlService = new StringBuilder();
@@ -271,31 +277,47 @@
            sqlService.append("SELECT \n");
            sqlService.append(" A.uuid, \n");
            sqlService.append(" B.uuid AS service_uuid, \n");
            sqlService.append(" A.upload_user, A.upload_date, \n");
            sqlService.append(" A.upload_user, A.upload_date, A.function_uuid, A.function_name, \n");
            sqlService.append(" ").append(fseConfig.getString(CmnConst.TITLE_FIELD)).append(" AS service_title, \n");
            sqlService.append(" ").append(fseConfig.getString(CmnConst.TIME_FIELD)).append(" AS service_time \n");
            sqlService.append("FROM ( \n");
            sqlService.append(" SELECT \n");
            sqlService.append("  '").append(fseConfig.getString(CoreConst.UUID)).append("' AS uuid, \n");
            sqlService.append("  '").append(fseConfig.getString(CmnConst.UPLOAD_USER)).append("' AS upload_user, \n");
            sqlService.append("  '").append(fseConfig.getString(CmnConst.UPLOAD_DATE)).append("' AS upload_date \n");
            sqlService.append("  '").append(fseConfig.getString(CmnConst.UPLOAD_DATE)).append("' AS upload_date, \n");
            sqlService.append("  '").append(fseConfig.getString(CoreConst.FUNCTION_UUID)).append("' AS function_uuid, \n");
            sqlService.append("  '").append(fseConfig.getString(CoreConst.FUNCTION_NAME)).append("' AS function_name \n");
            sqlService.append(") A \n");
            sqlService.append("LEFT JOIN ").append(fseConfig.getString("attachment_data_table")).append(" B \n");
            sqlService.append("LEFT JOIN ").append(fseConfig.getString(CmnConst.ATTACHMENT_DATA_TABLE)).append(" B \n");
            sqlService.append("ON B.").append(fseConfig.getString(CmnConst.ATTACHMENT_DATA_FIELD)).append(" LIKE '%").append(fseConfig.getUUID()).append("%' \n");
         }
         // 获取文档业务数据信息
         DataTableEntity dtService = baseDao.listTable(sqlService.toString(), new Object[] {});
         if (!BaseUtil.dataTableIsEmpty(dtService)) {
            Map<String, FieldSetEntity> map = dtService.getData().stream()
                      .collect(Collectors.toMap(
                              fseService -> fseService.getUUID(),
                              fseService -> fseService
                      ));
            for (int i = 0; i < listJsonFile.size(); i++) {
               JSONObject jsonFile = listJsonFile.get(i);
               FieldSetEntity fseService = map.get(jsonFile.get(CmnConst.FILE_UUID));
               if (fseService != null) {
                  jsonFile.put(CoreConst.FUNCTION_NAME, fseService.getString(CoreConst.FUNCTION_NAME));
                  jsonFile.put(CoreConst.FUNCTION_UUID, fseService.getString(CoreConst.FUNCTION_UUID));
                  jsonFile.put(CmnConst.UPLOAD_USER, fseService.getString(CmnConst.UPLOAD_USER));
                  jsonFile.put(CmnConst.UPLOAD_DATE, fseService.getString(CmnConst.UPLOAD_DATE));
                  jsonFile.put(CmnConst.SERVICE_UUID, fseService.getString(CmnConst.SERVICE_UUID));
                  jsonFile.put(CmnConst.SERVICE_TITLE, fseService.getString(CmnConst.SERVICE_TITLE));
                  jsonFile.put(CmnConst.SERVICE_TIME, fseService.getString(CmnConst.SERVICE_TIME));
               }
            }
         }
      }
   }
   
   /**
    * 获取文档检索配置信息(权限,功能名称,跳转按钮)
    *
    * @param json          检索文件对应信息
    * @param function_uuid 所属功能UUID
    * @param service_uuid  附件UUID
    */
   public void getConfig(JSONObject json, String function_uuid, String uuid) {
   public JSONObject permission(String uuid, String function_uuid) {
      JSONObject json = new JSONObject();
      StringBuilder searchConfigSql = new StringBuilder();
      searchConfigSql.append("SELECT \n");
      searchConfigSql.append(" A.uuid,A.function_uuid,A.function_button_uuid,A.table_name, \n");
@@ -307,15 +329,17 @@
      searchConfigSql.append("WHERE A.function_uuid=?");
      // 获取文档检索配置(功能,跳转按钮)
      FieldSetEntity fseConfig = baseDao.getFieldSetEntityBySQL(searchConfigSql.toString(), new Object[] { function_uuid },
            false);
      FieldSetEntity fseConfig = baseDao.getFieldSetEntityBySQL(searchConfigSql.toString(), new Object[] { function_uuid }, false);
      if (fseConfig != null) {
         // 获取跳转按钮 并 判断跳转权限
         String buttonName = fseConfig.getString(CoreConst.BUTTON_NAME);
         if (BaseUtil.strIsNull(buttonName)) {
            json.put(CmnConst.IS_PERSSION, 2);
            json.put(CmnConst.IS_PERSSION, 1);
            json.put("permission_msg", "数据配置-该文档配置不支持跳转");
            return json;
         } else {
            json.put(CmnConst.IS_PERSSION, 0);
            json.put(CmnConst.SKIPBUTTON, fseConfig.getString(CoreConst.BUTTON_NAME));
         }
@@ -325,7 +349,6 @@
         // 获取附件信息,结合数据权限字段,生成原数据的过滤SQL
         FieldSetEntity fseAttachment = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false);
         baseDao.loadPromptData(fseAttachment);
         if (fseAttachment != null) {
            StringBuilder filter = new StringBuilder();
            filter.append(fseAttachment.getString(CmnConst.ATTACHMENT_DATA_FIELD));
@@ -358,13 +381,11 @@
                     fseAttachment.getString(CmnConst.ATTACHMENT_DATA_FIELD) + "=?", new Object[] { uuid },
                     false);
               if (fseService2 == null) {
                  json.put(CmnConst.TITLE, "业务数据不存在");
                  json.put(CmnConst.IS_PERSSION, 1);
                  json.put("permission_msg", "数据异常-业务数据不存在");
                  json.put(CmnConst.IS_PERSSION, 2);
               } else {
                  json.put(CmnConst.SERVICE_UUID, fseService2.getString(CoreConst.UUID));
                  json.put(CmnConst.DATE_TIME, fseService2.getString(fseConfig.getString(CmnConst.TIME_FIELD)));
                  json.put(CmnConst.TITLE, fseService2.getString(fseConfig.getString(CmnConst.TITLE_FIELD)));
                  json.put(CmnConst.IS_PERSSION, 0);
                  json.put("permission_msg", "数据策略-此文档您没有查看权限");
                  json.put(CmnConst.IS_PERSSION, 3);
               }
            } else {
@@ -373,7 +394,8 @@
               if ("e4fa2c88-88a4-4ef4-9020-ebbe0440b4cf".equals(function_uuid)) {
                  if (!SpringMVCContextHolder.getCurrentUserId()
                        .equals(fseService.getString(CoreConst.CREATED_BY))) {
                     json.put(CmnConst.IS_PERSSION, 0);
                     json.put("permission_msg", "文档权限策略-此文档您没有查看权限");
                     json.put(CmnConst.IS_PERSSION, 4);
                  }
               } else if ("01513ff6-c758-4384-b861-e58dfe146fdd".equals(function_uuid)) {
                  // 获取单位文档所属目录权限
@@ -381,24 +403,22 @@
                        CmnConst.PRODUCT_OA_DIRECTORY_RIGHTS, "directory_uuid=?",
                        new Object[] { fseService.getString(CmnConst.DIRECTORY_UUID) }, false);
                  if (fseDirectRight == null) {
                     json.put(CmnConst.IS_PERSSION, 0);
                     json.put("permission_msg", "文档权限策略-此文档您没有查看权限");
                     json.put(CmnConst.IS_PERSSION, 4);
                  } else {
                     boolean succ = BaseUtil.multipleTypeConcat(fseDirectRight.getString(CmnConst.STORAGE_UUID),
                           SpringMVCContextHolder.getCurrentUserId());
                     if (!succ) {
                        json.put(CmnConst.IS_PERSSION, 0);
                        json.put("permission_msg", "文档权限策略-此文档您没有查看权限");
                        json.put(CmnConst.IS_PERSSION, 4);
                     }
                  }
               }
               json.put(CmnConst.SERVICE_UUID, fseService.getString(CoreConst.UUID));
               json.put(CmnConst.DATE_TIME, fseService.getString(fseConfig.getString(CmnConst.TIME_FIELD)));
               json.put(CmnConst.TITLE, fseService.getString(fseConfig.getString(CmnConst.TITLE_FIELD)));
            }
            json.put(CoreConst.FUNCTION_NAME, fseConfig.getString(CoreConst.FUNCTION_NAME));
            json.put(CoreConst.CREATED_BY, fseAttachment.getString(CoreConst.CREATED_BY));
         }
      }
   }
      return json;
   }
   /**
    * 文件检索查询分页