add
This commit is contained in:
@ -1,7 +1,20 @@
|
||||
package com.qiaoba.module.tenant.handle;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Snowflake;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.DbUtil;
|
||||
import cn.hutool.db.sql.SqlExecutor;
|
||||
import com.qiaoba.common.database.utils.JdbcUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* MysqlHandler
|
||||
*
|
||||
@ -10,14 +23,55 @@ import org.springframework.stereotype.Component;
|
||||
* @since 2023/6/5 16:28
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class MysqlHandler {
|
||||
|
||||
@Value("${qiaoba.datasource.master.driver}")
|
||||
private String driver;
|
||||
@Value("${qiaoba.datasource.master.url}")
|
||||
private String url;
|
||||
@Value("${qiaoba.datasource.master.username}")
|
||||
private String username;
|
||||
@Value("${qiaoba.datasource.master.password}")
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 角色-超管ID
|
||||
*/
|
||||
private static final ThreadLocal<Long> ROLE_SUPER_ADMIN_ID = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 用户-超管ID
|
||||
*/
|
||||
private static final ThreadLocal<Long> USER_SUPER_ADMIN_ID = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 处理字段模式
|
||||
*/
|
||||
private void handleColumnType() {
|
||||
|
||||
public void handleColumnType(String tenantId) {
|
||||
log.info("Mysql-Column-Start");
|
||||
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = JdbcUtil.getConnection(driver, url, username, password);
|
||||
// 处理 sys_config
|
||||
handleSysConfig(conn, tenantId);
|
||||
// 处理 sys_post
|
||||
handleSysPost(conn, tenantId);
|
||||
// 处理 sys_role
|
||||
handleSysRole(conn, tenantId);
|
||||
// 处理 sys_user
|
||||
handleSysUser(conn, tenantId);
|
||||
// 处理 sys_user_role
|
||||
bindUserAndRole(conn, tenantId);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
DbUtil.close(conn);
|
||||
ROLE_SUPER_ADMIN_ID.remove();
|
||||
USER_SUPER_ADMIN_ID.remove();
|
||||
}
|
||||
log.info("Mysql-Column-End");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -35,4 +89,73 @@ public class MysqlHandler {
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void handleSysConfig(Connection conn, String tenantId) throws Exception {
|
||||
handleSql(conn, tenantId, "data/sys_config_data");
|
||||
}
|
||||
|
||||
private void handleSql(Connection conn, String tenantId, String fileName) throws Exception {
|
||||
ClassPathResource resource = new ClassPathResource(fileName);
|
||||
List<String> lines = FileUtil.readLines(resource.getFile(), Charset.defaultCharset());
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String line : lines) {
|
||||
if (StrUtil.isNotBlank(line)) {
|
||||
Snowflake snowflake = new Snowflake();
|
||||
line = StrUtil.format(line, snowflake.nextId(), tenantId);
|
||||
sb.append(line);
|
||||
}
|
||||
}
|
||||
if (StrUtil.isNotBlank(sb.toString())) {
|
||||
SqlExecutor.execute(conn, sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private void handleSysPost(Connection conn, String tenantId) throws Exception {
|
||||
handleSql(conn, tenantId, "data/sys_post_data");
|
||||
}
|
||||
|
||||
private void handleSysRole(Connection conn, String tenantId) throws Exception {
|
||||
handleUserOrRole(conn, tenantId, "data/sys_role_data", true);
|
||||
}
|
||||
|
||||
private void handleUserOrRole(Connection conn, String tenantId, String fileName, Boolean isRole) throws Exception {
|
||||
ClassPathResource resource = new ClassPathResource(fileName);
|
||||
List<String> lines = FileUtil.readLines(resource.getFile(), Charset.defaultCharset());
|
||||
StringBuilder sb = new StringBuilder();
|
||||
long superAdminId = new Snowflake().nextId();
|
||||
if (isRole) {
|
||||
ROLE_SUPER_ADMIN_ID.set(superAdminId);
|
||||
} else {
|
||||
USER_SUPER_ADMIN_ID.set(superAdminId);
|
||||
}
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
String line = lines.get(i);
|
||||
if (StrUtil.isBlank(line)) {
|
||||
continue;
|
||||
}
|
||||
if (i == 0) {
|
||||
// 第1行是超管
|
||||
line = StrUtil.format(line, superAdminId, tenantId);
|
||||
} else {
|
||||
Snowflake snowflake = new Snowflake();
|
||||
line = StrUtil.format(line, snowflake.nextId(), tenantId);
|
||||
}
|
||||
sb.append(line);
|
||||
}
|
||||
if (StrUtil.isNotBlank(sb.toString())) {
|
||||
SqlExecutor.execute(conn, sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private void handleSysUser(Connection conn, String tenantId) throws Exception {
|
||||
handleUserOrRole(conn, tenantId, "data/sys_user_data", false);
|
||||
}
|
||||
|
||||
private void bindUserAndRole(Connection conn, String tenantId) throws Exception {
|
||||
// user_id role_id tenant_id
|
||||
String sql = StrUtil.format("INSERT INTO `sys_user_role` VALUES ({}, {}, {});",
|
||||
USER_SUPER_ADMIN_ID.get(),
|
||||
ROLE_SUPER_ADMIN_ID.get(), tenantId);
|
||||
SqlExecutor.execute(conn, sql);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,24 @@
|
||||
package com.qiaoba.module.tenant.runner;
|
||||
|
||||
import com.qiaoba.module.tenant.handle.MysqlHandler;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/6 15:23
|
||||
*/
|
||||
//@Component
|
||||
@RequiredArgsConstructor
|
||||
public class TestRunner implements ApplicationRunner {
|
||||
|
||||
private final MysqlHandler mysqlHandler;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
mysqlHandler.handleColumnType("2");
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
INSERT INTO `sys_post` VALUES ({}, 'ceo', '总经理', 1, '1', 'admin', '2023-05-19 14:06:53', 'admin', '2023-05-22 14:56:56', NULL, {});
|
||||
INSERT INTO `sys_post` VALUES ({}, 'cto', '技术总监', 2, '1', 'admin', '2023-05-19 14:07:06', 'admin', '2023-05-22 14:57:10', NULL,{});
|
||||
INSERT INTO `sys_post` VALUES ({}, 'hr', '人事', 3, '1', 'admin', '2023-05-19 14:07:16', '', NULL, NULL, {});
|
||||
INSERT INTO `sys_post` VALUES ({}, 'acct', '会计', 4, '1', 'admin', '2023-05-19 14:07:51', '', NULL, NULL, {});
|
@ -0,0 +1,2 @@
|
||||
INSERT INTO `sys_role` VALUES ({}, '超级管理员', 'admin', 1, '4', '1', 'admin', '2023-04-23 14:35:29', 'admin', '2023-05-31 11:05:52', '超级管理员', {});
|
||||
INSERT INTO `sys_role` VALUES ({}, '测试', 'test', 1, '1', '1', 'admin', '2023-05-22 14:38:31', '', NULL, NULL, {});
|
@ -0,0 +1 @@
|
||||
INSERT INTO `sys_user` VALUES ({}, NULL, 'admin', '超级管理员', '', '', '0', '', '$2a$10$mDfrzPMZuxvKeKmqUPA4hOgNC2Zdgb8vOgVL8hP8IIdT6fAWpe0Bm', '1', '', NULL, '', NULL, NULL, {});
|
Reference in New Issue
Block a user