zhongrj
2024-04-09 66b6525861e566adb3db8f06f824de7751fbca9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//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) {
//        // 可以接收配置属性
//    }
//}