CREATE DATABASE IF NOT EXISTS monitor CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE monitor;

-- 用户告警订阅配置表
CREATE TABLE `alert_subscribe` (
  `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
  `uid` varchar(64) NOT NULL COMMENT '登录用户唯一标识',
  `severity` varchar(255) DEFAULT '' COMMENT '订阅级别，逗号分隔 critical,warning,info',
  `instance` varchar(500) DEFAULT '' COMMENT '订阅实例模糊匹配，多值逗号分隔',
  `alertname` varchar(500) DEFAULT '' COMMENT '订阅告警名称模糊匹配，多值逗号分隔',
  `dingtalk_webhook` varchar(600) DEFAULT '' COMMENT '钉钉机器人地址',
  `wecom_webhook` varchar(600) DEFAULT '' COMMENT '企业微信机器人地址',
  `mail_receiver` varchar(600) DEFAULT '' COMMENT '接收邮箱，多邮箱逗号分隔',
  `enable` tinyint unsigned DEFAULT 1 COMMENT '1启用订阅 0关闭',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY idx_uid (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户告警订阅配置';

-- 推送记录日志表（带分页查询）
CREATE TABLE `alert_push_log` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `uid` varchar(64) NOT NULL,
  `alert_info` text NOT NULL COMMENT '告警JSON信息',
  `channel` varchar(20) NOT NULL COMMENT 'dingtalk / wecom / mail',
  `status` tinyint unsigned NOT NULL COMMENT '1成功 0失败',
  `remark` text DEFAULT '' COMMENT '返回日志',
  `push_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '推送时间',
  PRIMARY KEY (`id`),
  KEY idx_uid_time (`uid`,`push_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='告警推送日志';