-- MySQL数据库初始化脚本
-- 创建数据库
CREATE DATABASE IF NOT EXISTS rtsp_cameras CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 使用数据库
USE rtsp_cameras;

-- 创建摄像头表
CREATE TABLE IF NOT EXISTS cameras (
    id VARCHAR(36) PRIMARY KEY COMMENT '摄像头唯一标识符',
    name VARCHAR(255) NOT NULL COMMENT '摄像头名称',
    ip VARCHAR(45) NOT NULL COMMENT 'IP地址',
    username VARCHAR(100) DEFAULT NULL COMMENT '用户名',
    password VARCHAR(255) DEFAULT NULL COMMENT '密码',
    rtsp_url VARCHAR(500) NOT NULL COMMENT 'RTSP URL',
    enabled BOOLEAN DEFAULT TRUE COMMENT '是否启用',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_name (name),
    INDEX idx_ip (ip),
    INDEX idx_enabled (enabled),
    INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='摄像头信息表';

-- 插入示例数据
INSERT INTO cameras (id, name, ip, username, password, rtsp_url, enabled) VALUES 
('27aec28e-6181-4753-9acd-0456a75f0289', '测试摄像头1', '192.168.1.100', 'admin', 'admin123', 'rtsp://192.168.1.100:554/stream1', TRUE),
('38bfd39f-7292-5864-0bde-1567b86f1390', '测试摄像头2', '192.168.1.101', 'admin', 'admin123', 'rtsp://192.168.1.101:554/stream1', TRUE)
ON DUPLICATE KEY UPDATE 
    name = VALUES(name),
    ip = VALUES(ip),
    username = VALUES(username),
    password = VALUES(password),
    rtsp_url = VALUES(rtsp_url),
    enabled = VALUES(enabled),
    updated_at = CURRENT_TIMESTAMP;

-- SQL Server数据库初始化脚本（注释版本）
/*
-- 创建数据库
IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'rtsp_cameras')
BEGIN
    CREATE DATABASE rtsp_cameras;
END
GO

-- 使用数据库
USE rtsp_cameras;
GO

-- 创建摄像头表
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='cameras' AND xtype='U')
BEGIN
    CREATE TABLE cameras (
        id NVARCHAR(36) PRIMARY KEY,
        name NVARCHAR(255) NOT NULL,
        ip NVARCHAR(45) NOT NULL,
        username NVARCHAR(100) NULL,
        password NVARCHAR(255) NULL,
        rtsp_url NVARCHAR(500) NOT NULL,
        enabled BIT DEFAULT 1,
        created_at DATETIME2 DEFAULT GETDATE(),
        updated_at DATETIME2 DEFAULT GETDATE()
    );
    
    -- 创建索引
    CREATE INDEX idx_cameras_name ON cameras(name);
    CREATE INDEX idx_cameras_ip ON cameras(ip);
    CREATE INDEX idx_cameras_enabled ON cameras(enabled);
    CREATE INDEX idx_cameras_created_at ON cameras(created_at);
END
GO

-- 创建更新触发器
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_cameras_update')
BEGIN
    EXEC('CREATE TRIGGER tr_cameras_update ON cameras
    AFTER UPDATE
    AS
    BEGIN
        UPDATE cameras 
        SET updated_at = GETDATE() 
        WHERE id IN (SELECT id FROM inserted);
    END');
END
GO

-- 插入示例数据
IF NOT EXISTS (SELECT * FROM cameras WHERE id = '27aec28e-6181-4753-9acd-0456a75f0289')
BEGIN
    INSERT INTO cameras (id, name, ip, username, password, rtsp_url, enabled) VALUES 
    ('27aec28e-6181-4753-9acd-0456a75f0289', N'测试摄像头1', N'192.168.1.100', N'admin', N'admin123', N'rtsp://192.168.1.100:554/stream1', 1);
END

IF NOT EXISTS (SELECT * FROM cameras WHERE id = '38bfd39f-7292-5864-0bde-1567b86f1390')
BEGIN
    INSERT INTO cameras (id, name, ip, username, password, rtsp_url, enabled) VALUES 
    ('38bfd39f-7292-5864-0bde-1567b86f1390', N'测试摄像头2', N'192.168.1.101', N'admin', N'admin123', N'rtsp://192.168.1.101:554/stream1', 1);
END
GO
*/