From 7b35e72c742333924e785d64ee01f6485fe05546 Mon Sep 17 00:00:00 2001
From: 354798ggg <1074825718@qq.com>
Date: 星期四, 01 六月 2023 14:41:36 +0800
Subject: [PATCH] 文档检索逻辑优化和文档管理权限处理

---
 src/main/java/com/product/lucene/service/LuceneService.java |  206 +++++++++++++++++++++++----------------------------
 1 files changed, 92 insertions(+), 114 deletions(-)

diff --git a/src/main/java/com/product/lucene/service/LuceneService.java b/src/main/java/com/product/lucene/service/LuceneService.java
index ef6a94a..fc77c7b 100644
--- a/src/main/java/com/product/lucene/service/LuceneService.java
+++ b/src/main/java/com/product/lucene/service/LuceneService.java
@@ -2,13 +2,15 @@
 
 import com.alibaba.fastjson.JSONObject;
 import com.product.common.lang.StringUtils;
+import com.product.core.config.CoreConst;
 import com.product.core.dao.BaseDao;
 import com.product.core.entity.FieldSetEntity;
 import com.product.core.permission.PermissionService;
+import com.product.core.spring.context.SpringMVCContextHolder;
+import com.product.lucene.config.CmnConst;
 import com.product.lucene.util.FileUtils;
 import com.product.util.BaseUtil;
 
-import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
 import org.apache.lucene.document.*;
 import org.apache.lucene.index.IndexWriter;
@@ -54,20 +56,20 @@
      * @throws IOException
      */
     public void createdIndex(FieldSetEntity fse) {
-    	String function_uuid=fse.getString("function_uuid");	//鑾峰彇鍔熻兘
+    	String function_uuid=fse.getString(CoreConst.FUNCTION_UUID);	//鑾峰彇鍔熻兘
 		File file=(File)fse.getObject("file");	//鑾峰彇闄勪欢
 		
     	//鑾峰彇鍏ㄦ枃妫�绱㈤厤缃�
-    	FieldSetEntity fseConfig=baseDao.getFieldSetEntityByFilter("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) {
-			String table_uuid=fseConfig.getString("table_name");
+			String table_uuid=fseConfig.getString(CmnConst.TABLE_NAME);
 			
 			//鑾峰彇缂撳瓨琛ㄩ厤缃�
-			FieldSetEntity fseTable=baseDao.getFieldSetEntity("product_sys_datamodel_table", table_uuid, false);
+			FieldSetEntity fseTable=baseDao.getFieldSetEntity(CoreConst.PRODUCT_SYS_DATAMODEL_TABLE, table_uuid, false);
 			if (fseTable!=null) {
 				
 				//鑾峰彇闄勪欢uuids
-				String attachments=fse.getString("attachment_uuid");
+				String attachments=fse.getString(CmnConst.ATTACHMENT_UUID);
 				if (!StringUtils.isEmpty(attachments)) {
 					
 					//閬嶅巻鑾峰彇闄勪欢淇℃伅
@@ -76,16 +78,16 @@
 					for (int i = 0; i < attachment.length; i++) {
 						
 						//鑾峰彇闄勪欢淇℃伅
-						FieldSetEntity fseAttachment=baseDao.getFieldSetEntity("product_sys_attachments", attachment[i], false);
+						FieldSetEntity fseAttachment=baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, attachment[i], false);
 						if (fseAttachment!=null) {
 							
 							if (file!=null) {
 								//鐢熸垚绱㈠紩淇℃伅
 								Document doc=new Document();
-								doc.add(new StringField("uuid", attachment[i], Field.Store.YES));	//闄勪欢UUID
-								doc.add(new TextField("function_uuid", function_uuid, Field.Store.YES));	//鍔熻兘UUID
-								doc.add(new TextField("file_name", fseAttachment.getString("file_name"), Field.Store.YES));	//鏂囦欢鍚�
-								doc.add(new TextField("file_content", FileUtils.FileToString(file), Field.Store.YES));	//鏂囦欢鍐呭唴瀹�
+								doc.add(new StringField(CoreConst.UUID, attachment[i], Field.Store.YES));	//闄勪欢UUID
+								doc.add(new TextField(CoreConst.FUNCTION_UUID, function_uuid, Field.Store.YES));	//鍔熻兘UUID
+								doc.add(new TextField(CmnConst.FILE_NAME, fseAttachment.getString(CmnConst.FILE_NAME), Field.Store.YES));	//鏂囦欢鍚�
+								doc.add(new TextField(CmnConst.FILE_CONTENT, FileUtils.FileToString(file), Field.Store.YES));	//鏂囦欢鍐呭唴瀹�
 								docs.add(doc);
 							}
 						}
@@ -95,7 +97,6 @@
 						indexWriter.addDocuments(docs);
 						indexWriter.commit();
 					} catch (IOException e) {
-						// TODO Auto-generated catch block
 						e.printStackTrace();
 					}
 				}
@@ -104,62 +105,6 @@
     	file.delete();
     }
     
-    
-/*    
-    public void createdIndex(FieldSetEntity fse) throws IOException {
-    	String function_uuid=fse.getString("function_uuid");
-    	String service_uuid=fse.getString("service_uuid");
-    	
-    	//鑾峰彇鍏ㄦ枃妫�绱㈤厤缃�
-    	FieldSetEntity fseConfig=baseDao.getFieldSetEntityByFilter("product_sys_document_search", "function_uuid=?", new Object[] {function_uuid}, false);
-    	if (fseConfig!=null) {
-			String table_uuid=fseConfig.getString("table_name");
-			String attachment_field=fseConfig.getString("attachment_file");
-			
-			//鑾峰彇缂撳瓨琛ㄩ厤缃�
-			FieldSetEntity fseTable=baseDao.getFieldSetEntity("product_sys_datamodel_table", table_uuid, false);
-			if (fseTable!=null) {
-				
-				//鑾峰彇鍘熸暟鎹�
-				FieldSetEntity fseService=baseDao.getFieldSetEntityBySQL("SELECT * FROM "+fseTable.getString("table_name")+" WHERE uuid=?", new Object[] {service_uuid}, false);
-				if (fseService!=null) {
-					
-					//鑾峰彇闄勪欢uuids
-					String attachments=fseService.getString(attachment_field);
-					if (!StringUtils.isEmpty(attachments)) {
-						
-						//閬嶅巻鑾峰彇闄勪欢淇℃伅
-						String [] attachment=attachments.split(",");
-						List<Document> docs=new ArrayList<>();
-						for (int i = 0; i < attachment.length; i++) {
-							
-							//鑾峰彇闄勪欢淇℃伅
-							FieldSetEntity fseAttachment=baseDao.getFieldSetEntity("product_sys_attachments", attachment[i], false);
-							if (fseAttachment!=null) {
-								//鑾峰彇闄勪欢
-								File file=fileManagerService.getFile(attachment[i]);
-								if (file!=null) {
-									//鐢熸垚绱㈠紩淇℃伅
-									Document doc=new Document();
-									doc.add(new StringField("uuid", attachment[i], Field.Store.YES));	//闄勪欢UUID
-									doc.add(new TextField("service_uuid", service_uuid, Field.Store.YES));	//涓氬姟UUID
-									doc.add(new TextField("function_uuid", function_uuid, Field.Store.YES));	//鍏卞棷閭d釜UUID
-									doc.add(new TextField("file_name", fseAttachment.getString("file_name"), Field.Store.YES));	//鏂囦欢鍚�
-									doc.add(new TextField("file_content", FileUtils.FileToString(file), Field.Store.YES));	//鏂囦欢鍐呭唴瀹�
-									docs.add(doc);
-								}
-								file.delete();
-							}
-						}
-						//鍐欏叆绱㈠紩
-						indexWriter.addDocuments(docs);
-						indexWriter.commit();
-					}
-				}
-			}
-		}
-    }
-*/    
     
     /**
      * 	鏂囨。妫�绱�
@@ -177,81 +122,98 @@
         JSONObject jsonReturn=new JSONObject();
         
         // 妯$硦鍖归厤,鍖归厤璇�
-        String search_key=fse.getString("search_key");
+        String search_key=fse.getString(CmnConst.SEARCH_KEY);
         if (!BaseUtil.strIsNull(search_key)) {
         	//鏌ヨ瑙f瀽鍣�
-            QueryParser parser = new MultiFieldQueryParser(new String [] {"file_name","file_content"}, new SmartChineseAnalyzer());
+            QueryParser parser = new MultiFieldQueryParser(new String [] {CmnConst.FILE_NAME,CmnConst.FILE_CONTENT}, new SmartChineseAnalyzer());
             Query query = parser.parse(search_key);
             
             //楂樹寒鏍煎紡
-            SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<span style='color:red'>", "</span>");
+            SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter(CmnConst.HIGHLIGHT_BEFORE, CmnConst.HIGHLIGHT_AFTER);
             //楂樹寒鍖归厤鍣�
             Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query));
             Fragmenter fragmenter = new SimpleFragmenter(100);
             highlighter.setTextFragmenter(fragmenter);
             
             //鍒嗛〉鏌ヨ鏂囨。
-            TopDocs topDocs = searchByPage(fse.getInteger("cpage"), fse.getInteger("pagesize"), indexSearcher, query);
-            
-            
+            TopDocs topDocs = searchByPage(fse.getInteger(CoreConst.CPAGE), fse.getInteger(CoreConst.PAGESIZE), indexSearcher, query);
             
             ScoreDoc[] hits = topDocs.scoreDocs;
             for (int i = 0; i < hits.length; i++) {
                 Document doc = indexSearcher.doc(hits[i].doc);
                 JSONObject json=new JSONObject();
-                if (!StringUtils.isEmpty(doc.get("function_uuid"))) {
-                	getConfig(json, doc.get("function_uuid"), doc.get("uuid"));
+                if (!StringUtils.isEmpty(doc.get(CoreConst.FUNCTION_UUID))) {
+                	getConfig(json, doc.get(CoreConst.FUNCTION_UUID), doc.get(CoreConst.UUID));
     			}
-                json.put("function_uuid", doc.get("function_uuid"));
+                json.put(CoreConst.FUNCTION_UUID, doc.get(CoreConst.FUNCTION_UUID));
                 
                 // 鍐呭澧炲姞楂樹寒鏄剧ず
-                String file_name=highlighter.getBestFragment(new SmartChineseAnalyzer(), "file_name",doc.get("file_name"));
+                String file_name=highlighter.getBestFragment(new SmartChineseAnalyzer(), CmnConst.FILE_NAME,doc.get(CmnConst.FILE_NAME));
                 if (StringUtils.isEmpty(file_name)) {
-                	file_name=doc.get("file_name");
+                	file_name=doc.get(CmnConst.FILE_NAME);
     			}
-                String file_content=highlighter.getBestFragment(new SmartChineseAnalyzer(), "file_content",doc.get("file_content"));
+                String file_content=highlighter.getBestFragment(new SmartChineseAnalyzer(), CmnConst.FILE_CONTENT,doc.get(CmnConst.FILE_CONTENT));
                 if (StringUtils.isEmpty(file_content)) {
-    				file_content=doc.get("file_content");
+    				file_content=doc.get(CmnConst.FILE_CONTENT);
     			}
-                json.put("file_name", file_name);
-                json.put("file_content", file_content);
+                json.put(CmnConst.FILE_NAME, file_name);
+                json.put(CmnConst.FILE_CONTENT, file_content);
                 jsonTotal.add(json);
             }
             
-            jsonReturn.put("totalCount", topDocs.totalHits.value);
+            jsonReturn.put(CmnConst.TOTALCOUNT, topDocs.totalHits.value);
 		}
 
-        jsonReturn.put("data", jsonTotal);
+        jsonReturn.put(CmnConst.DATA, jsonTotal);
         return jsonReturn;
     }
     
     /**
      * 	鑾峰彇鏂囨。妫�绱㈤厤缃俊鎭紙鏉冮檺锛屽姛鑳藉悕绉帮紝璺宠浆鎸夐挳锛�
-     * @param json
-     * @param function_uuid
-     * @param service_uuid
+     * 	
+     * @param json			妫�绱㈡枃浠跺搴斾俊鎭�
+     * @param function_uuid	鎵�灞炲姛鑳経UID
+     * @param service_uuid	闄勪欢UUID
      */
     public void getConfig(JSONObject json, String function_uuid, String uuid) {
     	
-    	FieldSetEntity fseConfig=baseDao.getFieldSetEntityByFilter("product_sys_document_search", "function_uuid=?", new Object[] {function_uuid}, false);
+    	StringBuilder searchConfigSql = new StringBuilder();
+    	searchConfigSql.append("SELECT ")
+    	.append(" A.uuid,A.function_uuid,A.function_button_uuid,A.table_name,")
+    	.append(" A.attachment_file,A.org_fields,A.user_fields,title_field,time_field,")
+    	.append(" B.function_name,C.button_name")
+    	.append(" FROM product_sys_document_search A")
+    	.append(" LEFT JOIN product_sys_functions B ON A.function_uuid = B.uuid")
+    	.append(" LEFT JOIN product_sys_function_buttons C ON A.function_button_uuid = C.uuid")
+    	.append(" WHERE function_uuid=?");
+    	
+    	//鑾峰彇鏂囨。妫�绱㈤厤缃�
+    	FieldSetEntity fseConfig = baseDao.getFieldSetBySQL(searchConfigSql.toString(), new Object[] {function_uuid}, false);
     	if (fseConfig!=null) {
     		
-    		FieldSetEntity fseFunction=baseDao.getFieldSetEntity("product_sys_functions", function_uuid, false);
+    		//鑾峰彇璺宠浆鎸夐挳 骞� 鍒ゆ柇璺宠浆鏉冮檺
+    		String buttonName = fseConfig.getString(CoreConst.BUTTON_NAME);
+    		if (BaseUtil.strIsNull(buttonName)) {
+    			json.put(CmnConst.IS_PERSSION, 2);
+			}else {
+				json.put(CmnConst.SKIPBUTTON, fseConfig.getString(CoreConst.BUTTON_NAME));
+			}
     		
     		//鑾峰彇鏉冮檺杩囨护瀛楁
-    		String org_fields=fseConfig.getString("org_fileds");
-    		String user_fields=fseConfig.getString("user_fileds");
+    		String org_fields=fseConfig.getString(CmnConst.ORG_FIELDS);
+    		String user_fields=fseConfig.getString(CmnConst.USER_FIELDS);
     		
-    		//鑾峰彇闄勪欢淇℃伅
-    		FieldSetEntity fseAttachment=baseDao.getFieldSetEntity("product_sys_attachments", uuid, false);
+    		//鑾峰彇闄勪欢淇℃伅锛岀粨鍚堟暟鎹潈闄愬瓧娈碉紝鐢熸垚鍘熸暟鎹殑杩囨护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("attachment_data_field"));
+				filter.append(fseAttachment.getString(CmnConst.ATTACHMENT_DATA_FIELD));
 				filter.append(" =? ");
 				if (StringUtils.isEmpty(org_fields)) {
 					if (!StringUtils.isEmpty(user_fields)) {
 						filter.append(" AND ");
-						filter.append(permissionService.getDataFilter(fseAttachment.getString("attachment_data_table"), user_fields));
+						filter.append(permissionService.getDataFilter(fseAttachment.getString(CmnConst.ATTACHMENT_DATA_TABLE), user_fields));
 					}
 				}else {
 					if (StringUtils.isEmpty(user_fields)) {
@@ -259,34 +221,50 @@
 						filter.append(permissionService.getDataFilter(org_fields));
 					}else {
 						filter.append(" AND ");
-						filter.append(permissionService.getDataFilter(fseAttachment.getString("attachment_data_table"), user_fields, org_fields));
+						filter.append(permissionService.getDataFilter(fseAttachment.getString(CmnConst.ATTACHMENT_DATA_TABLE), user_fields, org_fields));
 					}
 				}
 				
-				
 				//鑾峰彇鍘熸暟鎹�
-				FieldSetEntity fseService=baseDao.getFieldSetEntityByFilter(fseAttachment.getString("attachment_data_table"), filter.toString(), new Object[] {uuid}, false);
-				if (fseService==null) {//鏉冮檺杩囨护鏃犲師鏁版嵁
-					FieldSetEntity fseService2=baseDao.getFieldSetEntityByFilter(fseAttachment.getString("attachment_data_table"), fseAttachment.getString("attachment_data_field")+"=?", new Object[] {uuid}, false);
+				FieldSetEntity fseService=baseDao.getFieldSetEntityByFilter(fseAttachment.getString(CmnConst.ATTACHMENT_DATA_TABLE), filter.toString(), 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}, false);
 					if (fseService2==null) {
-//						json.put("date_time", fseService2.getString(fseConfig.getString("time_field")));
-						json.put("title", "鏃犵浉鍏充笟鍔℃暟鎹�");
-						json.put("function_name", fseFunction.getString("function_name"));
-						json.put("is_perssion", 1);
+						json.put(CmnConst.TITLE, "涓氬姟鏁版嵁涓嶅瓨鍦�");
+						json.put(CmnConst.IS_PERSSION, 1);
 					}else {
-						json.put("service_uuid", fseService2.getString("uuid"));
-						json.put("date_time", fseService2.getString(fseConfig.getString("time_field")));
-						json.put("title", fseService2.getString(fseConfig.getString("title_field")));
-						json.put("function_name", fseFunction.getString("function_name"));
-						json.put("is_perssion", 0);
+						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);
 					}
 					
 				}else {
-					json.put("service_uuid", fseService.getString("uuid"));
-					json.put("date_time", fseService.getString(fseConfig.getString("time_field")));
-					json.put("title", fseService.getString(fseConfig.getString("title_field")));
-					json.put("function_name", fseFunction.getString("function_name"));
+					
+					//鍒ゆ柇鏄惁涓烘枃妗g鐞嗗姛鑳斤紝骞跺鐞嗗崟浣嶆枃妗e拰涓汉鏂囨。鏉冮檺
+					if("e4fa2c88-88a4-4ef4-9020-ebbe0440b4cf".equals(function_uuid)) {
+						if(!SpringMVCContextHolder.getCurrentUserId().equals(fseService.getString(CoreConst.CREATED_BY))) {
+							json.put(CmnConst.IS_PERSSION, 0);
+						}
+					}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);
+						if (fseDirectRight == null) {
+							json.put(CmnConst.IS_PERSSION, 0);
+						}else {
+							boolean succ= BaseUtil.multipleTypeConcat(fseDirectRight.getString(CmnConst.STORAGE_UUID), SpringMVCContextHolder.getCurrentUserId());
+							if (!succ) {
+								json.put(CmnConst.IS_PERSSION, 0);
+							}
+						}
+					}
+					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));
 			}
 		}
     }

--
Gitblit v1.9.2