fix(dashboard): add paid amount to sales trend

This commit is contained in:
Zuncle 2026-04-03 18:03:29 +08:00
parent 25d32831ea
commit 6284966d3c
2 changed files with 39 additions and 13 deletions

View File

@ -220,11 +220,12 @@ type trendRequest struct {
} }
type trendPoint struct { type trendPoint struct {
Date string `json:"date"` Date string `json:"date"`
Value int64 `json:"value"` Value int64 `json:"value"`
Gmv int64 `json:"gmv"` Gmv int64 `json:"gmv"`
Orders int64 `json:"orders"` PaidAmount int64 `json:"paidAmount"`
NewUsers int64 `json:"newUsers"` Orders int64 `json:"orders"`
NewUsers int64 `json:"newUsers"`
} }
type salesDrawTrendResponse struct { type salesDrawTrendResponse struct {
@ -1760,8 +1761,9 @@ func (h *handler) DashboardSalesDrawTrend() core.HandlerFunc {
// 总业务价值 (GMV) + 订单数:仅统计小程序内已支付订单 // 总业务价值 (GMV) + 订单数:仅统计小程序内已支付订单
var orderStats struct { var orderStats struct {
Orders int64 `gorm:"column:orders"` Orders int64 `gorm:"column:orders"`
Gmv int64 `gorm:"column:gmv"` Gmv int64 `gorm:"column:gmv"`
PaidAmount int64 `gorm:"column:paid_amount"`
} }
_ = h.readDB.Orders.WithContext(ctx.RequestContext()).UnderlyingDB(). _ = h.readDB.Orders.WithContext(ctx.RequestContext()).UnderlyingDB().
Model(&model.Orders{}). Model(&model.Orders{}).
@ -1770,7 +1772,7 @@ func (h *handler) DashboardSalesDrawTrend() core.HandlerFunc {
Where("(ext_order_id = '' OR ext_order_id IS NULL)"). Where("(ext_order_id = '' OR ext_order_id IS NULL)").
Where("paid_at >= ?", b.Start). Where("paid_at >= ?", b.Start).
Where("paid_at <= ?", b.End). Where("paid_at <= ?", b.End).
Select("COUNT(id) as orders, COALESCE(SUM(actual_amount + discount_amount), 0) as gmv"). Select("COUNT(id) as orders, COALESCE(SUM(actual_amount + discount_amount), 0) as gmv, COALESCE(SUM(actual_amount), 0) as paid_amount").
Scan(&orderStats) Scan(&orderStats)
// 新注册用户数 // 新注册用户数
@ -1780,11 +1782,12 @@ func (h *handler) DashboardSalesDrawTrend() core.HandlerFunc {
Count() Count()
list[i] = trendPoint{ list[i] = trendPoint{
Date: b.Label, Date: b.Label,
Value: draws, Value: draws,
Gmv: orderStats.Gmv / 100, // 转为元 Gmv: orderStats.Gmv / 100, // 转为元
Orders: orderStats.Orders, PaidAmount: orderStats.PaidAmount / 100, // 转为元
NewUsers: newUsers, Orders: orderStats.Orders,
NewUsers: newUsers,
} }
} }

View File

@ -1,6 +1,8 @@
package admin package admin
import ( import (
"encoding/json"
"strings"
"testing" "testing"
"time" "time"
) )
@ -116,3 +118,24 @@ func TestShouldUseMonthlyGranularityForAll(t *testing.T) {
t.Fatalf("non-day granularity should not switch") t.Fatalf("non-day granularity should not switch")
} }
} }
func TestTrendPointJSONIncludesPaidAmount(t *testing.T) {
point := trendPoint{
Date: "2026-03-01",
Value: 10,
Gmv: 20,
PaidAmount: 15,
Orders: 3,
NewUsers: 2,
}
payload, err := json.Marshal(point)
if err != nil {
t.Fatalf("marshal trendPoint: %v", err)
}
data := string(payload)
if !strings.Contains(data, "\"paidAmount\":15") {
t.Fatalf("expected paidAmount in json, got: %s", data)
}
}