feat: 按原型更新商务明细与游戏分析并清理临时资料

This commit is contained in:
Yvan
2026-11-04 18:13:03 +08:00
parent 19926d275c
commit f866cac768
41 changed files with 392 additions and 2955 deletions
+9
View File
@@ -234,6 +234,15 @@ func (uc *AnalyticsUsecase) List(ctx context.Context, kind AnalyticsKind, filter
return out, nil
}
func sortAnalytics(rows []*AnalyticsRow, kind AnalyticsKind, field string, ascending bool) error {
if kind == GameAnalytics && field == "" {
sort.SliceStable(rows, func(i, j int) bool {
if rows[i].SourceDB != rows[j].SourceDB {
return rows[i].SourceDB < rows[j].SourceDB
}
return rows[i].ID < rows[j].ID
})
return nil
}
allowed := map[string]bool{"newRegisterCount": true, "activeCount": true, "payUserCount": true, "newPayUserCount": true, "totalPay": true, "payRate": true}
if kind == GameAnalytics {
allowed["arpu"] = true
+19
View File
@@ -91,6 +91,25 @@ func TestAnalyticsSortAndMissingData(t *testing.T) {
}
}
func TestGameAnalyticsCancelSortRestoresDefaultOrder(t *testing.T) {
uc, repo := analyticsFixture()
repo.data.Rows = []*AnalyticsRow{
{AnalyticsKey: AnalyticsKey{SourceDB: "lx", ID: 2}, Pay: "5"},
{AnalyticsKey: AnalyticsKey{SourceDB: "lx", ID: 1}, Pay: "10"},
{AnalyticsKey: AnalyticsKey{SourceDB: "lx", ID: 3}, Pay: "20"},
}
for _, sample := range []struct {
field string
ascending bool
first int64
}{{"totalPay", true, 2}, {"totalPay", false, 3}, {"", false, 1}} {
page, err := uc.List(accountContext(7, 901), GameAnalytics, AnalyticsFilter{SortField: sample.field, Ascending: sample.ascending}, 1, 1)
if err != nil || page.Rows[0].ID != sample.first || page.Summary.Pay != "35.00" {
t.Fatalf("切换排序不应改变汇总,取消时恢复默认顺序: %+v %v", page, err)
}
}
}
func TestAnalyticsLoadsMetadataOnlyForSelectedPage(t *testing.T) {
for _, kind := range []AnalyticsKind{ChannelAnalytics, GameAnalytics} {
t.Run(string(kind), func(t *testing.T) {
+10 -3
View File
@@ -37,6 +37,7 @@ type LedgerListOptions struct {
type LedgerNames struct {
PromoteID *int64
PromoteAccount, BindingAccount, BindingUserName string
BusinessPath []string
}
func (n *LedgerNames) DisplayPromote() string {
@@ -52,6 +53,9 @@ func (n *LedgerNames) DisplayPromote() string {
return "未知渠道(ID:" + strconv.FormatInt(*n.PromoteID, 10) + ")"
}
func (n *LedgerNames) DisplayBusiness() string {
if len(n.BusinessPath) > 0 {
return strings.Join(n.BusinessPath, "→")
}
return (&Channel{BindingAccount: n.BindingAccount, BindingUserName: n.BindingUserName}).BusinessDisplayName()
}
@@ -91,7 +95,7 @@ type GameOption struct {
type LedgerRepo interface {
Registrations(context.Context, LedgerScope, LedgerFilter, LedgerListOptions) (*RegistrationPage, error)
Spends(context.Context, LedgerScope, LedgerFilter, LedgerListOptions) (*SpendPage, error)
Games(context.Context, []string, string, int) ([]*GameOption, error)
Games(context.Context, UserScope, []string, string, int) ([]*GameOption, error)
}
type LedgerUsecase struct {
repo LedgerRepo
@@ -226,14 +230,17 @@ func (uc *LedgerUsecase) ExportSpends(ctx context.Context, filter LedgerFilter)
return page.Rows, nil
}
func (uc *LedgerUsecase) Games(ctx context.Context, sources []string, keyword string, limit int) ([]*GameOption, error) {
if _, _, err := uc.accounts.actor(ctx); err != nil {
actor, account, err := uc.accounts.actor(ctx)
if err != nil {
return nil, err
}
viewer := portraitUserScope(account)
viewer.All = actor.AuthorityID == 888
if limit <= 0 {
limit = 20
}
if limit > 50 {
limit = 10000
}
return uc.repo.Games(ctx, sources, strings.TrimSpace(keyword), limit)
return uc.repo.Games(ctx, viewer, sources, strings.TrimSpace(keyword), limit)
}
+8 -1
View File
@@ -28,7 +28,8 @@ func (r *ledgerRepoFake) Spends(_ context.Context, s LedgerScope, f LedgerFilter
r.options = o
return &SpendPage{Rows: []*Spend{}, Total: r.total, Available: r.available, TotalPayAmount: stringPointer("0")}, nil
}
func (r *ledgerRepoFake) Games(_ context.Context, _ []string, _ string, limit int) ([]*GameOption, error) {
func (r *ledgerRepoFake) Games(_ context.Context, viewer UserScope, _ []string, _ string, limit int) ([]*GameOption, error) {
r.scope.Viewer = viewer
r.gameLimit = limit
return []*GameOption{}, nil
}
@@ -89,4 +90,10 @@ func TestLedgerExportLimitAndDisplayFallback(t *testing.T) {
if r.gameLimit != 10000 {
t.Fatal("原游戏选项的特殊批量上限丢失")
}
if !r.scope.Viewer.All {
t.Fatal("超管游戏选项范围未传递")
}
if _, err := uc.Games(accountContext(9, 903), nil, "", 20); err != nil || !r.scope.Viewer.OnlySelf || r.scope.Viewer.All {
t.Fatal("普通商务游戏选项必须限定本人范围")
}
}
+19 -3
View File
@@ -47,7 +47,7 @@ type ServerOption struct {
}
type ServerAnalysisRepo interface {
Aggregate(context.Context, AnalyticsScope, ServerAnalysisFilter) (*ServerAnalysisData, error)
Servers(context.Context, []string, []string, string, int) ([]*ServerOption, error)
Servers(context.Context, UserScope, []string, []string, string, int) ([]*ServerOption, error)
}
type ServerAnalysisUsecase struct {
repo ServerAnalysisRepo
@@ -60,9 +60,12 @@ func NewServerAnalysisUsecase(repo ServerAnalysisRepo, accounts *AccountUsecase,
return &ServerAnalysisUsecase{repo: repo, accounts: accounts, preferences: preferences, now: time.Now}
}
func (uc *ServerAnalysisUsecase) Servers(ctx context.Context, sources, games []string, keyword string, limit int) ([]*ServerOption, error) {
if _, _, err := uc.accounts.actor(ctx); err != nil {
actor, account, err := uc.accounts.actor(ctx)
if err != nil {
return nil, err
}
viewer := portraitUserScope(account)
viewer.All = actor.AuthorityID == 888
if len(games) == 0 {
return nil, ErrServerAnalysisGame
}
@@ -72,7 +75,7 @@ func (uc *ServerAnalysisUsecase) Servers(ctx context.Context, sources, games []s
if limit > 100 {
limit = 100000
}
return uc.repo.Servers(ctx, sources, games, strings.TrimSpace(keyword), limit)
return uc.repo.Servers(ctx, viewer, sources, games, strings.TrimSpace(keyword), limit)
}
func (uc *ServerAnalysisUsecase) List(ctx context.Context, f ServerAnalysisFilter, page, size int) (*ServerAnalysisPage, error) {
actor, account, err := uc.accounts.actor(ctx)
@@ -151,6 +154,19 @@ func (uc *ServerAnalysisUsecase) List(ctx context.Context, f ServerAnalysisFilte
return out, nil
}
func sortServerAnalysis(rows []*ServerAnalysisRow, field string, ascending bool) error {
if field == "" {
sort.SliceStable(rows, func(i, j int) bool {
a, b := rows[i], rows[j]
if a.SourceDB != b.SourceDB {
return a.SourceDB < b.SourceDB
}
if a.GameID != b.GameID {
return a.GameID < b.GameID
}
return a.ServerNum < b.ServerNum
})
return nil
}
values := map[*ServerAnalysisRow]*big.Rat{}
for _, row := range rows {
var n *big.Rat
+19 -1
View File
@@ -22,7 +22,8 @@ func (r *serverAnalysisFake) Aggregate(_ context.Context, s AnalyticsScope, f Se
}
return out, nil
}
func (r *serverAnalysisFake) Servers(_ context.Context, _, _ []string, _ string, limit int) ([]*ServerOption, error) {
func (r *serverAnalysisFake) Servers(_ context.Context, viewer UserScope, _, _ []string, _ string, limit int) ([]*ServerOption, error) {
r.scope.Viewer = viewer
r.limit = limit
return []*ServerOption{}, nil
}
@@ -67,3 +68,20 @@ func TestServerAnalysisSummaryZerosSortingAndLimits(t *testing.T) {
}
}
}
func TestServerAnalysisCancelSortRestoresIdentityOrder(t *testing.T) {
rows := []*ServerAnalysisRow{
{ServerAnalysisKey: ServerAnalysisKey{SourceDB: "lx", GameID: 1, ServerNum: "002"}, Pay: "5"},
{ServerAnalysisKey: ServerAnalysisKey{SourceDB: "lx", GameID: 1, ServerNum: "001"}, Pay: "10"},
{ServerAnalysisKey: ServerAnalysisKey{SourceDB: "lx", GameID: 1, ServerNum: "003"}, Pay: "20"},
}
if err := sortServerAnalysis(rows, "totalPay", true); err != nil || rows[0].ServerNum != "002" {
t.Fatal("升序错误")
}
if err := sortServerAnalysis(rows, "totalPay", false); err != nil || rows[0].ServerNum != "003" {
t.Fatal("降序错误")
}
if err := sortServerAnalysis(rows, "", false); err != nil || rows[0].ServerNum != "001" {
t.Fatal("取消排序应恢复稳定的区服顺序")
}
}