-- 创建无人车违规事件表
-- 专门针对机场无人车冲突检测场景设计的简化表结构
-- 作者: AI Assistant
-- 创建日期: 2025-01-17
-- 版本: 1.0

-- 如果表已存在则删除
DROP TABLE IF EXISTS unmanned_vehicle_violations CASCADE;

-- 创建无人车违规事件表
CREATE TABLE unmanned_vehicle_violations (
    -- 主键ID
    id BIGSERIAL PRIMARY KEY,
    
    -- 无人车车牌号
    vehicle_license VARCHAR(20) NOT NULL,
    
    -- 违规规则名称
    rule_name VARCHAR(100) NOT NULL,
    
    -- 违规类型（SPEED-超速, ACCESS-未授权进入, HEIGHT-超高, WEIGHT-超重）
    violation_type VARCHAR(10) NOT NULL CHECK (violation_type IN ('SPEED', 'ACCESS', 'HEIGHT', 'WEIGHT', 'OTHER')),
    
    -- 违规描述
    description VARCHAR(150),
    
    -- 违规位置（PostGIS几何点）
    location GEOMETRY(Point, 4326),
    
    -- 实际值（如实际速度：19.8）
    actual_value DOUBLE PRECISION,
    
    -- 限制值（如限速：5.0）
    limit_value DOUBLE PRECISION,
    
    -- 违规发生时间
    violation_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    
    -- 记录创建时间
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- 创建索引以提高查询性能
CREATE INDEX idx_uv_violations_vehicle ON unmanned_vehicle_violations(vehicle_license);
CREATE INDEX idx_uv_violations_rule ON unmanned_vehicle_violations(rule_name);
CREATE INDEX idx_uv_violations_time ON unmanned_vehicle_violations(violation_time);
CREATE INDEX idx_uv_violations_type ON unmanned_vehicle_violations(violation_type);

-- 创建复合索引用于常见查询组合
CREATE INDEX idx_uv_violations_vehicle_time ON unmanned_vehicle_violations(vehicle_license, violation_time DESC);
CREATE INDEX idx_uv_violations_type_time ON unmanned_vehicle_violations(violation_type, violation_time DESC);

-- 如果启用了PostGIS，创建空间索引
CREATE INDEX idx_uv_violations_location ON unmanned_vehicle_violations USING GIST(location);

-- 添加表注释
COMMENT ON TABLE unmanned_vehicle_violations IS '无人车违规事件表 - 专门记录机场无人车的各种违规行为';
COMMENT ON COLUMN unmanned_vehicle_violations.id IS '主键ID，自增';
COMMENT ON COLUMN unmanned_vehicle_violations.vehicle_license IS '无人车车牌号';
COMMENT ON COLUMN unmanned_vehicle_violations.rule_name IS '违规的规则名称';
COMMENT ON COLUMN unmanned_vehicle_violations.violation_type IS '违规类型：SPEED超速/ACCESS未授权进入/HEIGHT超高/WEIGHT超重';
COMMENT ON COLUMN unmanned_vehicle_violations.description IS '违规描述，如"超速: 19.8km/h"';
COMMENT ON COLUMN unmanned_vehicle_violations.location IS '违规发生的地理位置（PostGIS Point）';
COMMENT ON COLUMN unmanned_vehicle_violations.actual_value IS '实际测量值，如实际速度19.8';
COMMENT ON COLUMN unmanned_vehicle_violations.limit_value IS '规则限制值，如限速5.0';
COMMENT ON COLUMN unmanned_vehicle_violations.violation_time IS '违规发生的时间';
COMMENT ON COLUMN unmanned_vehicle_violations.created_at IS '记录创建时间';

-- 插入示例数据（可选）
-- INSERT INTO unmanned_vehicle_violations (vehicle_license, rule_name, violation_type, description, actual_value, limit_value) 
-- VALUES 
--     ('无人车001', 'APRON_01区域限速规则', 'SPEED', '超速: 19.8km/h', 19.8, 5.0),
--     ('无人车002', 'RESTRICTED_AREA_01访问控制', 'ACCESS', '未授权进入区域', NULL, NULL),
--     ('无人车001', 'VEHICLE_HEIGHT_LIMIT', 'HEIGHT', '超高: 3.2m', 3.2, 3.0);

-- 显示表结构
\d unmanned_vehicle_violations;

-- 查询表中的数据
-- SELECT * FROM unmanned_vehicle_violations ORDER BY violation_time DESC LIMIT 10; 