-- B10: iot_project + iot_subsystem 表创建
-- 版本: V2.0.2

CREATE TABLE IF NOT EXISTS iot_project (
    id          BIGINT PRIMARY KEY AUTO_INCREMENT,
    name        VARCHAR(128) NOT NULL,
    description TEXT,
    icon        VARCHAR(256),
    status      TINYINT NOT NULL DEFAULT 1,
    sort        INT DEFAULT 0,
    tenant_id   BIGINT NOT NULL,
    creator     VARCHAR(64),
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
    updater     VARCHAR(64),
    update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted     BIT NOT NULL DEFAULT b'0',
    UNIQUE KEY uk_name (name, tenant_id, deleted)
) COMMENT='项目（架构预留，本期不开放 API）';

CREATE TABLE IF NOT EXISTS iot_subsystem (
    id          BIGINT PRIMARY KEY AUTO_INCREMENT,
    name        VARCHAR(128) NOT NULL,
    code        VARCHAR(64) NOT NULL,
    description TEXT,
    icon        VARCHAR(256),
    status      TINYINT NOT NULL DEFAULT 1,
    sort        INT DEFAULT 0,
    project_id  BIGINT COMMENT '预留（允许 NULL）',
    config      JSON,
    tenant_id   BIGINT NOT NULL,
    creator     VARCHAR(64),
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
    updater     VARCHAR(64),
    update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted     BIT NOT NULL DEFAULT b'0',
    -- 【评审 A4】加 project_id 维度，MySQL NULL 不参与 UK 唯一性 → 应用层兜底
    UNIQUE KEY uk_name (name, tenant_id, project_id, deleted),
    UNIQUE KEY uk_code (code, tenant_id, project_id, deleted)
) COMMENT='子系统';
