-- 工单表：统计字段 + ticket_total 整数化（供列表展示与后续同步接口写入）
SET NAMES utf8mb4;

-- 1. 同步统计字段（若已存在则跳过需手工处理）
ALTER TABLE `fa_split_ticket`
  ADD COLUMN `complete_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '完成数量(同步)' AFTER `status`,
  ADD COLUMN `inbound_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '进线人数(同步)' AFTER `complete_count`,
  ADD COLUMN `speed_per_hour` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '当前速度:每小时进线(同步)' AFTER `inbound_count`,
  ADD COLUMN `number_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '号码数量含离线封号(同步)' AFTER `speed_per_hour`,
  ADD COLUMN `number_offline_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '离线号码数(同步)' AFTER `number_count`,
  ADD COLUMN `number_banned_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '封号号码数(同步)' AFTER `number_offline_count`,
  ADD COLUMN `online_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '在线人数(同步)' AFTER `number_banned_count`,
  ADD COLUMN `sync_status` enum('success','error','pending') NOT NULL DEFAULT 'pending' COMMENT '同步状态' AFTER `online_count`,
  ADD COLUMN `sync_time` bigint(16) DEFAULT NULL COMMENT '最近同步时间' AFTER `sync_status`,
  ADD COLUMN `sync_message` varchar(255) NOT NULL DEFAULT '' COMMENT '同步失败摘要' AFTER `sync_time`;

ALTER TABLE `fa_split_ticket`
  ADD KEY `sync_status` (`sync_status`),
  ADD KEY `sync_time` (`sync_time`);

-- 2. ticket_total：varchar -> int unsigned（非数字旧值变为 0）
UPDATE `fa_split_ticket` SET `ticket_total` = '0' WHERE `ticket_total` REGEXP '[^0-9]' OR `ticket_total` = '';
ALTER TABLE `fa_split_ticket`
  MODIFY COLUMN `ticket_total` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '工单总量';

-- 3. 号码表组合索引：加速按工单汇总 visit_count
ALTER TABLE `fa_split_number`
  ADD KEY `idx_ticket_agg` (`admin_id`, `split_link_id`, `ticket_name`(50));
