-- V2: 调度指令全生命周期管理
-- 新增/重建表，支持 创建→下发→接收确认→执行反馈→完成归档 全流程

-- 1. 调度指令主表（增强）
CREATE TABLE IF NOT EXISTS disp_dispatch_command (
    id              BIGSERIAL PRIMARY KEY,
    command_no      VARCHAR(64)  NOT NULL UNIQUE,
    title           VARCHAR(200) NOT NULL,
    content         TEXT,
    command_type    VARCHAR(30)  NOT NULL DEFAULT 'NORMAL',   -- NORMAL / EMERGENCY / MAINTENANCE
    priority        VARCHAR(20)  NOT NULL DEFAULT 'MEDIUM',   -- LOW / MEDIUM / HIGH / URGENT
    status          VARCHAR(30)  NOT NULL DEFAULT 'DRAFT',    -- DRAFT / ISSUED / RECEIVED / EXECUTING / COMPLETED / REJECTED / CANCELLED
    issuer_id       BIGINT,
    issuer_name     VARCHAR(64),
    receiver_id     BIGINT,
    receiver_name   VARCHAR(64),
    facility_id     BIGINT,
    deadline        TIMESTAMP,
    issued_at       TIMESTAMP,
    received_at     TIMESTAMP,
    executed_at     TIMESTAMP,
    completed_at    TIMESTAMP,
    rejected_at     TIMESTAMP,
    reject_reason   TEXT,
    execute_result  TEXT,
    remark          TEXT,
    deleted         INT DEFAULT 0,
    created_at      TIMESTAMP DEFAULT NOW(),
    updated_at      TIMESTAMP DEFAULT NOW()
);

CREATE INDEX idx_cmd_status      ON disp_dispatch_command(status);
CREATE INDEX idx_cmd_type        ON disp_dispatch_command(command_type);
CREATE INDEX idx_cmd_priority    ON disp_dispatch_command(priority);
CREATE INDEX idx_cmd_issuer      ON disp_dispatch_command(issuer_id);
CREATE INDEX idx_cmd_receiver    ON disp_dispatch_command(receiver_id);
CREATE INDEX idx_cmd_deadline    ON disp_dispatch_command(deadline);
CREATE INDEX idx_cmd_created_at  ON disp_dispatch_command(created_at);

-- 2. 执行记录表
CREATE TABLE IF NOT EXISTS disp_command_execution_record (
    id              BIGSERIAL PRIMARY KEY,
    command_id      BIGINT       NOT NULL,
    command_no      VARCHAR(64),
    executor_id     BIGINT       NOT NULL,
    executor_name   VARCHAR(64),
    action          VARCHAR(30)  NOT NULL,    -- RECEIVE / START / PROGRESS / COMPLETE / REJECT
    description     TEXT,
    attachments     TEXT,                      -- JSON array of file URLs
    progress        INT DEFAULT 0,            -- 0-100
    created_at      TIMESTAMP DEFAULT NOW()
);

CREATE INDEX idx_exec_cmd_id ON disp_command_execution_record(command_id);
CREATE INDEX idx_exec_cmd_no ON disp_command_execution_record(command_no);

-- 3. 过程追踪表（时间线）
CREATE TABLE IF NOT EXISTS disp_command_tracking (
    id              BIGSERIAL PRIMARY KEY,
    command_id      BIGINT       NOT NULL,
    command_no      VARCHAR(64),
    stage           VARCHAR(30)  NOT NULL,    -- CREATED / ISSUED / RECEIVED / EXECUTING / COMPLETED / REJECTED / CANCELLED
    operator_id     BIGINT,
    operator_name   VARCHAR(64),
    action_desc     VARCHAR(500),
    from_status     VARCHAR(30),
    to_status       VARCHAR(30),
    created_at      TIMESTAMP DEFAULT NOW()
);

CREATE INDEX idx_track_cmd_id ON disp_command_tracking(command_id);
CREATE INDEX idx_track_cmd_no ON disp_command_tracking(command_no);
CREATE INDEX idx_track_stage  ON disp_command_tracking(stage);
