From 684cc72830cc05d94c51decbd99f0428c6054ae8 Mon Sep 17 00:00:00 2001
From: zhongrj <646384940@qq.com>
Date: Tue, 09 Apr 2024 22:25:40 +0800
Subject: [PATCH] 公共,房屋,场所,住户数据同步操作修改完善

---
 src/main/java/org/springblade/common/interceptor/DataSyncInterceptor.java |  331 +++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 225 insertions(+), 106 deletions(-)

diff --git a/src/main/java/org/springblade/common/interceptor/DataSyncInterceptor.java b/src/main/java/org/springblade/common/interceptor/DataSyncInterceptor.java
index a51e955..a23c31b 100644
--- a/src/main/java/org/springblade/common/interceptor/DataSyncInterceptor.java
+++ b/src/main/java/org/springblade/common/interceptor/DataSyncInterceptor.java
@@ -1,106 +1,225 @@
-//package org.springblade.common.interceptor;
-//
-//import org.apache.ibatis.binding.MapperMethod;
-//import org.apache.ibatis.executor.Executor;
-//import org.apache.ibatis.executor.statement.StatementHandler;
-//import org.apache.ibatis.mapping.BoundSql;
-//import org.apache.ibatis.mapping.MappedStatement;
-//import org.apache.ibatis.plugin.*;
-//import org.apache.logging.log4j.util.Strings;
-//import org.springblade.common.utils.SQLParseUtils;
-//import org.springblade.es.service.ElasticsearchDocumentService;
-//import org.springblade.es.vo.EsParam;
-//import org.springblade.modules.article.entity.Article;
-//import org.springframework.beans.factory.annotation.Autowired;
-//import java.util.Properties;
-//
-//@Intercepts({
-//	@Signature(type = StatementHandler.class, method = "update", args = {MappedStatement.class, Object.class})
-//})
-//public class DataSyncInterceptor implements Interceptor {
-//
-//	@Autowired
-//	private ElasticsearchDocumentService elasticsearchDocumentService;
-//
-//	/**
-//	 * 拦截器在sql执行成功后同步到es,
-//	 * 如果同步失败抛出异常,保证数据一致性
-//	 *
-//	 * @param invocation
-//	 * @return
-//	 * @throws Throwable
-//	 */
-//	@Override
-//	public Object intercept(Invocation invocation) throws Throwable {
-//		Object res = invocation.proceed();
-//
-//		Object[] args = invocation.getArgs();
-//		if (args.length >= 2) {
-//			MappedStatement mappedStatement = (MappedStatement) args[0];
-//			//参数
-//			Object parameter = invocation.getArgs()[1];
-//			BoundSql boundSql = mappedStatement.getBoundSql(parameter);
-//			//sql
-//			String sql = boundSql.getSql();
-//			sql = sql.replaceAll("\n", "");
-//			//获取表名
-//			String tableName = SQLParseUtils.getTableName(sql);
-//			String sqlType = SQLParseUtils.parseSQLType(sql);
-//			if (!Strings.isBlank(tableName)) {
-//				if (tableName.equals("jczz_article") ||
-//					tableName.equals("jczz_house") ||
-//					tableName.equals("jczz_household") ||
-//					tableName.equals("jczz_place"))
-//				syncDataAfterUpdate(sql,tableName,sqlType,args[1]);
-//			}
-//		}
-//		return res;
-//	}
-//
-//
-//	/**
-//	 * 数据同步
-//	 * @param tableName
-//	 * @param sqlType
-//	 * @param parameter
-//	 */
-//	private void syncDataAfterUpdate(String sql,String tableName,String sqlType,Object parameter) {
-//		EsParam esParam = new EsParam();
-//		esParam.setIndexName("test");
-//		esParam.setTableName(tableName);
-//		// 判断操作类型
-//		if (sqlType.equals("INSERT")){
-//			//insert 可用直接拦截到实体类
-//			if (tableName.equals("jczz_article")) {
-//				Article entity = (Article) parameter;
-//				elasticsearchDocumentService.addArticle(esParam,entity);
-//			}
-//		}
-//		if(sqlType.equals("UPDATE")){
-//			//update 方法需要特殊处理
-//			if (tableName.equals("jczz_article")) {
-//				Article entity = (Article) ((MapperMethod.ParamMap) parameter).get("param1");
-//				elasticsearchDocumentService.update(esParam,entity);
-//			}
-//		}
-//		// 删除处理
-//		if(sqlType.equals("DELETE")){
-//			esParam.setTableId("test");
-//			elasticsearchDocumentService.removeByQuery(esParam);
-//		}
-//	}
-//
-//
-//	@Override
-//	public Object plugin(Object target) {
-//		if (target instanceof Executor || target instanceof StatementHandler) {
-//			return Plugin.wrap(target, this);
-//		}
-//		return target;
-//	}
-//
-//	@Override
-//	public void setProperties(Properties properties) {
-//		// 可以接收配置属性
-//	}
-//}
+package org.springblade.common.interceptor;
+
+import org.apache.ibatis.binding.MapperMethod;
+import org.apache.ibatis.executor.Executor;
+import org.apache.ibatis.mapping.BoundSql;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.plugin.*;
+import org.apache.logging.log4j.util.Strings;
+import org.springblade.common.constant.EsTableConstant;
+import org.springblade.common.utils.SQLParseUtils;
+import org.springblade.es.service.ElasticsearchDocumentService;
+import org.springblade.es.vo.EsParam;
+import org.springblade.modules.article.entity.Article;
+import org.springblade.modules.house.entity.HouseEntity;
+import org.springblade.modules.house.entity.HouseholdEntity;
+import org.springblade.modules.house.vo.HouseVO;
+import org.springblade.modules.house.vo.HouseholdVO;
+import org.springblade.modules.place.entity.PlaceEntity;
+import org.springblade.modules.place.vo.PlaceVO;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Lazy;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+@Component
+@Intercepts({
+	@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
+})
+public class DataSyncInterceptor implements Interceptor {
+
+	@Lazy
+	@Autowired
+	private ElasticsearchDocumentService elasticsearchDocumentService;
+
+	@Value("${elasticsearch.indexName}")
+	private String indexName;
+
+	/**
+	 * 拦截器在sql执行成功后同步到es,
+	 * 如果同步失败抛出异常,保证数据一致性
+	 *
+	 * @param invocation
+	 * @return
+	 * @throws Throwable
+	 */
+	@Override
+	public Object intercept(Invocation invocation) throws Throwable {
+		// 获取StatementHandler,进行自定义处理
+//		StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
+
+		Object res = invocation.proceed();
+		System.out.println("res = " + res);
+
+		Object[] args = invocation.getArgs();
+		MappedStatement ms = (MappedStatement) args[0];
+		if (args.length >= 2) {
+			//参数
+			Object parameter = invocation.getArgs()[1];
+			BoundSql boundSql = ms.getBoundSql(parameter);
+			//sql
+			String sql = boundSql.getSql();
+			sql = sql.replaceAll("\n", "");
+			//获取表名
+			String tableName = SQLParseUtils.getTableName(sql);
+			String sqlType = SQLParseUtils.parseSQLType(sql);
+			if (!Strings.isBlank(tableName)) {
+				if (tableName.equals("jczz_article") ||
+					tableName.equals("jczz_house") ||
+					tableName.equals("jczz_household") ||
+					tableName.equals("jczz_place"))
+				syncDataAfterUpdate(tableName,sqlType,invocation.getArgs()[1]);
+			}
+		}
+		return res;
+	}
+
+
+	/**
+	 * 数据同步
+	 * @param tableName
+	 * @param sqlType
+	 * @param parameter
+	 */
+	private void syncDataAfterUpdate(String tableName,String sqlType,Object parameter) {
+		EsParam esParam = new EsParam();
+		esParam.setIndexName(indexName);
+		esParam.setTableName(tableName);
+		// 判断操作类型
+		if (sqlType.equals("INSERT")){
+			//insert 可用直接拦截到实体类
+			if (tableName.equals("jczz_article")) {
+				Article entity = (Article) parameter;
+				elasticsearchDocumentService.addArticle(esParam,entity);
+			}
+			if (tableName.equals("jczz_place")) {
+				PlaceVO entity = (PlaceVO) parameter;
+				elasticsearchDocumentService.addPlace(esParam,entity);
+			}
+			if (tableName.equals("jczz_house")) {
+				HouseVO entity = (HouseVO) parameter;
+				elasticsearchDocumentService.addHouse(esParam,entity);
+			}
+			if (tableName.equals("jczz_household")) {
+				HouseholdVO entity = (HouseholdVO) parameter;
+				elasticsearchDocumentService.addHousehold(esParam,entity);
+			}
+		}
+		if(sqlType.equals("UPDATE")){
+			//update 方法需要特殊处理
+			if (tableName.equals("jczz_article")) {
+				Article entity = (Article) ((MapperMethod.ParamMap) parameter).get("param1");
+				if (null!=entity.getId()) {
+					esParam.setTableId(entity.getId().toString());
+					elasticsearchDocumentService.update(esParam, entity, EsTableConstant.articleList);
+				}
+			}
+			if (tableName.equals("jczz_place")) {
+				PlaceEntity entity = new PlaceEntity();
+				PlaceEntity placeEntity = new PlaceEntity();
+				if (parameter instanceof MapperMethod.ParamMap){
+					placeEntity = (PlaceEntity) ((MapperMethod.ParamMap) parameter).get("param1");
+					if (null!=placeEntity.getId()) {
+						setPlaceInfo(entity, placeEntity);
+						esParam.setTableId(entity.getId().toString());
+						elasticsearchDocumentService.update(esParam, entity, EsTableConstant.placeList);
+					}
+				}else {
+					placeEntity = (PlaceEntity) parameter;
+					// 删除
+					esParam.setTableId(placeEntity.getId().toString());
+					elasticsearchDocumentService.removeByQuery(esParam);
+				}
+			}
+			if (tableName.equals("jczz_house")) {
+				HouseEntity houseEntity = new HouseEntity();
+				HouseEntity entity = new HouseEntity();
+				if (parameter instanceof MapperMethod.ParamMap){
+					entity = (HouseEntity) ((MapperMethod.ParamMap) parameter).get("param1");
+					if (null!=entity.getId()) {
+						setHouseInfo(houseEntity, entity);
+						esParam.setTableId(entity.getId().toString());
+						elasticsearchDocumentService.update(esParam, entity, EsTableConstant.houseList);
+					}
+				}else {
+					entity = (HouseEntity) parameter;
+					// 删除
+					esParam.setTableId(entity.getId().toString());
+					elasticsearchDocumentService.removeByQuery(esParam);
+				}
+			}
+			if (tableName.equals("jczz_household")) {
+				HouseholdEntity householdEntity = new HouseholdEntity();
+				HouseholdEntity entity = new HouseholdEntity();
+				if (parameter instanceof MapperMethod.ParamMap){
+					entity = (HouseholdEntity) ((MapperMethod.ParamMap) parameter).get("param1");
+					if (null!=entity.getId()) {
+						setHouseholdInfo(householdEntity, entity);
+						esParam.setTableId(entity.getId().toString());
+						elasticsearchDocumentService.update(esParam, entity, EsTableConstant.householdList);
+					}
+				}else {
+					entity = (HouseholdEntity) parameter;
+					// 删除
+					esParam.setTableId(entity.getId().toString());
+					elasticsearchDocumentService.removeByQuery(esParam);
+				}
+			}
+		}
+		// 删除处理
+		if(sqlType.equals("DELETE")){
+			List<Long> list =(List<Long>) ((MapperMethod.ParamMap) parameter).get("param1");
+			esParam.setTableId(list.get(0).toString());
+			elasticsearchDocumentService.removeByQuery(esParam);
+		}
+	}
+
+	/**
+	 * 场所值复制
+	 * @param entity
+	 * @param placeEntity
+	 */
+	private void setPlaceInfo(PlaceEntity entity, PlaceEntity placeEntity) {
+		entity.setId(placeEntity.getId());
+		entity.setPlaceName(placeEntity.getPlaceName());
+		entity.setPrincipal(placeEntity.getPrincipal());
+		entity.setPrincipalPhone(placeEntity.getPrincipalPhone());
+		entity.setPrincipalIdCard(placeEntity.getPrincipalIdCard());
+		entity.setLocation(placeEntity.getLocation());
+	}
+	/**
+	 * 房屋值复制
+	 * @param entity
+	 * @param houseEntity
+	 */
+	private void setHouseInfo(HouseEntity entity, HouseEntity houseEntity) {
+		entity.setId(houseEntity.getId());
+		entity.setHouseName(houseEntity.getHouseName());
+	}
+
+	/**
+	 * 住户值复制
+	 * @param entity
+	 * @param householdEntity
+	 */
+	private void setHouseholdInfo(HouseholdEntity entity, HouseholdEntity householdEntity) {
+		entity.setId(householdEntity.getId());
+		entity.setName(householdEntity.getName());
+		entity.setPhoneNumber(householdEntity.getPhoneNumber());
+		entity.setIdCard(householdEntity.getIdCard());
+		entity.setCurrentAddress(householdEntity.getCurrentAddress());
+	}
+
+
+	@Override
+	public Object plugin(Object target) {
+		return Plugin.wrap(target, this);
+	}
+
+	@Override
+	public void setProperties(Properties properties) {
+	}
+}

--
Gitblit v1.9.3