-- WP-WB4: S8 个人工作台查询索引 + 实时通知渠道（幂等）
SET @exist := (
    SELECT COUNT(*) FROM information_schema.STATISTICS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'ado_s8_exception' AND INDEX_NAME = 'idx_s8_exception_assignee_status'
);
SET @sql := IF(@exist = 0,
    'ALTER TABLE ado_s8_exception ADD INDEX idx_s8_exception_assignee_status (tenant_id, factory_id, assignee_id, status, is_deleted)',
    'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @exist := (
    SELECT COUNT(*) FROM information_schema.STATISTICS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'ado_s8_exception' AND INDEX_NAME = 'idx_s8_exception_verifier_status'
);
SET @sql := IF(@exist = 0,
    'ALTER TABLE ado_s8_exception ADD INDEX idx_s8_exception_verifier_status (tenant_id, factory_id, verifier_id, status, is_deleted)',
    'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

UPDATE ado_s8_notification_layer
SET notify_channel = CASE
        WHEN notify_channel IS NULL OR notify_channel = '' THEN 'log,SignalR'
        WHEN notify_channel LIKE '%SignalR%' THEN notify_channel
        ELSE CONCAT(notify_channel, ',SignalR')
    END,
    updated_at = NOW()
WHERE notify_channel IS NULL
   OR notify_channel = ''
   OR notify_channel NOT LIKE '%SignalR%';

-- WP-WB7: 三套 UAT 建议首页（仅这些租户，且仅空首页）
UPDATE SysUser
SET Homepage = '/aidop/smart-ops/grid'
WHERE TenantId IN (838257186181189, 838257212780613, 838257237606469)
  AND Account IN ('UATAdminA', 'UATAdminB', 'UATDemoAdmin')
  AND (Homepage IS NULL OR Homepage = '');

UPDATE SysUser
SET Homepage = '/dashboard/home'
WHERE TenantId IN (838257186181189, 838257212780613, 838257237606469)
  AND Account NOT IN ('UATAdminA', 'UATAdminB', 'UATDemoAdmin')
  AND (Homepage IS NULL OR Homepage = '');
