//package com.ruoyi.web.controller.interceptor;
|
//
|
//import com.ruoyi.framework.web.service.TokenService;
|
//import lombok.extern.slf4j.Slf4j;
|
//import org.apache.ibatis.executor.Executor;
|
//import org.apache.ibatis.mapping.MappedStatement;
|
//import org.apache.ibatis.mapping.SqlCommandType;
|
//import org.apache.ibatis.plugin.*;
|
//import org.springframework.beans.factory.annotation.Autowired;
|
//import org.springframework.stereotype.Component;
|
//
|
//import java.lang.reflect.Field;
|
//import java.time.LocalDateTime;
|
//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];
|
// 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];
|
//
|
// 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, LocalDateTime.now());
|
// 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;
|
// }
|
//
|
//}
|