-- 车辆异常运行告警记录表
CREATE TABLE `tb_vehicle_abnormal_alert` (
  `alert_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '告警ID',
  `vehicle_id` bigint(20) NOT NULL COMMENT '车辆ID',
  `vehicle_no` varchar(20) DEFAULT NULL COMMENT '车牌号',
  `alert_date` date NOT NULL COMMENT '告警日期',
  `alert_time` datetime NOT NULL COMMENT '告警时间',
  `mileage` decimal(10,3) DEFAULT 0.000 COMMENT '累计运行公里数(公里)',
  `alert_type` varchar(20) DEFAULT 'NO_TASK_MILEAGE' COMMENT '告警类型(NO_TASK_MILEAGE-无任务超公里)',
  `alert_reason` varchar(500) DEFAULT NULL COMMENT '告警原因描述',
  `start_time` datetime DEFAULT NULL COMMENT '开始运行时间',
  `end_time` datetime DEFAULT NULL COMMENT '结束运行时间',
  `alert_count` int(11) DEFAULT 1 COMMENT '当日告警次数',
  `status` char(1) DEFAULT '0' COMMENT '状态（0-未处理 1-已处理 2-已忽略）',
  `handler_id` bigint(20) DEFAULT NULL COMMENT '处理人ID',
  `handler_name` varchar(64) DEFAULT NULL COMMENT '处理人姓名',
  `handle_time` datetime DEFAULT NULL COMMENT '处理时间',
  `handle_remark` varchar(500) DEFAULT NULL COMMENT '处理备注',
  `notify_status` char(1) DEFAULT '0' COMMENT '通知状态（0-未发送 1-已发送 2-发送失败）',
  `notify_time` datetime DEFAULT NULL COMMENT '通知时间',
  `notify_users` varchar(500) DEFAULT NULL COMMENT '通知用户ID列表（逗号分隔）',
  `dept_id` bigint(20) DEFAULT NULL COMMENT '归属部门ID',
  `dept_name` varchar(100) DEFAULT NULL COMMENT '归属部门名称',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`alert_id`),
  KEY `idx_vehicle_id` (`vehicle_id`),
  KEY `idx_alert_date` (`alert_date`),
  KEY `idx_alert_time` (`alert_time`),
  KEY `idx_vehicle_date` (`vehicle_id`, `alert_date`),
  KEY `idx_dept_id` (`dept_id`),
  KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='车辆异常运行告警记录表';

-- 车辆异常告警配置表
DROP TABLE IF EXISTS `tb_vehicle_alert_config`;
CREATE TABLE `tb_vehicle_alert_config` (
  `config_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '配置ID',
  `config_type` varchar(50) NOT NULL COMMENT '配置类型(GLOBAL-全局/DEPT-部门/VEHICLE-车辆)',
  `dept_id` bigint(20) DEFAULT NULL COMMENT '部门ID（部门配置时使用）',
  `vehicle_id` bigint(20) DEFAULT NULL COMMENT '车辆ID（车辆配置时使用）',
  `mileage_threshold` decimal(10,3) DEFAULT 10.000 COMMENT '公里数告警阈值(公里)',
  `daily_alert_limit` int(11) DEFAULT 5 COMMENT '每日最大告警次数',
  `alert_interval` int(11) DEFAULT 5 COMMENT '告警间隔时间(分钟)',
  `notify_user_ids` varchar(1000) DEFAULT NULL COMMENT '通知用户ID列表（逗号分隔）',
  `status` char(1) DEFAULT '0' COMMENT '状态（0-启用 1-停用）',
  `create_by` varchar(64) DEFAULT '' COMMENT '创建者',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_by` varchar(64) DEFAULT '' COMMENT '更新者',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`config_id`),
  UNIQUE KEY `uk_vehicle_config` (`config_type`, `vehicle_id`),
  UNIQUE KEY `uk_dept_config` (`config_type`, `dept_id`),
  KEY `idx_dept_id` (`dept_id`),
  KEY `idx_vehicle_id` (`vehicle_id`),
  KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='车辆异常告警配置表';

-- 插入全局默认配置
INSERT INTO `tb_vehicle_alert_config` (
  `config_type`, `dept_id`, `vehicle_id`, `mileage_threshold`, `daily_alert_limit`, 
  `alert_interval`, `status`, `create_by`, `remark`
) VALUES (
  'GLOBAL', NULL, NULL, 10.000, 5, 5, '0', 'admin', '全局默认配置：车辆无任务超10公里告警，每天最多5次，间隔5分钟'
);

-- 添加系统配置参数
INSERT INTO sys_config (config_name, config_key, config_value, config_type, remark, create_by, create_time) 
VALUES ('车辆异常告警启用开关', 'vehicle.alert.enabled', 'true', 'Y', '控制车辆异常运行告警功能的总开关。true=启用，false=禁用', 'admin', NOW())
ON DUPLICATE KEY UPDATE config_value = config_value;

INSERT INTO sys_config (config_name, config_key, config_value, config_type, remark, create_by, create_time) 
VALUES ('车辆异常告警公里数阈值', 'vehicle.alert.mileage.threshold', '10', 'Y', '车辆无任务运行超过该公里数时触发告警（单位：公里）', 'admin', NOW())
ON DUPLICATE KEY UPDATE config_value = config_value;

INSERT INTO sys_config (config_name, config_key, config_value, config_type, remark, create_by, create_time) 
VALUES ('车辆异常告警每日次数限制', 'vehicle.alert.daily.limit', '5', 'Y', '每台车每天最多告警次数', 'admin', NOW())
ON DUPLICATE KEY UPDATE config_value = config_value;

INSERT INTO sys_config (config_name, config_key, config_value, config_type, remark, create_by, create_time) 
VALUES ('车辆异常告警间隔时间', 'vehicle.alert.interval.minutes', '5', 'Y', '同一车辆两次告警之间的最小间隔时间（单位：分钟）', 'admin', NOW())
ON DUPLICATE KEY UPDATE config_value = config_value;

INSERT INTO sys_config (config_name, config_key, config_value, config_type, remark, create_by, create_time) 
VALUES ('车辆异常告警通知用户', 'vehicle.alert.notify.users', '', 'Y', '接收告警通知的用户ID列表，多个用户用逗号分隔。为空时根据车辆归属部门发送给分公司负责人', 'admin', NOW())
ON DUPLICATE KEY UPDATE config_value = config_value;

INSERT INTO sys_config (config_name, config_key, config_value, config_type, remark, create_by, create_time) 
VALUES ('车辆异常告警监控时间窗口', 'vehicle.alert.time.window', '10', 'Y', '监控时间窗口（单位：分钟），用于计算车辆在该时间窗口内的运行公里数', 'admin', NOW())
ON DUPLICATE KEY UPDATE config_value = config_value;

-- 创建定时任务
INSERT INTO sys_job (job_name, job_group, invoke_target, cron_expression, misfire_policy, concurrent, status, create_by, create_time, update_by, update_time, remark) 
VALUES ('车辆异常运行监控任务', 'DEFAULT', 'vehicleAbnormalAlertTask.monitorVehicleAbnormalRunning()', '0 */5 * * * ?', '3', '0', '1', 'admin', NOW(), 'admin', NOW(), '每5分钟执行一次，监控车辆无任务超公里运行情况')
ON DUPLICATE KEY UPDATE invoke_target = invoke_target;

-- =====================================================
-- 菜单权限配置
-- =====================================================

-- 1. 创建车辆监控父菜单（如果不存在）
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)
SELECT '车辆监控', 0, 5, 'vehicle-monitor', NULL, 1, 0, 'M', '0', '0', '', 'monitor', 'admin', NOW(), 'admin', NOW(), '车辆监控管理目录'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_name = '车辆监控' AND menu_type = 'M');

-- 获取车辆监控父菜单ID
SET @vehicleMonitorMenuId = (SELECT menu_id FROM sys_menu WHERE menu_name = '车辆监控' AND menu_type = 'M' LIMIT 1);

-- 2. 创建车辆异常告警菜单
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)
SELECT '车辆异常告警', @vehicleMonitorMenuId, 1, 'vehicleAlert', 'system/vehicleAlert/index', 1, 0, 'C', '0', '0', 'system:vehicleAlert:list', 'warning', 'admin', NOW(), 'admin', NOW(), '车辆异常运行告警管理'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_name = '车辆异常告警' AND perms = 'system:vehicleAlert:list');

-- 获取车辆异常告警菜单ID
SET @vehicleAlertMenuId = (SELECT menu_id FROM sys_menu WHERE menu_name = '车辆异常告警' AND perms = 'system:vehicleAlert:list' LIMIT 1);

-- 3. 创建车辆异常告警功能按钮
-- 3.1 查询按钮
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)
SELECT '告警查询', @vehicleAlertMenuId, 1, '#', '', 1, 0, 'F', '0', '0', 'system:vehicleAlert:query', '#', 'admin', NOW(), 'admin', NOW(), ''
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'system:vehicleAlert:query');

-- 3.2 处理告警按钮
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)
SELECT '处理告警', @vehicleAlertMenuId, 2, '#', '', 1, 0, 'F', '0', '0', 'system:vehicleAlert:handle', '#', 'admin', NOW(), 'admin', NOW(), ''
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'system:vehicleAlert:handle');

-- 3.3 删除告警按钮
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)
SELECT '删除告警', @vehicleAlertMenuId, 3, '#', '', 1, 0, 'F', '0', '0', 'system:vehicleAlert:remove', '#', 'admin', NOW(), 'admin', NOW(), ''
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'system:vehicleAlert:remove');

-- 3.4 导出告警按钮
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)
SELECT '导出告警', @vehicleAlertMenuId, 4, '#', '', 1, 0, 'F', '0', '0', 'system:vehicleAlert:export', '#', 'admin', NOW(), 'admin', NOW(), ''
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'system:vehicleAlert:export');

-- 4. 创建告警配置管理菜单
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)
SELECT '告警配置管理', @vehicleMonitorMenuId, 2, 'vehicleAlertConfig', 'system/vehicleAlertConfig/index', 1, 0, 'C', '0', '0', 'system:vehicleAlertConfig:list', 'edit', 'admin', NOW(), 'admin', NOW(), '车辆告警配置管理'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_name = '告警配置管理' AND perms = 'system:vehicleAlertConfig:list');

-- 获取告警配置管理菜单ID
SET @vehicleAlertConfigMenuId = (SELECT menu_id FROM sys_menu WHERE menu_name = '告警配置管理' AND perms = 'system:vehicleAlertConfig:list' LIMIT 1);

-- 5. 创建告警配置管理功能按钮
-- 5.1 查询按钮
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)
SELECT '配置查询', @vehicleAlertConfigMenuId, 1, '#', '', 1, 0, 'F', '0', '0', 'system:vehicleAlertConfig:query', '#', 'admin', NOW(), 'admin', NOW(), ''
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'system:vehicleAlertConfig:query');

-- 5.2 新增按钮
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)
SELECT '新增配置', @vehicleAlertConfigMenuId, 2, '#', '', 1, 0, 'F', '0', '0', 'system:vehicleAlertConfig:add', '#', 'admin', NOW(), 'admin', NOW(), ''
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'system:vehicleAlertConfig:add');

-- 5.3 修改按钮
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)
SELECT '修改配置', @vehicleAlertConfigMenuId, 3, '#', '', 1, 0, 'F', '0', '0', 'system:vehicleAlertConfig:edit', '#', 'admin', NOW(), 'admin', NOW(), ''
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'system:vehicleAlertConfig:edit');

-- 5.4 删除按钮
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)
SELECT '删除配置', @vehicleAlertConfigMenuId, 4, '#', '', 1, 0, 'F', '0', '0', 'system:vehicleAlertConfig:remove', '#', 'admin', NOW(), 'admin', NOW(), ''
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'system:vehicleAlertConfig:remove');

-- 5.5 导出按钮
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)
SELECT '导出配置', @vehicleAlertConfigMenuId, 5, '#', '', 1, 0, 'F', '0', '0', 'system:vehicleAlertConfig:export', '#', 'admin', NOW(), 'admin', NOW(), ''
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'system:vehicleAlertConfig:export');
