Explorar el Código

mq填充优化

24282 hace 1 año
padre
commit
1a6426ec1a

+ 25 - 48
ruoyi-framework/src/main/java/com/ruoyi/framework/mybatis/RemoveParam.java

@@ -6,14 +6,12 @@ import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.annotation.FieldFill;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableName;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.baomidou.mybatisplus.extension.service.IService;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.config.BeanPostProcessor;
 import org.springframework.context.annotation.Configuration;
 
 import java.lang.reflect.Field;
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -26,57 +24,36 @@ public class RemoveParam implements BeanPostProcessor {
     @Override
     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
 
-        if (bean instanceof ServiceImpl) {
-            Class<?> beanClass = bean.getClass();
-            try {
-                // spring代理对象类名转换成普通对象类名
-                String name = beanClass.getName().split("\\$")[0];
-                Class<?> aClass = Class.forName(name);
-                Type genericSuperclass = aClass.getGenericSuperclass();
-                if (genericSuperclass instanceof ParameterizedType) {
-                    ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
-                    Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
-                    Type actualTypeArgument = actualTypeArguments[1];
+        if (bean instanceof IService) {
+            Class<?> entityClass = ((IService<?>) bean).getEntityClass();
 
-                    Class<?> entityClass = Class.forName(actualTypeArgument.getTypeName());
-
-                    String tableName;
-                    TableName annotation = entityClass.getAnnotation(TableName.class);
-                    if (annotation != null && annotation.value() != null) {
-                        tableName = annotation.value();
-                    } else {
-                        tableName = StrUtil.toUnderlineCase(entityClass.getSimpleName());
-                    }
-
-                    Field[] fields = ReflectUtil.getFields(entityClass);
-
-                    // 字段
-                    for (Field field : fields) {
-                        TableField tableField = field.getAnnotation(TableField.class);
-                        if (tableField == null) {
-                            continue;
-                        }
+            TableName annotation = entityClass.getAnnotation(TableName.class);
+            String tableName;
+            if (annotation != null && StrUtil.isNotBlank(annotation.value())) {
+                tableName = annotation.value();
+            } else {
+                tableName = StrUtil.toUnderlineCase(entityClass.getSimpleName());
+            }
 
-                        FieldFill fill = tableField.fill();
-                        if (ObjectUtil.notEqual(fill, FieldFill.INSERT_UPDATE)) {
-                            continue;
-                        }
+            Field[] fields = ReflectUtil.getFields(entityClass);
+            for (Field field : fields) {
 
-                        if (ObjectUtil.equals(tableField.value(), "update_time") || field.getName().equals("updateTime")) {
-                            RemoveParam.updateTimeMap.put(tableName, true);
-                            continue;
-                        }
+                TableField tableField = field.getAnnotation(TableField.class);
+                if (tableField == null) {
+                    continue;
+                }
 
-                        if (ObjectUtil.equals(tableField.value(), "update_user") || field.getName().equals("updateUser")) {
-                            RemoveParam.updateUserMap.put(tableName, true);
-                        }
-                    }
+                FieldFill fill = tableField.fill();
+                if (ObjectUtil.notEqual(fill, FieldFill.INSERT_UPDATE) && ObjectUtil.notEqual(fill, FieldFill.UPDATE)) {
+                    continue;
+                }
 
-                    RemoveParam.updateTimeMap.putIfAbsent(tableName, false);
-                    RemoveParam.updateUserMap.putIfAbsent(tableName, false);
+                if ("update_time".equals(tableField.value()) || "updateTime".equals(field.getName())) {
+                    RemoveParam.updateTimeMap.put(tableName, true);
+                } else if ("update_user".equals(tableField.value()) || "updateUser".equals(field.getName())) {
+                    RemoveParam.updateUserMap.put(tableName, true);
                 }
-            } catch (ClassNotFoundException e) {
-                throw new RuntimeException(e);
+
             }
         }
 

+ 19 - 26
ruoyi-framework/src/main/java/com/ruoyi/framework/mybatis/interceptor/LogicInterceptor.java

@@ -132,41 +132,34 @@ public class LogicInterceptor extends BaseMultiTableInnerInterceptor implements
         }
 
         // 处理删除
-        if (sct == SqlCommandType.DELETE) {
+        else if (Objects.equals(sct, SqlCommandType.DELETE)) {
             if (LogicHolder.getLogicHolder()) {
                 return;
             }
             PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql();
-            String sql = mpBs.sql();
-            if (sql.trim().toLowerCase().startsWith("update")) {
-                return;
-            }
-            try {
-                Delete delete = (Delete) CCJSqlParserUtil.parse(sql);
-                Table table = delete.getTable();
-                if (isSkip(table)) {
-                    return;
-                }
 
-                String tableName = table.getName();
+            Delete delete;
+            try {
+                delete = (Delete) CCJSqlParserUtil.parse(mpBs.sql());
+            } catch (JSQLParserException e) {
+                throw new RuntimeException(e);
+            }
 
-                Boolean updateUser = RemoveParam.updateUserMap.get(tableName);
-                Boolean updateTime = RemoveParam.updateTimeMap.get(tableName);
+            if (isSkip(delete.getTable())) {
+                return;
+            }
 
-                sql = "UPDATE " + tableName + " SET " +
-                        delFlagName + " = " + delValue +
-                        (Boolean.TRUE.equals(updateUser) ? ", update_user = " + getUserUniqueId() : StringPool.EMPTY) +
-                        (Boolean.TRUE.equals(updateTime) ? ", update_time = '" + DateUtil.now() + "'" : StringPool.EMPTY);
+            String tableName = delete.getTable().getName();
+            Boolean updateUserExist = RemoveParam.updateUserMap.getOrDefault(tableName, false);
+            Boolean updateTimeExist = RemoveParam.updateTimeMap.getOrDefault(tableName, false);
+            Expression where = delete.getWhere();
 
-                Expression where = delete.getWhere();
-                if (where != null) {
-                    sql += " WHERE " + where;
-                }
+            String sql = "UPDATE " + tableName + " SET " + delFlagName + " = " + delValue +
+                    (updateUserExist ? ", update_user = " + getUserUniqueId() : StringPool.EMPTY) +
+                    (updateTimeExist ? ", update_time = '" + DateUtil.now() + "'" : StringPool.EMPTY) +
+                    (where != null ? " WHERE " + where : StringPool.EMPTY);
 
-                mpBs.sql(sql);
-            } catch (JSQLParserException e) {
-                throw new RuntimeException(e);
-            }
+            mpBs.sql(sql);
         }
     }