-- =============================================
-- 智慧水务管理系统 - PostgreSQL DDL
-- 版本: V2
-- 描述: 巡检执行模块（检查项/GPS轨迹/异常上报）
-- =============================================

-- ==================== 巡检执行表 ====================
CREATE TABLE IF NOT EXISTS patrol_execution (
    id              BIGSERIAL PRIMARY KEY,
    task_id         BIGINT NOT NULL REFERENCES patrol_task(id),
    inspector_id    BIGINT NOT NULL REFERENCES patrol_worker(id),
    inspector_name  VARCHAR(50),
    status          VARCHAR(20) NOT NULL DEFAULT 'pending',     -- pending/in_progress/completed/aborted
    start_time      TIMESTAMP,
    end_time        TIMESTAMP,
    total_check_items   INT DEFAULT 0,
    completed_check_items INT DEFAULT 0,
    issue_count     INT DEFAULT 0,
    total_distance  DECIMAL(10,2) DEFAULT 0,                    -- 巡检总距离(km)
    start_lng       DOUBLE PRECISION,
    start_lat       DOUBLE PRECISION,
    end_lng         DOUBLE PRECISION,
    end_lat         DOUBLE PRECISION,
    remark          VARCHAR(500),
    deleted         SMALLINT DEFAULT 0,
    created_at      TIMESTAMP DEFAULT NOW(),
    updated_at      TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE patrol_execution IS '巡检执行表';
COMMENT ON COLUMN patrol_execution.status IS '状态: pending(待开始)/in_progress(进行中)/completed(已完成)/aborted(已中止)';
COMMENT ON COLUMN patrol_execution.total_distance IS '巡检总距离(km)';
CREATE INDEX IF NOT EXISTS idx_patrol_execution_task ON patrol_execution(task_id);
CREATE INDEX IF NOT EXISTS idx_patrol_execution_inspector ON patrol_execution(inspector_id);
CREATE INDEX IF NOT EXISTS idx_patrol_execution_status ON patrol_execution(status);
CREATE INDEX IF NOT EXISTS idx_patrol_execution_start_time ON patrol_execution(start_time);

-- ==================== 检查项记录表 ====================
CREATE TABLE IF NOT EXISTS check_item_record (
    id              BIGSERIAL PRIMARY KEY,
    execution_id    BIGINT NOT NULL REFERENCES patrol_execution(id),
    point_seq       INT,                                        -- 检查点序号
    point_name      VARCHAR(100),                               -- 检查点名称
    item_name       VARCHAR(200),                               -- 检查项名称
    check_status    VARCHAR(20) NOT NULL DEFAULT 'normal',      -- normal/abnormal/skipped
    record_value    DOUBLE PRECISION,                           -- 数值录入
    value_unit      VARCHAR(20),                                -- 数值单位
    description     TEXT,                                       -- 文字描述
    photo_urls      TEXT,                                       -- 照片URL列表(逗号分隔)
    remark          VARCHAR(500),
    record_time     TIMESTAMP NOT NULL DEFAULT NOW(),
    lng             DOUBLE PRECISION,
    lat             DOUBLE PRECISION,
    deleted         SMALLINT DEFAULT 0,
    created_at      TIMESTAMP DEFAULT NOW(),
    updated_at      TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE check_item_record IS '检查项记录表';
COMMENT ON COLUMN check_item_record.check_status IS '状态: normal(正常)/abnormal(异常)/skipped(跳过)';
CREATE INDEX IF NOT EXISTS idx_check_item_record_execution ON check_item_record(execution_id);
CREATE INDEX IF NOT EXISTS idx_check_item_record_point ON check_item_record(execution_id, point_seq);
CREATE INDEX IF NOT EXISTS idx_check_item_record_status ON check_item_record(check_status);

-- ==================== GPS轨迹点表 ====================
CREATE TABLE IF NOT EXISTS gps_track_point (
    id              BIGSERIAL PRIMARY KEY,
    execution_id    BIGINT NOT NULL REFERENCES patrol_execution(id),
    lng             DOUBLE PRECISION NOT NULL,
    lat             DOUBLE PRECISION NOT NULL,
    altitude        DOUBLE PRECISION,                           -- 海拔(米)
    speed           DOUBLE PRECISION,                           -- 速度(m/s)
    bearing         DOUBLE PRECISION,                           -- 方向(角度, 0-360)
    accuracy        DOUBLE PRECISION,                           -- 精度(米)
    record_time     TIMESTAMP NOT NULL DEFAULT NOW(),
    deleted         SMALLINT DEFAULT 0,
    created_at      TIMESTAMP DEFAULT NOW(),
    updated_at      TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE gps_track_point IS 'GPS轨迹点表';
CREATE INDEX IF NOT EXISTS idx_gps_track_execution ON gps_track_point(execution_id);
CREATE INDEX IF NOT EXISTS idx_gps_track_time ON gps_track_point(execution_id, record_time);

-- ==================== 巡检问题表 ====================
CREATE TABLE IF NOT EXISTS patrol_issue (
    id              BIGSERIAL PRIMARY KEY,
    execution_id    BIGINT NOT NULL REFERENCES patrol_execution(id),
    check_record_id BIGINT REFERENCES check_item_record(id),    -- 关联检查项记录(可选)
    issue_type      VARCHAR(30) NOT NULL,                       -- leak/damage/pollution/illegal/other
    severity        VARCHAR(20) NOT NULL DEFAULT 'medium',      -- low/medium/high/critical
    description     TEXT NOT NULL,
    photo_urls      TEXT,                                       -- 照片URL列表(逗号分隔)
    lng             DOUBLE PRECISION,
    lat             DOUBLE PRECISION,
    address         VARCHAR(300),                               -- 地址描述
    handle_status   VARCHAR(20) NOT NULL DEFAULT 'pending',     -- pending/processing/resolved/closed
    work_order_id   BIGINT,                                     -- 关联工单ID
    reporter_id     BIGINT NOT NULL,
    reporter_name   VARCHAR(50),
    report_time     TIMESTAMP NOT NULL DEFAULT NOW(),
    deleted         SMALLINT DEFAULT 0,
    created_at      TIMESTAMP DEFAULT NOW(),
    updated_at      TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE patrol_issue IS '巡检问题表';
COMMENT ON COLUMN patrol_issue.issue_type IS '问题类型: leak(漏水)/damage(损坏)/pollution(污染)/illegal(违规)/other(其他)';
COMMENT ON COLUMN patrol_issue.severity IS '严重程度: low(低)/medium(中)/high(高)/critical(紧急)';
COMMENT ON COLUMN patrol_issue.handle_status IS '处理状态: pending(待处理)/processing(处理中)/resolved(已解决)/closed(已关闭)';
CREATE INDEX IF NOT EXISTS idx_patrol_issue_execution ON patrol_issue(execution_id);
CREATE INDEX IF NOT EXISTS idx_patrol_issue_type ON patrol_issue(issue_type);
CREATE INDEX IF NOT EXISTS idx_patrol_issue_severity ON patrol_issue(severity);
CREATE INDEX IF NOT EXISTS idx_patrol_issue_handle_status ON patrol_issue(handle_status);
CREATE INDEX IF NOT EXISTS idx_patrol_issue_report_time ON patrol_issue(report_time);
