|
@@ -1,29 +1,43 @@
|
|
|
package com.lc.ibps.aop;
|
|
package com.lc.ibps.aop;
|
|
|
|
|
|
|
|
import cn.hutool.core.date.StopWatch;
|
|
import cn.hutool.core.date.StopWatch;
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.lc.ibps.base.core.util.BeanUtils;
|
|
|
|
|
+import com.lc.ibps.base.framework.table.ICommonDao;
|
|
|
import com.lc.ibps.sysdata.dao.UpdateDataTableDao;
|
|
import com.lc.ibps.sysdata.dao.UpdateDataTableDao;
|
|
|
import com.lc.ibps.untils.LogAopUtil;
|
|
import com.lc.ibps.untils.LogAopUtil;
|
|
|
|
|
+import com.lc.ibps.untils.SqlUtil;
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
import org.aspectj.lang.annotation.Around;
|
|
import org.aspectj.lang.annotation.Around;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
-import java.io.StringWriter;
|
|
|
|
|
-import java.util.Arrays;
|
|
|
|
|
-import java.util.Objects;
|
|
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
@Aspect
|
|
@Aspect
|
|
|
@Component
|
|
@Component
|
|
|
public class UpdateDBLogAspect {
|
|
public class UpdateDBLogAspect {
|
|
|
|
|
|
|
|
|
|
+ private static final String BATCH_DELETE_METHOD = "batchDelete";
|
|
|
|
|
+
|
|
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(UpdateDBLogAspect.class);
|
|
|
|
|
+
|
|
|
@Autowired
|
|
@Autowired
|
|
|
UpdateDataTableDao updateDataTableDao;
|
|
UpdateDataTableDao updateDataTableDao;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private ICommonDao commonDao;
|
|
|
|
|
+
|
|
|
// addDataContextTable() updateDataContextTable() updatesDatasContextTable()
|
|
// addDataContextTable() updateDataContextTable() updatesDatasContextTable()
|
|
|
// updatesBatchContextTable() batchDelete() deleteDataContextTable()
|
|
// updatesBatchContextTable() batchDelete() deleteDataContextTable()
|
|
|
|
|
|
|
@@ -34,20 +48,92 @@ public class UpdateDBLogAspect {
|
|
|
StopWatch stopWatch = new StopWatch();
|
|
StopWatch stopWatch = new StopWatch();
|
|
|
stopWatch.start();
|
|
stopWatch.start();
|
|
|
Object result = null;
|
|
Object result = null;
|
|
|
|
|
+ String oldData = "";
|
|
|
|
|
+ String tableName = "";
|
|
|
|
|
+ String tableComment = "";
|
|
|
try {
|
|
try {
|
|
|
|
|
+ if (BATCH_DELETE_METHOD.equals(point.getSignature().getName())){
|
|
|
|
|
+ String params = LogAopUtil.filterParam(point.getArgs());
|
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(params);
|
|
|
|
|
+ tableName = jsonObject.get("tableName").toString();
|
|
|
|
|
+ Map<String,Object> map = JSONObject.parseObject(jsonObject.get("paramWhere").toString());
|
|
|
|
|
+ String key = "";
|
|
|
|
|
+ String[] value =null;
|
|
|
|
|
+ List<String> quotedValues = new ArrayList<>();
|
|
|
|
|
+ for (Map.Entry<String,Object> entry: map.entrySet()) {
|
|
|
|
|
+ key=entry.getKey();
|
|
|
|
|
+ value =((String)entry.getValue()).split(",");
|
|
|
|
|
+ }
|
|
|
|
|
+ for (String str : value) {
|
|
|
|
|
+ quotedValues.add("'" + str + "'");
|
|
|
|
|
+ }
|
|
|
|
|
+ String inClause = "IN (" + String.join(",", quotedValues) + ")";
|
|
|
|
|
+ String query = String.format("SELECT * FROM %s WHERE %s %s", tableName, key, inClause);
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String,Object>> list = commonDao.query(query);
|
|
|
|
|
+ oldData = getOldData(list,tableName);
|
|
|
|
|
+ Map<String,Object> boTable = commonDao.queryOne("SELECT COMMENT_ from ibps_bo_table WHERE name_='"+tableName+"' limit 1");
|
|
|
|
|
+ if (BeanUtils.isNotEmpty(boTable) && BeanUtils.isNotEmpty(boTable.get("COMMENT_"))){
|
|
|
|
|
+ tableComment = boTable.get("COMMENT_").toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
result = point.proceed();
|
|
result = point.proceed();
|
|
|
} catch (Throwable throwable) {
|
|
} catch (Throwable throwable) {
|
|
|
- updateDataTableDao.insertLog(LogAopUtil.logError(request, point, throwable));
|
|
|
|
|
- throw throwable;
|
|
|
|
|
|
|
+ if (BATCH_DELETE_METHOD.equals(point.getSignature().getName())){
|
|
|
|
|
+ Map<String,Object> map = LogAopUtil.logError(request, point, throwable);
|
|
|
|
|
+ map.put("table_name" , tableName);
|
|
|
|
|
+ map.put("table_comment" , tableComment);
|
|
|
|
|
+ map.put("data_" , oldData);
|
|
|
|
|
+ String sql = SqlUtil.buildInsertSql(map,LogAopUtil.TABLE_NAME);
|
|
|
|
|
+ try {
|
|
|
|
|
+ commonDao.execute(sql);
|
|
|
|
|
+ }catch (Exception e){
|
|
|
|
|
+ logger.error(e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }else {
|
|
|
|
|
+ updateDataTableDao.insertLog(LogAopUtil.logError(request, point, throwable));
|
|
|
|
|
+ }
|
|
|
|
|
+ logger.error(throwable.getMessage(), throwable);
|
|
|
} finally {
|
|
} finally {
|
|
|
stopWatch.stop();
|
|
stopWatch.stop();
|
|
|
}
|
|
}
|
|
|
- if (ifAddLog(point.getSignature().getName())){
|
|
|
|
|
|
|
+ if (BATCH_DELETE_METHOD.equals(point.getSignature().getName())){
|
|
|
|
|
+ Map<String,Object> map = LogAopUtil.log(request, point, result, stopWatch);
|
|
|
|
|
+ map.put("table_name" , tableName);
|
|
|
|
|
+ map.put("table_comment" , tableComment);
|
|
|
|
|
+ map.put("data_" , oldData);
|
|
|
|
|
+ String sql = SqlUtil.buildInsertSql(map,LogAopUtil.TABLE_NAME);
|
|
|
|
|
+ try {
|
|
|
|
|
+ commonDao.execute(sql);
|
|
|
|
|
+ }catch (Exception e){
|
|
|
|
|
+ logger.error(e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }else if (ifAddLog(point.getSignature().getName())){
|
|
|
updateDataTableDao.insertLog(LogAopUtil.log( request, point, result, stopWatch));
|
|
updateDataTableDao.insertLog(LogAopUtil.log( request, point, result, stopWatch));
|
|
|
}
|
|
}
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private String getOldData(List<Map<String,Object>> list,String tableName){
|
|
|
|
|
+ JSONArray jsonArray = new JSONArray();
|
|
|
|
|
+ for (Map<String,Object> map : list) {
|
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
|
+ jsonObject.put("tableName",tableName);
|
|
|
|
|
+ for (Object key : map.keySet()){
|
|
|
|
|
+ jsonObject.put(key.toString(), map.get(key));
|
|
|
|
|
+ if (key.equals("create_time_")){
|
|
|
|
|
+ jsonObject.put("create_time_", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(map.get(key)));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (key.equals("update_time_")){
|
|
|
|
|
+ jsonObject.put("update_time_", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(map.get(key)));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ jsonArray.add(jsonObject);
|
|
|
|
|
+ }
|
|
|
|
|
+ return jsonArray.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public boolean ifAddLog(String methodName){
|
|
public boolean ifAddLog(String methodName){
|
|
|
String[] names = {"addDataContextTable","updateDataContextTable","updatesDatasContextTable",
|
|
String[] names = {"addDataContextTable","updateDataContextTable","updatesDatasContextTable",
|
|
|
"updatesBatchContextTable","batchDelete","deleteDataContextTable"};
|
|
"updatesBatchContextTable","batchDelete","deleteDataContextTable"};
|