-- 兼容旧库：确保 biz_shop_manage 存在 group_id 字段与索引

SET @db_name = DATABASE();

-- 1) 新增 group_id 字段（仅当不存在时）
SET @col_exists := (
  SELECT COUNT(*)
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = @db_name
    AND TABLE_NAME = 'biz_shop_manage'
    AND COLUMN_NAME = 'group_id'
);
SET @sql_add_col := IF(@col_exists = 0,
  'ALTER TABLE biz_shop_manage ADD COLUMN group_id BIGINT NULL COMMENT ''分组ID'' AFTER id',
  'SELECT 1'
);
PREPARE stmt FROM @sql_add_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 2) 新增 group_id 索引（仅当不存在时）
SET @idx_exists := (
  SELECT COUNT(*)
  FROM information_schema.STATISTICS
  WHERE TABLE_SCHEMA = @db_name
    AND TABLE_NAME = 'biz_shop_manage'
    AND INDEX_NAME = 'idx_group_id'
);
SET @sql_add_idx := IF(@idx_exists = 0,
  'ALTER TABLE biz_shop_manage ADD KEY idx_group_id (group_id)',
  'SELECT 1'
);
PREPARE stmt2 FROM @sql_add_idx;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
