
CREATE TABLE IF NOT EXISTS "sys_core_dictionary" (
    id                  VARCHAR(50)                 NOT NULL,
    name                VARCHAR(500)                NOT NULL,
    code                VARCHAR(200)                NOT NULL,
    type                VARCHAR(50)                 NOT NULL,
    description         TEXT,
    create_time         TIMESTAMPTZ                 NOT NULL DEFAULT current_timestamp,
    update_time         TIMESTAMPTZ                 NOT NULL DEFAULT current_timestamp,
    PRIMARY KEY (id)
);
CREATE UNIQUE INDEX uidx_sys_core_dictionary_code ON sys_core_dictionary (code);
COMMENT ON TABLE sys_core_dictionary IS '字典';
COMMENT ON COLUMN sys_core_dictionary.id IS '主键';
COMMENT ON COLUMN sys_core_dictionary.name IS '字典名称';
COMMENT ON COLUMN sys_core_dictionary.code IS '字典代码';
COMMENT ON COLUMN sys_core_dictionary.type IS '字典类型: enum-枚举、tree-树型';
COMMENT ON COLUMN sys_core_dictionary.description IS '字典描述';
COMMENT ON COLUMN sys_core_dictionary.create_time IS '创建时间';
COMMENT ON COLUMN sys_core_dictionary.update_time IS '修改时间';

CREATE TABLE IF NOT EXISTS sys_core_dictionary_item (
    id                  VARCHAR(50)                 NOT NULL,
    dictionary_id       VARCHAR(50)                 NOT NULL,
    parent_id           VARCHAR(50),
    name                VARCHAR(500)                NOT NULL,
    code                VARCHAR(200)                NOT NULL,
    sort                INTEGER                     NOT NULL DEFAULT 0,
    description         TEXT,
    create_time         TIMESTAMPTZ                 NOT NULL DEFAULT current_timestamp,
    update_time         TIMESTAMPTZ                 NOT NULL DEFAULT current_timestamp,
    PRIMARY KEY (id)
);
CREATE UNIQUE INDEX uidx1_sys_core_dictionary_item ON sys_core_dictionary_item (dictionary_id, parent_id, code);
COMMENT ON TABLE sys_core_dictionary_item IS '字典项';
COMMENT ON COLUMN sys_core_dictionary_item.id IS '主键';
COMMENT ON COLUMN sys_core_dictionary_item.parent_id IS '父级ID，如果为空则为根节点';
COMMENT ON COLUMN sys_core_dictionary_item.dictionary_id IS '字典ID';
COMMENT ON COLUMN sys_core_dictionary_item.name IS '字典项名称';
COMMENT ON COLUMN sys_core_dictionary_item.code IS '字典项代码';
COMMENT ON COLUMN sys_core_dictionary_item.sort IS '字典项排序，升序排列';
COMMENT ON COLUMN sys_core_dictionary_item.description IS '字典项描述';
COMMENT ON COLUMN sys_core_dictionary_item.create_time IS '创建时间';
COMMENT ON COLUMN sys_core_dictionary_item.update_time IS '修改时间';

INSERT INTO sys_core_dictionary
VALUES('019ac45a-124d-7fe6-9a03-b9b9c54aacd0', '表-字典-类型', 'table.sys_core_dictionary.type', 'enum');

INSERT INTO sys_core_dictionary_item
VALUES ('019ac45c-e548-7bf5-845f-702bc75ba8a4', '019ac45a-124d-7fe6-9a03-b9b9c54aacd0', NULL, '枚举', 'enum', 0),
       ('019ac45f-6a73-7dad-8334-1961f75ffc7d', '019ac45a-124d-7fe6-9a03-b9b9c54aacd0', NULL, '树型', 'tree', 1);

CREATE TABLE IF NOT EXISTS sys_core_sequence (
    id                  VARCHAR(50)                 NOT NULL,
    name                VARCHAR(500)                NOT NULL,
    code                VARCHAR(200)                NOT NULL,
    type                VARCHAR(50)                 NOT NULL,
    min_value           BIGINT                      NOT NULL DEFAULT 0,
    inc_value           BIGINT                      NOT NULL DEFAULT 1,
    description         TEXT,
    create_time         TIMESTAMPTZ                 NOT NULL DEFAULT current_timestamp,
    update_time         TIMESTAMPTZ                 NOT NULL DEFAULT current_timestamp,
    PRIMARY KEY (id)
);
CREATE UNIQUE INDEX uidx1_sys_core_sequence ON sys_core_sequence (code);
COMMENT ON TABLE sys_core_sequence IS '序列';
COMMENT ON COLUMN sys_core_sequence.id IS '主键';
COMMENT ON COLUMN sys_core_sequence.name IS '名称';
COMMENT ON COLUMN sys_core_sequence.code IS '代码';
COMMENT ON COLUMN sys_core_sequence.type IS '类型';
COMMENT ON COLUMN sys_core_sequence.min_value IS '最小值';
COMMENT ON COLUMN sys_core_sequence.inc_value IS '递增值';
COMMENT ON COLUMN sys_core_sequence.description IS '描述';
COMMENT ON COLUMN sys_core_sequence.create_time IS '创建时间';
COMMENT ON COLUMN sys_core_sequence.update_time IS '修改时间';

CREATE TABLE IF NOT EXISTS sys_core_sequence_item (
    id                  VARCHAR(50)                 NOT NULL,
    sequence_id         VARCHAR(50)                 NOT NULL,
    value               BIGINT                      NOT NULL,
    year                INTEGER,
    quarter             INTEGER,
    month               INTEGER,
    day                 INTEGER,
    PRIMARY KEY (id)
);
COMMENT ON TABLE sys_core_sequence_item IS '序列项';
COMMENT ON COLUMN sys_core_sequence_item.id IS '主键';
COMMENT ON COLUMN sys_core_sequence_item.sequence_id IS '序列表主键';
COMMENT ON COLUMN sys_core_sequence_item.value IS '当前值';
COMMENT ON COLUMN sys_core_sequence_item.year IS '年';
COMMENT ON COLUMN sys_core_sequence_item.quarter IS '季';
COMMENT ON COLUMN sys_core_sequence_item.month IS '月';
COMMENT ON COLUMN sys_core_sequence_item.day IS '日';

CREATE TABLE IF NOT EXISTS sys_rbac_region (
    id                  VARCHAR(50)                 NOT NULL,
    parent_id           VARCHAR(50),
    parent_code         VARCHAR(50),
    root_id             VARCHAR(50),
    root_code           VARCHAR(50),
    name                VARCHAR(500)                NOT NULL,
    code                VARCHAR(200)                NOT NULL,
    ext_code            VARCHAR(200),
    sort                INTEGER                     NOT NULL DEFAULT 0,
    description         TEXT,
    create_time         TIMESTAMPTZ                 NOT NULL DEFAULT current_timestamp,
    update_time         TIMESTAMPTZ                 NOT NULL DEFAULT current_timestamp,
    PRIMARY KEY (id)
);
CREATE UNIQUE INDEX uidx1_sys_rbac_region ON sys_rbac_region (parent_id, code);
CREATE UNIQUE INDEX uidx2_sys_rbac_region ON sys_rbac_region (parent_code, code);
COMMENT ON TABLE sys_rbac_region IS '区划项';
COMMENT ON COLUMN sys_rbac_region.id IS '主键';
COMMENT ON COLUMN sys_rbac_region.parent_id IS '上级区划ID';
COMMENT ON COLUMN sys_rbac_region.parent_code IS '上级区划代码';
COMMENT ON COLUMN sys_rbac_region.root_id IS '主区划ID';
COMMENT ON COLUMN sys_rbac_region.root_code IS '主区划代码';
COMMENT ON COLUMN sys_rbac_region.name IS '区划名称';
COMMENT ON COLUMN sys_rbac_region.code IS '区划代码';
COMMENT ON COLUMN sys_rbac_region.ext_code IS '区划扩展代码';
COMMENT ON COLUMN sys_rbac_region.sort IS '区划排序';
COMMENT ON COLUMN sys_rbac_region.description IS '区划描述';
COMMENT ON COLUMN sys_rbac_region.create_time IS '创建时间';
COMMENT ON COLUMN sys_rbac_region.update_time IS '修改时间';

CREATE TABLE IF NOT EXISTS sys_rbac_department (
    id                  VARCHAR(50)                 NOT NULL,
    region_id           VARCHAR(50),
    parent_id           VARCHAR(50),
    name                VARCHAR(500)                NOT NULL,
    code                VARCHAR(200)                NOT NULL,
    sort                INTEGER                     NOT NULL DEFAULT 0,
    description         TEXT,
    create_time         TIMESTAMPTZ                 NOT NULL DEFAULT current_timestamp,
    update_time         TIMESTAMPTZ                 NOT NULL DEFAULT current_timestamp,
    PRIMARY KEY (id)
);
CREATE UNIQUE INDEX uidx_sys_rbac_department_code ON sys_rbac_department (code);
COMMENT ON TABLE sys_rbac_department IS '部门表';
COMMENT ON COLUMN sys_rbac_department.id IS '主键';
COMMENT ON COLUMN sys_rbac_department.region_id IS '区划ID';
COMMENT ON COLUMN sys_rbac_department.parent_id IS '上级部门ID';
COMMENT ON COLUMN sys_rbac_department.name IS '部门名称';
COMMENT ON COLUMN sys_rbac_department.code IS '部门代码';
COMMENT ON COLUMN sys_rbac_department.sort IS '部门排序';
COMMENT ON COLUMN sys_rbac_department.description IS '描述';
COMMENT ON COLUMN sys_rbac_department.create_time IS '创建时间';
COMMENT ON COLUMN sys_rbac_department.update_time IS '修改时间';

INSERT INTO sys_rbac_department
VALUES('019ac468-16b9-7abc-8f2d-7a8d59034969', NULL, NULL, '系统管理部', 'DEPT00001', 0);

CREATE TABLE IF NOT EXISTS sys_rbac_role (
    id                  VARCHAR(50)                 NOT NULL,
    name                VARCHAR(500)                NOT NULL,
    code                VARCHAR(200)                NOT NULL,
    sort                INTEGER                     NOT NULL DEFAULT 0,
    description         TEXT,
    create_time         TIMESTAMPTZ                 NOT NULL DEFAULT current_timestamp,
    update_time         TIMESTAMPTZ                 NOT NULL DEFAULT current_timestamp,
    PRIMARY KEY (id)
);
CREATE UNIQUE INDEX uidx_sys_rbac_role_code ON sys_rbac_role (code);
COMMENT ON TABLE sys_rbac_role IS '角色表';
COMMENT ON COLUMN sys_rbac_role.id IS '主键';
COMMENT ON COLUMN sys_rbac_role.name IS '角色名称';
COMMENT ON COLUMN sys_rbac_role.code IS '角色代码';
COMMENT ON COLUMN sys_rbac_role.sort IS '角色排序';
COMMENT ON COLUMN sys_rbac_role.description IS '描述';
COMMENT ON COLUMN sys_rbac_role.create_time IS '创建时间';
COMMENT ON COLUMN sys_rbac_role.update_time IS '修改时间';

INSERT INTO sys_rbac_role
VALUES('019ac46a-bf32-7183-a119-45d09f982a31', '系统管理员', 'ROLE00001', 0);

CREATE TABLE IF NOT EXISTS sys_rbac_user (
    id                  VARCHAR(50)                 NOT NULL,
    username            VARCHAR(500)                NOT NULL,
    nickname            VARCHAR(500),
    password            VARCHAR(200)                NOT NULL,
    enabled             VARCHAR(2)                  NOT NULL DEFAULT '1',
    description         TEXT,
    create_time         TIMESTAMPTZ                 NOT NULL DEFAULT current_timestamp,
    update_time         TIMESTAMPTZ                 NOT NULL DEFAULT current_timestamp,
    PRIMARY KEY (id)
);
CREATE UNIQUE INDEX ui_sys_rbac_user_username ON sys_rbac_user (username);
COMMENT ON TABLE sys_rbac_user IS '用户表';
COMMENT ON COLUMN sys_rbac_user.id IS '主键';
COMMENT ON COLUMN sys_rbac_user.username IS '用户名称';
COMMENT ON COLUMN sys_rbac_user.nickname IS '用户昵称';
COMMENT ON COLUMN sys_rbac_user.password IS '用户密码';
COMMENT ON COLUMN sys_rbac_user.enabled IS '是否启用：0-未启用/1-启用';
COMMENT ON COLUMN sys_rbac_user.description IS '描述';
COMMENT ON COLUMN sys_rbac_user.create_time IS '创建时间';
COMMENT ON COLUMN sys_rbac_user.update_time IS '修改时间';

INSERT INTO sys_rbac_user
VALUES('019ac46c-5a72-7af2-932b-bf0a31741e98', 'admin', '管理员', '$2a$10$5XwMwhPjHEL8XPsR1bgfQuBnOtCC5u3Y/CdD3MbTFMUiEFcazlYMq', '1');

CREATE TABLE IF NOT EXISTS sys_rbac_user_department (
    user_id                  VARCHAR(50)                 NOT NULL,
    department_id            VARCHAR(50)                 NOT NULL,
    PRIMARY KEY (user_id, department_id)
);
COMMENT ON TABLE sys_rbac_user_department IS '用户部门表';
COMMENT ON COLUMN sys_rbac_user_department.user_id IS '用户ID';
COMMENT ON COLUMN sys_rbac_user_department.department_id IS '部门ID';

INSERT INTO sys_rbac_user_department
VALUES('019ac46c-5a72-7af2-932b-bf0a31741e98', '019ac468-16b9-7abc-8f2d-7a8d59034969');

CREATE TABLE IF NOT EXISTS sys_rbac_user_role (
    user_id                  VARCHAR(50)                 NOT NULL,
    role_id                  VARCHAR(50)                 NOT NULL,
    PRIMARY KEY (user_id, role_id)
);
COMMENT ON TABLE sys_rbac_user_role IS '用户部门表';
COMMENT ON COLUMN sys_rbac_user_role.user_id IS '用户ID';
COMMENT ON COLUMN sys_rbac_user_role.role_id IS '角色ID';

INSERT INTO sys_rbac_user_role
VALUES('019ac46c-5a72-7af2-932b-bf0a31741e98', '019ac46a-bf32-7183-a119-45d09f982a31');
