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 } // Check authorization for the distinct games present in an aggregated report. func approvedGameKeys(db *gorm.DB, viewer business.UserScope, sources []string, keys []business.AnalyticsKey) (map[business.AnalyticsKey]bool, error) { allowed := map[business.AnalyticsKey]bool{} for start := 0; start < len(keys); start += 300 { var games []struct { SourceDB string `gorm:"column:source_db"` ID int64 } q := approvedGamesQuery(db, viewer, sources).Where("(g.source_db,g.id) IN ?", analyticsPairs(keys[start:min(start+300, len(keys))])) if err := q.Select("g.source_db,g.id").Scan(&games).Error; err != nil { return nil, business.ErrStorage } for _, game := range games { allowed[business.AnalyticsKey{SourceDB: game.SourceDB, ID: game.ID}] = true } } return allowed, nil } 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"}}, }) }