Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,114 @@

package com.frontleaves.mastercontrol.config.init;

import com.frontleaves.mastercontrol.constant.SystemConstant;
import com.frontleaves.mastercontrol.dao.InfoDAO;
import com.frontleaves.mastercontrol.dao.RoleDAO;
import com.frontleaves.mastercontrol.dao.TableDAO;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

/**
* 系统初始化
* <p>
* 该类用于系统初始化;
* 该类使用 {@code @Configuration} 注解标记;
*
* @author xiao_lfeng
* @version v1.0.0
* @since 2024/09/10
* @author xiao_lfeng
*/
@Slf4j
@Configuration
@RequiredArgsConstructor
public class Initial {
private final TableDAO tableDAO;
private final InfoDAO infoDAO;
private final RoleDAO roleDAO;
private final JdbcTemplate jdbcTemplate;

private PrepareAlgorithm prepare;

@PostConstruct
public void init() {
log.info("[INIT] 系统初始化");
log.info("========== Start of Initialization ==========");
//初始化算法
prepare = new PrepareAlgorithm(tableDAO, infoDAO, roleDAO, jdbcTemplate);

//进行检查和初始化
this.initSqlCheck();
this.initInfoTableCheck();
this.initGlobalVariableAssignment();
this.initRole();

}

/**
* 初始化结束
*
* @return {@link CommandLineRunner} 命令行运行器
*/
@Bean
public CommandLineRunner initFinal() {
return args -> {
log.info("=========== End of Initialization ===========");
System.out.print("""
\u001B[38;5;111m _____ __ __ __ \u001B[32m____ __ _ \s
\u001B[38;5;111m / ___/_____/ /_ ___ ____/ /_ __/ /__ \u001B[32m/ __ \\/ /___ _____ ____ (_)___ ____ _
\u001B[38;5;111m \\__ \\/ ___/ __ \\/ _ \\/ __ / / / / / _ \\ \u001B[32m/ /_/ / / __ `/ __ \\/ __ \\/ / __ \\/ __ `/
\u001B[38;5;111m ___/ / /__/ / / / __/ /_/ / /_/ / / __/ \u001B[32m/ ____/ / /_/ / / / / / / / / / / / /_/ /\s
\u001B[38;5;111m/____/\\___/_/ /_/\\___/\\__,_/\\__,_/_/\\___/\u001B[32m /_/ /_/\\__,_/_/ /_/_/ /_/_/_/ /_/\\__, / \s
\u001B[32m /____/ \s
""");
System.out.println("\t\t\t\u001B[33m::: " + SystemConstant.SYSTEM_AUTHOR + " :::\t\t\t\t\t\t\t ::: "+ SystemConstant.SYSTEM_VERSION +" :::\u001B[0m");
};
}

/**
* 初始化数据库表完整性检查
*/
private void initSqlCheck() {
log.info("[INIT] 检查表");
prepare.checkTable("mc_info");
prepare.checkTable("mc_logs");
prepare.checkTable("mc_role");
prepare.checkTable("mc_user");
}

/**
* 初始化 mc_info 表检查
*/
private void initInfoTableCheck() {
log.info("[INIT] 数据表 mc_info 缺陷检查...");

prepare.checkInfoTableFields("system_initial_mode", "true");
prepare.checkInfoTableFields("system_debug_mode", "true");
prepare.checkInfoTableFields("system_super_admin_uuid", null);
prepare.checkInfoTableFields("system_test_user_uuid", null);
}
/**
* 初始化全局变量赋值
*/
private void initGlobalVariableAssignment() {
log.info("[INIT] 全局变量初始化...");
SystemConstant.isInitialMode = prepare.initGetGlobalVariable("system_initial_mode");
SystemConstant.isDebugMode = Boolean.parseBoolean(prepare.initGetGlobalVariable("system_debug_mode"));
SystemConstant.superAdminUUID = prepare.initGetGlobalVariable("system_super_admin_uuid");
SystemConstant.testUserUUID = prepare.initGetGlobalVariable("system_test_user_uuid");
}
/**
* 初始化角色
*/
private void initRole() {
log.info("[INIT] 初始化角色...");

prepare.initRole("ADMIN", "管理员", "拥有软件的所有权限,包括用户管理、角色管理、日志管理、信息管理等。");
prepare.initRole("USER", "用户", "拥有软件的部分权限,包括日志查看、信息查看等。");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.frontleaves.mastercontrol.config.init;

import com.frontleaves.mastercontrol.dao.InfoDAO;
import com.frontleaves.mastercontrol.dao.RoleDAO;
import com.frontleaves.mastercontrol.dao.TableDAO;
import com.frontleaves.mastercontrol.model.entity.InfoDO;
import com.frontleaves.mastercontrol.model.entity.RoleDO;
import com.frontleaves.mastercontrol.model.entity.TableDO;
import com.xlf.utility.util.UuidUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
*准备算法
* @since v1.0.0
* @version v1.0.0
* @author FLASHLACK1314
*
*/
@Slf4j
@RequiredArgsConstructor
public class PrepareAlgorithm {
private final TableDAO tableDAO;
private final InfoDAO infoDAO;
private final RoleDAO roleDAO;
private final JdbcTemplate jdbcTemplate;

/**
* 检查表
* <p>
* 该方法用于检查表;当表不存在时,创建表;若表存在,忽略数据库的检查。
*
* @param tableName 表名
*/
public void checkTable(String tableName) {
TableDO tableDO = tableDAO.lambdaQuery().eq(TableDO::getTableName, tableName).one();
if (tableDO == null) {
ClassPathResource classPathResource = new ClassPathResource("/templates/sql/" + tableName + ".sql");
try {
String getSql = FileCopyUtils.copyToString(new InputStreamReader(classPathResource.getInputStream(), StandardCharsets.UTF_8));
getSql = getSql.replaceAll("(?s)/\\*.*?\\*/", "");
for (String sql : getSql.split(";")) {
jdbcTemplate.execute(sql);
}
log.debug("[INIT] 创建表 | {}", tableName);
} catch (IOException e) {
log.error("[INIT] 读取SQL文件失败 | {}", e.getMessage(), e);
}
}
}

/**
* 检查信息表字段
* <p>
* 该方法用于检查信息表字段,当字段不存在时,创建字段;若字段存在,忽略数据库的检查。
*
* @param key 键
* @param value 值
*/
public void checkInfoTableFields(String key, String value) {
InfoDO infoDO = infoDAO.lambdaQuery().eq(InfoDO::getKey, key).one();
if (infoDO == null) {
InfoDO newInfo = new InfoDO()
.setKey(key)
.setValue(value);
infoDAO.save(newInfo);
log.debug("[INIT] 创建信息表字段 | <{}>{}", key, value);
}
}

/**
* 获取全局变量
* <p>
*该方法用于获取全局变量;当全局变量不存在时,初始化全局变量。
*/
public String initGetGlobalVariable(String key) {
InfoDO infoDO = infoDAO.lambdaQuery().eq(InfoDO::getKey, key).one();
if (infoDO == null) {
return null;
}
return infoDO.getValue();
}

/**
* 初始化角色
* @param name 角色名
* @param displayName 显示名
* @param desc 描述
*/
public void initRole(String name, String displayName, String desc) {
RoleDO roleDO = roleDAO.lambdaQuery().eq(RoleDO::getName, name).one();
if (roleDO == null) {
RoleDO newRole = new RoleDO()
.setRoleUuid(UuidUtil.generateStringUuid())
.setName(name)
.setDisplayName(displayName)
.setRoleDesc(desc);
roleDAO.save(newRole);
log.debug("[INIT] 创建角色 | [{}]{}", name, displayName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.frontleaves.mastercontrol.constant;
/**
* 系统常量
* <p>
* 该类用于定义系统中的常量信息;
*
* @since v1.0.0
* @version v1.0.0
* @author FLASHLACK1314
*/
public class SystemConstant {
/**
* 系统名称
*/
public static final String SYSTEM_NAME = "master control";
/**
* 系统中文名称
*/
public static final String SYSTEM_CHINESE_NAME = "主控";
/**
* 系统版权
*/
public static final String SYSTEM_COPYRIGHT = "2023 锋楪技术(深圳)有限公司 . All Rights Reserved. ";
/**
* 系统中文版权
*/
public static final String SYSTEM_CHINESE_COPYRIGHT = "版权所有 2023 锋楪技术(深圳)有限公司 保留所有权利。";
/**
* 系统版本
*/
public static final String SYSTEM_VERSION = "v1.0.0";
/**
* 作者信息
*/
public static final String SYSTEM_AUTHOR = "XiaoLFeng and FLASHLACK1314";
/**
* 作者邮箱
*/
public static final String SYSTEM_AUTHOR_EMAIL = "gm@x-lf.cn";
/**
* 作者网址
*/
public static final String SYSTEM_AUTHOR_URL = "https://www.x-lf.com";
/**
* 系统许可证
*/
public static final String SYSTEM_LICENSE = "MIT";
/**
* 系统许可证URL
*/
public static final String SYSTEM_LICENSE_URL = "https://opensource.org/license/MIT";
/**
* 系统免责声明
*/
public static final String SYSTEM_DISCLAIMER = "Since this project is in the model design stage, we are not responsible for any losses caused by using this project for commercial purposes. If you modify the code and redistribute it, you need to clearly indicate what changes you made in the corresponding file. If you want to modify it for commercial use, please contact me.";
/**
* 系统中文免责声明
*/
public static final String SYSTEM_CHINESE_DISCLAIMER = "由于该项目处于模型设计阶段,我们不对使用该项目进行商业用途造成的任何损失负责。如果您修改了代码并重新分发它,您需要清楚地指出您在相应文件中所做的更改。如果您想将其用于商业用途,请与我联系。";
/**
* 系统关于
*/
public static final String SYSTEM_ABOUT = "The project contains the source code of com.frontleaves.mastercontrol. All source code for this project is licensed under the MIT open source license.";
/**
* 系统中文关于
*/
public static final String SYSTEM_CHINESE_ABOUT = "该项目包含 com.xlf.schedule 的源代码。该项目的所有源代码均根据 MIT 开源许可证授权。";
/**
* 系统许可证声明
*/
public static final String SYSTEM_LICENSE_STATEMENT = "For more information about the MIT license, please view the LICENSE file in the project root directory or visit: https://opensource.org/license/MIT";
/**
* 系统中文许可证声明
*/
public static final String SYSTEM_CHINESE_LICENSE_STATEMENT = "有关 MIT 许可证的更多信息,请查看项目根目录中的 LICENSE 文件或访问: https://opensource.org/license/MIT";

/**
* 是否是初始化模式
*/
public static String isInitialMode;

/**
* 是否是调试模式
*/
public static Boolean isDebugMode;

/**
* 超级管理员UUID
*/
public static String superAdminUUID;

/**
* 测试用户UUID
*/
public static String testUserUUID;
}
20 changes: 20 additions & 0 deletions src/main/java/com/frontleaves/mastercontrol/dao/InfoDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.frontleaves.mastercontrol.dao;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frontleaves.mastercontrol.mapper.InfoMapper;
import com.frontleaves.mastercontrol.model.entity.InfoDO;
import org.springframework.stereotype.Repository;

/**
* 信息数据访问对象
* <p>
* 该类用于定义信息数据访问对象
* 该类使用 {@link Repository} 注解标记
* @since v1.0.0
* @version v1.0.0
* @author FLASHLACK1314
*/
@Repository
public class InfoDAO extends ServiceImpl<InfoMapper, InfoDO> implements IService<InfoDO> {
}
20 changes: 20 additions & 0 deletions src/main/java/com/frontleaves/mastercontrol/dao/LogsDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.frontleaves.mastercontrol.dao;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frontleaves.mastercontrol.mapper.LogsMapper;
import com.frontleaves.mastercontrol.model.entity.LogsDO;
import org.springframework.stereotype.Repository;

/**
* 日志数据访问对象
* <p>
* 该类用于定义日志数据访问对象
* 该类使用 {@link Repository} 注解标记
* @since v1.0.0
* @version v1.0.0
* @author FLASHLACK1314
*/
@Repository
public class LogsDAO extends ServiceImpl<LogsMapper, LogsDO> implements IService<LogsDO> {
}
21 changes: 21 additions & 0 deletions src/main/java/com/frontleaves/mastercontrol/dao/RoleDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.frontleaves.mastercontrol.dao;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frontleaves.mastercontrol.mapper.RoleMapper;
import com.frontleaves.mastercontrol.model.entity.RoleDO;
import org.springframework.stereotype.Repository;

/**
* 角色数据访问对象
* <p>
* 该类用于定义角色数据访问对象
* 该类使用 {@link Repository} 注解标记
* @since v1.0.0
* @version v1.0.0
* @author FLASHLACK1314
*
*/
@Repository
public class RoleDAO extends ServiceImpl<RoleMapper, RoleDO> implements IService<RoleDO> {
}
Loading