|
@@ -1,5 +1,6 @@
|
|
|
package com.ruoyi.framework.mybatis.interceptor;
|
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
|
import com.alibaba.druid.stat.JdbcDataSourceStat;
|
|
@@ -13,6 +14,9 @@ import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.inner.BaseMultiTableInnerInterceptor;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.ruoyi.common.utils.SecurityUtils;
|
|
|
+import com.ruoyi.framework.config.ThreadPoolConfig;
|
|
|
+import com.ruoyi.framework.mybatis.RemoveParam;
|
|
|
import com.ruoyi.framework.mybatis.holder.LogicHolder;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import net.sf.jsqlparser.JSQLParserException;
|
|
@@ -43,10 +47,8 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
|
import java.sql.Connection;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
|
|
|
@Slf4j
|
|
|
@Component
|
|
@@ -74,23 +76,29 @@ public class LogicInterceptor extends BaseMultiTableInnerInterceptor implements
|
|
|
*/
|
|
|
private static final Map<String, List<String>> includeLogicIdTableNameMap = new HashMap<>();
|
|
|
|
|
|
- public LogicInterceptor(DataSource dataSource) {
|
|
|
+ public LogicInterceptor(DataSource dataSource, ThreadPoolConfig threadPoolConfig) {
|
|
|
DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) dataSource;
|
|
|
Map<String, DataSource> dataSources = dynamicRoutingDataSource.getDataSources();
|
|
|
+
|
|
|
+ List<CompletableFuture<Void>> list = new ArrayList<>();
|
|
|
dataSources.forEach((k, v) -> {
|
|
|
- // 获取dataSourceStat
|
|
|
- JdbcDataSourceStat dataSourceStat = ((DruidDataSource) ((ItemDataSource) v).getRealDataSource()).getDataSourceStat();
|
|
|
- // 获取链接url
|
|
|
- String url = dataSourceStat.getUrl();
|
|
|
- // 获取数据库名
|
|
|
- String dbName = url.split("/")[3].split("\\?")[0];
|
|
|
- // 查询包含逻辑删除字段的表名sql
|
|
|
- String sql = "SELECT table_name FROM information_schema.COLUMNS WHERE table_schema = ? AND column_name = ?";
|
|
|
- // 执行sql找出不包含逻辑删除字段的表名
|
|
|
- List<String> notContainsDelFlagTableNameList = new JdbcTemplate(v).queryForList(sql, String.class, dbName, delFlagName);
|
|
|
- // 数据赋值
|
|
|
- includeLogicIdTableNameMap.put(dataSourceStat.getName(), notContainsDelFlagTableNameList);
|
|
|
+ CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
|
|
|
+ // 获取dataSourceStat
|
|
|
+ JdbcDataSourceStat dataSourceStat = ((DruidDataSource) ((ItemDataSource) v).getRealDataSource()).getDataSourceStat();
|
|
|
+ // 获取链接url
|
|
|
+ String url = dataSourceStat.getUrl();
|
|
|
+ // 获取数据库名
|
|
|
+ String dbName = url.split("/")[3].split("\\?")[0];
|
|
|
+ // 查询包含逻辑删除字段的表名sql
|
|
|
+ String sql = "SELECT table_name FROM information_schema.COLUMNS WHERE table_schema = ? AND column_name = ?";
|
|
|
+ // 执行sql找出不包含逻辑删除字段的表名
|
|
|
+ List<String> notContainsDelFlagTableNameList = new JdbcTemplate(v).queryForList(sql, String.class, dbName, delFlagName);
|
|
|
+ // 数据赋值
|
|
|
+ includeLogicIdTableNameMap.put(dataSourceStat.getName(), notContainsDelFlagTableNameList);
|
|
|
+ }, threadPoolConfig.threadPoolTaskExecutor());
|
|
|
+ list.add(completableFuture);
|
|
|
});
|
|
|
+ list.forEach(CompletableFuture::join);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -123,12 +131,21 @@ public class LogicInterceptor extends BaseMultiTableInnerInterceptor implements
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- sql = "UPDATE " + table.getName() + " SET " + delFlagName + " = " + delValue;
|
|
|
+ String tableName = table.getName();
|
|
|
+
|
|
|
+ Boolean updateUser = RemoveParam.updateUserMap.get(tableName);
|
|
|
+ Boolean updateTime = RemoveParam.updateTimeMap.get(tableName);
|
|
|
+
|
|
|
+ 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);
|
|
|
|
|
|
Expression where = delete.getWhere();
|
|
|
if (where != null) {
|
|
|
sql += " WHERE " + where;
|
|
|
}
|
|
|
+
|
|
|
mpBs.sql(sql);
|
|
|
} catch (JSQLParserException e) {
|
|
|
throw new RuntimeException(e);
|
|
@@ -310,4 +327,16 @@ public class LogicInterceptor extends BaseMultiTableInnerInterceptor implements
|
|
|
return !tableNameList.contains(name);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户id
|
|
|
+ */
|
|
|
+ private Long getUserUniqueId() {
|
|
|
+ try {
|
|
|
+ return SecurityUtils.getUserId();
|
|
|
+ } catch (Exception e) {
|
|
|
+ return -1L;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|