-- 增强抄表功能相关表结构

-- 1. 批量抄表报告表
CREATE TABLE IF NOT EXISTS rev_batch_report (
    report_id VARCHAR(100) PRIMARY KEY,
    period VARCHAR(7) NOT NULL COMMENT '抄表周期 yyyy-MM',
    total_meters INTEGER NOT NULL DEFAULT 0 COMMENT '总表数',
    success_meters INTEGER NOT NULL DEFAULT 0 COMMENT '成功抄表数',
    failed_meters INTEGER NOT NULL DEFAULT 0 COMMENT '失败抄表数',
    abnormal_meters INTEGER NOT NULL DEFAULT 0 COMMENT '异常读数数',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- 2. 抄表异常记录表
CREATE TABLE IF NOT EXISTS rev_reading_exception (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    meter_id BIGINT NOT NULL,
    meter_no VARCHAR(50) NOT NULL,
    exception_type VARCHAR(50) NOT NULL COMMENT '异常类型: DECREASE/NEGATIVE/EXCESSIVE/ZERO',
    exception_reason TEXT COMMENT '异常原因描述',
    prev_reading DECIMAL(12,2) NOT NULL,
    curr_reading DECIMAL(12,2) NOT NULL,
    consumption DECIMAL(12,2) NOT NULL,
    reading_date DATE NOT NULL,
    area VARCHAR(100) NOT NULL,
    is_resolved BOOLEAN DEFAULT FALSE COMMENT '是否已处理',
    resolved_at TIMESTAMP NULL,
    resolved_by VARCHAR(100) NULL,
    remark TEXT COMMENT '处理备注',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_meter_id (meter_id),
    INDEX idx_reading_date (reading_date),
    INDEX idx_exception_type (exception_type),
    INDEX idx_area (area)
);

-- 3. 大表监控记录表
CREATE TABLE IF NOT EXISTS rev_large_meter_monitor (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    meter_id BIGINT NOT NULL,
    meter_no VARCHAR(50) NOT NULL,
    caliber VARCHAR(20) NOT NULL COMMENT '管径',
    customer_name VARCHAR(200) NOT NULL,
    area VARCHAR(100) NOT NULL,
    device_sn VARCHAR(100) COMMENT '设备号',
    current_reading DECIMAL(12,2) COMMENT '当前读数',
    last_reading_date DATE COMMENT '上次抄表日期',
    monthly_consumption DECIMAL(12,2) COMMENT '月用量',
    monitor_status VARCHAR(20) DEFAULT 'NORMAL' COMMENT '监控状态: NORMAL/ALARM/OFFLINE',
    alert_level VARCHAR(20) COMMENT '预警级别: LOW/MEDIUM/HIGH/CRITICAL',
    alert_count INTEGER DEFAULT 0 COMMENT '预警次数',
    last_alert_time TIMESTAMP NULL COMMENT '最后预警时间',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_meter_no (meter_no),
    INDEX idx_caliber (caliber),
    INDEX idx_area (area),
    INDEX idx_monitor_status (monitor_status),
    INDEX idx_alert_level (alert_level)
);

-- 4. 远传抄表任务表
CREATE TABLE IF NOT EXISTS rev_remote_reading_task (
    task_id BIGINT AUTO_INCREMENT PRIMARY KEY,
    task_name VARCHAR(200) NOT NULL,
    task_type VARCHAR(50) NOT NULL COMMENT '任务类型: SINGLE_AREA/MULTI_AREA/ALL_AREA',
    areas TEXT COMMENT '涉及区域列表(JSON)',
    status VARCHAR(20) DEFAULT 'PENDING' COMMENT '任务状态: PENDING/RUNNING/COMPLETED/FAILED',
    total_meters INTEGER DEFAULT 0,
    success_meters INTEGER DEFAULT 0,
    failed_meters INTEGER DEFAULT 0,
    abnormal_meters INTEGER DEFAULT 0,
    start_time TIMESTAMP NULL,
    end_time TIMESTAMP NULL,
    error_message TEXT,
    created_by VARCHAR(100) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_status (status),
    INDEX idx_created_at (created_at)
);

-- 5. 预警记录表
CREATE TABLE IF NOT EXISTS rev_alert_record (
    alert_id BIGINT AUTO_INCREMENT PRIMARY KEY,
    meter_id BIGINT NOT NULL,
    meter_no VARCHAR(50) NOT NULL,
    alert_type VARCHAR(50) NOT NULL COMMENT '预警类型: HIGH_CONSUMPTION/DEVICE_OFFLINE/ZERO_FLOW/ABNORMAL_DECREASE',
    alert_title VARCHAR(200) NOT NULL COMMENT '预警标题',
    alert_description TEXT COMMENT '预警描述',
    severity VARCHAR(20) DEFAULT 'MEDIUM' COMMENT '严重程度: LOW/MEDIUM/HIGH/CRITICAL',
    status VARCHAR(20) DEFAULT 'PENDING' COMMENT '处理状态: PENDING/ACKNOWLEDGED/RESOLVED',
    acknowledged_by VARCHAR(100) NULL,
    acknowledged_at TIMESTAMP NULL,
    resolved_by VARCHAR(100) NULL,
    resolved_at TIMESTAMP NULL,
    additional_issues TEXT COMMENT '附加问题(JSON)',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_meter_no (meter_no),
    INDEX idx_alert_type (alert_type),
    INDEX idx_severity (severity),
    INDEX idx_status (status),
    INDEX idx_created_at (created_at)
);

-- 6. 抄表结果统计视图
CREATE OR REPLACE VIEW v_reading_statistics AS
SELECT 
    r.period,
    r.area,
    r.total_meters,
    r.success_meters,
    r.failed_meters,
    r.abnormal_meters,
    ROUND((r.success_meters * 100.0 / NULLIF(r.total_meters, 0)), 2) as success_rate,
    ROUND((r.abnormal_meters * 100.0 / NULLIF(r.total_meters, 0)), 2) as abnormal_rate
FROM rev_batch_report r
ORDER BY r.period DESC, r.area;

-- 7. 大表监控统计视图
CREATE OR REPLACE VIEW v_large_meter_statistics AS
SELECT 
    caliber,
    COUNT(*) as total_count,
    SUM(CASE WHEN monitor_status = 'NORMAL' THEN 1 ELSE 0 END) as normal_count,
    SUM(CASE WHEN monitor_status = 'ALARM' THEN 1 ELSE 0 END) as alarm_count,
    SUM(CASE WHEN monitor_status = 'OFFLINE' THEN 1 ELSE 0 END) as offline_count,
    ROUND(SUM(monthly_consumption), 2) as total_consumption,
    ROUND(AVG(monthly_consumption), 2) as avg_consumption,
    MAX(monthly_consumption) as max_consumption
FROM rev_large_meter_monitor
GROUP BY caliber
ORDER BY caliber;

