|
@@ -1,163 +0,0 @@
|
|
|
-package com.github.jfcloud.gene.common.interceptor.interceptor;
|
|
|
-
|
|
|
-import cn.hutool.core.util.ReflectUtil;
|
|
|
-import com.github.jfcloud.gene.common.entity.BaseEntity;
|
|
|
-import com.github.jfcloud.gene.common.interceptor.anno.AutoStuff;
|
|
|
-import com.github.jfcloud.gene.common.interceptor.enums.AnnoEnum;
|
|
|
-import com.github.jfcloud.gene.common.util.UserUtil;
|
|
|
-import lombok.extern.log4j.Log4j2;
|
|
|
-import org.apache.ibatis.executor.Executor;
|
|
|
-import org.apache.ibatis.mapping.MappedStatement;
|
|
|
-import org.apache.ibatis.mapping.SqlCommandType;
|
|
|
-import org.apache.ibatis.plugin.Interceptor;
|
|
|
-import org.apache.ibatis.plugin.Intercepts;
|
|
|
-import org.apache.ibatis.plugin.Invocation;
|
|
|
-import org.apache.ibatis.plugin.Signature;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import java.lang.reflect.Field;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
-
|
|
|
-@Log4j2
|
|
|
-@Component
|
|
|
-@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
|
|
|
-public class StuffInterceptor implements Interceptor {
|
|
|
-
|
|
|
- @Override
|
|
|
- public Object intercept(Invocation invocation) throws Throwable {
|
|
|
- if (invocation.getTarget() instanceof Executor && invocation.getArgs().length == 2) {
|
|
|
-
|
|
|
- final Executor executor = (Executor) invocation.getTarget();
|
|
|
- // 获取第一个参数
|
|
|
- final MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
|
|
|
- final Object paramObj = invocation.getArgs()[1];
|
|
|
-
|
|
|
- if (ms.getSqlCommandType() == SqlCommandType.INSERT) {
|
|
|
- return this.executeInsert(executor, ms, paramObj);
|
|
|
- }
|
|
|
-
|
|
|
- if (ms.getSqlCommandType() == SqlCommandType.UPDATE) {
|
|
|
- return this.executeUpdate(executor, ms, paramObj);
|
|
|
- }
|
|
|
- }
|
|
|
- return invocation.proceed();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 新增操作
|
|
|
- */
|
|
|
- private Object executeInsert(final Executor executor, final MappedStatement ms, final Object paramObj) throws Exception {
|
|
|
-
|
|
|
- // 获取所有字段
|
|
|
- final Field[] fields = ReflectUtil.getFields(paramObj.getClass());
|
|
|
- for (final Field field : fields) {
|
|
|
- // 设置该字段对象的可访问标志
|
|
|
- field.setAccessible(true);
|
|
|
- // 判断字段是否有我们的AutoStuff注解
|
|
|
- if (field.isAnnotationPresent(AutoStuff.class)) {
|
|
|
- // 获取该注解
|
|
|
- AutoStuff autoStuff = field.getAnnotation(AutoStuff.class);
|
|
|
- // 获取注解里写的annoType
|
|
|
- String annoType = autoStuff.annoType().getAnnoType();
|
|
|
- // 获取原来的值
|
|
|
- Object originalValue = field.get(paramObj);
|
|
|
- if (Objects.isNull(originalValue)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- // 先进行字段类型判断,再判断归属,所有信息都是通过operInfo接口获取的
|
|
|
- if (AnnoEnum.TENANT_ID.getAnnoType().equals(annoType)) {
|
|
|
- field.set(paramObj, UserUtil.getTenantId());
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (AnnoEnum.DEPT_ID.getAnnoType().equals(annoType)) {
|
|
|
- field.set(paramObj, UserUtil.getDeptId());
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (AnnoEnum.CREATE_BY.getAnnoType().equals(annoType)) {
|
|
|
- field.set(paramObj, UserUtil.getUserName());
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (AnnoEnum.CREATE_TIME.getAnnoType().equals(annoType)) {
|
|
|
- field.set(paramObj, new Date());
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (AnnoEnum.UPDATE_BY.getAnnoType().equals(annoType)) {
|
|
|
- field.set(paramObj, UserUtil.getUserName());
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (AnnoEnum.UPDATE_TIME.getAnnoType().equals(annoType)) {
|
|
|
- field.set(paramObj, new Date());
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return executor.update(ms, paramObj);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @Title: executeUpdate
|
|
|
- * @Description: 编辑操作
|
|
|
- * @param: executor
|
|
|
- * @param: ms
|
|
|
- * @param: paramObj
|
|
|
- * @param: user
|
|
|
- * @return:
|
|
|
- * @author: hejin
|
|
|
- * @date: 2022/8/15 11:05
|
|
|
- */
|
|
|
- @SuppressWarnings({"unchecked"})
|
|
|
- private Object executeUpdate(final Executor executor, final MappedStatement ms, final Object paramObj) throws Exception {
|
|
|
- if (paramObj instanceof Map) {
|
|
|
- Map<String, Object> map = (Map<String, Object>) paramObj;
|
|
|
- Map<String, Object> newParam = new HashMap<>(map.size());
|
|
|
- for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
- String key = entry.getKey();
|
|
|
- Object value = entry.getValue();
|
|
|
- if (Objects.isNull(value)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (!(value instanceof BaseEntity)) {
|
|
|
- newParam.put(key, value);
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- final Field[] fields = ReflectUtil.getFields(value.getClass());
|
|
|
- for (final Field field : fields) {
|
|
|
- field.setAccessible(true);
|
|
|
- AutoStuff autoStuff = field.getAnnotation(AutoStuff.class);
|
|
|
- if (autoStuff != null) {
|
|
|
- String annoType = autoStuff.annoType().getAnnoType();
|
|
|
- if (AnnoEnum.UPDATE_BY.getAnnoType().equals(annoType)) {
|
|
|
- field.set(value, UserUtil.getUserName());
|
|
|
- }
|
|
|
- if (AnnoEnum.UPDATE_TIME.getAnnoType().equals(annoType)) {
|
|
|
- field.set(value, new Date());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- newParam.put(key, value);
|
|
|
- }
|
|
|
- return executor.update(ms, newParam);
|
|
|
- }
|
|
|
-
|
|
|
- final Field[] fields = ReflectUtil.getFields(paramObj.getClass());
|
|
|
- for (final Field field : fields) {
|
|
|
- field.setAccessible(true);
|
|
|
- AutoStuff autoStuff = field.getAnnotation(AutoStuff.class);
|
|
|
- if (autoStuff != null) {
|
|
|
- String annoType = autoStuff.annoType().getAnnoType();
|
|
|
- if (AnnoEnum.UPDATE_BY.getAnnoType().equals(annoType)) {
|
|
|
- field.set(paramObj, UserUtil.getUserName());
|
|
|
- }
|
|
|
- if (AnnoEnum.UPDATE_TIME.getAnnoType().equals(annoType)) {
|
|
|
- field.set(paramObj, new Date());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return executor.update(ms, paramObj);
|
|
|
- }
|
|
|
-}
|