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/common/interceptor/DataSyncInterceptor.java |  245 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 245 insertions(+), 0 deletions(-)

diff --git a/src/main/java/org/springblade/common/interceptor/DataSyncInterceptor.java b/src/main/java/org/springblade/common/interceptor/DataSyncInterceptor.java
new file mode 100644
index 0000000..a8588b5
--- /dev/null
+++ b/src/main/java/org/springblade/common/interceptor/DataSyncInterceptor.java
@@ -0,0 +1,245 @@
+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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+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.place.entity.PlaceEntity;
+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.Properties;
+
+@Component
+@Intercepts({
+	@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
+})
+public class DataSyncInterceptor implements Interceptor {
+
+	private static final Logger logger = LoggerFactory.getLogger(DataSyncInterceptor.class);
+	@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")) {
+				PlaceEntity entity = (PlaceEntity) parameter;
+				elasticsearchDocumentService.addPlace(esParam, entity);
+			}
+			if (tableName.equals("jczz_house")) {
+				HouseEntity entity = (HouseEntity) parameter;
+				elasticsearchDocumentService.addHouse(esParam, entity);
+			}
+			if (tableName.equals("jczz_household")) {
+				HouseholdEntity entity = (HouseholdEntity) parameter;
+				elasticsearchDocumentService.addHousehold(esParam, entity);
+			}
+		}
+		if (sqlType.equals("UPDATE")) {
+			//update 方法需要特殊处理
+			if (tableName.equals("jczz_article")) {
+				try {
+					Article entity = (Article) ((MapperMethod.ParamMap) parameter).get("param1");
+					if (entity != null && entity.getId() != null) {
+						esParam.setTableId(entity.getId().toString());
+						elasticsearchDocumentService.update(esParam, entity, EsTableConstant.articleList);
+					}
+				} catch (Exception e) {
+					logger.error("jczz_article 更新失败!", e);
+				}
+			}
+			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 (placeEntity != null && 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 (entity != null && entity.getId() != null) {
+						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) {
+					try {
+						entity = (HouseholdEntity) ((MapperMethod.ParamMap) parameter).get("param1");
+						if (entity != null && entity.getId() != null) {
+							setHouseholdInfo(householdEntity, entity);
+							esParam.setTableId(entity.getId().toString());
+							elasticsearchDocumentService.update(esParam, entity, EsTableConstant.householdList);
+						}
+					} catch (Exception e) {
+						entity = (HouseholdEntity) ((MapperMethod.ParamMap) parameter).get("et");
+						if (entity != null && entity.getId() != null) {
+							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")) {
+			if (parameter instanceof MapperMethod.ParamMap) {
+				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