保存日志加上前后变化的数据
This commit is contained in:
parent
0d2d0773d1
commit
46a8ec705e
@ -1,4 +1,4 @@
|
||||
package com.zhgd.xmgl.security;
|
||||
package com.zhgd.interceptor;
|
||||
|
||||
import com.zhgd.xmgl.constant.Cts;
|
||||
import com.zhgd.xmgl.security.util.SecurityUtils;
|
||||
@ -1,39 +1,56 @@
|
||||
package com.zhgd.mybatis;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
||||
import com.zhgd.annotation.DataScope;
|
||||
import com.zhgd.jeecg.common.util.SpringContextUtils;
|
||||
import com.zhgd.xmgl.constant.Cts;
|
||||
import com.zhgd.xmgl.entity.dto.OperLogDataChange;
|
||||
import com.zhgd.xmgl.entity.dto.OperLogInsertChange;
|
||||
import com.zhgd.xmgl.security.util.SecurityUtils;
|
||||
import com.zhgd.xmgl.util.EnvironmentUtil;
|
||||
import com.zhgd.xmgl.util.LogMdcUtil;
|
||||
import com.zhgd.xmgl.util.PrintColorUtil;
|
||||
import com.zhgd.xmgl.util.ThreadLocalUtil;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.schema.Table;
|
||||
import net.sf.jsqlparser.statement.insert.Insert;
|
||||
import net.sf.jsqlparser.statement.select.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
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.mapping.ParameterMapping;
|
||||
import org.apache.ibatis.mapping.SqlCommandType;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
import org.apache.ibatis.session.ResultHandler;
|
||||
import org.apache.ibatis.session.RowBounds;
|
||||
import org.apache.ibatis.type.TypeHandlerRegistry;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.text.DateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
@Slf4j
|
||||
public class DataScopeInterceptor extends JsqlParserSupport implements InnerInterceptor {
|
||||
@ -192,6 +209,105 @@ public class DataScopeInterceptor extends JsqlParserSupport implements InnerInte
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
|
||||
try {
|
||||
if (Objects.equals(ThreadLocalUtil.getByKey(Cts.TL_IS_FROM_WEB, Boolean.class), true)) {
|
||||
SqlCommandType sct = ms.getSqlCommandType();
|
||||
if (sct == SqlCommandType.UPDATE || sct == SqlCommandType.DELETE) {
|
||||
String sql = this.getShowSql(ms.getConfiguration(), ms.getBoundSql(parameter));
|
||||
String whereSql = StrUtil.subAfter(sql, "WHERE", true);
|
||||
saveQueryResult(StrUtil.sub(ms.getId(), 0, StringUtils.lastIndexOf(ms.getId(), ".")), whereSql);
|
||||
} else if (sct == SqlCommandType.INSERT) {
|
||||
List<OperLogInsertChange> paramList = ThreadLocalUtil.getByKey(Cts.TL_INSERT_BEFORE_PARAM, List.class);
|
||||
if (paramList == null) {
|
||||
paramList = new ArrayList<>();
|
||||
ThreadLocalUtil.addInKey(Cts.TL_INSERT_BEFORE_PARAM, paramList);
|
||||
}
|
||||
OperLogInsertChange operLogInsertChange = new OperLogInsertChange();
|
||||
operLogInsertChange.setMapperName(StrUtil.sub(ms.getId(), 0, StringUtils.lastIndexOf(ms.getId(), ".")));
|
||||
operLogInsertChange.setResult(new ArrayList<>(Arrays.asList(parameter)));
|
||||
operLogInsertChange.setTimestamp(System.currentTimeMillis());
|
||||
paramList.add(operLogInsertChange);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("前后数据变化错误", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveQueryResult(String mapperName, String whereSql) throws ClassNotFoundException {
|
||||
QueryWrapper<Object> wrapper = Wrappers.query().last("WHERE " + whereSql);
|
||||
Object mapperObj = SpringContextUtils.getBean(Class.forName(mapperName));
|
||||
Method selectListMethod = ReflectUtil.getMethod(mapperObj.getClass(), "selectList", QueryWrapper.class);
|
||||
Object rs = ReflectUtil.invoke(mapperObj, selectListMethod, wrapper);
|
||||
List<OperLogDataChange> paramList = ThreadLocalUtil.getByKey(Cts.TL_UPDATE_DEL_BEFORE_PARAM, List.class);
|
||||
if (paramList == null) {
|
||||
paramList = new ArrayList<>();
|
||||
ThreadLocalUtil.addInKey(Cts.TL_UPDATE_DEL_BEFORE_PARAM, paramList);
|
||||
}
|
||||
OperLogDataChange operLogDataChange = new OperLogDataChange();
|
||||
operLogDataChange.setMapperName(mapperName);
|
||||
operLogDataChange.setWhereSql(whereSql);
|
||||
operLogDataChange.setResult(rs);
|
||||
operLogDataChange.setTimestamp(System.currentTimeMillis());
|
||||
paramList.add(operLogDataChange);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取完整的sql
|
||||
*
|
||||
* @param configuration
|
||||
* @param boundSql
|
||||
* @return
|
||||
*/
|
||||
private String getShowSql(Configuration configuration, BoundSql boundSql) {
|
||||
Object parameterObject = boundSql.getParameterObject();
|
||||
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
|
||||
String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
|
||||
if (parameterMappings != null && parameterMappings.size() > 0 && parameterObject != null) {
|
||||
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
|
||||
if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
|
||||
sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(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("\\?", Matcher.quoteReplacement(getParameterValue(obj)));
|
||||
} else if (boundSql.hasAdditionalParameter(propertyName)) {
|
||||
Object obj = boundSql.getAdditionalParameter(propertyName);
|
||||
sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(getParameterValue(obj)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return sql;
|
||||
}
|
||||
|
||||
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) + "'";
|
||||
} else if (obj instanceof LocalDate) {
|
||||
value = "'" + ((LocalDate) obj).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "'";
|
||||
} else if (obj instanceof LocalDateTime) {
|
||||
value = "'" + ((LocalDateTime) obj).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "'";
|
||||
|
||||
} else {
|
||||
if (obj != null) {
|
||||
value = obj.toString();
|
||||
} else {
|
||||
value = "";
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processSelect(Select select, int index, String sql, Object obj) {
|
||||
this.processSelectBody(select.getSelectBody(), obj);
|
||||
|
||||
@ -1,16 +1,33 @@
|
||||
package com.zhgd.xmgl.config;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.zhgd.annotation.OperLog;
|
||||
import com.zhgd.jeecg.common.util.SpringContextUtils;
|
||||
import com.zhgd.xmgl.constant.Cts;
|
||||
import com.zhgd.xmgl.entity.dto.OperLogDataChange;
|
||||
import com.zhgd.xmgl.entity.dto.OperLogDataChangeField;
|
||||
import com.zhgd.xmgl.entity.dto.OperLogInsertChange;
|
||||
import com.zhgd.xmgl.modules.basicdata.entity.OperationLog;
|
||||
import com.zhgd.xmgl.modules.basicdata.service.IOperationLogService;
|
||||
import com.zhgd.xmgl.security.util.SecurityUtils;
|
||||
import com.zhgd.xmgl.util.IpUtil;
|
||||
import com.zhgd.xmgl.util.ThreadLocalUtil;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
@ -24,9 +41,14 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @program: wisdomSite
|
||||
@ -39,6 +61,8 @@ import java.util.stream.Collectors;
|
||||
@Slf4j
|
||||
public class OperLogAspect {
|
||||
|
||||
Pattern logNamePattern = Pattern.compile("(.+):(\\d+:.+;)+");
|
||||
Pattern logNamePattern1 = Pattern.compile("(\\d+)(\\D+)");
|
||||
@Autowired
|
||||
private IOperationLogService operationLogService;
|
||||
|
||||
@ -57,7 +81,6 @@ public class OperLogAspect {
|
||||
public void operExceptionLogPoinCut() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 正常返回通知,拦截用户操作日志,连接点正常执行完成后执行, 如果连接点抛出异常,则不会执行
|
||||
*
|
||||
@ -138,12 +161,317 @@ public class OperLogAspect {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
//前后数据变化
|
||||
List<OperLogDataChange> beforeList = ThreadLocalUtil.getByKey(Cts.TL_UPDATE_DEL_BEFORE_PARAM, List.class);
|
||||
List<OperLogInsertChange> insertList = ThreadLocalUtil.getByKey(Cts.TL_INSERT_BEFORE_PARAM, List.class);
|
||||
if (beforeList != null || insertList != null) {
|
||||
if (beforeList == null) {
|
||||
beforeList = new ArrayList<>();
|
||||
}
|
||||
List<OperLogDataChange> afterList = new ArrayList<>();
|
||||
for (OperLogDataChange jo : beforeList) {
|
||||
String mapperName = jo.getMapperName();
|
||||
String whereSql = jo.getWhereSql();
|
||||
Object queryResult = this.getQueryResult(mapperName, whereSql);
|
||||
OperLogDataChange operLogDataChange = new OperLogDataChange();
|
||||
operLogDataChange.setMapperName(mapperName);
|
||||
operLogDataChange.setWhereSql(whereSql);
|
||||
operLogDataChange.setResult(queryResult);
|
||||
operLogDataChange.setTimestamp(System.currentTimeMillis());
|
||||
afterList.add(operLogDataChange);
|
||||
}
|
||||
Pair<String, String> changePair = getBeforeAndAfterDataChange(beforeList, afterList);
|
||||
operlog.setDataChangeBefore(changePair.getLeft());
|
||||
operlog.setDataChangeAfter(changePair.getRight());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("处理前后数据异常", e);
|
||||
}
|
||||
operationLogService.save(operlog);
|
||||
} catch (Exception e) {
|
||||
log.error("error:", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前后数据变化
|
||||
*
|
||||
* @param beforeList
|
||||
* @param afterList
|
||||
* @return
|
||||
*/
|
||||
private Pair<String, String> getBeforeAndAfterDataChange(List<OperLogDataChange> beforeList, List<OperLogDataChange> afterList) {
|
||||
Map<String, List<OperLogDataChange>> beforeMapperNameMap = beforeList.stream().collect(Collectors.groupingBy(OperLogDataChange::getMapperName));
|
||||
Map<String, List<OperLogDataChange>> afterMapperNameMap = afterList.stream().collect(Collectors.groupingBy(OperLogDataChange::getMapperName));
|
||||
Set<String> mapperNames = new HashSet<>(CollUtil.addAll(new HashSet<>(beforeMapperNameMap.keySet()), new HashSet<>(afterMapperNameMap.keySet())));
|
||||
List<OperLogInsertChange> insertList = ThreadLocalUtil.getByKey(Cts.TL_INSERT_BEFORE_PARAM, List.class);
|
||||
Map<String, List<OperLogInsertChange>> insertMapperNameMap = new HashMap<>();
|
||||
if (CollUtil.isNotEmpty(insertList)) {
|
||||
List<String> names = insertList.stream().map(OperLogInsertChange::getMapperName).collect(Collectors.toList());
|
||||
mapperNames = new HashSet<>(CollUtil.addAll(mapperNames, names));
|
||||
insertMapperNameMap = insertList.stream().collect(Collectors.groupingBy(OperLogInsertChange::getMapperName));
|
||||
}
|
||||
List<OperLogDataChangeField> resultBeforeList = new ArrayList<>();
|
||||
List<OperLogDataChangeField> resultAfterList = new ArrayList<>();
|
||||
for (String mapperName : mapperNames) {
|
||||
List<OperLogDataChange> befores = beforeMapperNameMap.get(mapperName);
|
||||
Field idField;
|
||||
List beforeDataList = new ArrayList();
|
||||
if (befores != null) {
|
||||
//更新或删除时候
|
||||
idField = getIdField((List) befores.get(0).getResult());
|
||||
beforeDataList = getBeforeDataList(befores, idField);
|
||||
} else {
|
||||
//新增时候
|
||||
List<OperLogInsertChange> operLogInsertChanges = insertMapperNameMap.get(mapperName);
|
||||
idField = getIdField((List) operLogInsertChanges.get(0).getResult());
|
||||
}
|
||||
List afterDataList = getAfterDataList(idField, afterMapperNameMap.get(mapperName), insertMapperNameMap.get(mapperName));
|
||||
Map<String, Object> beforeDataIdMap = (Map<String, Object>) beforeDataList.stream().collect(Collectors.toMap(o -> {
|
||||
try {
|
||||
return idField.get(o).toString();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}, Function.identity()));
|
||||
Map<String, Object> afterDataIdMap = (Map<String, Object>) afterDataList.stream().collect(Collectors.toMap(o -> {
|
||||
try {
|
||||
return idField.get(o).toString();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}, Function.identity()));
|
||||
//对比集合的数据
|
||||
Field finalIdField = idField;
|
||||
List<String> idList = (List) CollUtil.addAll(beforeDataList, afterDataList).stream().map(o -> {
|
||||
try {
|
||||
return finalIdField.get(o).toString();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}).distinct().collect(Collectors.toList());
|
||||
for (String id : idList) {
|
||||
Object afterObj = afterDataIdMap.get(id);
|
||||
Object beforeObj = beforeDataIdMap.get(id);
|
||||
if (beforeObj != null && afterObj != null) {
|
||||
//更新时候,都不为null
|
||||
Field[] fields = afterObj.getClass().getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
field.setAccessible(true);
|
||||
if (!field.isAnnotationPresent(ApiModelProperty.class)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Object oldValue = field.get(beforeObj);
|
||||
Object newValue = field.get(afterObj);
|
||||
if (!Objects.equals(newValue, oldValue)) {
|
||||
resultBeforeList.add(getOperLogDataChangeField(field, oldValue));
|
||||
resultAfterList.add(getOperLogDataChangeField(field, newValue));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("更新时候报错", e);
|
||||
}
|
||||
}
|
||||
} else if (beforeObj != null && afterObj == null) {
|
||||
//删除时候
|
||||
Field[] fields = beforeObj.getClass().getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
field.setAccessible(true);
|
||||
if (!field.isAnnotationPresent(ApiModelProperty.class)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Object oldValue = field.get(beforeObj);
|
||||
if (oldValue != null) {
|
||||
resultBeforeList.add(getOperLogDataChangeField(field, oldValue));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("删除时候报错", e);
|
||||
}
|
||||
}
|
||||
} else if (beforeObj == null && afterObj != null) {
|
||||
//新增时候
|
||||
Field[] fields = afterObj.getClass().getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
field.setAccessible(true);
|
||||
if (!field.isAnnotationPresent(ApiModelProperty.class)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Object newValue = field.get(afterObj);
|
||||
if (newValue != null) {
|
||||
resultAfterList.add(getOperLogDataChangeField(field, newValue));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("新增时候报错", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ImmutablePair(StrUtil.join(",", resultBeforeList.stream().map(map -> map.getName() + ":" + map.getVal()).collect(Collectors.toList())),
|
||||
StrUtil.join(",", resultAfterList.stream().map(map -> map.getName() + ":" + map.getVal()).collect(Collectors.toList())));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前后变化的字段名和值
|
||||
*
|
||||
* @param field
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
private OperLogDataChangeField getOperLogDataChangeField(Field field, Object val) {
|
||||
OperLogDataChangeField bm = new OperLogDataChangeField();
|
||||
String name = field.getAnnotation(ApiModelProperty.class).value();
|
||||
String v = null;
|
||||
if (val instanceof Integer) {
|
||||
Matcher matcher = logNamePattern.matcher(name);
|
||||
if (matcher.matches()) {
|
||||
//如:类型:1:人员;2:人员照片;3:人员权限;4:固定车辆;5:固定车辆群组;6:临时车;7:车辆布防;
|
||||
for (String s : matcher.group(2).split(";")) {
|
||||
String[] split = s.split(":");
|
||||
if (Objects.equals(split[0], val.toString())) {
|
||||
v = split[1];
|
||||
name = name.split(":")[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
boolean isMatch = false;
|
||||
Matcher matcher1 = logNamePattern1.matcher(name);
|
||||
while (matcher1.find()) {
|
||||
isMatch = true;
|
||||
if (matcher1.group(1).equals(val.toString())) {
|
||||
if (name.contains(":")) {
|
||||
name = StrUtil.subBefore(name, ":", false);
|
||||
} else {
|
||||
name = field.getName();
|
||||
}
|
||||
v = matcher1.group(2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!isMatch) {
|
||||
name = field.getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (v == null) {
|
||||
v = getVal(val);
|
||||
}
|
||||
bm.setName(name);
|
||||
bm.setVal(v);
|
||||
return bm;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取值
|
||||
*
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
private String getVal(Object val) {
|
||||
if (val instanceof Date) {
|
||||
return DateUtil.formatDateTime((Date) val);
|
||||
} else {
|
||||
return Convert.toStr(val);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主键id的属性
|
||||
*
|
||||
* @param result
|
||||
* @return
|
||||
*/
|
||||
private Field getIdField(List result) {
|
||||
Field idField = null;
|
||||
Object obj = result.get(0);
|
||||
Field[] fields = obj.getClass().getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
field.setAccessible(true);
|
||||
if (field.isAnnotationPresent(TableId.class)) {
|
||||
idField = field;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return idField;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取之后的数据
|
||||
*
|
||||
* @param idField
|
||||
* @param afterList
|
||||
* @param insertList
|
||||
* @return
|
||||
*/
|
||||
private List getAfterDataList(Field idField, List<OperLogDataChange> afterList, List<OperLogInsertChange> insertList) {
|
||||
Stream stream1;
|
||||
if (afterList != null) {
|
||||
stream1 = afterList.stream().flatMap(map -> ((List) map.getResult()).stream());
|
||||
} else {
|
||||
stream1 = Collections.emptyList().stream();
|
||||
}
|
||||
if (CollUtil.isNotEmpty(insertList)) {
|
||||
Stream stream2 = insertList.stream().flatMap(map -> ((List) map.getResult()).stream());
|
||||
stream1 = Stream.concat(stream1, stream2);
|
||||
}
|
||||
List rtList = (List) stream1.collect(Collectors.collectingAndThen(
|
||||
Collectors.toCollection(() -> new TreeSet<>(
|
||||
Comparator.comparing(
|
||||
o -> {
|
||||
try {
|
||||
return idField.get(o).toString();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}))), ArrayList::new));
|
||||
return rtList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取之前的数据
|
||||
*
|
||||
* @param list
|
||||
* @param idField
|
||||
* @return
|
||||
*/
|
||||
private List getBeforeDataList(List<OperLogDataChange> list, Field idField) {
|
||||
return (List) list.stream().flatMap(map -> ((List) map.getResult()).stream()).collect(Collectors.collectingAndThen(
|
||||
Collectors.toCollection(() -> new TreeSet<>(
|
||||
Comparator.comparing(
|
||||
o -> {
|
||||
try {
|
||||
return idField.get(o).toString();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}))), ArrayList::new));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据mapperName和whereSql查询数据
|
||||
*
|
||||
* @param mapperName
|
||||
* @param whereSql
|
||||
* @return
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
private Object getQueryResult(String mapperName, String whereSql) throws ClassNotFoundException {
|
||||
QueryWrapper<Object> wrapper = Wrappers.query().last("WHERE " + whereSql);
|
||||
Object mapperObj = SpringContextUtils.getBean(Class.forName(mapperName));
|
||||
Method selectListMethod = ReflectUtil.getMethod(mapperObj.getClass(), "selectList", QueryWrapper.class);
|
||||
return ReflectUtil.invoke(mapperObj, selectListMethod, wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换request 请求参数
|
||||
*
|
||||
@ -156,5 +484,4 @@ public class OperLogAspect {
|
||||
}
|
||||
return rtnMap;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -47,6 +47,9 @@ public interface Cts {
|
||||
*/
|
||||
String TL_XZ_HIKVISION_COMPARE_DATA_RETRY = "tl_xz_hikvision_compare_data";
|
||||
String TL_IS_FROM_TASK = "isFromTask";
|
||||
/**
|
||||
* 是否请求接口
|
||||
*/
|
||||
String TL_IS_FROM_WEB = "isFromWeb";
|
||||
String TL_SEND_BATCH_WORKER_COUNT_DOWN_LATCH = "sendBatchWorkerCountDownLatch";
|
||||
|
||||
@ -64,6 +67,14 @@ public interface Cts {
|
||||
String HK_SYNC_AUTH_SUCCESS_KEY = "hkSyncAuthSuccess:";
|
||||
String TL_HK_SYNC_ID = "tl_hk_sync_id";
|
||||
String TL_IS_NOMAL_INTERFACE = "isNormalInterface";
|
||||
/**
|
||||
* 日志前后对比数据前参数,更新和删除
|
||||
*/
|
||||
String TL_UPDATE_DEL_BEFORE_PARAM = "tlUpdateDelBeforeParams";
|
||||
/**
|
||||
* 日志前后对比数据前参数,插入
|
||||
*/
|
||||
String TL_INSERT_BEFORE_PARAM = "tlInsertBeforeParams";
|
||||
/**
|
||||
* 忽略数据权限
|
||||
*/
|
||||
|
||||
@ -0,0 +1,11 @@
|
||||
package com.zhgd.xmgl.entity.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class OperLogDataChange {
|
||||
private String mapperName;
|
||||
private String whereSql;
|
||||
private Object result;
|
||||
private Long timestamp;
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package com.zhgd.xmgl.entity.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class OperLogDataChangeField {
|
||||
private String name;
|
||||
private String val;
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package com.zhgd.xmgl.entity.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class OperLogInsertChange {
|
||||
private String mapperName;
|
||||
private Object result;
|
||||
private Long timestamp;
|
||||
}
|
||||
@ -84,6 +84,14 @@ public class OperationLog implements Serializable {
|
||||
@Excel(name = "body值", width = 15)
|
||||
@ApiModelProperty(value="body值")
|
||||
private java.lang.String body ;
|
||||
/**数据变更前*/
|
||||
@Excel(name = "数据变更前", width = 15)
|
||||
@ApiModelProperty(value="数据变更前")
|
||||
private java.lang.String dataChangeBefore ;
|
||||
/**数据变更后*/
|
||||
@Excel(name = "数据变更后", width = 15)
|
||||
@ApiModelProperty(value="数据变更后")
|
||||
private java.lang.String dataChangeAfter ;
|
||||
@TableField(exist = false)
|
||||
private String projectSn;
|
||||
@TableField(exist = false)
|
||||
|
||||
@ -2,6 +2,7 @@ package com.zhgd.xmgl.modules.car.controller;
|
||||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
|
||||
@ -43,13 +43,13 @@ public class XzHikvisionSync implements Serializable {
|
||||
* 1人员2人员照片3人员权限4固定车辆5固定车辆群组6临时车7车辆布防
|
||||
*/
|
||||
@Excel(name = "1人员2人员照片3人员权限4固定车辆5固定车辆群组6临时车7车辆布防", width = 15)
|
||||
@ApiModelProperty(value = "1人员2人员照片3人员权限4固定车辆5固定车辆群组6临时车7车辆布防")
|
||||
@ApiModelProperty(value = "类型:1:人员;2:人员照片;3:人员权限;4:固定车辆;5:固定车辆群组;6:临时车;7:车辆布防;")
|
||||
private java.lang.Integer type;
|
||||
/**
|
||||
* 1新增2修改3删除
|
||||
*/
|
||||
@Excel(name = "1新增2修改3删除", width = 15)
|
||||
@ApiModelProperty(value = "1新增2修改3删除")
|
||||
@ApiModelProperty(value = "操作:1新增2修改3删除")
|
||||
private java.lang.Integer operate;
|
||||
/**
|
||||
* 设备sn
|
||||
@ -85,7 +85,7 @@ public class XzHikvisionSync implements Serializable {
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
|
||||
@ApiModelProperty(value = "更新时间")
|
||||
private java.util.Date updateDate;
|
||||
@ApiModelProperty(value = "1人员2车辆")
|
||||
@ApiModelProperty(value = "类型:1人员2车辆")
|
||||
private java.lang.Integer bigType;
|
||||
@TableField(exist = false)
|
||||
@ApiModelProperty(value = "详情")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user