fix: 完成商务迁移复核与接口优化并清理历史资料
This commit is contained in:
@@ -2,6 +2,8 @@ package business
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
"gorm.io/gorm"
|
||||
"kra/internal/biz/business"
|
||||
)
|
||||
@@ -44,7 +46,10 @@ func rankingReportQuery(db *gorm.DB, kind business.RankingKind, scope business.A
|
||||
return analyticsQuery(db, scope, f, table, mode)
|
||||
}
|
||||
all := business.AnalyticsScope{Viewer: business.UserScope{All: true}, IncludeSub: true}
|
||||
return analyticsQuery(db, all, f, table, mode).Where("EXISTS (?)", rankingBusinessPromotes(db, scope, f.Sources).Select("1").Where("p.source_db=r.source_db AND p.id=r.promote_id"))
|
||||
// 同一渠道会出现在多级商务的范围中,先去重并一次性展开可见渠道。
|
||||
// 避免对每一条报表身份记录重复扫描整棵商务关系树。
|
||||
visible := rankingBusinessPromotes(db, scope, f.Sources).Select("DISTINCT p.source_db,p.id")
|
||||
return analyticsQuery(db, all, f, table, mode).Joins("JOIN (?) visible_promotes ON visible_promotes.source_db=r.source_db AND visible_promotes.id=r.promote_id", visible)
|
||||
}
|
||||
func (r *rankingRepo) Collect(ctx context.Context, kind business.RankingKind, scope business.AnalyticsScope, f business.RankingRange) (*business.RankingData, error) {
|
||||
db := r.data.DB().WithContext(ctx)
|
||||
@@ -109,60 +114,72 @@ func (r *rankingRepo) Collect(ctx context.Context, kind business.RankingKind, sc
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
group, queryContext := errgroup.WithContext(ctx)
|
||||
group.SetLimit(2)
|
||||
queryDB := db.WithContext(queryContext)
|
||||
for _, period := range []struct {
|
||||
begin, end string
|
||||
target map[business.AnalyticsKey]*business.RankingMetrics
|
||||
}{{f.Begin, f.End, out.Current}, {f.PreviousBegin, f.PreviousEnd, out.Previous}} {
|
||||
query := f.AnalyticsFilter
|
||||
query.Begin, query.End = period.begin, period.end
|
||||
rowFor := func(po analyticsAggPO) *business.RankingMetrics {
|
||||
key := business.AnalyticsKey{SourceDB: po.SourceDB, ID: *po.ID}
|
||||
if period.target[key] == nil {
|
||||
period.target[key] = &business.RankingMetrics{}
|
||||
}
|
||||
return period.target[key]
|
||||
}
|
||||
for _, pay := range []bool{true, false} {
|
||||
table, mode := "tab_sw_report_day", 0
|
||||
if !pay {
|
||||
table = "tab_sw_report_day_key"
|
||||
if scope.IDCardDedup {
|
||||
mode = 1
|
||||
group.Go(func() error {
|
||||
query := f.AnalyticsFilter
|
||||
query.Begin, query.End = period.begin, period.end
|
||||
rowFor := func(po analyticsAggPO) *business.RankingMetrics {
|
||||
key := business.AnalyticsKey{SourceDB: po.SourceDB, ID: *po.ID}
|
||||
if period.target[key] == nil {
|
||||
period.target[key] = &business.RankingMetrics{}
|
||||
}
|
||||
return period.target[key]
|
||||
}
|
||||
q := rankingReportQuery(db, kind, scope, query, table, mode)
|
||||
id := "r.promote_id"
|
||||
if kind == business.GameRanking {
|
||||
id = "r.game_id"
|
||||
}
|
||||
if kind == business.ChannelRanking {
|
||||
q = q.Joins("LEFT JOIN tab_promote root_p ON root_p.source_db=r.source_db AND root_p.id=r.promote_id")
|
||||
id = "COALESCE(NULLIF(root_p.top_promote_id,0),r.promote_id)"
|
||||
}
|
||||
selects := "r.source_db," + id + " AS entity_id"
|
||||
group := "r.source_db," + id
|
||||
if pay {
|
||||
selects += ",COALESCE(SUM(r.total_pay),0) AS amount"
|
||||
} else {
|
||||
selects += ",r.metric,COUNT(DISTINCT r.identity_key) AS count"
|
||||
group += ",r.metric"
|
||||
}
|
||||
var rows []analyticsAggPO
|
||||
if err := q.Select(selects).Group(group).Scan(&rows).Error; err != nil {
|
||||
return nil, business.ErrStorage
|
||||
}
|
||||
for _, po := range rows {
|
||||
if po.ID == nil || (pay && *po.ID < 0) {
|
||||
continue
|
||||
for _, pay := range []bool{true, false} {
|
||||
table, mode := "tab_sw_report_day", 0
|
||||
if !pay {
|
||||
table = "tab_sw_report_day_key"
|
||||
if scope.IDCardDedup {
|
||||
mode = 1
|
||||
}
|
||||
}
|
||||
row := rowFor(po)
|
||||
q := rankingReportQuery(queryDB, kind, scope, query, table, mode)
|
||||
id := "r.promote_id"
|
||||
if kind == business.GameRanking {
|
||||
id = "r.game_id"
|
||||
}
|
||||
if kind == business.ChannelRanking {
|
||||
if pay {
|
||||
q = analyticsPayLeaves(queryDB, q)
|
||||
}
|
||||
q = q.Joins("LEFT JOIN tab_promote root_p ON root_p.source_db=r.source_db AND root_p.id=r.promote_id")
|
||||
id = "COALESCE(NULLIF(root_p.top_promote_id,0),r.promote_id)"
|
||||
}
|
||||
selects := "r.source_db," + id + " AS entity_id"
|
||||
group := "r.source_db," + id
|
||||
if pay {
|
||||
row.Pay = po.Amount
|
||||
selects += ",COALESCE(SUM(r.total_pay),0) AS amount"
|
||||
} else {
|
||||
putAnalyticsCount(&row.Counts, po.Metric, po.Count)
|
||||
selects += ",r.metric,COUNT(DISTINCT r.identity_key) AS count"
|
||||
group += ",r.metric"
|
||||
}
|
||||
var rows []analyticsAggPO
|
||||
if err := q.Select(selects).Group(group).Scan(&rows).Error; err != nil {
|
||||
return business.ErrStorage
|
||||
}
|
||||
for _, po := range rows {
|
||||
if po.ID == nil || (pay && *po.ID < 0) {
|
||||
continue
|
||||
}
|
||||
row := rowFor(po)
|
||||
if pay {
|
||||
row.Pay = po.Amount
|
||||
} else {
|
||||
putAnalyticsCount(&row.Counts, po.Metric, po.Count)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
if err := group.Wait(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if kind != business.BusinessRanking && len(out.Current) > 0 {
|
||||
table, column := "tab_promote", "account"
|
||||
|
||||
Reference in New Issue
Block a user