-- CasualtySightPlus 数据库表结构
-- 数据库: Client
-- 创建日期: 2025-06-18

USE Client;

-- 1. UAV设备表 (无人机)
CREATE TABLE IF NOT EXISTS uav_devices (
    id VARCHAR(50) PRIMARY KEY,
    name VARCHAR(100) NOT NULL DEFAULT 'UAV设备',
    state INT DEFAULT 0 COMMENT '设备状态: 0=离线, 1=在线, 2=工作中, 3=错误',
    ip VARCHAR(15),
    port INT,
    longitude DOUBLE,
    latitude DOUBLE,
    signal_strength INT DEFAULT 0 COMMENT '信号强度 0-100',
    last_heartbeat TIMESTAMP NULL COMMENT '最后心跳时间',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_state (state),
    INDEX idx_location (longitude, latitude)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='无人机设备表';

-- 2. Dog机器人表 (地面机器人)
CREATE TABLE IF NOT EXISTS dog_devices (
    id VARCHAR(50) PRIMARY KEY,
    name VARCHAR(100) NOT NULL DEFAULT '地面机器人',
    state INT DEFAULT 0 COMMENT '设备状态: 0=离线, 1=在线, 2=工作中, 3=错误',
    ip VARCHAR(15),
    port INT,
    longitude DOUBLE,
    latitude DOUBLE,
    signal_strength INT DEFAULT 0 COMMENT '信号强度 0-100',
    last_heartbeat TIMESTAMP NULL COMMENT '最后心跳时间',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_state (state),
    INDEX idx_location (longitude, latitude)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='地面机器人设备表';

-- 3. 伤员记录表
CREATE TABLE IF NOT EXISTS injury_records (
    id VARCHAR(50) PRIMARY KEY,
    rank INT COMMENT '伤员等级',
    longitude DOUBLE,
    latitude DOUBLE,
    flag INT DEFAULT 1 COMMENT '有效标志: 0=无效, 1=有效',
    severity INT DEFAULT 0 COMMENT '严重程度: 0=轻微, 1=中等, 2=严重, 3=危重',
    description TEXT COMMENT '伤情描述',
    discovered_by VARCHAR(50) COMMENT '发现设备ID',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_flag (flag),
    INDEX idx_severity (severity),
    INDEX idx_location (longitude, latitude)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='伤员记录表';

-- 4. 系统配置表
CREATE TABLE IF NOT EXISTS system_config (
    config_key VARCHAR(100) PRIMARY KEY,
    config_value TEXT,
    config_type VARCHAR(20) DEFAULT 'string' COMMENT '配置类型: string, int, float, boolean, json',
    description VARCHAR(500),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='系统配置表';

-- 5. 用户会话表
CREATE TABLE IF NOT EXISTS user_sessions (
    session_id VARCHAR(100) PRIMARY KEY,
    user_name VARCHAR(50),
    login_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_activity TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    ip_address VARCHAR(45),
    user_agent TEXT,
    status INT DEFAULT 1 COMMENT '会话状态: 0=已注销, 1=活跃',
    INDEX idx_user (user_name),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户会话表';

-- 6. 设备操作日志表
CREATE TABLE IF NOT EXISTS device_operation_logs (
    log_id BIGINT AUTO_INCREMENT PRIMARY KEY,
    device_id VARCHAR(50),
    device_type VARCHAR(20) COMMENT '设备类型: uav, dog',
    operation VARCHAR(50) COMMENT '操作类型: connect, disconnect, control, move',
    operation_result VARCHAR(20) DEFAULT 'success' COMMENT '操作结果: success, failed, timeout',
    operator VARCHAR(50) COMMENT '操作员',
    operation_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    details JSON COMMENT '操作详细信息',
    INDEX idx_device (device_id),
    INDEX idx_operation_time (operation_time),
    INDEX idx_device_type (device_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='设备操作日志表';

-- 插入默认系统配置
INSERT INTO system_config (config_key, config_value, config_type, description) VALUES
('app.version', '2.0.0', 'string', '应用程序版本'),
('app.theme', 'military', 'string', '默认主题'),
('database.connection_timeout', '30000', 'int', '数据库连接超时时间(毫秒)'),
('ui.auto_refresh_interval', '5000', 'int', '界面自动刷新间隔(毫秒)'),
('ui.device_card_style', 'modern', 'string', '设备卡片样式'),
('map.default_center_lat', '39.9', 'float', '地图默认中心纬度'),
('map.default_center_lon', '116.4', 'float', '地图默认中心经度'),
('map.default_zoom', '12', 'int', '地图默认缩放级别'),
('device.heartbeat_interval', '10000', 'int', '设备心跳间隔(毫秒)'),
('device.connection_timeout', '30000', 'int', '设备连接超时(毫秒)')
ON DUPLICATE KEY UPDATE 
    config_value = VALUES(config_value),
    updated_at = CURRENT_TIMESTAMP;

-- 为兼容现有代码，创建旧表名的视图
CREATE OR REPLACE VIEW uavdatabase AS
SELECT 
    id,
    state,
    ip,
    port,
    longitude as lon,
    latitude as lat
FROM uav_devices;

CREATE OR REPLACE VIEW dogdatabase AS
SELECT 
    id,
    state,
    ip,
    port,
    longitude as lon,
    latitude as lat
FROM dog_devices;

CREATE OR REPLACE VIEW injurydatabase AS
SELECT 
    id,
    rank as injuryrank,
    longitude as lon,
    latitude as lat,
    flag
FROM injury_records;

-- 显示创建的表
SHOW TABLES;

-- 显示表结构概要
SELECT 
    TABLE_NAME as '表名',
    TABLE_COMMENT as '说明',
    TABLE_ROWS as '记录数'
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA = 'Client'
ORDER BY TABLE_NAME;