-- 红绿灯系统数据库表创建脚本
-- 创建时间: 2025-01-05
-- 描述: 创建路口信息表和红绿灯设备表，支持多路口多设备管理

-- 1. 路口信息表
CREATE TABLE IF NOT EXISTS intersections (
    id BIGSERIAL PRIMARY KEY,
    intersection_id VARCHAR(50) UNIQUE NOT NULL,  -- 路口编号
    intersection_name VARCHAR(100) NOT NULL,      -- 路口名称
    latitude DECIMAL(10, 8) NOT NULL,             -- 纬度
    longitude DECIMAL(11, 8) NOT NULL,            -- 经度
    area_code VARCHAR(20),                        -- 区域编码
    description TEXT,                             -- 路口描述
    is_active BOOLEAN DEFAULT true,               -- 是否激活
    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 2. 红绿灯设备表
CREATE TABLE IF NOT EXISTS traffic_lights (
    id BIGSERIAL PRIMARY KEY,
    device_id VARCHAR(50) UNIQUE,                -- 红绿灯设备编号（可选）
    device_name VARCHAR(100) NOT NULL,           -- 设备名称
    ip_address VARCHAR(45) NOT NULL DEFAULT '0.0.0.0', -- 设备IP地址
    port INTEGER,                                -- 设备端口号
    intersection_id VARCHAR(50) NOT NULL,        -- 关联的路口编号
    device_type VARCHAR(20) DEFAULT 'STANDARD',  -- 设备类型
    manufacturer VARCHAR(50),                    -- 制造商
    model VARCHAR(50),                           -- 型号
    install_date DATE,                           -- 安装日期
    is_online BOOLEAN DEFAULT false,             -- 是否在线
    last_heartbeat TIMESTAMP,                    -- 最后心跳时间
    is_active BOOLEAN DEFAULT true,              -- 是否激活
    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    
    -- 外键约束
    CONSTRAINT fk_traffic_light_intersection 
        FOREIGN KEY (intersection_id) 
        REFERENCES intersections(intersection_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);

-- 创建索引
CREATE INDEX IF NOT EXISTS idx_intersection_id ON intersections(intersection_id);
CREATE INDEX IF NOT EXISTS idx_intersection_area_code ON intersections(area_code);
CREATE INDEX IF NOT EXISTS idx_intersection_active ON intersections(is_active);

CREATE INDEX IF NOT EXISTS idx_traffic_light_device_id ON traffic_lights(device_id);
CREATE INDEX IF NOT EXISTS idx_traffic_light_intersection ON traffic_lights(intersection_id);
CREATE INDEX IF NOT EXISTS idx_traffic_light_online ON traffic_lights(is_online);
CREATE INDEX IF NOT EXISTS idx_traffic_light_active ON traffic_lights(is_active);
CREATE UNIQUE INDEX IF NOT EXISTS idx_traffic_light_ip_port_unique ON traffic_lights(ip_address, port);
CREATE INDEX IF NOT EXISTS idx_traffic_light_ip_address ON traffic_lights(ip_address);

-- 创建更新时间触发器函数
CREATE OR REPLACE FUNCTION update_updated_time_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_time = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ language 'plpgsql';

-- 为路口表创建更新时间触发器
CREATE TRIGGER update_intersections_updated_time 
    BEFORE UPDATE ON intersections 
    FOR EACH ROW 
    EXECUTE FUNCTION update_updated_time_column();

-- 为红绿灯设备表创建更新时间触发器
CREATE TRIGGER update_traffic_lights_updated_time 
    BEFORE UPDATE ON traffic_lights 
    FOR EACH ROW 
    EXECUTE FUNCTION update_updated_time_column();

-- 插入示例数据
INSERT INTO intersections (intersection_id, intersection_name, latitude, longitude, area_code, description) 
VALUES 
    ('INTERSECTION_001', '主要路口1号', 39.9042, 116.4074, 'AREA_A', '机场主要路口，连接航站楼和跑道区域'),
    ('INTERSECTION_002', '次要路口2号', 39.9050, 116.4080, 'AREA_B', '机场次要路口，连接货运区域'),
    ('INTERSECTION_003', '货运区路口', 39.9035, 116.4065, 'AREA_C', '货运区域主要路口'),
    ('INTERSECTION_004', '维修区路口', 39.9055, 116.4085, 'AREA_D', '维修区域路口'),
    ('INTERSECTION_005', '停机坪路口', 39.9045, 116.4070, 'AREA_A', '停机坪入口路口')
ON CONFLICT (intersection_id) DO NOTHING;

INSERT INTO traffic_lights (device_id, device_name, ip_address, port, intersection_id, device_type, manufacturer, model, install_date) 
VALUES 
    ('TL_001', '主路口红绿灯1号', '192.168.1.101', 8082, 'INTERSECTION_001', 'STANDARD', '海康威视', 'DS-TL100', '2024-01-01'),
    ('TL_002', '次路口红绿灯2号', '192.168.1.102', 8082, 'INTERSECTION_002', 'STANDARD', '大华技术', 'DH-TL200', '2024-01-15'),
    ('TL_003', '货运区红绿灯', '192.168.1.103', 8082, 'INTERSECTION_003', 'STANDARD', '海康威视', 'DS-TL100', '2024-02-01'),
    ('TL_004', '维修区红绿灯', '192.168.1.104', 8082, 'INTERSECTION_004', 'HEAVY_DUTY', '大华技术', 'DH-TL300', '2024-02-15'),
    ('TL_005', '停机坪红绿灯', '192.168.1.105', 8082, 'INTERSECTION_005', 'STANDARD', '海康威视', 'DS-TL100', '2024-03-01')
ON CONFLICT (device_id) DO NOTHING;

-- 添加注释
COMMENT ON TABLE intersections IS '路口信息表，存储机场内各个路口的基本信息';
COMMENT ON TABLE traffic_lights IS '红绿灯设备表，存储红绿灯设备信息及其与路口的绑定关系';

COMMENT ON COLUMN intersections.intersection_id IS '路口唯一编号';
COMMENT ON COLUMN intersections.intersection_name IS '路口名称';
COMMENT ON COLUMN intersections.latitude IS '路口纬度坐标';
COMMENT ON COLUMN intersections.longitude IS '路口经度坐标';
COMMENT ON COLUMN intersections.area_code IS '所属区域编码';

COMMENT ON COLUMN traffic_lights.device_id IS '红绿灯设备唯一编号（可选）';
COMMENT ON COLUMN traffic_lights.device_name IS '设备名称';
COMMENT ON COLUMN traffic_lights.ip_address IS '红绿灯设备IP地址';
COMMENT ON COLUMN traffic_lights.port IS '红绿灯设备端口号';
COMMENT ON COLUMN traffic_lights.intersection_id IS '关联的路口编号';
COMMENT ON COLUMN traffic_lights.is_online IS '设备是否在线';
COMMENT ON COLUMN traffic_lights.last_heartbeat IS '最后一次心跳时间';