From 764d883b5ea3bdc06abbec548b6df0511e567978 Mon Sep 17 00:00:00 2001
From: linwe <872216996@qq.com>
Date: Tue, 03 Sep 2024 09:46:05 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/binlog' into binlog

---
 src/main/java/org/springblade/es/service/ElasticsearchDocumentService.java |  236 +++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 164 insertions(+), 72 deletions(-)

diff --git a/src/main/java/org/springblade/es/service/ElasticsearchDocumentService.java b/src/main/java/org/springblade/es/service/ElasticsearchDocumentService.java
index 1906861..7fcfe2d 100644
--- a/src/main/java/org/springblade/es/service/ElasticsearchDocumentService.java
+++ b/src/main/java/org/springblade/es/service/ElasticsearchDocumentService.java
@@ -1,5 +1,7 @@
 package org.springblade.es.service;
 
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import org.apache.logging.log4j.util.Strings;
 import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
@@ -16,17 +18,24 @@
 import org.elasticsearch.client.indices.GetIndexRequest;
 import org.elasticsearch.index.query.BoolQueryBuilder;
 import org.elasticsearch.index.query.QueryBuilders;
-import org.elasticsearch.index.reindex.*;
+import org.elasticsearch.index.reindex.BulkByScrollResponse;
+import org.elasticsearch.index.reindex.DeleteByQueryRequest;
+import org.elasticsearch.index.reindex.UpdateByQueryRequest;
+import org.elasticsearch.script.Script;
 import org.elasticsearch.script.ScriptType;
 import org.elasticsearch.search.SearchHit;
 import org.elasticsearch.search.SearchHits;
 import org.elasticsearch.search.builder.SearchSourceBuilder;
+import org.springblade.common.cache.SysCache;
 import org.springblade.common.constant.EsTableConstant;
 import org.springblade.common.param.CommonParamSet;
 import org.springblade.common.utils.SpringUtils;
 import org.springblade.es.vo.EsParam;
 import org.springblade.modules.article.entity.Article;
 import org.springblade.modules.article.service.ArticleService;
+import org.springblade.modules.doorplateAddress.entity.DoorplateAddressEntity;
+import org.springblade.modules.doorplateAddress.service.IDoorplateAddressService;
+import org.springblade.modules.grid.service.IGridRangeService;
 import org.springblade.modules.house.entity.HouseEntity;
 import org.springblade.modules.house.entity.HouseholdEntity;
 import org.springblade.modules.house.service.IHouseService;
@@ -40,7 +49,6 @@
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
-import org.elasticsearch.script.Script;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -65,6 +73,12 @@
 
 	@Autowired
 	private IHouseholdService householdService;
+
+	@Autowired
+	private IGridRangeService gridRangeService;
+
+	@Autowired
+	private IDoorplateAddressService doorplateAddressService;
 
 	@Value("${elasticsearch.sync}")
 	private boolean elasticsearchSync;
@@ -96,14 +110,24 @@
 	 * @return
 	 */
 	public boolean init(EsParam esParam) {
-		// 场所
-		initPlace(esParam);
-		// 通知文章
-		initArticle(esParam);
-		// 房屋
-		initHouse(esParam);
-		// 住户
-		initHousehold(esParam);
+		if (!Strings.isBlank(esParam.getTableName())) {
+			if (esParam.getTableName().equals("jczz_place")) {
+				// 场所
+				initPlace(esParam);
+			}
+			if (esParam.getTableName().equals("jczz_article")) {
+				// 通知文章
+				initArticle(esParam);
+			}
+			if (esParam.getTableName().equals("jczz_house")) {
+				// 房屋
+				initHouse(esParam);
+			}
+			if (esParam.getTableName().equals("jczz_household")) {
+				// 住户
+				initHousehold(esParam);
+			}
+		}
 		// 返回
 		return true;
 	}
@@ -151,9 +175,9 @@
 					// 刷新索引,确保文档可搜索
 					client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT);
 				} catch (IOException e) {
-					e.printStackTrace();
+//					e.printStackTrace();
 				}
-				System.out.println("批量插入结果: " + !bulkResponse.hasFailures());
+//				System.out.println("批量插入结果: " + !bulkResponse.hasFailures());
 			}
 		}
 		// 返回
@@ -180,12 +204,17 @@
 				for (Article article : articleList) {
 					// 初始化Elasticsearch客户端
 					IndexRequest indexRequest = new IndexRequest(esParam.getIndexName());
+					String communityNameListString
+						= SysCache.getAllCommunityNameListString(article.getArticleRange(),article.getId().toString());
+					Map<String, Object> map = new HashMap<>(1);
+					map.put("communityCode",communityNameListString);
+					String toString = new JSONObject(map).toString();
 					// 不设置id
 					// indexRequest.id("1");
 					indexRequest.source("tableId", article.getId().toString(),
 						"tableName", "jczz_article",
 						"title", article.getTitle(),
-						"communityCode", article.getArticleRange(),
+						"communityCode", toString,
 						"type", null != article.getType() ? article.getType().toString() : "",
 						"content", article.getContent(),
 						"articleType", article.getArticleType());
@@ -199,9 +228,9 @@
 					// 刷新索引,确保文档可搜索
 					client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT);
 				} catch (IOException e) {
-					e.printStackTrace();
+//					e.printStackTrace();
 				}
-				System.out.println("批量插入结果: " + !bulkResponse.hasFailures());
+//				System.out.println("批量插入结果: " + !bulkResponse.hasFailures());
 			}
 		}
 		// 返回
@@ -234,6 +263,7 @@
 						"tableId", household.getId().toString(),
 						"tableName", "jczz_household",
 						"title", household.getName(),
+						"relationship", household.getRelationship(),
 						"name", household.getName(),
 						"phone", household.getPhoneNumber(),
 						"idCard", household.getIdCard(),
@@ -250,9 +280,9 @@
 					// 刷新索引,确保文档可搜索
 					client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT);
 				} catch (IOException e) {
-					e.printStackTrace();
+//					e.printStackTrace();
 				}
-				System.out.println("批量插入结果: " + !bulkResponse.hasFailures());
+//				System.out.println("批量插入结果: " + !bulkResponse.hasFailures());
 			}
 		}
 		// 返回
@@ -301,9 +331,9 @@
 					// 刷新索引,确保文档可搜索
 					client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT);
 				} catch (IOException e) {
-					e.printStackTrace();
+//					e.printStackTrace();
 				}
-				System.out.println("批量插入结果: " + !bulkResponse.hasFailures());
+//				System.out.println("批量插入结果: " + !bulkResponse.hasFailures());
 			}
 		}
 		// 返回
@@ -319,18 +349,23 @@
 	@Async
 	public void addArticle(EsParam esParam, Article article) {
 		if (elasticsearchSync) {
+			// 查询当前文章范围对应的社区名称字符串集合
+			String communityNameListString = SysCache.getAllCommunityNameListString(article.getArticleRange(),article.getId().toString());
+			Map<String, Object> map = new HashMap<>(1);
+			map.put("communityCode",communityNameListString);
+			String toString = new JSONObject(map).toString();
 			try {
 				indexDocument(esParam.getIndexName(),
 					"tableId", article.getId().toString(),
 					"tableName", "jczz_article",
 					"title", article.getTitle(),
-					"communityCode", article.getArticleRange(),
+					"communityCode", toString,
 					"type", null != article.getType() ? article.getType().toString() : "",
 					"content", article.getContent(),
 					"articleType", article.getArticleType()
 				);
 			} catch (IOException e) {
-				e.printStackTrace();
+//				e.printStackTrace();
 			}
 		}
 	}
@@ -342,11 +377,8 @@
 	 * @param place
 	 */
 	@Async
-	public void addPlace(EsParam esParam, PlaceVO place) {
+	public void addPlace(EsParam esParam, PlaceEntity place) {
 		if (elasticsearchSync) {
-			if (!Strings.isBlank(place.getCommunityCode())) {
-				place.setCommunityCode(placeService.getCommunityCode(place.getId()));
-			}
 			try {
 				indexDocument(esParam.getIndexName(),
 					"tableId", place.getId().toString(),
@@ -356,10 +388,10 @@
 					"phone", place.getPrincipalPhone(),
 					"idCard", place.getPrincipalIdCard(),
 					"content", place.getLocation(),
-					"communityCode", place.getCommunityCode()
+					"communityCode", placeService.getCommunityCode(place.getId())
 				);
 			} catch (IOException e) {
-				e.printStackTrace();
+//				e.printStackTrace();
 			}
 		}
 	}
@@ -371,11 +403,8 @@
 	 * @param house
 	 */
 	@Async
-	public void addHouse(EsParam esParam, HouseVO house) {
+	public void addHouse(EsParam esParam, HouseEntity house) {
 		if (elasticsearchSync) {
-			if (!Strings.isBlank(house.getCommunityCode())) {
-				house.setCommunityCode(houseService.getCommunityCode(house.getId()));
-			}
 			try {
 				indexDocument(esParam.getIndexName(),
 					"tableId", house.getId().toString(),
@@ -385,10 +414,10 @@
 					"phone", null,
 					"idCard", null,
 					"content", null,
-					"communityCode", house.getCommunityCode()
+					"communityCode", houseService.getCommunityCode(house.getId())
 				);
 			} catch (IOException e) {
-				e.printStackTrace();
+//				e.printStackTrace();
 			}
 		}
 	}
@@ -400,10 +429,11 @@
 	 * @param household
 	 */
 	@Async
-	public void addHousehold(EsParam esParam, HouseholdVO household) {
+	public void addHousehold(EsParam esParam, HouseholdEntity household) {
 		if (elasticsearchSync) {
-			if (!Strings.isBlank(household.getCommunityCode())) {
-				household.setCommunityCode(householdService.getCommunityCode(household.getId()));
+			// 如果关系为空,则默认为19
+			if (household.getRelationship() == null) {
+				household.setRelationship(19);
 			}
 			try {
 				indexDocument(esParam.getIndexName(),
@@ -413,13 +443,38 @@
 					"name", household.getName(),
 					"phone", household.getPhoneNumber(),
 					"idCard", household.getIdCard(),
-					"content", household.getAddress(),
-					"communityCode", household.getCommunityCode()
+					"relationship", household.getRelationship().toString(),
+					"content", getAddress(household),
+					"communityCode", householdService.getCommunityCode(household.getId())
 				);
 			} catch (IOException e) {
-				e.printStackTrace();
+//				e.printStackTrace();
 			}
 		}
+	}
+
+	/**
+	 * 获取房屋地址
+	 * @param household
+	 * @return
+	 */
+	public String getAddress(HouseholdEntity household) {
+		if (!Strings.isBlank(household.getHouseCode())){
+			// 查询对应的房屋地址
+			QueryWrapper<DoorplateAddressEntity> queryWrapper = new QueryWrapper<>();
+			queryWrapper.eq("address_code",household.getHouseCode());
+			List<DoorplateAddressEntity> list = doorplateAddressService.list(queryWrapper);
+			if (list.size()>0){
+				return list.get(0).getAddressName();
+			}
+			// 房屋查不到的情况
+			if (!Strings.isBlank(household.getCurrentAddress())){
+				return household.getCurrentAddress();
+			}
+		}else {
+			return household.getCurrentAddress();
+		}
+		return null;
 	}
 
 	/**
@@ -473,17 +528,39 @@
 			BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
 //			boolQueryBuilder.should(QueryBuilders.matchQuery("content", esParam.getSearchKey()));
 //			boolQueryBuilder.should(QueryBuilders.matchQuery("name", esParam.getSearchKey()));
-			boolQueryBuilder.should(QueryBuilders.matchQuery("title", esParam.getSearchKey()));
+			boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("title", esParam.getSearchKey()));
 //			boolQueryBuilder.should(QueryBuilders.matchQuery("location", esParam.getSearchKey()));
 //			boolQueryBuilder.should(QueryBuilders.matchQuery("phone", esParam.getSearchKey()));
 //			boolQueryBuilder.should(QueryBuilders.matchQuery("idCard", esParam.getSearchKey()));
-			String communityCode = SpringUtils.getRequestParam("communityCode");
-			if (!Strings.isBlank(communityCode)) {
-				boolQueryBuilder.must(QueryBuilders.matchQuery("communityCode", communityCode));
-			}
 			if (!Strings.isBlank(esParam.getTableName())) {
 				boolQueryBuilder.must(QueryBuilders.matchQuery("tableName", esParam.getTableName()));
 			}
+			String roleName = SpringUtils.getRequestParam("roleName");
+			String communityCode = SpringUtils.getRequestParam("communityCode");
+			if (!Strings.isBlank(communityCode)) {
+				if (!Strings.isBlank(roleName) && roleName.equals("inhabitant") && !Strings.isBlank(esParam.getHouseCode())){
+					// 居民根据房屋对应的小区过滤
+					boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("communityCode",gridRangeService.getDistrictIdByHouseCode(esParam.getHouseCode())));
+				}else {
+					// 设置查询社区名称,编号有多个,数字类型目前无法匹配到
+					boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("communityCode", communityCode));
+				}
+			}
+			if (!Strings.isBlank(roleName)){
+				if (roleName.equals("wgy") || roleName.equals("mj")){
+					// 不查公告
+					boolQueryBuilder.mustNot(QueryBuilders.matchQuery("tableName", "jczz_article"));
+				}
+				if (roleName.equals("inhabitant")){
+					// 只查公告
+					boolQueryBuilder.must(QueryBuilders.matchQuery("tableName", "jczz_article"));
+				}
+				if (roleName.equals("wzcj")){
+					// 只查场所
+					boolQueryBuilder.must(QueryBuilders.matchQuery("tableName", "jczz_place"));
+				}
+			}
+
 			boolQueryBuilder.minimumShouldMatch(1);
 			searchSourceBuilder.query(boolQueryBuilder);
 
@@ -539,16 +616,19 @@
 	 */
 	public boolean removeBatchByIndexNames(List<String> indexNames) {
 		for (String indexName : indexNames) {
-			DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
-			try {
-				// 执行删除索引操作
-				AcknowledgedResponse deleteResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
+			// 索引存在才删除
+			if (isIndexExists(indexName)) {
+				DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
+				try {
+					// 执行删除索引操作
+					AcknowledgedResponse deleteResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
 
-				// 输出操作结果
-				boolean acknowledged = deleteResponse.isAcknowledged();
-				System.out.println("索引删除成功: " + acknowledged);
-			} catch (IOException e) {
-				e.printStackTrace();
+					// 输出操作结果
+					boolean acknowledged = deleteResponse.isAcknowledged();
+//					System.out.println("索引删除成功: " + acknowledged);
+				} catch (IOException e) {
+//					e.printStackTrace();
+				}
 			}
 		}
 		return true;
@@ -565,7 +645,7 @@
 		try {
 			indexDocument(esParam.getIndexName(), str);
 		} catch (IOException e) {
-			e.printStackTrace();
+//			e.printStackTrace();
 		}
 		return true;
 	}
@@ -576,7 +656,8 @@
 	 */
 	@Async
 	public void update(EsParam esParam, T item,List<String> columnList) {
-		if (elasticsearchSync) {
+		// 判断索引是否存在
+		if (elasticsearchSync && isIndexExists(esParam.getIndexName())) {
 			// 创建更新请求
 			UpdateByQueryRequest updateRequest = new UpdateByQueryRequest(esParam.getIndexName());
 
@@ -589,17 +670,17 @@
 
 			Map<String, Object> data = new HashMap<String, Object>();
 			data.put("tableName", esParam.getTableName());
-			if (esParam.getTableName().equals("jczz_article")){
-				setArticleMap((Article)item,data);
+			if (esParam.getTableName().equals("jczz_article")) {
+				setArticleMap((Article) item, data);
 			}
-			if (esParam.getTableName().equals("jczz_place")){
-				setPlaceMap((PlaceEntity) item,data);
+			if (esParam.getTableName().equals("jczz_place")) {
+				setPlaceMap((PlaceEntity) item, data);
 			}
-			if (esParam.getTableName().equals("jczz_house")){
-				setHouseMap((HouseEntity) item,data);
+			if (esParam.getTableName().equals("jczz_house")) {
+				setHouseMap((HouseEntity) item, data);
 			}
-			if (esParam.getTableName().equals("jczz_household")){
-				setHousehouldMap((HouseholdEntity) item,data);
+			if (esParam.getTableName().equals("jczz_household")) {
+				setHouseholdMap((HouseholdEntity) item, data);
 			}
 			Map<String, Object> param = new HashMap<String, Object>();
 			param.put("data", data);
@@ -614,7 +695,7 @@
 				// 刷新索引,确保文档可搜索
 				client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT);
 			} catch (IOException e) {
-				e.printStackTrace();
+//				e.printStackTrace();
 			}
 		}
 	}
@@ -625,12 +706,17 @@
 	 * @param data
 	 */
 	private void setArticleMap(Article item, Map<String, Object> data) {
+		// 查询当前文章范围对应的社区名称字符串集合
+		String communityNameListString = SysCache.getAllCommunityNameListString(item.getArticleRange(),item.getId().toString());
+		Map<String, Object> map = new HashMap<>(1);
+		map.put("communityCode",communityNameListString);
+		String toString = new JSONObject(map).toString();
 		data.put("tableId",item.getId());
 		data.put("title",item.getTitle());
 		data.put("type",item.getType());
 		data.put("content",item.getContent());
 		data.put("articleType",item.getArticleType());
-		data.put("communityCode",item.getArticleRange());
+		data.put("communityCode",toString);
 	}
 	/**
 	 * 设置场所map
@@ -661,33 +747,39 @@
 	 * @param item
 	 * @param data
 	 */
-	private void setHousehouldMap(HouseholdEntity item, Map<String, Object> data) {
+	private void setHouseholdMap(HouseholdEntity item, Map<String, Object> data) {
 		data.put("communityCode",householdService.getCommunityCode(item.getId()));
 		data.put("tableId",item.getId());
 		data.put("title",item.getName());
+		data.put("relationship",item.getRelationship());
 		data.put("name",item.getName());
 		data.put("phone",item.getPhoneNumber());
 		data.put("idCard",item.getIdCard());
-		data.put("content",item.getCurrentAddress());
+		data.put("content",getAddress(item));
 	}
 
 	/**
 	 * 删除数据--根据条件
 	 */
 	public boolean removeByQuery(EsParam esParam) {
-		if (elasticsearchSync) {
+		if (elasticsearchSync && isIndexExists(esParam.getIndexName())) {
 			DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(esParam.getIndexName());
 			// 根据多个条件 生成 boolQueryBuilder
 			BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
-			boolQueryBuilder
-				.must(QueryBuilders.matchQuery("tableId", esParam.getTableId()))
-				.must(QueryBuilders.matchQuery("tableName", esParam.getTableName()));
+			if (!Strings.isBlank(esParam.getTableId())){
+				boolQueryBuilder
+					.must(QueryBuilders.matchQuery("tableId", esParam.getTableId()));
+			}
+			if (!Strings.isBlank(esParam.getTableName())) {
+				boolQueryBuilder
+					.must(QueryBuilders.matchQuery("tableName", esParam.getTableName()));
+			}
 			deleteByQueryRequest.setQuery(boolQueryBuilder);
 			try {
 				BulkByScrollResponse bulkResponse =
 					client.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT);
 			} catch (IOException e) {
-				e.printStackTrace();
+//				e.printStackTrace();
 			}
 		}
 		return true;

--
Gitblit v1.9.3