package settle // profit_test.go S2-D5:利润分析单测——时间线价收入/成本占比分摊/直线下级 // 返点(R4,单级)/子树过滤(Path 前缀)/汇总守恒;通道侧成本直算+收入分摊。 // 口径差异声明:阶梯不进分析(与账单金额的差=口径差异,见 profit.go 头注)。 import ( "context" "testing" "time" "gorm.io/gorm" "go-sms/internal/model/entity" ) // newProfitDB 出账全家桶 + 账号树。 func newProfitDB(t *testing.T) *gorm.DB { t.Helper() db := newBillDB(t) if err := db.AutoMigrate(&entity.AccountUser{}); err != nil { t.Fatal(err) } return db } // seedProfitTree 树:1(root)→{10(rate .10),12(rate 0)};10→11(rate .20)。 // 价:acc1=0.10 / acc10=0.20 / acc11=0.30 / acc12 无价(Q1:0 元入账)。 func seedProfitTree(t *testing.T, db *gorm.DB) { t.Helper() d := func(s string) time.Time { v, _ := time.ParseInLocation("2006-01-02", s, time.Local) return v } if err := db.Create(&[]entity.AccountUser{ {Model: entity.Model{ID: 1}, Username: "root", Role: "group", Path: "/1/"}, {Model: entity.Model{ID: 10}, Username: "a10", Role: "customer", ParentID: 1, Path: "/1/10/", RebateRate: 0.10}, {Model: entity.Model{ID: 11}, Username: "a11", Role: "customer", ParentID: 10, Path: "/1/10/11/", RebateRate: 0.20}, {Model: entity.Model{ID: 12}, Username: "a12", Role: "customer", ParentID: 1, Path: "/1/12/"}, }).Error; err != nil { t.Fatal(err) } if err := db.Create(&[]entity.AccountPrice{ {AccountID: 1, Business: "sms", Price: 0.10, EffectiveAt: d("2026-11-01")}, {AccountID: 10, Business: "sms", Price: 0.20, EffectiveAt: d("2026-11-01")}, {AccountID: 11, Business: "sms", Price: 0.30, EffectiveAt: d("2026-11-01")}, }).Error; err != nil { t.Fatal(err) } } // seedProfitFlow 消耗(acc1:100/acc10:50/acc11:40/acc12:10 无价)+ 通道侧 // (ch1:150 + ch2:50=200,成本价均 0.04 → 总成本 8)。账号/通道成功总数一致=200。 func seedProfitFlow(t *testing.T, db *gorm.DB) { t.Helper() d := func(s string) time.Time { v, _ := time.ParseInLocation("2006-01-02", s, time.Local) return v } if err := db.Create(&[]entity.BillingConsumeLog{ {AccountID: 1, Business: "sms", StatDate: d("2026-11-10"), SuccessCount: 100}, {AccountID: 10, Business: "sms", StatDate: d("2026-11-10"), SuccessCount: 50}, {AccountID: 11, Business: "sms", StatDate: d("2026-11-20"), SuccessCount: 40}, {AccountID: 12, Business: "sms", StatDate: d("2026-11-10"), SuccessCount: 10}, }).Error; err != nil { t.Fatal(err) } if err := db.Create(&[]entity.ChannelChannel{ {Model: entity.Model{ID: 1}, Name: "c1", Code: "pfc1", UpstreamID: 7}, {Model: entity.Model{ID: 2}, Name: "c2", Code: "pfc2"}, }).Error; err != nil { t.Fatal(err) } if err := db.Create(&[]entity.ChannelPrice{ {ChannelID: 1, Price: 0.04, EffectiveAt: d("2026-11-01")}, {ChannelID: 2, Price: 0.04, EffectiveAt: d("2026-11-01")}, }).Error; err != nil { t.Fatal(err) } if err := db.Create(&[]entity.StatsChannelDaily{ {ChannelID: 1, StatDate: d("2026-11-10"), SuccessCount: 150}, {ChannelID: 2, StatDate: d("2026-11-20"), SuccessCount: 50}, }).Error; err != nil { t.Fatal(err) } } // findRow 按账号取行。 func findRow(t *testing.T, res *UserProfitResult, id int64) UserProfitRow { t.Helper() for _, r := range res.List { if r.AccountID == id { return r } } t.Fatalf("账号 %d 不在结果中: %+v", id, res.List) return UserProfitRow{} } // TestProfit_Users 时间线价收入+占比分摊成本+单级返点+子树过滤+平台净利守恒。 // // 期望:acc1 rev10/cost4/rebate1.0(=子10 rev10×0.10)/profit7; // acc10 rev10/cost2/rebate2.4(=子11 rev12×0.20)/profit10.4; // acc11 rev12/cost1.6/profit10.4;acc12 无价 rev0/cost0.4/profit-0.4; // 汇总 rev32/cost8/rebate3.4/平台净利 32-8-3.4=20.6。 func TestProfit_Users(t *testing.T) { db := newProfitDB(t) seedProfitTree(t, db) seedProfitFlow(t, db) svc := NewService(db) ctx := context.Background() from, _ := time.ParseInLocation("2006-01-02", "2026-11-01", time.Local) to, _ := time.ParseInLocation("2006-01-02", "2026-11-30", time.Local) res, err := svc.UserProfit(ctx, UserProfitQuery{From: from, To: to}) if err != nil { t.Fatal(err) } if res.Total != 4 || res.Summary.Count != 4 { t.Fatalf("行数=%d/%d 期望 4", res.Total, res.Summary.Count) } r1 := findRow(t, res, 1) if r1.SuccessCount != 100 || !feq(r1.Revenue, 10) || !feq(r1.Cost, 4) || !feq(r1.Rebate, 1.0) || !feq(r1.Profit, 7) { t.Fatalf("acc1 错: %+v", r1) } r10 := findRow(t, res, 10) if !feq(r10.Revenue, 10) || !feq(r10.Cost, 2) || !feq(r10.Rebate, 2.4) || !feq(r10.Profit, 10.4) { t.Fatalf("acc10 错: %+v", r10) } r11 := findRow(t, res, 11) if !feq(r11.Revenue, 12) || !feq(r11.Cost, 1.6) || r11.Rebate != 0 || !feq(r11.Profit, 10.4) { t.Fatalf("acc11 错: %+v", r11) } r12 := findRow(t, res, 12) if r12.Revenue != 0 || !feq(r12.Cost, 0.4) || !feq(r12.Profit, -0.4) { t.Fatalf("acc12 错(无价 0 元入账): %+v", r12) } s := res.Summary if !feq(s.TotalRevenue, 32) || !feq(s.TotalCost, 8) || !feq(s.TotalRebate, 3.4) || !feq(s.PlatformProfit, 20.6) { t.Fatalf("汇总错: %+v", s) } // 子树过滤 parent_id=10:仅 10/11 两行,汇总随 scope 收窄。 res, err = svc.UserProfit(ctx, UserProfitQuery{From: from, To: to, ParentID: 10}) if err != nil { t.Fatal(err) } if res.Total != 2 || res.Summary.Count != 2 { t.Fatalf("子树行数=%d 期望 2", res.Total) } findRow(t, res, 10) findRow(t, res, 11) if !feq(res.Summary.TotalRevenue, 22) || !feq(res.Summary.TotalCost, 3.6) || !feq(res.Summary.TotalRebate, 2.4) { t.Fatalf("子树汇总错: %+v", res.Summary) } // parent 不存在 → 404 业务错误。 if _, err := svc.UserProfit(ctx, UserProfitQuery{From: from, To: to, ParentID: 999}); err == nil { t.Fatal("parent_id=999 应报错") } } // TestProfit_Users_ParentWithoutTraffic 无自身流量的上级也有行(代理纯返点场景)。 func TestProfit_Users_ParentWithoutTraffic(t *testing.T) { db := newProfitDB(t) d := func(s string) time.Time { v, _ := time.ParseInLocation("2006-01-02", s, time.Local) return v } if err := db.Create(&[]entity.AccountUser{ {Model: entity.Model{ID: 20}, Username: "agent", Role: "group", Path: "/20/"}, {Model: entity.Model{ID: 21}, Username: "c21", Role: "customer", ParentID: 20, Path: "/20/21/", RebateRate: 0.5}, }).Error; err != nil { t.Fatal(err) } if err := db.Create(&[]entity.AccountPrice{ {AccountID: 21, Business: "sms", Price: 0.10, EffectiveAt: d("2026-11-01")}, }).Error; err != nil { t.Fatal(err) } if err := db.Create(&[]entity.BillingConsumeLog{ {AccountID: 21, Business: "sms", StatDate: d("2026-11-10"), SuccessCount: 100}, }).Error; err != nil { t.Fatal(err) } res, err := NewService(db).UserProfit(context.Background(), UserProfitQuery{ From: d("2026-11-01"), To: d("2026-11-30"), }) if err != nil { t.Fatal(err) } if res.Total != 2 { t.Fatalf("行数=%d 期望 2(agent+c21)", res.Total) } agent := findRow(t, res, 20) if agent.SuccessCount != 0 || agent.Revenue != 0 || !feq(agent.Rebate, 5.0) || !feq(agent.Profit, 5.0) { t.Fatalf("agent 行错(纯返点): %+v", agent) } } // TestProfit_Channels 通道成本直算+收入占比分摊+channel_id 过滤。 // ch1: succ150 cost6(150×0.04) rev24(32×150/200) profit18; // ch2: succ50 cost2 rev8 profit6。 func TestProfit_Channels(t *testing.T) { db := newProfitDB(t) seedProfitTree(t, db) seedProfitFlow(t, db) svc := NewService(db) ctx := context.Background() from, _ := time.ParseInLocation("2006-01-02", "2026-11-01", time.Local) to, _ := time.ParseInLocation("2006-01-02", "2026-11-30", time.Local) res, err := svc.ChannelProfit(ctx, ChannelProfitQuery{From: from, To: to}) if err != nil { t.Fatal(err) } if res.Total != 2 { t.Fatalf("通道行数=%d 期望 2", res.Total) } if res.List[0].ChannelID != 1 { // succ 降序 t.Fatalf("排序错: %+v", res.List) } c1 := res.List[0] if c1.Name != "c1" || c1.UpstreamID != 7 || c1.SuccessCount != 150 || !feq(c1.Cost, 6) || !feq(c1.Revenue, 24) || !feq(c1.Profit, 18) { t.Fatalf("ch1 错: %+v", c1) } c2 := res.List[1] if !feq(c2.Cost, 2) || !feq(c2.Revenue, 8) || !feq(c2.Profit, 6) { t.Fatalf("ch2 错: %+v", c2) } // channel_id 过滤。 res, err = svc.ChannelProfit(ctx, ChannelProfitQuery{From: from, To: to, ChannelID: 2}) if err != nil || res.Total != 1 || res.List[0].ChannelID != 2 { t.Fatalf("过滤错: %+v err=%v", res, err) } }