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

View File

@ -1,6 +1,8 @@
package admin
import (
"encoding/json"
"strings"
"testing"
"time"
)
@ -116,3 +118,24 @@ func TestShouldUseMonthlyGranularityForAll(t *testing.T) {
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)
}
}