-- =============================================
-- 智慧水务管理系统 - 巡检问题上报 + 工单管理 DDL
-- 版本: V1
-- =============================================

-- 巡检问题上报表
CREATE TABLE IF NOT EXISTS patrol_problem (
    id BIGSERIAL PRIMARY KEY,
    problem_no VARCHAR(30) UNIQUE NOT NULL,   -- 问题编号：WQ-2026-001
    task_id BIGINT REFERENCES patrol_task(id),
    point_seq INT,
    device_id BIGINT,
    device_name VARCHAR(200),
    problem_type VARCHAR(50) NOT NULL,       -- 设备故障/水质异常/安全隐患/环境卫生/其他
    problem_level VARCHAR(20) DEFAULT 'normal', -- low/normal/high/critical
    problem_title VARCHAR(200) NOT NULL,
    problem_description TEXT,
    location VARCHAR(300),
    lng DOUBLE PRECISION,
    lat DOUBLE PRECISION,
    photo_urls JSONB,                        -- 现场照片URL数组
    reporter_id BIGINT REFERENCES sys_user(id),
    reporter_name VARCHAR(50),
    report_time TIMESTAMP DEFAULT NOW(),
    status VARCHAR(20) DEFAULT 'reported',    -- reported/processing/completed/closed
    work_order_id BIGINT,                    -- 关联工单ID
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE patrol_problem IS '巡检问题上报表';
CREATE INDEX IF NOT EXISTS idx_problem_task ON patrol_problem(task_id);
CREATE INDEX IF NOT EXISTS idx_problem_status ON patrol_problem(status);
CREATE INDEX IF NOT EXISTS idx_problem_device ON patrol_problem(device_id);
CREATE INDEX IF NOT EXISTS idx_problem_type ON patrol_problem(problem_type);

-- 工单表
CREATE TABLE IF NOT EXISTS work_order (
    id BIGSERIAL PRIMARY KEY,
    order_no VARCHAR(30) UNIQUE NOT NULL,     -- 工单编号：WO-2026-001
    problem_id BIGINT REFERENCES patrol_problem(id),
    order_type VARCHAR(50) NOT NULL,        -- 设备维修/水质处理/安全隐患处理/清洁/其他
    priority VARCHAR(20) DEFAULT 'normal',   -- low/normal/high/critical
    title VARCHAR(200) NOT NULL,
    description TEXT,
    location VARCHAR(300),
    contact_person VARCHAR(50),
    contact_phone VARCHAR(20),
    reporter_id BIGINT REFERENCES sys_user(id),
    reporter_name VARCHAR(50),
    assignee_id BIGINT REFERENCES sys_user(id),
    assignee_name VARCHAR(50),
    status VARCHAR(20) DEFAULT 'pending',    -- pending/assigned/processing/completed/cancelled
    process_status VARCHAR(20) DEFAULT 'created', -- created/accepted/in_progress/completed
    estimated_duration INT,                  -- 预计工时(分钟)
    actual_start_time TIMESTAMP,
    actual_end_time TIMESTAMP,
    completion_time TIMESTAMP,
    photos_before JSONB,                     -- 处理前照片
    photos_after JSONB,                      -- 处理后照片
    solution_description TEXT,               -- 处理方案描述
    solution_result TEXT,                    -- 处理结果
    customer_feedback TEXT,                  -- 客户反馈
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE work_order IS '工单表';
CREATE INDEX IF NOT EXISTS idx_order_problem ON work_order(problem_id);
CREATE INDEX IF NOT EXISTS idx_order_status ON work_order(status, process_status);
CREATE INDEX IF NOT EXISTS idx_order_assignee ON work_order(assignee_id);

-- 工单处理记录表
CREATE TABLE IF NOT EXISTS work_order_process (
    id BIGSERIAL PRIMARY KEY,
    work_order_id BIGINT REFERENCES work_order(id),
    process_step VARCHAR(50) NOT NULL,       -- created/accepted/in_progress/completed
    processor_id BIGINT REFERENCES sys_user(id),
    processor_name VARCHAR(50),
    action VARCHAR(50) NOT NULL,            -- create/assign/start/complete/cancel
    comment TEXT,
    photos JSONB,                            -- 处理过程照片
    created_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE work_order_process IS '工单处理记录表';
CREATE INDEX IF NOT EXISTS idx_process_order ON work_order_process(work_order_id);
CREATE INDEX IF NOT EXISTS idx_process_step ON work_order_process(process_step);

-- 工单附件表
CREATE TABLE IF NOT EXISTS work_order_attachment (
    id BIGSERIAL PRIMARY KEY,
    work_order_id BIGINT REFERENCES work_order(id),
    file_name VARCHAR(200) NOT NULL,
    file_path VARCHAR(500) NOT NULL,
    file_type VARCHAR(50),                  -- image/pdf/doc/other
    file_size BIGINT,
    uploaded_by BIGINT REFERENCES sys_user(id),
    uploaded_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE work_order_attachment IS '工单附件表';
CREATE INDEX IF NOT EXISTS idx_attachment_order ON work_order_attachment(work_order_id);

-- 巡检问题与工单关联触发记录
CREATE TABLE IF NOT EXISTS patrol_work_order_trigger (
    id BIGSERIAL PRIMARY KEY,
    patrol_problem_id BIGINT REFERENCES patrol_problem(id),
    work_order_id BIGINT REFERENCES work_order(id),
    trigger_type VARCHAR(20) NOT NULL,      -- auto/manual
    trigger_condition JSONB,                 -- 触发条件
    created_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE patrol_work_order_trigger IS '巡检问题与工单关联触发记录';
CREATE INDEX IF NOT EXISTS idx_trigger_problem ON patrol_work_order_trigger(patrol_problem_id);
CREATE INDEX IF NOT EXISTS idx_trigger_order ON patrol_work_order_trigger(work_order_id);