--------------------------------------------------------------------------------------------------------------
-- function
CREATE
    OR REPLACE FUNCTION update_updated_at_column()
    RETURNS TRIGGER AS
$$
BEGIN
    NEW.updated_at
        := NOW();
    RETURN NEW;
END;
$$
    language 'plpgsql';
--------------------------------------------------------------------------------------------------------------
CREATE TABLE sys_users
(
    id            INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    account       TEXT      NOT NULL UNIQUE,
    username      TEXT      NOT NULL,
    password_hash TEXT      NOT NULL,
    avatar_id     INTEGER,
    created_at    TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at    TIMESTAMPTZ
);

CREATE INDEX idx_sys_users_account ON sys_users (account);

CREATE TRIGGER update_sys_users_updated_at
    BEFORE UPDATE
    ON sys_users
    FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();


COMMENT ON COLUMN sys_users.id IS '主键ID';
COMMENT ON COLUMN sys_users.account IS '用户账号';
COMMENT ON COLUMN sys_users.username IS '用户名称';
COMMENT ON COLUMN sys_users.password_hash IS 'hash密码';
COMMENT ON COLUMN sys_users.avatar_id IS '头像文件id';
COMMENT ON COLUMN sys_users.created_at IS '创建时间';
COMMENT ON COLUMN sys_users.updated_at IS '更新时间';
--------------------------------------------------------------------------------------------------------------
CREATE TABLE sys_roles
(
    id         INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    name       TEXT      NOT NULL,
    code       TEXT      NOT NULL UNIQUE,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ
);

CREATE TRIGGER update_sys_roles_updated_at
    BEFORE UPDATE
    ON sys_roles
    FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();

COMMENT ON COLUMN sys_roles.id IS '主键ID';
COMMENT ON COLUMN sys_roles.name IS '角色名称';
COMMENT ON COLUMN sys_roles.code IS '角色编码，唯一';
COMMENT ON COLUMN sys_roles.created_at IS '创建时间';
COMMENT ON COLUMN sys_roles.updated_at IS '更新时间';
--------------------------------------------------------------------------------------------------------------
CREATE TABLE sys_permissions
(
    id         INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    code       TEXT UNIQUE,
    type       SMALLINT  NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ,

    CONSTRAINT unique_type_code UNIQUE (type, code)
);

CREATE TRIGGER update_sys_permissions_updated_at
    BEFORE UPDATE
    ON sys_permissions
    FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();

COMMENT ON COLUMN sys_permissions.id IS '主键ID';
COMMENT ON COLUMN sys_permissions.code IS '权限字符，唯一';
COMMENT ON COLUMN sys_permissions.code IS '权限类型，0: 菜单 1: api';
COMMENT ON COLUMN sys_permissions.created_at IS '创建时间';
COMMENT ON COLUMN sys_permissions.updated_at IS '更新时间';
--------------------------------------------------------------------------------------------------------------
CREATE TABLE sys_menus
(
    id         INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    name       TEXT      NOT NULL,
    path       TEXT      NOT NULL,
    component  TEXT,
    type       SMALLINT  NOT NULL,
    hidden     BOOLEAN            DEFAULT FALSE,
    sort       INTEGER            DEFAULT 0,
    status     SMALLINT  NOT NULL DEFAULT 1,
    parent_id  INTEGER,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ,

    CHECK (type IN (0, 1, 2)),
    CHECK (status IN (0, 1))
);


CREATE TRIGGER update_sys_menus_updated_at
    BEFORE UPDATE
    ON sys_menus
    FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();

COMMENT ON COLUMN sys_menus.id IS '主键ID';
COMMENT ON COLUMN sys_menus.name IS '菜单名称';
COMMENT ON COLUMN sys_menus.path IS '菜单路径';
COMMENT ON COLUMN sys_menus.component IS '组件路径';
COMMENT ON COLUMN sys_menus.type IS '菜单类型 0: 目录 1: 菜单 2: button';
COMMENT ON COLUMN sys_menus.sort IS '菜单排序';
COMMENT ON COLUMN sys_menus.hidden IS '是否隐藏菜单';
COMMENT ON COLUMN sys_menus.status IS '菜单状态 1: 启用 0: 禁用';
COMMENT ON COLUMN sys_menus.parent_id IS '父级id';
COMMENT ON COLUMN sys_menus.created_at IS '创建时间';
COMMENT ON COLUMN sys_menus.updated_at IS '更新时间';
--------------------------------------------------------------------------------------------------------------
CREATE TABLE sys_apis
(
    id         INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    group_name TEXT      NOT NULL,
    name       TEXT      NOT NULL,
    method     TEXT      NOT NULL,
    path       TEXT      NOT NULL,
    sort       INTEGER            DEFAULT 0,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ,

    UNIQUE (method, path)
);

CREATE TRIGGER update_sys_apis_updated_at
    BEFORE UPDATE
    ON sys_apis
    FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();

COMMENT ON COLUMN sys_apis.id IS '主键ID';
COMMENT ON COLUMN sys_apis.group_name IS '分组名称';
COMMENT ON COLUMN sys_apis.name IS '接口名称';
COMMENT ON COLUMN sys_apis.method IS 'api方法';
COMMENT ON COLUMN sys_apis.path IS 'api路径';
COMMENT ON COLUMN sys_apis.sort IS '排序';
COMMENT ON COLUMN sys_apis.created_at IS '创建时间';
COMMENT ON COLUMN sys_apis.updated_at IS '更新时间';
--------------------------------------------------------------------------------------------------------------
CREATE TABLE files
(
    id            INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    file_name     TEXT      NOT NULL,
    file_path     TEXT      NOT NULL,
    original_name TEXT      NOT NULL,
    folder_name   TEXT      NOT NULL,
    mime_type     TEXT      NOT NULL,
    file_size     BIGINT    NOT NULL,
    created_at    TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at    TIMESTAMPTZ
);

CREATE TRIGGER update_files_updated_at
    BEFORE UPDATE
    ON files
    FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();

COMMENT ON COLUMN files.id IS '主键ID';
COMMENT ON COLUMN files.file_name IS '文件名 xxx.jpg';
COMMENT ON COLUMN files.file_path IS '文件路径 带path /xxx/xxx.jpg';
COMMENT ON COLUMN files.original_name IS '原始文件名';
COMMENT ON COLUMN files.folder_name IS '文件夹名称';
COMMENT ON COLUMN files.mime_type IS 'mime_type';
COMMENT ON COLUMN files.file_size IS '文件大小';
COMMENT ON COLUMN files.created_at IS '上传时间';
COMMENT ON COLUMN files.updated_at IS '修改时间';
--------------------------------------------------------------------------------------------------------------
CREATE TABLE sys_user_role
(
    user_id INTEGER NOT NULL,
    role_id INTEGER NOT NULL,
    PRIMARY KEY (user_id, role_id)
);

COMMENT ON COLUMN sys_user_role.user_id IS '用户ID';
COMMENT ON COLUMN sys_user_role.role_id IS '角色ID';
--------------------------------------------------------------------------------------------------------------
CREATE TABLE sys_role_permission
(
    role_id       INTEGER NOT NULL,
    permission_id INTEGER NOT NULL,
    PRIMARY KEY (role_id, permission_id)
);

COMMENT ON COLUMN sys_role_permission.role_id IS '角色id';
COMMENT ON COLUMN sys_role_permission.permission_id IS '权限id';
--------------------------------------------------------------------------------------------------------------
CREATE TABLE sys_menu_permission
(
    menu_id       INTEGER NOT NULL,
    permission_id INTEGER NOT NULL,
    PRIMARY KEY (menu_id, permission_id)
);

COMMENT ON COLUMN sys_menu_permission.menu_id IS '菜单id';
COMMENT ON COLUMN sys_menu_permission.permission_id IS '权限id';
--------------------------------------------------------------------------------------------------------------
CREATE TABLE sys_api_permission
(
    api_id        INTEGER NOT NULL,
    permission_id INTEGER NOT NULL,
    PRIMARY KEY (api_id, permission_id)
);

COMMENT ON COLUMN sys_api_permission.api_id IS 'api ID';
COMMENT ON COLUMN sys_api_permission.permission_id IS '权限id';
--------------------------------------------------------------------------------------------------------------
