package business import ( "gorm.io/gorm" "kra/internal/biz/business" ) func approvedGamesAvailable(db *gorm.DB) (bool, error) { for _, table := range []string{"tab_game", "tab_promote", "tab_promote_apply"} { available, err := businessTableAvailable(db, table) if err != nil || !available { return available, err } } return true, nil } // 游戏可选范围来自可见渠道的有效申请,不受统计时是否包含下级的开关影响。 // 用平台和 ID 配对,避免同号游戏或其他商务的申请扩大可选范围。 func approvedGamesQuery(db *gorm.DB, viewer business.UserScope, sources []string) *gorm.DB { promotes := ledgerBasePromotes(db, business.LedgerScope{Viewer: viewer, IncludeSub: true, Unrestricted: viewer.All}, business.LedgerFilter{Sources: sources}, "approved_p").Select("approved_p.source_db,approved_p.id") // 逐个游戏检查有效申请,命中即停止;非相关 IN 在 MySQL 5.7 会先 // 物化整张百万级申请表,即使下拉只需要前 50 项也要扫描全部申请。 applications := db.Table("tab_promote_apply a").Select("1").Where("a.source_db=g.source_db AND a.game_id=g.id AND a.status=1").Where("(a.source_db,a.promote_id) IN (?)", promotes) q := db.Table("tab_game g").Where("EXISTS (?)", applications) if len(sources) > 0 { q = q.Where("g.source_db IN ?", sources) } return q } func migrateApprovedGameIndexes(db *gorm.DB) error { return migrateQueryIndexes(db, []businessQueryIndex{ {"tab_promote_apply", "idx_sw_apply_game_scope", []string{"game_id", "source_db", "status", "promote_id"}}, }) }