-- =============================================
-- 智慧水务管理系统 - 综合工单管理 DDL
-- 版本: V3
-- 描述: 工单创建/分派/处理/完成/统计
-- =============================================

-- ==================== 工单主表 ====================
CREATE TABLE IF NOT EXISTS dispatch_work_order (
    id BIGSERIAL PRIMARY KEY,
    order_no VARCHAR(50) UNIQUE NOT NULL,
    title VARCHAR(200) NOT NULL,
    description TEXT,
    type VARCHAR(30) NOT NULL DEFAULT 'REPAIR',       -- REPAIR/INSPECTION/COMPLAINT/EMERGENCY
    priority VARCHAR(20) NOT NULL DEFAULT 'MEDIUM',    -- LOW/MEDIUM/HIGH/URGENT
    status INT NOT NULL DEFAULT 0,                     -- 0:待办 1:已分派 2:处理中 4:已完成 5:已驳回 6:已取消
    assignee_id BIGINT,
    assignee_name VARCHAR(50),
    creator_id BIGINT,
    creator_name VARCHAR(50),
    source VARCHAR(50),                                 -- 来源: manual/phone/app/alert
    facility_id BIGINT,
    location VARCHAR(500),
    longitude DOUBLE PRECISION,
    latitude DOUBLE PRECISION,
    attachments TEXT,                                   -- JSON数组
    deadline TIMESTAMP,
    assigned_at TIMESTAMP,
    started_at TIMESTAMP,
    completed_at TIMESTAMP,
    result TEXT,
    reject_reason TEXT,
    remark TEXT,
    deleted SMALLINT DEFAULT 0,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE dispatch_work_order IS '工单主表';
COMMENT ON COLUMN dispatch_work_order.type IS '工单类型: REPAIR(维修)/INSPECTION(巡检)/COMPLAINT(投诉)/EMERGENCY(应急)';
COMMENT ON COLUMN dispatch_work_order.priority IS '优先级: LOW/MEDIUM/HIGH/URGENT';
COMMENT ON COLUMN dispatch_work_order.status IS '状态: 0待办/1已分派/2处理中/4已完成/5已驳回/6已取消';

CREATE INDEX IF NOT EXISTS idx_wo_order_no ON dispatch_work_order(order_no);
CREATE INDEX IF NOT EXISTS idx_wo_status ON dispatch_work_order(status);
CREATE INDEX IF NOT EXISTS idx_wo_type ON dispatch_work_order(type);
CREATE INDEX IF NOT EXISTS idx_wo_assignee ON dispatch_work_order(assignee_id);
CREATE INDEX IF NOT EXISTS idx_wo_creator ON dispatch_work_order(creator_id);
CREATE INDEX IF NOT EXISTS idx_wo_created_at ON dispatch_work_order(created_at);

-- ==================== 工单处理日志 ====================
CREATE TABLE IF NOT EXISTS dispatch_work_order_log (
    id BIGSERIAL PRIMARY KEY,
    work_order_id BIGINT NOT NULL REFERENCES dispatch_work_order(id),
    order_no VARCHAR(50),
    stage VARCHAR(30),
    from_status INT,
    to_status INT,
    operator_id BIGINT,
    operator_name VARCHAR(50),
    action_desc TEXT,
    attachments TEXT,
    created_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE dispatch_work_order_log IS '工单处理日志';

CREATE INDEX IF NOT EXISTS idx_wolog_wo_id ON dispatch_work_order_log(work_order_id);
CREATE INDEX IF NOT EXISTS idx_wolog_order_no ON dispatch_work_order_log(order_no);

-- ==================== 工单分派记录 ====================
CREATE TABLE IF NOT EXISTS dispatch_work_order_assignment (
    id BIGSERIAL PRIMARY KEY,
    work_order_id BIGINT NOT NULL REFERENCES dispatch_work_order(id),
    order_no VARCHAR(50),
    assigner_id BIGINT,
    assigner_name VARCHAR(50),
    assignee_id BIGINT,
    assignee_name VARCHAR(50),
    role VARCHAR(20) NOT NULL DEFAULT 'PRIMARY',       -- PRIMARY/ASSISTANT
    assignment_note TEXT,
    assignment_type VARCHAR(20) DEFAULT 'MANUAL',      -- MANUAL/AUTO
    status INT DEFAULT 1,                              -- 1:活跃 0:已移除
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE dispatch_work_order_assignment IS '工单分派记录（支持多人协作）';

CREATE INDEX IF NOT EXISTS idx_woassign_wo_id ON dispatch_work_order_assignment(work_order_id);
CREATE INDEX IF NOT EXISTS idx_woassign_assignee ON dispatch_work_order_assignment(assignee_id);
