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:
2026-11-14 23:06:12 +08:00
parent 59483ed15f
commit c9cd6e51a5
14 changed files with 1506 additions and 28 deletions
+89
View File
@@ -65,6 +65,10 @@ func registerSettleRoutes(protected *gin.RouterGroup, deps Deps) {
middleware.RequirePermission(deps.Perm, "settle", "write"), h.ladderUpdate)
protected.DELETE("/settle/ladder/:id",
middleware.RequirePermission(deps.Perm, "settle", "write"), h.ladderDelete)
// S2-D5:利润分析双端点(平台角色门——利润含全平台资金信息,group/customer
// 403,与充值订单列表同口径;查询类不挂 settle:write)。
protected.GET("/stats/profit/users", h.profitUsers)
protected.GET("/stats/profit/channels", h.profitChannels)
}
// StartSettleAggregateJob v1 层装配入口(照 StartStatsRefresher 模式,仅
@@ -517,3 +521,88 @@ func (h *settleHandler) ladderDelete(c *gin.Context) {
}
response.Success(c, nil)
}
// ---- S2-D5:利润分析 ----
// parseProfitDates date_from/date_to 解析(YYYY-MM-DD;零值兜底=本月 1 号~今天,
// 月度利润视角)。返回 ok=false 时已写响应。
func parseProfitDates(c *gin.Context) (from, to time.Time, ok bool) {
now := time.Now()
from = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
to = now
if s := c.Query("date_from"); s != "" {
d, err := time.ParseInLocation("2006-01-02", s, time.Local)
if err != nil {
response.Error(c, errs.InvalidParam.WithMsg("date_from 须为 YYYY-MM-DD"))
return time.Time{}, time.Time{}, false
}
from = d
}
if s := c.Query("date_to"); s != "" {
d, err := time.ParseInLocation("2006-01-02", s, time.Local)
if err != nil {
response.Error(c, errs.InvalidParam.WithMsg("date_to 须为 YYYY-MM-DD"))
return time.Time{}, time.Time{}, false
}
to = d
}
return from, to, true
}
// profitUsers GET /stats/profit/users:用户利润(时间线价收入/成本分摊/直线下级
// 返点 R4/子树过滤;平台角色)。口径详见 settle/profit.go 头注。
func (h *settleHandler) profitUsers(c *gin.Context) {
if !statsIsPlatform(c) {
response.Error(c, errs.Forbidden.WithMsg("仅平台角色可查看利润分析"))
return
}
from, to, ok := parseProfitDates(c)
if !ok {
return
}
q := settle.UserProfitQuery{From: from, To: to, Page: 1, PageSize: 20}
if v, err := strconv.ParseInt(c.Query("parent_id"), 10, 64); err == nil && v > 0 {
q.ParentID = v
}
if v, err := strconv.Atoi(c.Query("page")); err == nil && v > 0 {
q.Page = v
}
if v, err := strconv.Atoi(c.Query("page_size")); err == nil && v > 0 {
q.PageSize = v
}
res, err := h.svc.UserProfit(c.Request.Context(), q)
if err != nil {
bizResp(c, err)
return
}
response.Success(c, res)
}
// profitChannels GET /stats/profit/channels:通道利润(通道价成本直算/收入占比
// 分摊;平台角色)。
func (h *settleHandler) profitChannels(c *gin.Context) {
if !statsIsPlatform(c) {
response.Error(c, errs.Forbidden.WithMsg("仅平台角色可查看利润分析"))
return
}
from, to, ok := parseProfitDates(c)
if !ok {
return
}
q := settle.ChannelProfitQuery{From: from, To: to, Page: 1, PageSize: 20}
if v, err := strconv.ParseInt(c.Query("channel_id"), 10, 64); err == nil && v > 0 {
q.ChannelID = v
}
if v, err := strconv.Atoi(c.Query("page")); err == nil && v > 0 {
q.Page = v
}
if v, err := strconv.Atoi(c.Query("page_size")); err == nil && v > 0 {
q.PageSize = v
}
res, err := h.svc.ChannelProfit(c.Request.Context(), q)
if err != nil {
bizResp(c, err)
return
}
response.Success(c, res)
}