From f0f6e53ba2005e111d127e26cfbf8ede2aa05793 Mon Sep 17 00:00:00 2001 From: XiaoLFeng Date: Tue, 16 Apr 2024 22:31:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BA=8615=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=A1=A8=E7=BB=93=E6=9E=84=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=EF=BC=8C=E8=B0=83=E6=95=B4=E4=BA=86=E9=83=A8=E5=88=86=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=A1=A8=E7=9A=84=E5=88=9B=E5=BB=BA=E8=AF=AD=E5=8F=A5?= =?UTF-8?q?=EF=BC=8C=E6=9B=B4=E6=96=B0=E4=BA=86=E6=95=B0=E6=8D=AE=E5=87=86?= =?UTF-8?q?=E5=A4=87=E8=84=9A=E6=9C=AC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jsl/oa/config/startup/PrepareData.java | 44 +++++++++++++++++++ .../config/startup/StartupConfiguration.java | 40 ++++++++++++++--- src/main/resources/mysql/oa_config.sql | 12 +++++ src/main/resources/mysql/oa_message.sql | 19 ++++++++ src/main/resources/mysql/oa_news.sql | 13 ++++++ src/main/resources/mysql/oa_news_user.sql | 15 +++++++ src/main/resources/mysql/oa_permissions.sql | 14 ++++++ src/main/resources/mysql/oa_project.sql | 24 ++++++++++ .../resources/mysql}/oa_project_child.sql | 2 - src/main/resources/mysql/oa_project_daily.sql | 19 ++++++++ .../resources/mysql}/oa_project_modules.sql | 2 - src/main/resources/mysql/oa_project_tags.sql | 17 +++++++ src/main/resources/mysql/oa_review.sql | 35 +++++++++++++++ src/main/resources/mysql/oa_role.sql | 10 +++++ .../resources/mysql/oa_role_permissions.sql | 10 +++++ src/main/resources/mysql/oa_role_user.sql | 14 ++++++ src/main/resources/mysql/oa_user.sql | 34 ++++++++++++++ src/main/resources/mysql/oa_user_tags.sql | 13 ++++++ .../main/resources/mysql}/organize_oa.sql | 0 19 files changed, 328 insertions(+), 9 deletions(-) create mode 100644 src/main/resources/mysql/oa_config.sql create mode 100644 src/main/resources/mysql/oa_message.sql create mode 100644 src/main/resources/mysql/oa_news.sql create mode 100644 src/main/resources/mysql/oa_news_user.sql create mode 100644 src/main/resources/mysql/oa_permissions.sql create mode 100644 src/main/resources/mysql/oa_project.sql rename {mysql => src/main/resources/mysql}/oa_project_child.sql (98%) create mode 100644 src/main/resources/mysql/oa_project_daily.sql rename {mysql => src/main/resources/mysql}/oa_project_modules.sql (98%) create mode 100644 src/main/resources/mysql/oa_project_tags.sql create mode 100644 src/main/resources/mysql/oa_review.sql create mode 100644 src/main/resources/mysql/oa_role.sql create mode 100644 src/main/resources/mysql/oa_role_permissions.sql create mode 100644 src/main/resources/mysql/oa_role_user.sql create mode 100644 src/main/resources/mysql/oa_user.sql create mode 100644 src/main/resources/mysql/oa_user_tags.sql rename {mysql => src/main/resources/mysql}/organize_oa.sql (100%) diff --git a/src/main/java/com/jsl/oa/config/startup/PrepareData.java b/src/main/java/com/jsl/oa/config/startup/PrepareData.java index 46449ee..a713f4c 100644 --- a/src/main/java/com/jsl/oa/config/startup/PrepareData.java +++ b/src/main/java/com/jsl/oa/config/startup/PrepareData.java @@ -2,8 +2,15 @@ package com.jsl.oa.config.startup; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.util.FileCopyUtils; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; @Slf4j @RequiredArgsConstructor @@ -32,4 +39,41 @@ public class PrepareData { ); } } + + /** + * 检查数据库是否完整 + *
+ * 检查数据库是否完整,若数据库保持完整则不进行任何操作,若数据库不完整将会创建对应的数据表 + * @param tableName 数据表名字 + */ + public void checkDatabase(String tableName) { + try { + jdbcTemplate.queryForObject( + "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME = ?", + String.class, + tableName + ); + } catch (DataAccessException e) { + log.debug("[Preparation] 创建数据表 {}", tableName); + // 读取文件 + PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); + // 读取 resources/mysql 目录下的所有 SQL 文件 + Resource resource = resolver.getResource("classpath:/mysql/" + tableName + ".sql"); + // 创建数据表 + try { + String sql = FileCopyUtils + .copyToString(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)); + // 分割 SQL 语句并执行 + jdbcTemplate.execute("USE organize_oa"); + String[] sqlStatements = sql.split(";"); + for (String statement : sqlStatements) { + if (!statement.trim().isEmpty()) { + jdbcTemplate.execute(statement.trim()); + } + } + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } } diff --git a/src/main/java/com/jsl/oa/config/startup/StartupConfiguration.java b/src/main/java/com/jsl/oa/config/startup/StartupConfiguration.java index 37b4a3e..04ab84b 100644 --- a/src/main/java/com/jsl/oa/config/startup/StartupConfiguration.java +++ b/src/main/java/com/jsl/oa/config/startup/StartupConfiguration.java @@ -52,13 +52,43 @@ public class StartupConfiguration { /** * 对数据库进行完整性检查 *
- * 对数据库进行完整性检查,检查数据库是否有数据缺失等信息 + * 对数据库进行完整性检查,检查数据库是否出现缺失数据表的情况,若出现缺失数据表的情况将会对数据表进行创建,若数据保持完整将不进行任何操作 */ @Bean @Order(2) - public CommandLineRunner roleDataPreparation() { + public CommandLineRunner checkDatabaseExist() { return args -> { log.info("[Preparation] 系统进行数据库完整性检查"); + // 数据表的检查 + prepareData.checkDatabase("oa_config"); + prepareData.checkDatabase("oa_user"); + prepareData.checkDatabase("oa_role"); + prepareData.checkDatabase("oa_permissions"); + prepareData.checkDatabase("oa_news"); + prepareData.checkDatabase("oa_project_tags"); + prepareData.checkDatabase("oa_project"); + prepareData.checkDatabase("oa_project_child"); + prepareData.checkDatabase("oa_project_modules"); + prepareData.checkDatabase("oa_review"); + prepareData.checkDatabase("oa_message"); + prepareData.checkDatabase("oa_news_user"); + prepareData.checkDatabase("oa_project_daily"); + prepareData.checkDatabase("oa_role_permissions"); + prepareData.checkDatabase("oa_role_user"); + prepareData.checkDatabase("oa_user_tags"); + }; + } + + /** + * 对数据表进行完整性检查 + *
+ * 对数据表进行完整性检查,检查数据表是否有数据缺失等信息 + */ + @Bean + @Order(3) + public CommandLineRunner roleDataPreparation() { + return args -> { + log.info("[Preparation] 系统进行数据表完整性检查"); // 检查角色信息是否完整 prepareData.checkRole("console", "超级管理员"); prepareData.checkRole("principal", "负责人"); @@ -73,7 +103,7 @@ public class StartupConfiguration { * 账户。 */ @Bean - @Order(3) + @Order(4) public CommandLineRunner defaultConsoleDataPreparation() { return args -> { log.info("[Preparation] 系统进行默认超级管理员信息检查"); @@ -133,7 +163,7 @@ public class StartupConfiguration { } @Bean - @Order(4) + @Order(5) public CommandLineRunner prepareDefaultConfigData(Gson gson) { return args -> { // 检查加密密钥是否存在 @@ -180,7 +210,7 @@ public class StartupConfiguration { * 准备安全密钥,用于加密解密等操作 */ @Bean - @Order(5) + @Order(6) public CommandLineRunner prepareKey() { return args -> { log.info("[Preparation] 系统进行安全密钥准备"); diff --git a/src/main/resources/mysql/oa_config.sql b/src/main/resources/mysql/oa_config.sql new file mode 100644 index 0000000..2154b3e --- /dev/null +++ b/src/main/resources/mysql/oa_config.sql @@ -0,0 +1,12 @@ +create table oa_config +( + id bigint unsigned auto_increment comment '主键' + primary key, + value varchar(50) not null comment '调用关键字', + data json null comment 'json数据', + created_at timestamp default CURRENT_TIMESTAMP not null comment '创建时间', + updated_at timestamp null comment '修改时间', + constraint oa_config_value_uindex + unique (value) +) + comment '配置数据表'; \ No newline at end of file diff --git a/src/main/resources/mysql/oa_message.sql b/src/main/resources/mysql/oa_message.sql new file mode 100644 index 0000000..0b476b6 --- /dev/null +++ b/src/main/resources/mysql/oa_message.sql @@ -0,0 +1,19 @@ +create table oa_message +( + id bigint unsigned auto_increment comment '消息主键' + primary key, + uid bigint unsigned not null comment '用户主键', + title varchar(100) not null comment '消息抬头', + text text not null comment '消息正文', + is_delete tinyint(1) default 0 not null comment '消息是否删除', + created_at timestamp default CURRENT_TIMESTAMP not null comment '创建时间', + deleted_at timestamp null comment '删除时间', + sid bigint unsigned null comment '发送用户id', + type varchar(100) null comment '跳转类型', + to_id int unsigned null comment '跳转的id', + constraint oa_message_oa_user_id_fk + foreign key (uid) references oa_user (id) + on update cascade on delete cascade +) + comment '消息'; + diff --git a/src/main/resources/mysql/oa_news.sql b/src/main/resources/mysql/oa_news.sql new file mode 100644 index 0000000..7c22adc --- /dev/null +++ b/src/main/resources/mysql/oa_news.sql @@ -0,0 +1,13 @@ +create table oa_news +( + id bigint unsigned auto_increment comment '主键' + primary key, + title varchar(255) not null comment '标题', + content text not null comment '内容', + tags varchar(10) null comment '标签(项目,通知)', + likes int default 0 not null comment '点赞数', + comments int default 0 not null comment '评论数', + status tinyint not null comment '状态(0:草稿;1:发布;2:隐藏)', + created_at timestamp default CURRENT_TIMESTAMP not null comment '创建时间', + updated_at timestamp null comment '更新时间' +); \ No newline at end of file diff --git a/src/main/resources/mysql/oa_news_user.sql b/src/main/resources/mysql/oa_news_user.sql new file mode 100644 index 0000000..732b3f8 --- /dev/null +++ b/src/main/resources/mysql/oa_news_user.sql @@ -0,0 +1,15 @@ +create table oa_news_user +( + id int unsigned auto_increment comment '主键' + primary key, + uid bigint unsigned not null comment '用户id', + nid bigint unsigned not null comment '新闻id', + created_at timestamp default CURRENT_TIMESTAMP not null comment '创建时间', + updated_at timestamp null comment '修改时间', + constraint oa_news_user_nid_oa_news_id_fk + foreign key (nid) references oa_news (id) + on update cascade, + constraint oa_news_user_uid_oa_user_id_fk + foreign key (uid) references oa_user (id) + on update cascade +); \ No newline at end of file diff --git a/src/main/resources/mysql/oa_permissions.sql b/src/main/resources/mysql/oa_permissions.sql new file mode 100644 index 0000000..51105f6 --- /dev/null +++ b/src/main/resources/mysql/oa_permissions.sql @@ -0,0 +1,14 @@ +create table oa_permissions +( + id bigint unsigned auto_increment comment '主键' + primary key, + pid bigint unsigned null comment '权限父id', + name varchar(100) not null comment '权限名称', + code varchar(50) not null comment '权限编码', + type tinyint(1) default 1 not null comment '0为菜单,1为权限', + deleted_at timestamp null comment '删除时间(没有删除应当为空)', + constraint oa_permissions_oa_permissions_id_fk + foreign key (pid) references oa_permissions (id) + on update cascade on delete cascade +) + comment '权限表'; \ No newline at end of file diff --git a/src/main/resources/mysql/oa_project.sql b/src/main/resources/mysql/oa_project.sql new file mode 100644 index 0000000..fc257df --- /dev/null +++ b/src/main/resources/mysql/oa_project.sql @@ -0,0 +1,24 @@ +create table oa_project +( + id bigint unsigned auto_increment comment '项目id' + primary key, + name varchar(255) not null comment '项目名称', + principal_id bigint unsigned not null comment '项目负责人', + description json null comment '项目描述(技术选择,描述)', + tags json null comment '项目标签(项目类型:web,大数据等)', + cycle int unsigned not null comment '项目周期', + work_load int unsigned default '1' not null comment '工作量(人天)', + files json null comment '项目文件', + begin_time datetime default CURRENT_TIMESTAMP not null comment '项目开始时间', + complete_time date null comment '完成时间', + dead_line date not null comment '甲方要求结束', + status varchar(8) default 'progress' not null comment '项目状态(draft: 草稿,progress: 进行,pause: 暂停,abnormal: 异常,complete: 完成)', + created_at timestamp default CURRENT_TIMESTAMP not null comment '创建时间', + updated_at timestamp null comment '修改时间', + is_delete tinyint(1) default 0 not null comment '项目是否删除', + constraint oa_project_oa_user_id_fk + foreign key (principal_id) references oa_user (id) + on update cascade +) + comment '项目表'; + diff --git a/mysql/oa_project_child.sql b/src/main/resources/mysql/oa_project_child.sql similarity index 98% rename from mysql/oa_project_child.sql rename to src/main/resources/mysql/oa_project_child.sql index dfb119e..df9aeba 100644 --- a/mysql/oa_project_child.sql +++ b/src/main/resources/mysql/oa_project_child.sql @@ -1,5 +1,3 @@ -USE organize_oa; - create table oa_project_child ( id bigint unsigned auto_increment comment '项目id' diff --git a/src/main/resources/mysql/oa_project_daily.sql b/src/main/resources/mysql/oa_project_daily.sql new file mode 100644 index 0000000..47e5ac3 --- /dev/null +++ b/src/main/resources/mysql/oa_project_daily.sql @@ -0,0 +1,19 @@ +create table oa_project_daily +( + id bigint unsigned auto_increment comment '日报主键' + primary key, + user_id bigint unsigned not null comment '用户id', + project_id bigint unsigned not null comment '项目id', + content text not null comment '日报内容', + daily_time date not null comment '日志发布时间', + created_at timestamp default CURRENT_TIMESTAMP not null comment '创建时间', + updated_at timestamp null comment '修改时间', + is_delete tinyint(1) default 0 not null comment '是否删除', + constraint oa_project_daily_oa_project_id_fk + foreign key (project_id) references oa_project (id) + on update cascade on delete cascade, + constraint oa_project_daily_oa_user_id_fk + foreign key (user_id) references oa_user (id) + on update cascade +) + comment '项目日报'; \ No newline at end of file diff --git a/mysql/oa_project_modules.sql b/src/main/resources/mysql/oa_project_modules.sql similarity index 98% rename from mysql/oa_project_modules.sql rename to src/main/resources/mysql/oa_project_modules.sql index 98a0d73..34ee475 100644 --- a/mysql/oa_project_modules.sql +++ b/src/main/resources/mysql/oa_project_modules.sql @@ -1,5 +1,3 @@ -USE organize_oa; - create table oa_project_modules ( id bigint unsigned auto_increment comment '模块id' diff --git a/src/main/resources/mysql/oa_project_tags.sql b/src/main/resources/mysql/oa_project_tags.sql new file mode 100644 index 0000000..a248ad3 --- /dev/null +++ b/src/main/resources/mysql/oa_project_tags.sql @@ -0,0 +1,17 @@ +create table oa_project_tags +( + id bigint unsigned auto_increment comment '主键id' + primary key, + name varchar(20) not null comment '标签名称', + pid bigint unsigned null comment '父标签id', + created_at timestamp default CURRENT_TIMESTAMP not null comment '创建时间', + updated_at timestamp null comment '修改时间', + is_delete tinyint(1) default 0 not null comment '是否删除', + constraint oa_project_tags_name_uindex + unique (name), + constraint oa_project_tags_oa_project_tags_id_fk + foreign key (pid) references oa_project_tags (id) + on update cascade on delete cascade +) + comment '项目标签表'; + diff --git a/src/main/resources/mysql/oa_review.sql b/src/main/resources/mysql/oa_review.sql new file mode 100644 index 0000000..c2bd1ff --- /dev/null +++ b/src/main/resources/mysql/oa_review.sql @@ -0,0 +1,35 @@ +create table oa_review +( + id bigint unsigned auto_increment comment '主键' + primary key, + name varchar(255) not null comment '申请名称', + content longtext not null comment '申请理由', + sender_id bigint unsigned not null comment '申请者用户id', + recipient_id bigint unsigned null comment '审核者用户id', + category tinyint default 1 null comment '审核类别(0:子系统;1:子模块)', + project_id bigint unsigned not null comment '申请的项目id', + project_child_id bigint unsigned not null comment '申请的子系统id', + project_module_id bigint unsigned null comment '申请的子模块id', + application_time datetime default CURRENT_TIMESTAMP not null comment '申请时间', + review_time datetime null comment '审核时间', + review_result tinyint default 2 not null comment '审核结果(0:未通过;1:通过;2:未审批)', + is_delete tinyint default 0 not null comment '是否删除(0:未删除;1:已删除)', + created_time datetime default CURRENT_TIMESTAMP not null comment '创建时间', + updated_time datetime null comment '更新时间', + constraint oa_review_recipient_id_oa_user_id_fk + foreign key (recipient_id) references oa_user (id) + on update cascade on delete cascade, + constraint oa_review_sender_id_oa_user_id_fk + foreign key (sender_id) references oa_user (id) + on update cascade on delete cascade +); + +create index oa_review_project_id_oa_project_id_fk + on oa_review (project_id); + +create index oa_review_project_project_submodule_id_oa_project_work_id_fk + on oa_review (project_module_id); + +create index oa_review_project_subsystem_id_oa_project_work_id_fk + on oa_review (project_child_id); + diff --git a/src/main/resources/mysql/oa_role.sql b/src/main/resources/mysql/oa_role.sql new file mode 100644 index 0000000..2b40923 --- /dev/null +++ b/src/main/resources/mysql/oa_role.sql @@ -0,0 +1,10 @@ +create table oa_role +( + id int unsigned auto_increment comment '角色id' + primary key, + role_name varchar(20) not null comment '角色名称', + display_name varchar(10) null comment '中文描述', + created_at timestamp default CURRENT_TIMESTAMP not null comment '创建时间', + updated_at timestamp null comment '修改时间' +) + comment '角色表'; \ No newline at end of file diff --git a/src/main/resources/mysql/oa_role_permissions.sql b/src/main/resources/mysql/oa_role_permissions.sql new file mode 100644 index 0000000..1d2ea0e --- /dev/null +++ b/src/main/resources/mysql/oa_role_permissions.sql @@ -0,0 +1,10 @@ +create table oa_role_permissions +( + rid int unsigned not null comment 'Role ID', + pid bigint unsigned not null comment 'Permission ID', + created_at timestamp default CURRENT_TIMESTAMP not null comment '创建时间', + primary key (rid, pid) +); + +create index oa_role_permission_oa_permissions_id_fk + on oa_role_permissions (pid); \ No newline at end of file diff --git a/src/main/resources/mysql/oa_role_user.sql b/src/main/resources/mysql/oa_role_user.sql new file mode 100644 index 0000000..fc60a84 --- /dev/null +++ b/src/main/resources/mysql/oa_role_user.sql @@ -0,0 +1,14 @@ +create table oa_role_user +( + uid bigint unsigned not null comment '用户id' + primary key, + rid int unsigned not null comment '角色id', + created_at timestamp default CURRENT_TIMESTAMP not null comment '创建时间', + updated_at timestamp null comment '修改时间', + constraint oa_role_user_oa_role_id_fk + foreign key (rid) references oa_role (id), + constraint oa_role_user_oa_user_id_fk + foreign key (uid) references oa_user (id) + on update cascade on delete cascade +) + comment '角色用户表'; \ No newline at end of file diff --git a/src/main/resources/mysql/oa_user.sql b/src/main/resources/mysql/oa_user.sql new file mode 100644 index 0000000..dd567fe --- /dev/null +++ b/src/main/resources/mysql/oa_user.sql @@ -0,0 +1,34 @@ +create table oa_user +( + id bigint unsigned auto_increment comment '主键' + primary key, + job_id char(10) not null comment '工作ID:正则表达 "^[STU|TEA|OTH][0-9]{7}"', + username varchar(40) not null comment '用户名', + password varchar(255) not null comment '密码', + address varchar(255) not null comment '用户家庭地址', + phone varchar(11) not null comment '电话', + email varchar(100) not null comment '邮箱', + age tinyint unsigned not null comment '年龄', + signature varchar(50) null comment '一句话描述自己', + sex tinyint unsigned default '0' not null comment '0/1/2:保密/男/女', + avatar text null comment '头像地址', + nickname varchar(20) null comment '昵称', + enabled tinyint(1) default 1 not null comment '账户是否可用', + account_no_expired tinyint(1) default 1 not null comment '账户是否过期', + credentials_no_expired tinyint(1) default 0 not null comment '密码是否过期', + recommend tinyint(1) default 0 not null comment '账户是否被推荐', + account_no_locked tinyint(1) default 1 not null comment '账户是否被锁定', + description text null comment '个人简介', + created_at timestamp default CURRENT_TIMESTAMP not null comment '创建时间', + updated_at timestamp null comment '更新时间', + is_delete tinyint(1) default 0 not null, + constraint oa_user_email_uindex + unique (email), + constraint oa_user_job_id_uindex + unique (job_id), + constraint oa_user_phone_uindex + unique (phone), + constraint oa_user_username_uindex + unique (username) +) + comment '用户表'; \ No newline at end of file diff --git a/src/main/resources/mysql/oa_user_tags.sql b/src/main/resources/mysql/oa_user_tags.sql new file mode 100644 index 0000000..b683e67 --- /dev/null +++ b/src/main/resources/mysql/oa_user_tags.sql @@ -0,0 +1,13 @@ +create table oa_user_tags +( + id bigint unsigned auto_increment comment '标签主键' + primary key, + name varchar(20) not null comment '标签名', + pid bigint unsigned null comment '标签父id', + created_at timestamp default CURRENT_TIMESTAMP not null comment '创建时间', + updated_at timestamp null comment '修改时间', + is_delete tinyint(1) default 0 not null comment '是否删除', + constraint oa_user_tags_name_uindex + unique (name) +) + comment '用户标签'; \ No newline at end of file diff --git a/mysql/organize_oa.sql b/src/main/resources/mysql/organize_oa.sql similarity index 100% rename from mysql/organize_oa.sql rename to src/main/resources/mysql/organize_oa.sql