-- 升级已有的 yz_system_reminderlist 表，添加 batch_id 以及发送目标相关字段
ALTER TABLE `yz_system_reminderlist`
  ADD COLUMN `batch_id` varchar(64) NOT NULL DEFAULT '' COMMENT '批次号/分组ID' AFTER `delete_time`,
  ADD COLUMN `target_type` varchar(32) NOT NULL DEFAULT '' COMMENT '发送目标类型: platform, tenant_all, role, tenant' AFTER `batch_id`,
  ADD COLUMN `target_role_id` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '目标角色ID' AFTER `target_type`,
  ADD COLUMN `target_tenant_id` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '目标租户ID' AFTER `target_role_id`,
  ADD INDEX `idx_batch` (`batch_id`);

-- 初始化已有记录的 batch_id (如果存在空值，使用 create_time 和 sender_id 进行分组初始化)
UPDATE `yz_system_reminderlist`
SET `batch_id` = CONCAT(UNIX_TIMESTAMP(COALESCE(`create_time`, NOW())), '_', `sender_id`)
WHERE `batch_id` = '' OR `batch_id` IS NULL;
