feat: integrate OA activation workflows and business migration
This commit is contained in:
@@ -2,6 +2,7 @@ package business
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -12,6 +13,9 @@ import (
|
||||
|
||||
// OA 表只读,不由新商务的系统迁移创建或改写。
|
||||
type activationPO struct {
|
||||
BusierID uint64
|
||||
CreatedAt *time.Time
|
||||
RelatedChannels string
|
||||
ID uint64 `gorm:"primaryKey"`
|
||||
PromoteID *uint64
|
||||
PlatformName, PromoteAccount, BusierAccount, ApplicantOA, Status string
|
||||
@@ -25,7 +29,20 @@ func toBizActivation(po activationPO) *business.Activation {
|
||||
if po.AccumAmount != nil {
|
||||
amount = *po.AccumAmount
|
||||
}
|
||||
return &business.Activation{ID: po.ID, PromoteID: po.PromoteID, PlatformName: po.PlatformName, PromoteAccount: po.PromoteAccount, BusierAccount: po.BusierAccount, ApplicantOA: po.ApplicantOA, Status: po.Status, AccumAmount: amount, BindAt: po.BindAt, UnbindAt: po.UnbindAt, ReachAt: po.ReachAt}
|
||||
row := &business.Activation{BusierID: po.BusierID, CreatedAt: po.CreatedAt, ID: po.ID, PromoteID: po.PromoteID, PlatformName: po.PlatformName, PromoteAccount: po.PromoteAccount, BusierAccount: po.BusierAccount, ApplicantOA: po.ApplicantOA, Status: po.Status, AccumAmount: amount, BindAt: po.BindAt, UnbindAt: po.UnbindAt, ReachAt: po.ReachAt}
|
||||
var channels []struct {
|
||||
PlatformName string `json:"platformName"`
|
||||
PromoteID uint64 `json:"promoteId"`
|
||||
}
|
||||
if po.RelatedChannels != "" {
|
||||
if json.Unmarshal([]byte(po.RelatedChannels), &channels) != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
for _, ch := range channels {
|
||||
row.Channels = append(row.Channels, business.ChannelKey{SourceDB: ch.PlatformName, ID: ch.PromoteID})
|
||||
}
|
||||
return row
|
||||
}
|
||||
|
||||
type activationRepo struct {
|
||||
@@ -99,7 +116,41 @@ func (r *activationRepo) List(ctx context.Context, scope business.ActivationScop
|
||||
return nil, business.ErrActivationSource
|
||||
}
|
||||
for _, po := range pos {
|
||||
page.Rows = append(page.Rows, toBizActivation(po))
|
||||
row := toBizActivation(po)
|
||||
if row == nil {
|
||||
return nil, business.ErrActivationSource
|
||||
}
|
||||
if row.BindAt != nil {
|
||||
if err := r.applyTransferEnd(ctx, row); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query := activationMetricQuery(row)
|
||||
available, err := r.activationMetricsAvailable(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if available {
|
||||
items, err := r.Spend(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
amounts := make([]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
amounts = append(amounts, item.Amount)
|
||||
}
|
||||
total, err := sumActivationMoney(amounts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
row.AccumAmount = total
|
||||
row.MetricsAvailable = true
|
||||
}
|
||||
row.RegisteredPlayers, err = r.registered(ctx, query, filter.IDCardDedup)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
page.Rows = append(page.Rows, row)
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
@@ -116,49 +167,12 @@ func (r *activationRepo) Find(ctx context.Context, id uint64) (*business.Activat
|
||||
}
|
||||
return nil, business.ErrActivationSource
|
||||
}
|
||||
return toBizActivation(po), nil
|
||||
}
|
||||
|
||||
func (r *activationRepo) Spend(ctx context.Context, query business.ActivationSpendQuery) ([]*business.ActivationSpendItem, error) {
|
||||
db := r.data.DB().WithContext(ctx)
|
||||
available, err := businessTableAvailable(db, "tab_promote")
|
||||
if err != nil {
|
||||
row := toBizActivation(po)
|
||||
if row == nil {
|
||||
return nil, business.ErrActivationSource
|
||||
}
|
||||
if err := r.applyTransferEnd(ctx, row); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !available {
|
||||
return nil, business.ErrActivationMetrics
|
||||
}
|
||||
tree := func() *gorm.DB {
|
||||
return db.Table("tab_promote p").Where("p.source_db = ? AND (p.id = ? OR p.top_promote_id = ? OR p.parent_id = ?)", query.PlatformName, query.PromoteID, query.PromoteID, query.PromoteID)
|
||||
}
|
||||
var count int64
|
||||
if err := tree().Count(&count).Error; err != nil {
|
||||
return nil, business.ErrStorage
|
||||
}
|
||||
items := []*business.ActivationSpendItem{}
|
||||
if count == 0 {
|
||||
return items, nil
|
||||
}
|
||||
available, err = businessTableAvailable(db, "tab_spend")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !available {
|
||||
return nil, business.ErrActivationMetrics
|
||||
}
|
||||
q := db.Table("tab_spend s").Where("s.source_db = ? AND s.pay_status = ? AND s.pay_time >= ?", query.PlatformName, 1, query.Start).Where("EXISTS (?)", tree().Select("1").Where("p.id=s.promote_id"))
|
||||
if query.End != nil {
|
||||
q = q.Where("s.pay_time <= ?", *query.End)
|
||||
}
|
||||
var rows []struct {
|
||||
GameID uint64
|
||||
GameName, Amount string
|
||||
}
|
||||
if err := q.Select("s.game_id,COALESCE(MAX(s.game_name),'') AS game_name,COALESCE(SUM(s.pay_amount),0) AS amount").Group("s.game_id").Order("amount DESC,s.game_id").Scan(&rows).Error; err != nil {
|
||||
return nil, business.ErrStorage
|
||||
}
|
||||
for _, row := range rows {
|
||||
items = append(items, &business.ActivationSpendItem{GameName: row.GameName, Amount: row.Amount})
|
||||
}
|
||||
return items, nil
|
||||
return row, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user