CREATE DATABASE IF NOT EXISTS rc_console DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci;

USE rc_console;

CREATE TABLE IF NOT EXISTS sys_user (
  uid INT NOT NULL AUTO_INCREMENT,
  login_name VARCHAR(64) NOT NULL,
  password VARCHAR(255) NOT NULL,
  nickname VARCHAR(64) NOT NULL,
  email VARCHAR(128) DEFAULT NULL,
  phone VARCHAR(32) DEFAULT NULL,
  ip VARCHAR(64) DEFAULT NULL,
  status TINYINT NOT NULL DEFAULT 0,
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  create_user BIGINT DEFAULT NULL,
  update_user BIGINT DEFAULT NULL,
  PRIMARY KEY (uid),
  UNIQUE KEY uk_sys_user_login_name (login_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sys_role (
  id INT NOT NULL AUTO_INCREMENT,
  role_name VARCHAR(64) NOT NULL,
  remark VARCHAR(255) DEFAULT NULL,
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  create_user BIGINT DEFAULT NULL,
  update_user BIGINT DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sys_user_role (
  id INT NOT NULL AUTO_INCREMENT,
  uid INT NOT NULL,
  role_id INT NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uk_sys_user_role_uid_role (uid, role_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sys_menu (
  id INT NOT NULL AUTO_INCREMENT,
  parent_id INT NOT NULL DEFAULT 0,
  menu_name VARCHAR(64) NOT NULL,
  path VARCHAR(255) DEFAULT NULL,
  router VARCHAR(255) DEFAULT NULL,
  menu_type TINYINT NOT NULL,
  icon VARCHAR(64) DEFAULT NULL,
  alias VARCHAR(128) DEFAULT NULL,
  sort INT NOT NULL DEFAULT 0,
  status TINYINT NOT NULL DEFAULT 0,
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  create_user BIGINT DEFAULT NULL,
  update_user BIGINT DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sys_role_menu (
  id INT NOT NULL AUTO_INCREMENT,
  role_id INT NOT NULL,
  menu_id INT NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uk_sys_role_menu_role_menu (role_id, menu_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sys_operation_log (
  id BIGINT NOT NULL,
  user_id BIGINT DEFAULT NULL,
  method VARCHAR(255) DEFAULT NULL,
  operation VARCHAR(255) DEFAULT NULL,
  params TEXT,
  ip VARCHAR(64) DEFAULT NULL,
  create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

UPDATE sys_user
SET create_user = 1,
    update_user = 1
WHERE create_user IS NULL
   OR update_user IS NULL
   OR CAST(create_user AS CHAR) IN ('', 'local')
   OR CAST(update_user AS CHAR) IN ('', 'local');

UPDATE sys_role
SET create_user = 1,
    update_user = 1
WHERE create_user IS NULL
   OR update_user IS NULL
   OR CAST(create_user AS CHAR) IN ('', 'local')
   OR CAST(update_user AS CHAR) IN ('', 'local');

UPDATE sys_menu
SET create_user = 1,
    update_user = 1
WHERE create_user IS NULL
   OR update_user IS NULL
   OR CAST(create_user AS CHAR) IN ('', 'local')
   OR CAST(update_user AS CHAR) IN ('', 'local');

ALTER TABLE sys_user
  MODIFY COLUMN create_user BIGINT DEFAULT NULL,
  MODIFY COLUMN update_user BIGINT DEFAULT NULL;

ALTER TABLE sys_role
  MODIFY COLUMN create_user BIGINT DEFAULT NULL,
  MODIFY COLUMN update_user BIGINT DEFAULT NULL;

ALTER TABLE sys_menu
  MODIFY COLUMN create_user BIGINT DEFAULT NULL,
  MODIFY COLUMN update_user BIGINT DEFAULT NULL;

INSERT INTO sys_role (id, role_name, remark, create_user, update_user)
VALUES (1, '超级管理员', 'local bootstrap admin role', 1, 1)
ON DUPLICATE KEY UPDATE
  role_name = VALUES(role_name),
  remark = VALUES(remark),
  create_user = VALUES(create_user),
  update_user = VALUES(update_user);

INSERT INTO sys_user (uid, login_name, password, nickname, email, phone, ip, status, create_user, update_user)
VALUES (1, 'admin', '$apr1$admin$3NsFmc5jCaK5R97pgHRX21', '本地管理员', 'admin@example.com', '13800000000', '127.0.0.1', 0, 1, 1)
ON DUPLICATE KEY UPDATE
  password = VALUES(password),
  nickname = VALUES(nickname),
  email = VALUES(email),
  phone = VALUES(phone),
  ip = VALUES(ip),
  status = VALUES(status),
  create_user = VALUES(create_user),
  update_user = VALUES(update_user);

INSERT INTO sys_user_role (id, uid, role_id)
VALUES (1, 1, 1)
ON DUPLICATE KEY UPDATE
  uid = VALUES(uid),
  role_id = VALUES(role_id);

INSERT INTO sys_menu (id, parent_id, menu_name, path, router, menu_type, icon, alias, sort, status, create_user, update_user)
VALUES
  (1, 0, '系统管理', '/system', 'Layout', 1, 'setting', 'system', 100, 0, 1, 1),
  (2, 1, '控制台首页', 'home', '/system/home/index', 2, 'monitor', 'console-home', 90, 0, 1, 1)
ON DUPLICATE KEY UPDATE
  menu_name = VALUES(menu_name),
  path = VALUES(path),
  router = VALUES(router),
  menu_type = VALUES(menu_type),
  icon = VALUES(icon),
  alias = VALUES(alias),
  sort = VALUES(sort),
  status = VALUES(status),
  create_user = VALUES(create_user),
  update_user = VALUES(update_user);

INSERT INTO sys_role_menu (id, role_id, menu_id)
VALUES
  (1, 1, 1),
  (2, 1, 2)
ON DUPLICATE KEY UPDATE
  role_id = VALUES(role_id),
  menu_id = VALUES(menu_id);
