-- 巡检任务表
CREATE TABLE `v_inspection_task` (
  `task_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '巡检任务ID',
  `task_name` varchar(100) NOT NULL COMMENT '任务名称',
  `device_id` bigint(20) NOT NULL COMMENT '设备ID',
  `device_name` varchar(100) DEFAULT NULL COMMENT '设备名称',
  `cron_expression` varchar(100) NOT NULL COMMENT 'Cron表达式',
  `duration` int(11) NOT NULL DEFAULT '300' COMMENT '巡检时长(秒)',
  `status` char(1) NOT NULL DEFAULT '0' COMMENT '任务状态(0=启用,1=停用)',
  `enable_detection` char(1) NOT NULL DEFAULT '0' COMMENT '是否启用检测(0=启用,1=停用)',
  `threshold` decimal(3,2) NOT NULL DEFAULT '0.70' COMMENT '检测阈值',
  `last_execute_time` datetime DEFAULT NULL COMMENT '最后执行时间',
  `next_execute_time` datetime DEFAULT NULL COMMENT '下次执行时间',
  `execute_count` bigint(20) DEFAULT '0' COMMENT '执行次数',
  `alarm_count` bigint(20) DEFAULT '0' COMMENT '报警次数',
  `create_by` varchar(64) DEFAULT '' COMMENT '创建者',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_by` varchar(64) DEFAULT '' COMMENT '更新者',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`task_id`),
  KEY `idx_device_id` (`device_id`),
  KEY `idx_status` (`status`),
  KEY `idx_next_execute_time` (`next_execute_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='巡检任务表';

-- 报警记录表
CREATE TABLE `v_alarm_record` (
  `alarm_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '报警记录ID',
  `task_id` bigint(20) NOT NULL COMMENT '巡检任务ID',
  `task_name` varchar(100) DEFAULT NULL COMMENT '任务名称',
  `device_id` bigint(20) NOT NULL COMMENT '设备ID',
  `device_name` varchar(100) DEFAULT NULL COMMENT '设备名称',
  `alarm_type` varchar(50) NOT NULL COMMENT '报警类型',
  `alarm_level` char(1) NOT NULL DEFAULT '1' COMMENT '报警级别(1=低,2=中,3=高)',
  `alarm_desc` varchar(500) DEFAULT NULL COMMENT '报警描述',
  `confidence` decimal(5,4) DEFAULT NULL COMMENT '检测置信度',
  `image_path` varchar(500) DEFAULT NULL COMMENT '报警图片路径',
  `video_path` varchar(500) DEFAULT NULL COMMENT '报警视频路径',
  `alarm_time` datetime NOT NULL COMMENT '报警时间',
  `handle_status` char(1) NOT NULL DEFAULT '0' COMMENT '处理状态(0=未处理,1=已处理,2=已忽略)',
  `handle_by` varchar(64) DEFAULT NULL COMMENT '处理人',
  `handle_time` datetime DEFAULT NULL COMMENT '处理时间',
  `handle_remark` varchar(500) DEFAULT NULL COMMENT '处理备注',
  `create_by` varchar(64) DEFAULT '' COMMENT '创建者',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_by` varchar(64) DEFAULT '' COMMENT '更新者',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`alarm_id`),
  KEY `idx_task_id` (`task_id`),
  KEY `idx_device_id` (`device_id`),
  KEY `idx_alarm_time` (`alarm_time`),
  KEY `idx_handle_status` (`handle_status`),
  KEY `idx_alarm_level` (`alarm_level`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='报警记录表';

-- 插入测试数据
INSERT INTO `v_inspection_task` (`task_name`, `device_id`, `device_name`, `cron_expression`, `duration`, `status`, `enable_detection`, `threshold`, `create_time`) VALUES
('办公区域安全巡检', 1, '办公区摄像头', '0 0 8,12,18 * * ?', 600, '0', '0', 0.75, NOW()),
('停车场巡检任务', 2, '停车场摄像头', '0 0/30 * * * ?', 300, '0', '0', 0.70, NOW()),
('仓库区域夜间巡检', 3, '仓库摄像头', '0 0 22 * * ?', 1800, '1', '0', 0.80, NOW());

-- 菜单权限数据
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) VALUES
('视频巡检', 2000, 3, 'inspection', 'video/inspection/index', 1, 0, 'C', '0', '0', 'video:inspection:list', 'monitor', 'admin', NOW(), '', NULL, '视频巡检菜单'),
('报警记录', 2000, 4, 'alarm', 'video/alarm/index', 1, 0, 'C', '0', '0', 'video:alarm:list', 'bug', 'admin', NOW(), '', NULL, '报警记录菜单');

-- 巡检任务权限
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) VALUES
('巡检任务查询', (SELECT menu_id FROM sys_menu WHERE perms = 'video:inspection:list'), 1, '', '', 1, 0, 'F', '0', '0', 'video:inspection:query', '#', 'admin', NOW(), '', NULL, ''),
('巡检任务新增', (SELECT menu_id FROM sys_menu WHERE perms = 'video:inspection:list'), 2, '', '', 1, 0, 'F', '0', '0', 'video:inspection:add', '#', 'admin', NOW(), '', NULL, ''),
('巡检任务修改', (SELECT menu_id FROM sys_menu WHERE perms = 'video:inspection:list'), 3, '', '', 1, 0, 'F', '0', '0', 'video:inspection:edit', '#', 'admin', NOW(), '', NULL, ''),
('巡检任务删除', (SELECT menu_id FROM sys_menu WHERE perms = 'video:inspection:list'), 4, '', '', 1, 0, 'F', '0', '0', 'video:inspection:remove', '#', 'admin', NOW(), '', NULL, ''),
('巡检任务导出', (SELECT menu_id FROM sys_menu WHERE perms = 'video:inspection:list'), 5, '', '', 1, 0, 'F', '0', '0', 'video:inspection:export', '#', 'admin', NOW(), '', NULL, ''),
('巡检任务启动', (SELECT menu_id FROM sys_menu WHERE perms = 'video:inspection:list'), 6, '', '', 1, 0, 'F', '0', '0', 'video:inspection:start', '#', 'admin', NOW(), '', NULL, ''),
('巡检任务停止', (SELECT menu_id FROM sys_menu WHERE perms = 'video:inspection:list'), 7, '', '', 1, 0, 'F', '0', '0', 'video:inspection:stop', '#', 'admin', NOW(), '', NULL, ''),
('巡检任务执行', (SELECT menu_id FROM sys_menu WHERE perms = 'video:inspection:list'), 8, '', '', 1, 0, 'F', '0', '0', 'video:inspection:execute', '#', 'admin', NOW(), '', NULL, '');

-- 报警记录权限
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) VALUES
('报警记录查询', (SELECT menu_id FROM sys_menu WHERE perms = 'video:alarm:list'), 1, '', '', 1, 0, 'F', '0', '0', 'video:alarm:query', '#', 'admin', NOW(), '', NULL, ''),
('报警记录导出', (SELECT menu_id FROM sys_menu WHERE perms = 'video:alarm:list'), 2, '', '', 1, 0, 'F', '0', '0', 'video:alarm:export', '#', 'admin', NOW(), '', NULL, ''),
('报警记录处理', (SELECT menu_id FROM sys_menu WHERE perms = 'video:alarm:list'), 3, '', '', 1, 0, 'F', '0', '0', 'video:alarm:handle', '#', 'admin', NOW(), '', NULL, '');