| | |
| | | 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.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 { |
| | | |
| | | private static final Logger logger = LoggerFactory.getLogger(DataSyncInterceptor.class); |
| | | @Lazy |
| | | @Autowired |
| | | private ElasticsearchDocumentService elasticsearchDocumentService; |
| | |
| | | tableName.equals("jczz_house") || |
| | | tableName.equals("jczz_household") || |
| | | tableName.equals("jczz_place")) |
| | | syncDataAfterUpdate(tableName,sqlType,invocation.getArgs()[1]); |
| | | syncDataAfterUpdate(tableName, sqlType, invocation.getArgs()[1]); |
| | | } |
| | | } |
| | | return res; |
| | |
| | | |
| | | /** |
| | | * 数据同步 |
| | | * |
| | | * @param tableName |
| | | * @param sqlType |
| | | * @param parameter |
| | | */ |
| | | private void syncDataAfterUpdate(String tableName,String sqlType,Object parameter) { |
| | | private void syncDataAfterUpdate(String tableName, String sqlType, Object parameter) { |
| | | EsParam esParam = new EsParam(); |
| | | esParam.setIndexName(indexName); |
| | | esParam.setTableName(tableName); |
| | | // 判断操作类型 |
| | | if (sqlType.equals("INSERT")){ |
| | | if (sqlType.equals("INSERT")) { |
| | | //insert 可用直接拦截到实体类 |
| | | if (tableName.equals("jczz_article")) { |
| | | Article entity = (Article) parameter; |
| | | elasticsearchDocumentService.addArticle(esParam,entity); |
| | | elasticsearchDocumentService.addArticle(esParam, entity); |
| | | } |
| | | if (tableName.equals("jczz_place")) { |
| | | PlaceVO entity = (PlaceVO) parameter; |
| | | elasticsearchDocumentService.addPlace(esParam,entity); |
| | | PlaceEntity entity = (PlaceEntity) parameter; |
| | | elasticsearchDocumentService.addPlace(esParam, entity); |
| | | } |
| | | if (tableName.equals("jczz_house")) { |
| | | HouseVO entity = (HouseVO) parameter; |
| | | elasticsearchDocumentService.addHouse(esParam,entity); |
| | | HouseEntity entity = (HouseEntity) parameter; |
| | | elasticsearchDocumentService.addHouse(esParam, entity); |
| | | } |
| | | if (tableName.equals("jczz_household")) { |
| | | HouseholdVO entity = (HouseholdVO) parameter; |
| | | elasticsearchDocumentService.addHousehold(esParam,entity); |
| | | HouseholdEntity entity = (HouseholdEntity) parameter; |
| | | elasticsearchDocumentService.addHousehold(esParam, entity); |
| | | } |
| | | } |
| | | if(sqlType.equals("UPDATE")){ |
| | | 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); |
| | | 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){ |
| | | if (parameter instanceof MapperMethod.ParamMap) { |
| | | placeEntity = (PlaceEntity) ((MapperMethod.ParamMap) parameter).get("param1"); |
| | | if (null!=placeEntity.getId()) { |
| | | if (placeEntity != null && null != placeEntity.getId()) { |
| | | setPlaceInfo(entity, placeEntity); |
| | | esParam.setTableId(entity.getId().toString()); |
| | | elasticsearchDocumentService.update(esParam, entity, EsTableConstant.placeList); |
| | | } |
| | | }else { |
| | | } else { |
| | | placeEntity = (PlaceEntity) parameter; |
| | | // 删除 |
| | | esParam.setTableId(placeEntity.getId().toString()); |
| | |
| | | if (tableName.equals("jczz_house")) { |
| | | HouseEntity houseEntity = new HouseEntity(); |
| | | HouseEntity entity = new HouseEntity(); |
| | | if (parameter instanceof MapperMethod.ParamMap){ |
| | | if (parameter instanceof MapperMethod.ParamMap) { |
| | | entity = (HouseEntity) ((MapperMethod.ParamMap) parameter).get("param1"); |
| | | if (null!=entity.getId()) { |
| | | if (entity != null && entity.getId() != null) { |
| | | setHouseInfo(houseEntity, entity); |
| | | esParam.setTableId(entity.getId().toString()); |
| | | elasticsearchDocumentService.update(esParam, entity, EsTableConstant.houseList); |
| | | } |
| | | }else { |
| | | } else { |
| | | entity = (HouseEntity) parameter; |
| | | // 删除 |
| | | esParam.setTableId(entity.getId().toString()); |
| | |
| | | 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); |
| | | 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 { |
| | | } else { |
| | | entity = (HouseholdEntity) parameter; |
| | | // 删除 |
| | | esParam.setTableId(entity.getId().toString()); |
| | |
| | | } |
| | | } |
| | | // 删除处理 |
| | | if(sqlType.equals("DELETE")){ |
| | | List<Long> list =(List<Long>) ((MapperMethod.ParamMap) parameter).get("param1"); |
| | | esParam.setTableId(list.get(0).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 |
| | | */ |
| | |
| | | entity.setPrincipalIdCard(placeEntity.getPrincipalIdCard()); |
| | | entity.setLocation(placeEntity.getLocation()); |
| | | } |
| | | |
| | | /** |
| | | * 房屋值复制 |
| | | * |
| | | * @param entity |
| | | * @param houseEntity |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 住户值复制 |
| | | * |
| | | * @param entity |
| | | * @param householdEntity |
| | | */ |