-- 集团多俱乐部：业务表 club_id 扩展（默认 xq，现网数据自动归属星阙）
-- 执行前请备份数据库。可重复执行（列已存在则跳过）。

-- User 表俱乐部归属
SET @exist := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'User' AND COLUMN_NAME = 'ClubID'
);
SET @sql := IF(@exist = 0,
  'ALTER TABLE `User` ADD COLUMN `ClubID` varchar(16) NOT NULL DEFAULT ''xq'' COMMENT ''所属俱乐部''',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 订单
SET @exist := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'dingdan' AND COLUMN_NAME = 'club_id'
);
SET @sql := IF(@exist = 0,
  'ALTER TABLE `dingdan` ADD COLUMN `club_id` varchar(16) NOT NULL DEFAULT ''xq'', ADD INDEX `idx_dingdan_club` (`club_id`)',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 充值流水
SET @exist := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'czjilu' AND COLUMN_NAME = 'club_id'
);
SET @sql := IF(@exist = 0,
  'ALTER TABLE `czjilu` ADD COLUMN `club_id` varchar(16) NOT NULL DEFAULT ''xq'', ADD INDEX `idx_czjilu_club` (`club_id`)',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 会员购买记录
SET @exist := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'huiyuangoumai' AND COLUMN_NAME = 'club_id'
);
SET @sql := IF(@exist = 0,
  'ALTER TABLE `huiyuangoumai` ADD COLUMN `club_id` varchar(16) NOT NULL DEFAULT ''xq''',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 提现记录
SET @exist := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'tixianjilu' AND COLUMN_NAME = 'club_id'
);
SET @sql := IF(@exist = 0,
  'ALTER TABLE `tixianjilu` ADD COLUMN `club_id` varchar(16) NOT NULL DEFAULT ''xq''',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 罚单
SET @exist := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'fadan' AND COLUMN_NAME = 'club_id'
);
SET @sql := IF(@exist = 0,
  'ALTER TABLE `fadan` ADD COLUMN `club_id` varchar(16) NOT NULL DEFAULT ''xq''',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 平台收支汇总
SET @exist := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'szjilu' AND COLUMN_NAME = 'club_id'
);
SET @sql := IF(@exist = 0,
  'ALTER TABLE `szjilu` ADD COLUMN `club_id` varchar(16) NOT NULL DEFAULT ''xq''',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 轮播：club_id + page_key
SET @exist := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lunbo' AND COLUMN_NAME = 'club_id'
);
SET @sql := IF(@exist = 0,
  'ALTER TABLE `lunbo` ADD COLUMN `club_id` varchar(16) NOT NULL DEFAULT ''xq''',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @exist := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lunbo' AND COLUMN_NAME = 'page_key'
);
SET @sql := IF(@exist = 0,
  'ALTER TABLE `lunbo` ADD COLUMN `page_key` varchar(32) NOT NULL DEFAULT ''order_pool''',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 弹窗页
SET @exist := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'popup_page' AND COLUMN_NAME = 'club_id'
);
SET @sql := IF(@exist = 0,
  'ALTER TABLE `popup_page` ADD COLUMN `club_id` varchar(16) NOT NULL DEFAULT ''xq''',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 商品 catalog
SET @exist := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'shangpin' AND COLUMN_NAME = 'club_id'
);
SET @sql := IF(@exist = 0,
  'ALTER TABLE `shangpin` ADD COLUMN `club_id` varchar(16) NOT NULL DEFAULT ''xq''',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 每日收入/出款统计（按俱乐部）
SET @exist := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'daily_income_stat' AND COLUMN_NAME = 'club_id'
);
SET @sql := IF(@exist = 0,
  'ALTER TABLE `daily_income_stat` ADD COLUMN `club_id` varchar(16) NOT NULL DEFAULT ''xq'', ADD INDEX `idx_income_club_date` (`club_id`, `date`)',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @exist := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'daily_payout_stat' AND COLUMN_NAME = 'club_id'
);
SET @sql := IF(@exist = 0,
  'ALTER TABLE `daily_payout_stat` ADD COLUMN `club_id` varchar(16) NOT NULL DEFAULT ''xq'', ADD INDEX `idx_payout_club_date` (`club_id`, `date`)',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
