From d334dfae15726489656d582c8f9356ef02d0686a Mon Sep 17 00:00:00 2001 From: 许鹏程 <1821349743@qq.com> Date: 星期一, 20 一月 2025 13:47:54 +0800 Subject: [PATCH] fix:修复文件检索找不到功能,无法跳转的错误 --- src/main/java/com/product/lucene/service/LuceneService.java | 213 +++++++++++++++++++++++++++++++--------------------- 1 files changed, 126 insertions(+), 87 deletions(-) diff --git a/src/main/java/com/product/lucene/service/LuceneService.java b/src/main/java/com/product/lucene/service/LuceneService.java index f041b92..49597bd 100644 --- a/src/main/java/com/product/lucene/service/LuceneService.java +++ b/src/main/java/com/product/lucene/service/LuceneService.java @@ -12,6 +12,7 @@ import com.product.lucene.util.FileUtils; import com.product.util.BaseUtil; +import com.product.util.UnifySQLSymbol; import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer; import org.apache.lucene.document.*; import org.apache.lucene.index.IndexWriter; @@ -32,6 +33,9 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; /** * 鏂囦欢妫�绱� @@ -50,11 +54,11 @@ @Autowired private SearcherManager searcherManager; - + /** * 鍒涘缓绱㈠紩 - * + * * @param fse * @throws IOException */ @@ -63,7 +67,7 @@ File file = (File) fse.getObject("file"); // 鑾峰彇闄勪欢 // 鑾峰彇鍏ㄦ枃妫�绱㈤厤缃� - FieldSetEntity fseConfig = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_DOCUMENT_SEARCH, "function_uuid=?", new Object[] { function_uuid }, false); + FieldSetEntity fseConfig = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_DOCUMENT_SEARCH, "function_uuid=?", new Object[]{function_uuid}, false); if (fseConfig == null) return; String table_uuid = fseConfig.getString(CmnConst.TABLE_NAME); @@ -109,7 +113,7 @@ /** * 鏂囨。妫�绱� - * + * * @param fse * @return * @throws Exception @@ -118,22 +122,21 @@ public JSONObject searchProduct(FieldSetEntity fse) throws Exception { // 鍒锋柊IndexSearcher瀹炰緥 searcherManager.maybeRefresh(); - // 鑾峰彇瀹夊叏鐨処ndexSearcher瀹炰緥 + // 鍒涘缓IndexSearcher瀹炰緥 IndexSearcher indexSearcher = null; // 鏂囨。淇℃伅姹囨�� - List<JSONObject> jsonTotal = new ArrayList<>(); + List<JSONObject> listJsonFile = new ArrayList<>(); Long totalCount = 0L; try { - + // 鑾峰彇瀹夊叏鐨処ndexSearcher瀹炰緥 indexSearcher = searcherManager.acquire(); - + // 鑾峰彇鏉′欢鏌ヨ缁撴灉 Query finalQuery = searchFilterParse(fse); - + // 鑾峰彇楂樹寒鍖归厤鍣� Highlighter highlighter = highlighterParse(finalQuery); - - + // 鍒嗛〉鏌ヨ鏂囨。 TopDocs topDocs = searchByPage(fse.getInteger(CoreConst.CPAGE), fse.getInteger(CoreConst.PAGESIZE), indexSearcher, finalQuery); @@ -143,52 +146,72 @@ 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); + + JSONObject jsonFile = new JSONObject(); + jsonFile.put(CmnConst.FILE_UUID, doc.get(CoreConst.UUID)); + jsonFile.put(CoreConst.FUNCTION_UUID, doc.get(CoreConst.FUNCTION_UUID)); + + if (!StringUtils.isEmpty(doc.get(CoreConst.FUNCTION_UUID))) { + FieldSetEntity fs = BaseUtil.getSingleInfoByCache("鎵�鏈夊姛鑳�", new String[]{jsonFile.getString(CoreConst.FUNCTION_UUID)}); + if (FieldSetEntity.isEmpty(fs)) { + //鎵句笉鍒板姛鑳介噸鏂版牴鎹〃鍚嶆煡鎵� + //娓呯┖ + jsonFile.remove(CoreConst.FUNCTION_UUID); + } } - json.put(CoreConst.FUNCTION_UUID, doc.get(CoreConst.FUNCTION_UUID)); - + + if (StringUtils.isEmpty(jsonFile.get(CoreConst.FUNCTION_UUID))) { + //鏌ヨ鍔熻兘淇℃伅 + String sql = "SELECT * FROM product_sys_functions where table_uuid =(select uuid from product_sys_datamodel_table where table_name=?) limit 1"; + FieldSetEntity fseFunction = baseDao.getFieldSetBySQL(sql, new String[]{doc.get(CmnConst.TABLE_NAME)}, false); + if (fseFunction != null) { + jsonFile.put(CoreConst.FUNCTION_UUID, fseFunction.getString(CoreConst.UUID)); + //鍥炲啓鍔熻兘淇℃伅鍒版枃妗d俊鎭腑 + doc.add(new StoredField(CoreConst.FUNCTION_UUID, fseFunction.getString(CoreConst.UUID))); + indexWriter.updateDocument(new Term(CoreConst.UUID, doc.get(CoreConst.UUID)), doc); + } + } // 鏂囦欢鍚嶅鍔犻珮浜樉绀� + 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; } - + /** * 瑙f瀽鏌ヨ鏉′欢 * @param fse * @return * @throws ParseException */ - public Query searchFilterParse(FieldSetEntity fse) throws ParseException { + public Query searchFilterParse(FieldSetEntity fse) throws ParseException { // 涓婁紶浜� String uploadUser = fse.getString("upload_user"); String startDate = fse.getString("start_date"); @@ -197,7 +220,7 @@ // 鍒涘缓鏌ヨ鏉′欢 BooleanQuery.Builder boolQuery = new BooleanQuery.Builder(); - + // 涓婁紶浜鸿繃婊� if (!BaseUtil.strIsNull(uploadUser)) { Query uploaderQuery = new TermQuery(new Term("upload_user", uploadUser)); @@ -220,13 +243,13 @@ // 鍏抽敭璇嶈繃婊� if (!BaseUtil.strIsNull(searchKey)) { QueryParser queryParser = new MultiFieldQueryParser( - new String[] { CmnConst.FILE_NAME, CmnConst.FILE_CONTENT }, new SmartChineseAnalyzer()); + new String[]{CmnConst.FILE_NAME, CmnConst.FILE_CONTENT}, new SmartChineseAnalyzer()); Query query = queryParser.parse(searchKey); boolQuery.add(query, BooleanClause.Occur.MUST); } return boolQuery.build(); } - + /** * 鏌ヨ鏂囦欢楂樹寒鍖归厤鍣� * @param finalQuery @@ -235,7 +258,7 @@ public Highlighter highlighterParse(Query finalQuery) { // 楂樹寒鏍煎紡 SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter(CmnConst.HIGHLIGHT_BEFORE, - CmnConst.HIGHLIGHT_AFTER); + CmnConst.HIGHLIGHT_AFTER); // 楂樹寒鍖归厤鍣� Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(finalQuery)); Fragmenter fragmenter = new SimpleFragmenter(100); @@ -243,24 +266,26 @@ return highlighter; } - + /** + * 鑾峰彇鏂囨。灞曠ず淇℃伅 + * @param listJsonFile + */ public void getFileInfo(List<JSONObject> listJsonFile) { - StringBuilder sql = new StringBuilder(); - for (int i = 0; i < listJsonFile.size(); i++) { - JSONObject jsonFile = listJsonFile.get(i); - if (i > 0) { - 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("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[] {}); + StringBuilder sql = new StringBuilder(); + Set<String> fileUuids = listJsonFile.stream().map(jsonFile -> jsonFile.getString(CmnConst.FILE_UUID)).collect(Collectors.toSet()); + + sql.append("SELECT \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(" JOIN product_sys_datamodel_table as " + UnifySQLSymbol.getSymbol("table") + " on A.attachment_data_table=" + UnifySQLSymbol.getSymbol("table") + ".table_name \n"); + sql.append("LEFT JOIN product_sys_document_search B ON B.table_name = " + UnifySQLSymbol.getSymbol("table") + ".uuid \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 ").append(BaseUtil.buildQuestionMarkFilter("A.uuid", fileUuids.size(), true)); + // 鑾峰彇鏂囨。闄勪欢銆侀厤缃俊鎭� + DataTableEntity dtConfig = baseDao.listTable(sql.toString(), fileUuids.toArray()); if (!BaseUtil.dataTableIsEmpty(dtConfig)) { StringBuilder sqlService = new StringBuilder(); for (int i = 0; i < dtConfig.getRows(); i++) { @@ -271,31 +296,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[] {}); + // 鑾峰彇鏂囨。涓氬姟鏁版嵁淇℃伅 + 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 鎵�灞炲姛鑳経UID - * @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 +348,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", "鏁版嵁閰嶇疆-璇ユ枃妗i厤缃笉鏀寔璺宠浆"); + return json; } else { + json.put(CmnConst.IS_PERSSION, 0); json.put(CmnConst.SKIPBUTTON, fseConfig.getString(CoreConst.BUTTON_NAME)); } @@ -325,7 +368,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)); @@ -350,21 +392,19 @@ // 鑾峰彇鍘熸暟鎹� FieldSetEntity fseService = baseDao.getFieldSetEntityByFilter( fseAttachment.getString(CmnConst.ATTACHMENT_DATA_TABLE), filter.toString(), - new Object[] { uuid }, false); + new Object[]{uuid}, false); if (fseService == null) { // 鑾峰彇鏃犳潈闄愯繃婊ょ殑鍘熸暟鎹� FieldSetEntity fseService2 = baseDao.getFieldSetEntityByFilter( fseAttachment.getString(CmnConst.ATTACHMENT_DATA_TABLE), - fseAttachment.getString(CmnConst.ATTACHMENT_DATA_FIELD) + "=?", new Object[] { uuid }, + 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", "鏁版嵁绛栫暐-姝ゆ枃妗f偍娌℃湁鏌ョ湅鏉冮檺"); + json.put(CmnConst.IS_PERSSION, 3); } } else { @@ -373,36 +413,35 @@ 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", "鏂囨。鏉冮檺绛栫暐-姝ゆ枃妗f偍娌℃湁鏌ョ湅鏉冮檺"); + json.put(CmnConst.IS_PERSSION, 4); } } else if ("01513ff6-c758-4384-b861-e58dfe146fdd".equals(function_uuid)) { // 鑾峰彇鍗曚綅鏂囨。鎵�灞炵洰褰曟潈闄� FieldSetEntity fseDirectRight = baseDao.getFieldSetByFilter( CmnConst.PRODUCT_OA_DIRECTORY_RIGHTS, "directory_uuid=?", - new Object[] { fseService.getString(CmnConst.DIRECTORY_UUID) }, false); + new Object[]{fseService.getString(CmnConst.DIRECTORY_UUID)}, false); if (fseDirectRight == null) { - json.put(CmnConst.IS_PERSSION, 0); + json.put("permission_msg", "鏂囨。鏉冮檺绛栫暐-姝ゆ枃妗f偍娌℃湁鏌ョ湅鏉冮檺"); + 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", "鏂囨。鏉冮檺绛栫暐-姝ゆ枃妗f偍娌℃湁鏌ョ湅鏉冮檺"); + 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; } /** * 鏂囦欢妫�绱㈡煡璇㈠垎椤� - * + * * @param cpage 褰撳墠椤� * @param pagesize 姣忛〉澶у皬 * @param searcher 鏌ヨ鍣� @@ -430,7 +469,7 @@ /** * 鏍规嵁闄勪欢uuid鍒犻櫎绱㈠紩 - * + * * @param uuid * @throws IOException */ -- Gitblit v1.9.2