-- =============================================
-- 智慧水务管理系统 - 物联网设备相关 DDL
-- 版本: V1
-- =============================================

-- 扩展
CREATE EXTENSION IF NOT EXISTS postgis;

-- 设备模型定义
CREATE TABLE IF NOT EXISTS iot_device_model (
    id BIGSERIAL PRIMARY KEY,
    model_key VARCHAR(50) UNIQUE NOT NULL,
    model_name VARCHAR(100) NOT NULL,
    vendor VARCHAR(100),
    protocol VARCHAR(20) NOT NULL,  -- MQTT/Modbus/CoAP/HTTP
    properties JSONB,               -- [{key, name, unit, data_type, range_min, range_max}]
    commands JSONB,                 -- [{key, name, params: [{key, name, type}]}]
    status SMALLINT DEFAULT 1,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE iot_device_model IS '设备模型定义表';
COMMENT ON COLUMN iot_device_model.properties IS '属性定义JSON数组';
COMMENT ON COLUMN iot_device_model.commands IS '支持指令JSON数组';

-- 设备实例
CREATE TABLE IF NOT EXISTS iot_device (
    id BIGSERIAL PRIMARY KEY,
    device_sn VARCHAR(100) UNIQUE NOT NULL,
    device_name VARCHAR(200) NOT NULL,
    model_id BIGINT REFERENCES iot_device_model(id),
    device_type VARCHAR(30) NOT NULL,       -- flow_meter/pressure_sensor/valve/water_quality/level_sensor/pump
    position_type VARCHAR(20),              -- water_plant/pressure_station/pipe_network/village
    label VARCHAR(50),                      -- 设备标签(便于搜索)
    loc_lng DOUBLE PRECISION,
    loc_lat DOUBLE PRECISION,
    geom GEOMETRY(Point, 4326),
    area VARCHAR(50) NOT NULL,              -- 所属片区
    station_id BIGINT,                      -- 所属站点
    address VARCHAR(300),
    install_date DATE,
    warranty_until DATE,
    status VARCHAR(20) DEFAULT 'offline',   -- online/offline/maintenance/fault
    firmware_version VARCHAR(20),
    last_report_time TIMESTAMP,
    metadata JSONB,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE iot_device IS '设备实例表';
COMMENT ON COLUMN iot_device.geom IS 'PostGIS空间坐标点';
COMMENT ON COLUMN iot_device.metadata IS '扩展元数据';

-- 空间索引
CREATE INDEX IF NOT EXISTS idx_iot_device_geom ON iot_device USING GIST(geom);
CREATE INDEX IF NOT EXISTS idx_iot_device_area ON iot_device(area);
CREATE INDEX IF NOT EXISTS idx_iot_device_type ON iot_device(device_type);
CREATE INDEX IF NOT EXISTS idx_iot_device_status ON iot_device(status);

-- 设备影子
CREATE TABLE IF NOT EXISTS iot_device_shadow (
    id BIGSERIAL PRIMARY KEY,
    device_id BIGINT UNIQUE REFERENCES iot_device(id),
    reported_state JSONB,   -- 设备上报的最新状态
    desired_state JSONB,    -- 期望状态(云端下发)
    delta_state JSONB,      -- 差异状态
    version BIGINT DEFAULT 1,
    updated_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE iot_device_shadow IS '设备影子表';

-- 设备事件
CREATE TABLE IF NOT EXISTS iot_device_event (
    id BIGSERIAL PRIMARY KEY,
    device_id BIGINT REFERENCES iot_device(id),
    device_sn VARCHAR(100) NOT NULL,
    event_type VARCHAR(30) NOT NULL,  -- online/offline/fault/recovery/ota
    event_data JSONB,
    created_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE iot_device_event IS '设备事件表';
CREATE INDEX IF NOT EXISTS idx_iot_device_event_sn ON iot_device_event(device_sn, created_at DESC);

-- 站点管理（水厂/调压站等）
CREATE TABLE IF NOT EXISTS iot_station (
    id BIGSERIAL PRIMARY KEY,
    station_name VARCHAR(100) NOT NULL,
    station_type VARCHAR(30) NOT NULL,       -- water_plant/pressure_station/pump_station
    area VARCHAR(50) NOT NULL,
    loc_lng DOUBLE PRECISION,
    loc_lat DOUBLE PRECISION,
    geom GEOMETRY(Point, 4326),
    address VARCHAR(300),
    manager VARCHAR(50),
    manager_phone VARCHAR(20),
    status SMALLINT DEFAULT 1,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE iot_station IS '站点管理表';
CREATE INDEX IF NOT EXISTS idx_iot_station_geom ON iot_station USING GIST(geom);
