S2-D5 利润/返点/三方对账——S2 泳道收官: profit.go(UserProfit 时间线价收入+成本占比分摊+直线下级返点 R4 历史不重算=不落档现算+parent_id 子树 Path 过滤+平台净利双口径;ChannelProfit 同分母分摊 Σ守恒;阶梯不进分析=与账单金额差为口径差异)+reconcile.go(三方对账:账号=明细直查↔billing_consume_log↔settle_customer 条数/通道两方/上家=当前归并口径重推↔快照;金额不比=快照语义防调价噪音;bill_missing 不判失败;AllZero=出口判据)+cmd/reconcile/settle-check(-period/-json,exit 0=all_zero)+handler /stats/profit/users·channels 双端点(平台角色门,缺省窗本月至今)+openapi 描述对齐;测试 5 件(利润树/返点/分摊/子树/守恒+对账全零+篡改逐项暴露+missing 语义+handler 数值/403/坏日期);真机全链:2026-10 空月种链→汇总 1/1→下家 0.2→阶梯覆写 0.1→结账 SettleRecord→上家 2/0.1→利润双端点(rev0.2 时间线 vs 账单 0.1 口径差异实证)→settle-check all_zero=true EXIT=0(S2 出口判据)→清理 11 项零残留;走查脚本扩 D4/D5 全域 AI 预走查 27 PASS/0 FAIL+修复两存量 bug(明细 ID 9302 段超 int64 拒收→7302 段/清理段 $ON $B0 单引号不展开致 SQL 语法错→printf|mysql_pipe);门禁 vet/-race 39 包/lint 0;收官判定 s2-closeout.md(出口两项全过+六项裁决 R1~R6 全闭环)+验收单 s2-d5.md+CHANGELOG+memory-bank 回填
This commit is contained in:
@@ -39,7 +39,8 @@ func newSettleRouterWithPerm(t *testing.T, perm middleware.PermissionChecker) (*
|
||||
s.SetMaxOpenConns(1)
|
||||
}
|
||||
if err := db.AutoMigrate(&entity.MessageDetail{}, &entity.BillingConsumeLog{},
|
||||
&entity.StatsChannelDaily{}, &entity.AccountUser{}, &entity.BillingOrder{}); err != nil {
|
||||
&entity.StatsChannelDaily{}, &entity.AccountUser{}, &entity.BillingOrder{},
|
||||
&entity.AccountPrice{}, &entity.ChannelPrice{}, &entity.ChannelChannel{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 流水表手动建:idx_account_time 与 BillingOrder 同名,glebarez 索引名
|
||||
@@ -368,3 +369,104 @@ func TestSettleBill_FullFlow(t *testing.T) {
|
||||
t.Fatalf("无 settle:write 应 403: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestSettleProfit_Endpoints S2-D5:利润双端点——平台角色可查(数值口径),
|
||||
// customer 403;坏日期 400。种子=helper 明细(账号1/100 今日各 1 成功,通道1)
|
||||
// + 价(账号1=1.0,通道1=0.5):acc1 rev1.0/cost0.5(分摊 1.0×1/2)/profit0.5;
|
||||
// ch1 succ2/cost1.0/rev1.0/profit0(账号100 无账号行,消耗行不入结果)。
|
||||
func TestSettleProfit_Endpoints(t *testing.T) {
|
||||
rg, db, setClaims := newSettleRouter(t)
|
||||
today := time.Now().Format("2006-01-02")
|
||||
yday := time.Now().AddDate(0, 0, -1)
|
||||
if err := db.Create(&[]entity.AccountPrice{
|
||||
{AccountID: 1, Business: "sms", Price: 1.0, EffectiveAt: yday},
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&[]entity.ChannelPrice{
|
||||
{ChannelID: 1, Price: 0.5, EffectiveAt: yday},
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := settle.NewService(db).AggregateDay(t.Context(), time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
setClaims(&middleware.Claims{AccountID: 1, Username: "admin", Roles: []string{"admin"}})
|
||||
w := httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet,
|
||||
"/api/v1/stats/profit/users?date_from="+today+"&date_to="+today, nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("users status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var users struct {
|
||||
Data struct {
|
||||
Total int64 `json:"total"`
|
||||
List []struct {
|
||||
AccountID int64 `json:"account_id"`
|
||||
Revenue float64 `json:"revenue"`
|
||||
Cost float64 `json:"cost"`
|
||||
Profit float64 `json:"profit"`
|
||||
} `json:"list"`
|
||||
Summary struct {
|
||||
TotalRevenue float64 `json:"total_revenue"`
|
||||
} `json:"summary"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &users); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if users.Data.Total != 1 || len(users.Data.List) != 1 {
|
||||
t.Fatalf("users 应 1 行(账号100 无账号实体): %+v", users.Data)
|
||||
}
|
||||
r := users.Data.List[0]
|
||||
if r.AccountID != 1 || r.Revenue != 1.0 || r.Cost != 0.5 || r.Profit != 0.5 {
|
||||
t.Fatalf("acc1 利润行错: %+v", r)
|
||||
}
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet,
|
||||
"/api/v1/stats/profit/channels?date_from="+today+"&date_to="+today, nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("channels status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var channels struct {
|
||||
Data struct {
|
||||
Total int64 `json:"total"`
|
||||
List []struct {
|
||||
ChannelID int64 `json:"channel_id"`
|
||||
SuccessCount int64 `json:"success_count"`
|
||||
Revenue float64 `json:"revenue"`
|
||||
Cost float64 `json:"cost"`
|
||||
Profit float64 `json:"profit"`
|
||||
} `json:"list"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &channels); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if channels.Data.Total != 1 || len(channels.Data.List) != 1 {
|
||||
t.Fatalf("channels 应 1 行: %+v", channels.Data)
|
||||
}
|
||||
c := channels.Data.List[0]
|
||||
if c.ChannelID != 1 || c.SuccessCount != 2 || c.Revenue != 1.0 || c.Cost != 1.0 || c.Profit != 0 {
|
||||
t.Fatalf("ch1 利润行错: %+v", c)
|
||||
}
|
||||
|
||||
// customer → 403;坏日期 → 400。
|
||||
setClaims(&middleware.Claims{AccountID: 100, Username: "cust", Roles: []string{"customer"}})
|
||||
for _, path := range []string{"/stats/profit/users", "/stats/profit/channels"} {
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1"+path, nil))
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Fatalf("customer %s 应 403: %d %s", path, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
setClaims(&middleware.Claims{AccountID: 1, Username: "admin", Roles: []string{"admin"}})
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet,
|
||||
"/api/v1/stats/profit/users?date_from=2026/11/14", nil))
|
||||
if w.Code == 200 {
|
||||
t.Fatalf("坏日期应 4xx: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user