-- 学校租户表
CREATE TABLE IF NOT EXISTS upms.tb_sys_tenant (
    tenant_id        VARCHAR(64) PRIMARY KEY,
    parent_tenant_id VARCHAR(64),
    tenant_name      VARCHAR(128) NOT NULL,
    tenant_type      VARCHAR(32) NOT NULL,
    adcode           VARCHAR(12) NOT NULL,
    tenant_path      VARCHAR(255) NOT NULL,
    status           VARCHAR(32) NOT NULL DEFAULT 'ACTIVE',
    created_at       TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE upms.tb_sys_tenant IS '学校租户表';
COMMENT ON COLUMN upms.tb_sys_tenant.tenant_id IS '租户ID';
COMMENT ON COLUMN upms.tb_sys_tenant.parent_tenant_id IS '父租户ID';
COMMENT ON COLUMN upms.tb_sys_tenant.tenant_name IS '租户名称';
COMMENT ON COLUMN upms.tb_sys_tenant.tenant_type IS '租户类型';
COMMENT ON COLUMN upms.tb_sys_tenant.adcode IS '行政区划编码';
COMMENT ON COLUMN upms.tb_sys_tenant.tenant_path IS '租户路径';
COMMENT ON COLUMN upms.tb_sys_tenant.status IS '租户状态';
COMMENT ON COLUMN upms.tb_sys_tenant.created_at IS '创建时间';

CREATE TABLE IF NOT EXISTS upms.tb_sys_dept (
    dept_id          VARCHAR(64) PRIMARY KEY,
    parent_dept_id   VARCHAR(64),
    tenant_id        VARCHAR(64) NOT NULL,
    dept_name        VARCHAR(128) NOT NULL,
    dept_type        VARCHAR(32) NOT NULL,
    adcode           VARCHAR(12) NOT NULL,
    tenant_path      VARCHAR(255) NOT NULL,
    dept_path        VARCHAR(255) NOT NULL,
    created_at       TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE upms.tb_sys_dept IS '组织部门表';
COMMENT ON COLUMN upms.tb_sys_dept.dept_id IS '部门ID';
COMMENT ON COLUMN upms.tb_sys_dept.parent_dept_id IS '父部门ID';
COMMENT ON COLUMN upms.tb_sys_dept.tenant_id IS '租户ID';
COMMENT ON COLUMN upms.tb_sys_dept.dept_name IS '部门名称';
COMMENT ON COLUMN upms.tb_sys_dept.dept_type IS '部门类型';
COMMENT ON COLUMN upms.tb_sys_dept.adcode IS '行政区划编码';
COMMENT ON COLUMN upms.tb_sys_dept.tenant_path IS '租户路径';
COMMENT ON COLUMN upms.tb_sys_dept.dept_path IS '部门路径';
COMMENT ON COLUMN upms.tb_sys_dept.created_at IS '创建时间';

CREATE TABLE IF NOT EXISTS upms.tb_sys_user (
    user_id          VARCHAR(64) PRIMARY KEY,
    username         VARCHAR(64) UNIQUE NOT NULL,
    display_name     VARCHAR(128) NOT NULL,
    password_hash    VARCHAR(255) NOT NULL,
    adcode           VARCHAR(12) NOT NULL,
    tenant_id        VARCHAR(64) NOT NULL,
    tenant_path      VARCHAR(255) NOT NULL,
    dept_id          VARCHAR(64) NOT NULL,
    dept_path        VARCHAR(255) NOT NULL,
    status           VARCHAR(32) NOT NULL DEFAULT 'ACTIVE',
    created_at       TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE upms.tb_sys_user IS '系统用户表';
COMMENT ON COLUMN upms.tb_sys_user.user_id IS '用户ID';
COMMENT ON COLUMN upms.tb_sys_user.username IS '用户名';
COMMENT ON COLUMN upms.tb_sys_user.display_name IS '显示名称';
COMMENT ON COLUMN upms.tb_sys_user.password_hash IS '密码哈希';
COMMENT ON COLUMN upms.tb_sys_user.adcode IS '行政区划编码';
COMMENT ON COLUMN upms.tb_sys_user.tenant_id IS '租户ID';
COMMENT ON COLUMN upms.tb_sys_user.tenant_path IS '租户路径';
COMMENT ON COLUMN upms.tb_sys_user.dept_id IS '部门ID';
COMMENT ON COLUMN upms.tb_sys_user.dept_path IS '部门路径';
COMMENT ON COLUMN upms.tb_sys_user.status IS '用户状态';
COMMENT ON COLUMN upms.tb_sys_user.created_at IS '创建时间';

CREATE TABLE IF NOT EXISTS upms.tb_sys_role (
    role_id          VARCHAR(64) PRIMARY KEY,
    role_code        VARCHAR(64) UNIQUE NOT NULL,
    role_name        VARCHAR(128) NOT NULL,
    adcode           VARCHAR(12) NOT NULL,
    tenant_id        VARCHAR(64) NOT NULL,
    tenant_path      VARCHAR(255) NOT NULL,
    dept_id          VARCHAR(64),
    dept_path        VARCHAR(255),
    created_at       TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE upms.tb_sys_role IS '角色表';
COMMENT ON COLUMN upms.tb_sys_role.role_id IS '角色ID';
COMMENT ON COLUMN upms.tb_sys_role.role_code IS '角色编码';
COMMENT ON COLUMN upms.tb_sys_role.role_name IS '角色名称';
COMMENT ON COLUMN upms.tb_sys_role.adcode IS '行政区划编码';
COMMENT ON COLUMN upms.tb_sys_role.tenant_id IS '租户ID';
COMMENT ON COLUMN upms.tb_sys_role.tenant_path IS '租户路径';
COMMENT ON COLUMN upms.tb_sys_role.dept_id IS '部门ID';
COMMENT ON COLUMN upms.tb_sys_role.dept_path IS '部门路径';
COMMENT ON COLUMN upms.tb_sys_role.created_at IS '创建时间';

CREATE TABLE IF NOT EXISTS upms.tb_sys_permission (
    permission_id    VARCHAR(64) PRIMARY KEY,
    permission_code  VARCHAR(128) UNIQUE NOT NULL,
    permission_name  VARCHAR(128) NOT NULL,
    adcode           VARCHAR(12) NOT NULL,
    tenant_id        VARCHAR(64) NOT NULL,
    tenant_path      VARCHAR(255) NOT NULL,
    dept_id          VARCHAR(64),
    dept_path        VARCHAR(255),
    created_at       TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE upms.tb_sys_permission IS '权限表';
COMMENT ON COLUMN upms.tb_sys_permission.permission_id IS '权限ID';
COMMENT ON COLUMN upms.tb_sys_permission.permission_code IS '权限编码';
COMMENT ON COLUMN upms.tb_sys_permission.permission_name IS '权限名称';
COMMENT ON COLUMN upms.tb_sys_permission.adcode IS '行政区划编码';
COMMENT ON COLUMN upms.tb_sys_permission.tenant_id IS '租户ID';
COMMENT ON COLUMN upms.tb_sys_permission.tenant_path IS '租户路径';
COMMENT ON COLUMN upms.tb_sys_permission.dept_id IS '部门ID';
COMMENT ON COLUMN upms.tb_sys_permission.dept_path IS '部门路径';
COMMENT ON COLUMN upms.tb_sys_permission.created_at IS '创建时间';

CREATE TABLE IF NOT EXISTS upms.tb_sys_menu (
    route_id         VARCHAR(64) PRIMARY KEY,
    parent_route_id  VARCHAR(64),
    route_path       VARCHAR(255) NOT NULL,
    route_name       VARCHAR(128) NOT NULL,
    component_key    VARCHAR(128) NOT NULL,
    layout_type      VARCHAR(32) NOT NULL,
    title            VARCHAR(128) NOT NULL,
    icon             VARCHAR(64),
    permission_code  VARCHAR(128),
    hidden           BOOLEAN NOT NULL DEFAULT FALSE,
    adcode           VARCHAR(12) NOT NULL,
    tenant_id        VARCHAR(64) NOT NULL,
    tenant_path      VARCHAR(255) NOT NULL,
    dept_id          VARCHAR(64),
    dept_path        VARCHAR(255),
    created_at       TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE upms.tb_sys_menu IS '菜单表';
COMMENT ON COLUMN upms.tb_sys_menu.route_id IS '路由ID';
COMMENT ON COLUMN upms.tb_sys_menu.parent_route_id IS '父路由ID';
COMMENT ON COLUMN upms.tb_sys_menu.route_path IS '路由路径';
COMMENT ON COLUMN upms.tb_sys_menu.route_name IS '路由名称';
COMMENT ON COLUMN upms.tb_sys_menu.component_key IS '组件标识';
COMMENT ON COLUMN upms.tb_sys_menu.layout_type IS '布局类型';
COMMENT ON COLUMN upms.tb_sys_menu.title IS '菜单标题';
COMMENT ON COLUMN upms.tb_sys_menu.icon IS '图标';
COMMENT ON COLUMN upms.tb_sys_menu.permission_code IS '权限编码';
COMMENT ON COLUMN upms.tb_sys_menu.hidden IS '是否隐藏';
COMMENT ON COLUMN upms.tb_sys_menu.adcode IS '行政区划编码';
COMMENT ON COLUMN upms.tb_sys_menu.tenant_id IS '租户ID';
COMMENT ON COLUMN upms.tb_sys_menu.tenant_path IS '租户路径';
COMMENT ON COLUMN upms.tb_sys_menu.dept_id IS '部门ID';
COMMENT ON COLUMN upms.tb_sys_menu.dept_path IS '部门路径';
COMMENT ON COLUMN upms.tb_sys_menu.created_at IS '创建时间';

CREATE INDEX IF NOT EXISTS idx_sys_tenant_adcode ON upms.tb_sys_tenant(adcode);
CREATE INDEX IF NOT EXISTS idx_dept_tenant ON upms.tb_sys_dept(tenant_id, dept_path);
CREATE INDEX IF NOT EXISTS idx_user_tenant ON upms.tb_sys_user(tenant_id, dept_id);
CREATE INDEX IF NOT EXISTS idx_route_tenant ON upms.tb_sys_menu(tenant_id, route_path);
