-- 学校租户表
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,
    province_code    VARCHAR(12) NOT NULL,
    area_code        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
);

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,
    province_code    VARCHAR(12) NOT NULL,
    area_code        VARCHAR(12) NOT NULL,
    tenant_path      VARCHAR(255) NOT NULL,
    dept_path        VARCHAR(255) NOT NULL,
    created_at       TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

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,
    province_code    VARCHAR(12) NOT NULL,
    area_code        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
);

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,
    province_code    VARCHAR(12) NOT NULL,
    area_code        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
);

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,
    province_code    VARCHAR(12) NOT NULL,
    area_code        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
);

CREATE TABLE IF NOT EXISTS upms.tb_sys_menu_route (
    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,
    province_code    VARCHAR(12) NOT NULL,
    area_code        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
);

CREATE INDEX IF NOT EXISTS idx_sys_tenant_area ON upms.tb_sys_tenant(province_code, area_code);
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_route(tenant_id, route_path);
