java110
2020-07-07 70862b9228ccb2156971a22085ac31b6c8b6d2cd
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.java110.db;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.client.RestTemplate;
import com.java110.core.factory.Java110TransactionalFactory;
import com.java110.db.dao.IQueryServiceDAO;
import com.java110.dto.order.OrderItemDto;
import com.java110.utils.constant.ServiceConstant;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.StringUtil;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
 
import java.text.DateFormat;
import java.util.*;
 
@Intercepts({
        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class,
                Object.class})
})
public class Java110MybatisInterceptor implements Interceptor {
    private static Logger logger = LoggerFactory.getLogger(Java110MybatisInterceptor.class);
 
    IQueryServiceDAO queryServiceDAOImpl;
    RestTemplate restTemplate;
 
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        if (StringUtil.isEmpty(Java110TransactionalFactory.getOId())) { //未开启事务
            return invocation.proceed();
        }
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
        Object parameter = null;
        if (invocation.getArgs().length > 1) {
            parameter = invocation.getArgs()[1];
        }
 
        BoundSql boundSql = mappedStatement.getBoundSql(parameter);
        Configuration configuration = mappedStatement.getConfiguration();
        //获取sql语句
        String sql = showSql(configuration, boundSql);
        restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
        switch (sqlCommandType) {
            case INSERT:
                dealInsertSql(mappedStatement, parameter, sql);
                break;
            case UPDATE:
                dealUpdateSql(mappedStatement, parameter, sql);
                break;
            case DELETE:
                dealDeleteSql(mappedStatement, parameter, sql);
                break;
        }
        return invocation.proceed();
    }
 
    /**
     * 处理删除sql
     *
     * @param mappedStatement
     * @param parameter
     */
    private void dealDeleteSql(MappedStatement mappedStatement, Object parameter, String sql) {
 
        String tmpTable = sql.substring(sql.indexOf("into") + 4, sql.indexOf("("));
        String tmpWhere = sql.substring(sql.indexOf("where"));
        //插入操作时之前的 没有数据 所以 preValue 为空对象
        JSONArray preValues = new JSONArray();
 
        String execSql = "select * from " + tmpTable + " " + tmpWhere;
 
        queryServiceDAOImpl = ApplicationContextFactory.getBean("queryServiceDAOImpl", IQueryServiceDAO.class);
        List<Map<String, Object>> deleteDatas = queryServiceDAOImpl.executeSql(execSql, null);
 
        if (deleteDatas != null && deleteDatas.size() > 0) {
            for (Map<String, Object> map : deleteDatas) {
                preValues.add(map);
            }
        }
 
        JSONArray afterValues = new JSONArray();
 
        JSONObject logText = new JSONObject();
        logText.put("preValue", preValues);
        logText.put("afterValue", afterValues);
 
        OrderItemDto orderItemDto = new OrderItemDto();
        orderItemDto.setbId("-1");
        orderItemDto.setAction("DEL");
        orderItemDto.setActionObj(tmpTable.trim());
        orderItemDto.setLogText(logText.toJSONString());
        orderItemDto.setServiceName(ApplicationContextFactory.getApplicationName());
        orderItemDto.setoId(Java110TransactionalFactory.getOId());
 
        String url = ServiceConstant.SERVICE_ORDER_URL + "/order/oIdApi/createOrderItem";
        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity httpEntity = new HttpEntity(orderItemDto.toString(), httpHeaders);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
 
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new IllegalArgumentException("注册事务回滚日志失败" + responseEntity);
        }
    }
 
    /**
     * 处理修改 sql
     *
     * @param mappedStatement
     * @param parameter
     */
    private void dealUpdateSql(MappedStatement mappedStatement, Object parameter, String sql) {
        //RestTemplate restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
 
        String tmpTable = sql.substring(sql.indexOf("update") + 6, sql.indexOf("set"));
        String tmpWhere = sql.substring(sql.indexOf("where"));
        String tmpKey = sql.substring(sql.indexOf("set") + 3, sql.indexOf("where"));
        String[] tmpString = tmpKey.split(",");
        Map tmpAfterMap = new HashMap();
        for (String tmp : tmpString) {
            String[] keyValues = tmp.split("=");
            String key = "";
            if (keyValues.length != 2) {
                throw new IllegalArgumentException("update 语句可能有问题,没有 set 中出错 " + sql);
            }
            if (keyValues[0].contains(".")) {
                key = keyValues[0].substring(keyValues[0].indexOf(".") + 1);
            } else {
                key = keyValues[0];
            }
            tmpAfterMap.put(key.trim(), keyValues[1].trim());
        }
 
 
        if (tmpString == null || tmpString.length < 1) {
            throw new IllegalArgumentException("update 语句可能有问题,没有 set 内容 " + sql);
        }
        //插入操作时之前的 没有数据 所以 preValue 为空对象
        JSONArray preValues = new JSONArray();
        JSONArray afterValues = new JSONArray();
        JSONObject afterVaule = null;
        String execSql = "select * from " + tmpTable + " " + tmpWhere;
 
        queryServiceDAOImpl = ApplicationContextFactory.getBean("queryServiceDAOImpl", IQueryServiceDAO.class);
        List<Map<String, Object>> deleteDatas = queryServiceDAOImpl.executeSql(execSql, null);
 
        if (deleteDatas != null && deleteDatas.size() > 0) {
            for (Map<String, Object> map : deleteDatas) {
                preValues.add(map);
                afterVaule = new JSONObject();
                afterVaule.putAll(map);
                afterVaule.putAll(tmpAfterMap);
                afterValues.add(afterVaule);
            }
        }
 
        JSONObject logText = new JSONObject();
        logText.put("preValue", preValues);
        logText.put("afterValue", afterValues);
 
        OrderItemDto orderItemDto = new OrderItemDto();
        orderItemDto.setbId("-1");
        orderItemDto.setAction("MOD");
        orderItemDto.setActionObj(tmpTable.trim());
        orderItemDto.setLogText(logText.toJSONString());
        orderItemDto.setServiceName(ApplicationContextFactory.getApplicationName());
        orderItemDto.setoId(Java110TransactionalFactory.getOId());
 
        String url = ServiceConstant.SERVICE_ORDER_URL + "/order/oIdApi/createOrderItem";
        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity httpEntity = new HttpEntity(orderItemDto.toString(), httpHeaders);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
 
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new IllegalArgumentException("注册事务回滚日志失败" + responseEntity);
        }
    }
 
    /**
     * 处理insert 语句
     *
     * @param mappedStatement
     * @param parameter
     */
    private void dealInsertSql(MappedStatement mappedStatement, Object parameter, String sql) {
 
        // RestTemplate restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
 
        //插入操作时之前的 没有数据 所以 preValue 为空对象
        JSONArray preValues = new JSONArray();
 
        JSONArray afterValues = new JSONArray();
        JSONObject afterValue = new JSONObject();
        String tmpTable = sql.substring(sql.toLowerCase().indexOf("into") + 4, sql.indexOf("("));
        String tmpKey = sql.substring(sql.indexOf("(") + 1, sql.indexOf(")"));
        String tmpValue = sql.substring(sql.lastIndexOf("(") + 1, sql.lastIndexOf(")"));
        String[] tmpKeys = tmpKey.split(",");
        String[] tmpValues = tmpValue.split(",");
 
        if (tmpKeys.length != tmpValues.length) {
            throw new IllegalArgumentException("sql 错误 key 和value 个数不等" + sql);
        }
 
        if (tmpKeys.length < 1) {
            throw new IllegalArgumentException("sql 错误 未找到key" + sql);
        }
        for (int keyIndex = 0; keyIndex < tmpKeys.length; keyIndex++) {
            afterValue.put(tmpKeys[keyIndex], tmpValues[keyIndex]);
        }
        afterValues.add(afterValue);
 
        JSONObject logText = new JSONObject();
        logText.put("preValue", preValues);
        logText.put("afterValue", afterValues);
 
        OrderItemDto orderItemDto = new OrderItemDto();
        orderItemDto.setbId("-1");
        orderItemDto.setAction("ADD");
        orderItemDto.setActionObj(tmpTable.trim());
        orderItemDto.setLogText(logText.toJSONString());
        orderItemDto.setServiceName(ApplicationContextFactory.getApplicationName());
        orderItemDto.setoId(Java110TransactionalFactory.getOId());
 
        String url = ServiceConstant.SERVICE_ORDER_URL + "/order/oIdApi/createOrderItem";
        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity httpEntity = new HttpEntity(orderItemDto.toString(), httpHeaders);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
 
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new IllegalArgumentException("注册事务回滚日志失败" + responseEntity);
        }
 
    }
 
    @Override
    public Object plugin(Object target) {
 
        return Plugin.wrap(target, this);
 
    }
 
    @Override
    public void setProperties(Properties properties) {
 
    }
 
    public String showSql(Configuration configuration, BoundSql boundSql) {
        Object parameterObject = boundSql.getParameterObject();
        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
        String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
        if (parameterMappings.size() > 0 && parameterObject != null) {
            TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
            if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
                sql = sql.replaceFirst("\\?", getParameterValue(parameterObject));
 
            } else {
                MetaObject metaObject = configuration.newMetaObject(parameterObject);
                for (ParameterMapping parameterMapping : parameterMappings) {
                    String propertyName = parameterMapping.getProperty();
                    if (metaObject.hasGetter(propertyName)) {
                        Object obj = metaObject.getValue(propertyName);
                        sql = sql.replaceFirst("\\?", getParameterValue(obj));
                    } else if (boundSql.hasAdditionalParameter(propertyName)) {
                        Object obj = boundSql.getAdditionalParameter(propertyName);
                        sql = sql.replaceFirst("\\?", getParameterValue(obj));
                    }
                }
            }
        }
        return sql.toLowerCase();
    }
 
    private String getParameterValue(Object obj) {
        String value = null;
        if (obj instanceof String) {
            value = "'" + obj.toString() + "'";
        } else if (obj instanceof Date) {
            DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
            value = "'" + formatter.format(obj) + "'";
//                System.out.println(value);
        } else {
            if (obj != null) {
                value = obj.toString();
            } else {
                value = "";
            }
 
        }
        return value;
    }
}