-- 工单表：本地开启号码数（status=normal），列表直接读字段，避免子查询
SET NAMES utf8mb4;

ALTER TABLE `fa_split_ticket`
  ADD COLUMN `active_number_count` int(10) unsigned NOT NULL DEFAULT 0
  COMMENT '本地开启号码数(status=normal)' AFTER `number_banned_count`;

-- 历史数据回填
UPDATE `fa_split_ticket` t
SET t.`active_number_count` = (
  SELECT COUNT(*)
  FROM `fa_split_number` n
  WHERE n.`admin_id` = t.`admin_id`
    AND n.`split_link_id` = t.`split_link_id`
    AND n.`ticket_name` = t.`ticket_name`
    AND n.`status` = 'normal'
);

-- 加速按工单统计 status=normal 数量
ALTER TABLE `fa_split_number`
  ADD KEY `idx_ticket_active` (`admin_id`, `split_link_id`, `ticket_name`(50), `status`);
