-- =============================================
-- 远传集抄模块 DDL
-- 批量抄表 + 大表监控(DN80+) + 任务管理 + 数据质量
-- =============================================

-- 抄表任务
CREATE TABLE IF NOT EXISTS mr_task (
    id              BIGSERIAL PRIMARY KEY,
    task_no         VARCHAR(30) UNIQUE NOT NULL,
    task_name       VARCHAR(200) NOT NULL,
    task_type       VARCHAR(20) NOT NULL,           -- batch_read / large_monitor / manual_check
    area            VARCHAR(50),
    meter_count     INT DEFAULT 0,
    success_count   INT DEFAULT 0,
    fail_count      INT DEFAULT 0,
    abnormal_count  INT DEFAULT 0,
    progress        SMALLINT DEFAULT 0,             -- 0-100
    status          VARCHAR(20) DEFAULT 'pending',  -- pending / running / completed / failed / cancelled
    scheduled_time  TIMESTAMP,
    started_at      TIMESTAMP,
    completed_at    TIMESTAMP,
    assigned_to     VARCHAR(50),
    remark          VARCHAR(500),
    deleted         SMALLINT DEFAULT 0,
    created_at      TIMESTAMP DEFAULT NOW(),
    updated_at      TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE mr_task IS '抄表任务表';
CREATE INDEX IF NOT EXISTS idx_mr_task_status ON mr_task(status);

-- 远传抄表记录
CREATE TABLE IF NOT EXISTS mr_meter_reading (
    id              BIGSERIAL PRIMARY KEY,
    task_id         BIGINT REFERENCES mr_task(id),
    meter_id        BIGINT NOT NULL,
    meter_no        VARCHAR(50) NOT NULL,
    device_sn       VARCHAR(50),
    prev_reading    DECIMAL(12,2),
    curr_reading    DECIMAL(12,2),
    consumption     DECIMAL(12,2),
    reading_time    TIMESTAMP NOT NULL,
    reading_period  VARCHAR(10),                    -- 2026-06
    signal_strength SMALLINT,                       -- 信号强度 0-100
    battery_level   SMALLINT,                       -- 电池电量 0-100
    read_status     VARCHAR(20) DEFAULT 'success',  -- success / failed / timeout / abnormal
    abnormal_type   VARCHAR(50),                    -- reverse_flow / over_range / stuck / low_battery / signal_lost
    abnormal_desc   VARCHAR(500),
    verified        SMALLINT DEFAULT 0,
    verified_by     BIGINT,
    verified_at     TIMESTAMP,
    deleted         SMALLINT DEFAULT 0,
    created_at      TIMESTAMP DEFAULT NOW(),
    updated_at      TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE mr_meter_reading IS '远传抄表记录表';
CREATE INDEX IF NOT EXISTS idx_mr_reading_task ON mr_meter_reading(task_id);
CREATE INDEX IF NOT EXISTS idx_mr_reading_meter ON mr_meter_reading(meter_id);
CREATE INDEX IF NOT EXISTS idx_mr_reading_period ON mr_meter_reading(reading_period);

-- 大表监控记录 (DN80+)
CREATE TABLE IF NOT EXISTS mr_large_meter_monitor (
    id              BIGSERIAL PRIMARY KEY,
    meter_id        BIGINT NOT NULL,
    meter_no        VARCHAR(50) NOT NULL,
    caliber         VARCHAR(10) NOT NULL,           -- DN80/DN100/DN150/DN200/DN300/DN400
    customer_name   VARCHAR(100),
    area            VARCHAR(50),
    instant_flow    DECIMAL(12,4),                  -- 瞬时流量 m³/h
    cumulative_flow DECIMAL(12,2),                  -- 累计流量 m³
    pressure        DECIMAL(8,4),                   -- 压力 MPa
    flow_direction  SMALLINT DEFAULT 1,             -- 1:正向 -1:反向
    temperature     DECIMAL(6,2),                   -- 水温 ℃
    monitor_time    TIMESTAMP NOT NULL,
    status          VARCHAR(20) DEFAULT 'normal',   -- normal / warning / alarm / offline
    alarm_type      VARCHAR(50),                    -- over_flow / reverse_flow / pressure_low / pressure_high / offline
    alarm_desc      VARCHAR(500),
    deleted         SMALLINT DEFAULT 0,
    created_at      TIMESTAMP DEFAULT NOW(),
    updated_at      TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE mr_large_meter_monitor IS '大表监控记录表(DN80+)';
CREATE INDEX IF NOT EXISTS idx_mr_large_meter ON mr_large_meter_monitor(meter_id);
CREATE INDEX IF NOT EXISTS idx_mr_large_time ON mr_large_meter_monitor(monitor_time);
CREATE INDEX IF NOT EXISTS idx_mr_large_status ON mr_large_meter_monitor(status);

-- 数据质量分析
CREATE TABLE IF NOT EXISTS mr_data_quality (
    id              BIGSERIAL PRIMARY KEY,
    analysis_period VARCHAR(10) NOT NULL,           -- 2026-06
    area            VARCHAR(50),
    total_meters    INT DEFAULT 0,
    read_success    INT DEFAULT 0,
    read_failed     INT DEFAULT 0,
    read_timeout    INT DEFAULT 0,
    abnormal_count  INT DEFAULT 0,
    read_rate       DECIMAL(6,2),                   -- 抄表率 %
    success_rate    DECIMAL(6,2),                   -- 成功率 %
    abnormal_rate   DECIMAL(6,2),                   -- 异常率 %
    avg_signal      DECIMAL(6,2),                   -- 平均信号强度
    avg_battery     DECIMAL(6,2),                   -- 平均电池电量
    quality_score   DECIMAL(6,2),                   -- 数据质量评分 0-100
    quality_level   VARCHAR(10),                    -- excellent / good / fair / poor
    detail          JSONB,                          -- 详细分析数据
    deleted         SMALLINT DEFAULT 0,
    created_at      TIMESTAMP DEFAULT NOW(),
    updated_at      TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE mr_data_quality IS '抄表数据质量分析表';
CREATE INDEX IF NOT EXISTS idx_mr_quality_period ON mr_data_quality(analysis_period);
