package com.ruoyi.web.controller.interceptor;
|
|
import com.ruoyi.framework.web.service.TokenService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.ibatis.mapping.MappedStatement;
|
import org.apache.ibatis.mapping.SqlCommandType;
|
import org.apache.ibatis.plugin.Interceptor;
|
import org.apache.ibatis.plugin.Invocation;
|
import org.apache.ibatis.plugin.Plugin;
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import java.lang.reflect.Field;
|
import java.util.*;
|
|
@Slf4j
|
/*@Component
|
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })*/
|
public class MybatisInterceptor implements Interceptor {
|
|
@Autowired
|
private TokenService tokenService;
|
|
@Override
|
public Object intercept(Invocation invocation) throws Throwable {
|
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
|
log.debug("{}:"+mappedStatement);
|
log.debug("------sqlId------" + mappedStatement.getId());
|
if("com.ruoyi.system.mapper.SysLogininforMapper.insertLogininfor".equals(mappedStatement.getId())){
|
return invocation.proceed();
|
}
|
// sql类型:insert、update、select、delete
|
SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
|
Object parameter = invocation.getArgs()[1];
|
log.debug("------sqlCommandType------" + sqlCommandType);
|
|
if (parameter == null) {
|
return invocation.proceed();
|
}
|
|
// 当sql为新增或更新类型时,自动填充操作人相关信息
|
if (SqlCommandType.INSERT == sqlCommandType) {
|
|
Field[] fields = getAllFields(parameter);
|
for (Field field : fields) {
|
try {
|
// 注入创建人
|
if ("createBy".equals(field.getName())) {
|
// 获取当前登录用户信息
|
if(Objects.nonNull(tokenService.getLoginUser())){
|
String userName = tokenService.getLoginUser().getUser().getUserName();
|
field.setAccessible(true);
|
field.set(parameter, userName);
|
field.setAccessible(false);
|
}
|
}
|
//注入创建时间
|
if ("createTime".equals(field.getName())) {
|
field.setAccessible(true);
|
field.set(parameter, new Date());
|
field.setAccessible(false);
|
}
|
} catch (Exception e) {
|
log.error("failed to insert data, exception = ", e);
|
}
|
}
|
}
|
if (SqlCommandType.UPDATE == sqlCommandType) {
|
Field[] fields = getAllFields(parameter);
|
for (Field field : fields) {
|
try {
|
if ("updateBy".equals(field.getName())) {
|
// 获取当前登录用户信息
|
if(Objects.nonNull(tokenService.getLoginUser())){
|
String userName = tokenService.getLoginUser().getUser().getUserName();
|
field.setAccessible(true);
|
field.set(parameter, userName);
|
field.setAccessible(false);
|
}
|
}
|
if ("updateTime".equals(field.getName())) {
|
field.setAccessible(true);
|
field.set(parameter, new Date());
|
field.setAccessible(false);
|
}
|
} catch (Exception e) {
|
log.error("failed to update data, exception = ", e);
|
}
|
}
|
}
|
return invocation.proceed();
|
}
|
|
@Override
|
public Object plugin(Object target) {
|
return Plugin.wrap(target, this);
|
}
|
|
@Override
|
public void setProperties(Properties properties) {
|
// TODO Auto-generated method stub
|
}
|
|
/**
|
* 获取类的所有属性,包括父类
|
*
|
* @param object
|
* @return
|
*/
|
private Field[] getAllFields(Object object) {
|
Class<?> clazz = object.getClass();
|
List<Field> fieldList = new ArrayList<>();
|
while (clazz != null) {
|
fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
|
clazz = clazz.getSuperclass();
|
}
|
Field[] fields = new Field[fieldList.size()];
|
fieldList.toArray(fields);
|
return fields;
|
}
|
|
}
|