-- +goose Up
-- 财务仪表盘会按完成/取消日期读取提号，并按调整创建日汇总利润。
-- 补齐复合索引，避免数据增长后统计退化为全表扫描。
-- 该版本曾在部分开发库中部分执行，以下写法兼容已有同名索引后再次运行。

-- +goose StatementBegin
SET @pickup_completed_index_exists = (
  SELECT COUNT(*)
  FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'admin_pickups'
    AND index_name = 'idx_admin_pickups_status_completed_at'
);
SET @pickup_completed_index_sql = IF(
  @pickup_completed_index_exists = 0,
  'ALTER TABLE admin_pickups ADD KEY idx_admin_pickups_status_completed_at (status, completed_at)',
  'SELECT 1'
);
PREPARE pickup_completed_index_statement FROM @pickup_completed_index_sql;
EXECUTE pickup_completed_index_statement;
DEALLOCATE PREPARE pickup_completed_index_statement;

SET @pickup_cancelled_index_exists = (
  SELECT COUNT(*)
  FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'admin_pickups'
    AND index_name = 'idx_admin_pickups_status_cancelled_at'
);
SET @pickup_cancelled_index_sql = IF(
  @pickup_cancelled_index_exists = 0,
  'ALTER TABLE admin_pickups ADD KEY idx_admin_pickups_status_cancelled_at (status, cancelled_at)',
  'SELECT 1'
);
PREPARE pickup_cancelled_index_statement FROM @pickup_cancelled_index_sql;
EXECUTE pickup_cancelled_index_statement;
DEALLOCATE PREPARE pickup_cancelled_index_statement;

SET @pickup_adjustment_index_exists = (
  SELECT COUNT(*)
  FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'admin_pickup_financial_adjustments'
    AND index_name = 'idx_admin_pickup_financial_adjustments_created_at'
);
SET @pickup_adjustment_index_sql = IF(
  @pickup_adjustment_index_exists = 0,
  'ALTER TABLE admin_pickup_financial_adjustments ADD KEY idx_admin_pickup_financial_adjustments_created_at (created_at)',
  'SELECT 1'
);
PREPARE pickup_adjustment_index_statement FROM @pickup_adjustment_index_sql;
EXECUTE pickup_adjustment_index_statement;
DEALLOCATE PREPARE pickup_adjustment_index_statement;
-- +goose StatementEnd

-- +goose Down

-- 补偿迁移不删除索引，避免回滚时误删此前已存在的同名索引。
