zhongrj
2024-04-09 684cc72830cc05d94c51decbd99f0428c6054ae8
公共,房屋,场所,住户数据同步操作修改完善
11 files modified
533 ■■■■■ changed files
pom.xml 5 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/common/config/MyBatisPlusConfig.java 30 ●●●● patch | view | raw | blame | history
src/main/java/org/springblade/common/constant/EsTableConstant.java 41 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/common/interceptor/DataSyncInterceptor.java 331 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/es/controller/EsController.java 14 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/es/service/ElasticsearchDocumentService.java 84 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/article/controller/ArticleController.java 22 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/place/entity/PlaceEntity.java 3 ●●●●● patch | view | raw | blame | history
src/main/resources/application-dev.yml 1 ●●●● patch | view | raw | blame | history
src/main/resources/application-prod.yml 1 ●●●● patch | view | raw | blame | history
src/main/resources/application-test.yml 1 ●●●● patch | view | raw | blame | history
pom.xml
@@ -292,6 +292,11 @@
            <version>7.17.4</version>
<!--            <version>6.7.2</version>-->
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
src/main/java/org/springblade/common/config/MyBatisPlusConfig.java
@@ -1,15 +1,15 @@
//package org.springblade.common.config;
//
//import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
//import org.springblade.common.interceptor.DataSyncInterceptor;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//@Configuration
//public class MyBatisPlusConfig {
//
//    @Bean
//    public ConfigurationCustomizer configurationCustomizer() {
//        return configuration -> configuration.addInterceptor(new DataSyncInterceptor());
//    }
//}
package org.springblade.common.config;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import org.springblade.common.interceptor.DataSyncInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
    @Bean
    public DataSyncInterceptor dataSyncInterceptor() {
        return new DataSyncInterceptor();
    }
}
src/main/java/org/springblade/common/constant/EsTableConstant.java
@@ -22,6 +22,47 @@
        add("type");
        add("content");
        add("articleType");
        add("articleRange");
    }};
    /**
     * 2. 场所
     */
    public static List<String> placeList = new ArrayList<String>()
    {{
        add("tableId");
        add("tableName");
        add("placeName");
        add("principal");
        add("principalPhone");
        add("principalIdCard");
        add("location");
    }};
    /**
     * 3. 房屋
     */
    public static List<String> houseList = new ArrayList<String>()
    {{
        add("tableId");
        add("tableName");
        add("houseName");
    }};
    /**
     * 4. 住户
     */
    public static List<String> householdList = new ArrayList<String>()
    {{
        add("tableId");
        add("tableName");
        add("name");
        add("idCard");
        add("phoneNumber");
        add("address");
    }};
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) {
    }
}
src/main/java/org/springblade/es/controller/EsController.java
@@ -1,27 +1,15 @@
package org.springblade.es.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.es.service.ElasticsearchDocumentService;
import org.springblade.es.service.ElasticsearchIndexService;
import org.springblade.es.vo.EsParam;
import org.springblade.modules.answerRecord.entity.AnswerRecordEntity;
import org.springblade.modules.answerRecord.vo.AnswerRecordVO;
import org.springblade.modules.answerRecord.wrapper.AnswerRecordWrapper;
import org.springblade.modules.subjectChoices.vo.SubjectChoicesVO;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
/**
 * es 全文检索
@@ -67,7 +55,7 @@
    @GetMapping("/update")
    @ApiOperationSupport(order = 4)
    public R update(EsParam esParam) {
        elasticsearchDocumentService.update(esParam,null);
        elasticsearchDocumentService.update(esParam,null,null);
        return R.status(true);
    }
src/main/java/org/springblade/es/service/ElasticsearchDocumentService.java
@@ -27,10 +27,13 @@
import org.springblade.es.vo.EsParam;
import org.springblade.modules.article.entity.Article;
import org.springblade.modules.article.service.ArticleService;
import org.springblade.modules.house.entity.HouseEntity;
import org.springblade.modules.house.entity.HouseholdEntity;
import org.springblade.modules.house.service.IHouseService;
import org.springblade.modules.house.service.IHouseholdService;
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.service.IPlaceService;
import org.springblade.modules.place.vo.PlaceVO;
import org.springframework.beans.factory.annotation.Autowired;
@@ -65,6 +68,9 @@
    @Value("${elasticsearch.sync}")
    private boolean elasticsearchSync;
    @Value("${elasticsearch.indexName}")
    private String indexName;
    /**
     * 检查索引是否已存在
@@ -462,12 +468,12 @@
            //            .type(MultiMatchQueryBuilder.Type.BEST_FIELDS)
            //        );
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.should(QueryBuilders.matchQuery("content", esParam.getSearchKey()));
            boolQueryBuilder.should(QueryBuilders.matchQuery("name", esParam.getSearchKey()));
//            boolQueryBuilder.should(QueryBuilders.matchQuery("content", esParam.getSearchKey()));
//            boolQueryBuilder.should(QueryBuilders.matchQuery("name", esParam.getSearchKey()));
            boolQueryBuilder.should(QueryBuilders.matchQuery("title", esParam.getSearchKey()));
            boolQueryBuilder.should(QueryBuilders.matchQuery("location", esParam.getSearchKey()));
            boolQueryBuilder.should(QueryBuilders.matchQuery("phone", esParam.getSearchKey()));
            boolQueryBuilder.should(QueryBuilders.matchQuery("idCard", 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));
@@ -566,7 +572,7 @@
     * 修改数据
     */
    @Async
    public void update(EsParam esParam, T item) {
    public void update(EsParam esParam, T item,List<String> columnList) {
        if (elasticsearchSync) {
            // 创建更新请求
            UpdateByQueryRequest updateRequest = new UpdateByQueryRequest(esParam.getIndexName());
@@ -580,8 +586,18 @@
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("tableName", esParam.getTableName());
            new CommonParamSet<>().setFieldValueByMap(item.getClass(), item, EsTableConstant.articleList, 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_house")){
                setHouseMap((HouseEntity) item,data);
            }
            if (esParam.getTableName().equals("jczz_household")){
                setHousehouldMap((HouseholdEntity) item,data);
            }
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("data", data);
@@ -601,6 +617,58 @@
    }
    /**
     * 设置公告map
     * @param item
     * @param data
     */
    private void setArticleMap(Article item, Map<String, Object> data) {
        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());
    }
    /**
     * 设置场所map
     * @param item
     * @param data
     */
    private void setPlaceMap(PlaceEntity item, Map<String, Object> data) {
        data.put("communityCode",placeService.getCommunityCode(item.getId()));
        data.put("tableId",item.getId());
        data.put("title",item.getPlaceName());
        data.put("name",item.getPrincipal());
        data.put("phone",item.getPrincipalPhone());
        data.put("idCard",item.getPrincipalIdCard());
        data.put("content",item.getLocation());
    }
    /**
     * 设置房屋map
     * @param item
     * @param data
     */
    private void setHouseMap(HouseEntity item, Map<String, Object> data) {
        data.put("communityCode",houseService.getCommunityCode(item.getId()));
        data.put("tableId",item.getId());
        data.put("title",item.getHouseName());
    }
    /**
     * 设置住户map
     * @param item
     * @param data
     */
    private void setHousehouldMap(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("name",item.getName());
        data.put("phone",item.getPhoneNumber());
        data.put("idCard",item.getIdCard());
        data.put("content",item.getCurrentAddress());
    }
    /**
     * 删除数据--根据条件
     */
    public boolean removeByQuery(EsParam esParam) {
src/main/java/org/springblade/modules/article/controller/ArticleController.java
@@ -178,19 +178,6 @@
        }
        article.setUpdateTime(new Date());
        boolean saveOrUpdate = articleService.saveOrUpdate(article);
        if (saveOrUpdate){
            EsParam esParam = new EsParam();
            esParam.setIndexName("test");
            esParam.setTableName("jczz_article");
            if (flag) {
                // 更新到es库
                elasticsearchDocumentService.addArticle(esParam, article);
            }else {
                esParam.setTableId(article.getId().toString());
                // 更新
                elasticsearchDocumentService.update(esParam,article);
            }
        }
        return R.status(saveOrUpdate);
    }
@@ -206,15 +193,6 @@
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        List<Long> longs = Func.toLongList(ids);
        boolean removeByIds = articleService.removeByIds(longs);
        if (removeByIds){
            for (Long id : longs) {
                EsParam esParam = new EsParam();
                esParam.setIndexName("test");
                esParam.setTableName("jczz_article");
                esParam.setTableId(id.toString());
                elasticsearchDocumentService.removeByQuery(esParam);
            }
        }
        return R.status(removeByIds);
    }
src/main/java/org/springblade/modules/place/entity/PlaceEntity.java
@@ -199,7 +199,4 @@
    @ApiModelProperty(value = "1:是 2:否  三级消防单位", example = "")
    @TableField("three_fire_protection")
    private Integer threeFireProtection;
}
src/main/resources/application-dev.yml
@@ -89,3 +89,4 @@
  enabled: false
  host: localhost
  sync: true
  indexName: jczz
src/main/resources/application-prod.yml
@@ -77,3 +77,4 @@
  enabled: false
  host: localhost
  sync: false
  indexName: jczz
src/main/resources/application-test.yml
@@ -77,3 +77,4 @@
  enabled: false
  host: localhost
  sync: true
  indexName: test