-- ============================================================
-- 需水量预测 + 调度推荐 DDL
-- ============================================================

-- 1. 历史需水量数据表
CREATE TABLE IF NOT EXISTS bi_historical_water_demand (
    id              BIGSERIAL PRIMARY KEY,
    area_code       VARCHAR(32) NOT NULL,          -- 区域编码
    area_name       VARCHAR(64),                   -- 区域名称
    record_date     DATE NOT NULL,                 -- 日期
    volume          DECIMAL(12,2),                 -- 需水量(m³)
    max_temp        DECIMAL(5,1),                  -- 最高温度(℃)
    min_temp        DECIMAL(5,1),                  -- 最低温度(℃)
    weather         VARCHAR(16),                   -- 天气: sunny/cloudy/rainy/snowy
    rainfall        DECIMAL(8,1),                  -- 降雨量(mm)
    is_holiday      BOOLEAN DEFAULT FALSE,         -- 是否节假日
    day_of_week     INTEGER,                       -- 星期几(1=周一,7=周日)
    data_source     VARCHAR(16) DEFAULT 'METER',   -- 数据来源: METER/ESTIMATED/SIMULATED
    deleted         INTEGER DEFAULT 0,
    created_at      TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at      TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE bi_historical_water_demand IS '历史需水量数据';
CREATE INDEX IF NOT EXISTS idx_hwd_area_date ON bi_historical_water_demand(area_code, record_date);

-- 2. 需水量预测记录表
CREATE TABLE IF NOT EXISTS bi_water_demand_forecast (
    id              BIGSERIAL PRIMARY KEY,
    area_code       VARCHAR(32) NOT NULL,          -- 区域编码
    area_name       VARCHAR(64),                   -- 区域名称
    forecast_date   DATE NOT NULL,                 -- 预测日期
    forecast_volume DECIMAL(12,2),                 -- 预测水量(m³)
    actual_volume   DECIMAL(12,2),                 -- 实际水量(用于评估)
    model_type      VARCHAR(32) NOT NULL,          -- 模型类型: MOVING_AVERAGE/EXPONENTIAL_SMOOTHING/SEASONAL_DECOMPOSITION
    model_params    TEXT,                          -- 模型参数JSON
    mae             DECIMAL(10,2),                 -- 平均绝对误差
    rmse            DECIMAL(10,2),                 -- 均方根误差
    mape            DECIMAL(8,2),                  -- 平均绝对百分比误差(%)
    confidence      DECIMAL(5,4),                  -- 置信度(0-1)
    lower_bound     DECIMAL(12,2),                 -- 置信区间下限
    upper_bound     DECIMAL(12,2),                 -- 置信区间上限
    generate_time   TIMESTAMP,                     -- 预测生成时间
    remark          VARCHAR(255),
    deleted         INTEGER DEFAULT 0,
    created_at      TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at      TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE bi_water_demand_forecast IS '需水量预测记录';
CREATE INDEX IF NOT EXISTS idx_forecast_area_date ON bi_water_demand_forecast(area_code, forecast_date);
CREATE INDEX IF NOT EXISTS idx_forecast_model ON bi_water_demand_forecast(model_type);

-- 3. 调度推荐方案表
CREATE TABLE IF NOT EXISTS bi_scheduling_recommendation (
    id                  BIGSERIAL PRIMARY KEY,
    forecast_id         BIGINT,                    -- 关联预测ID
    scheme_name         VARCHAR(128) NOT NULL,      -- 方案名称
    scheme_type         VARCHAR(16) NOT NULL,       -- 方案类型: NORMAL/PEAK_SHIFT/EMERGENCY
    description         TEXT,                       -- 方案描述
    pump_combination    TEXT,                       -- 泵站组合JSON
    estimated_saving    DECIMAL(12,2),              -- 预计节水量(m³)
    estimated_energy_saving DECIMAL(12,2),          -- 预计节能(kWh)
    score               DECIMAL(6,1),               -- 方案评分(0-100)
    execute_start       TIMESTAMP,                  -- 执行时间窗口-开始
    execute_end         TIMESTAMP,                  -- 执行时间窗口-结束
    risk_level          VARCHAR(8),                 -- 风险等级: LOW/MEDIUM/HIGH
    status              VARCHAR(16) DEFAULT 'PENDING', -- 状态: PENDING/ACCEPTED/EXECUTING/COMPLETED/REJECTED
    remark              VARCHAR(255),
    deleted             INTEGER DEFAULT 0,
    created_at          TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at          TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE bi_scheduling_recommendation IS '调度推荐方案';
CREATE INDEX IF NOT EXISTS idx_scheduling_forecast ON bi_scheduling_recommendation(forecast_id);
CREATE INDEX IF NOT EXISTS idx_scheduling_status ON bi_scheduling_recommendation(status);
