159 lines
7.4 KiB
Go
159 lines
7.4 KiB
Go
package admindashboard
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"hfb_sys/backend/internal/database"
|
|
"hfb_sys/backend/internal/listingstatus"
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/timeutil"
|
|
)
|
|
|
|
func TestListingDailyOverviewGroupsBySourceChannel(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
if err := database.AutoMigrateIfMissing(db, &model.ListingStatusEvent{}, &model.ListingUpload{}); err != nil {
|
|
t.Fatalf("AutoMigrate() error = %v", err)
|
|
}
|
|
loc := timeutil.ShanghaiLocation()
|
|
now := time.Date(2026, 7, 25, 12, 0, 0, 0, loc)
|
|
listingID1 := uint64(1)
|
|
listingID2 := uint64(2)
|
|
listingID4 := uint64(4)
|
|
|
|
uploads := []model.ListingUpload{
|
|
{ListingID: &listingID1, UploaderName: "客服1", SourceChannel: "淘宝"},
|
|
{ListingID: &listingID2, UploaderName: "客服2", SourceChannel: "微信"},
|
|
{ListingID: &listingID4, UploaderName: "客服3"},
|
|
}
|
|
if err := db.Create(&uploads).Error; err != nil {
|
|
t.Fatalf("create uploads error = %v", err)
|
|
}
|
|
|
|
events := []model.ListingStatusEvent{
|
|
{ListingID: listingID1, EventType: listingstatus.EventPublished, Source: listingstatus.SourceReview, CreatedAt: now},
|
|
{ListingID: listingID1, EventType: listingstatus.EventOffline, Source: listingstatus.SourceAdmin, CreatedAt: now.Add(time.Minute)},
|
|
{ListingID: listingID2, EventType: listingstatus.EventRented, Source: listingstatus.SourceOrder, CreatedAt: now.Add(2 * time.Minute)},
|
|
{ListingID: 3, EventType: listingstatus.EventPublished, Source: listingstatus.SourceSeller, CreatedAt: now.Add(3 * time.Minute)},
|
|
{ListingID: listingID4, EventType: listingstatus.EventPublished, Source: listingstatus.SourceReview, CreatedAt: now.Add(4 * time.Minute)},
|
|
}
|
|
if err := db.Create(&events).Error; err != nil {
|
|
t.Fatalf("create events error = %v", err)
|
|
}
|
|
|
|
overview, err := NewRepository(db).listingDailyOverview(t.Context(), now, 1)
|
|
if err != nil {
|
|
t.Fatalf("listingDailyOverview() error = %v", err)
|
|
}
|
|
if overview.Today.PublishedCount != 3 || overview.Today.ActiveOfflineCount != 1 || overview.Today.TradeLeaveCount != 1 {
|
|
t.Fatalf("today stats = %#v", overview.Today)
|
|
}
|
|
|
|
statsByChannel := make(map[string]ListingChannelDayStatsDTO)
|
|
for _, item := range overview.TodayChannels {
|
|
statsByChannel[item.SourceChannel] = item
|
|
}
|
|
assertChannelStats(t, statsByChannel["淘宝"], 1, 1, 0)
|
|
assertChannelStats(t, statsByChannel["微信"], 0, 0, 1)
|
|
assertChannelStats(t, statsByChannel[listingChannelWebsite], 1, 0, 0)
|
|
assertChannelStats(t, statsByChannel[listingChannelExternalUnknown], 1, 0, 0)
|
|
}
|
|
|
|
func assertChannelStats(t *testing.T, item ListingChannelDayStatsDTO, published, offline, tradeLeave int64) {
|
|
t.Helper()
|
|
if item.PublishedCount != published || item.ActiveOfflineCount != offline || item.TradeLeaveCount != tradeLeave {
|
|
t.Fatalf("channel %q stats = %#v, want published=%d offline=%d tradeLeave=%d", item.SourceChannel, item, published, offline, tradeLeave)
|
|
}
|
|
}
|
|
|
|
func TestListingDailyOverviewGroupsExternalUploadsByUploader(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
if err := database.AutoMigrateIfMissing(db, &model.ListingStatusEvent{}, &model.ListingUpload{}); err != nil {
|
|
t.Fatalf("AutoMigrate() error = %v", err)
|
|
}
|
|
loc := timeutil.ShanghaiLocation()
|
|
now := time.Date(2026, 7, 25, 12, 0, 0, 0, loc)
|
|
uploaderX := uint64(11)
|
|
uploaderY := uint64(12)
|
|
listingOne := uint64(101)
|
|
listingTwo := uint64(102)
|
|
listingThree := uint64(103)
|
|
uploads := []model.ListingUpload{
|
|
{UploaderName: "小晴", MatchedAdminID: &uploaderX, ListingID: &listingOne, CreatedAt: now},
|
|
{UploaderName: "小晴", MatchedAdminID: &uploaderX, ListingID: &listingTwo, CreatedAt: now.Add(time.Hour)},
|
|
{UploaderName: "小美", MatchedAdminID: &uploaderY, ListingID: &listingThree, CreatedAt: now.Add(2 * time.Hour)},
|
|
{UploaderName: "小晴", MatchedAdminID: &uploaderX, ListingID: &listingOne, CreatedAt: now.AddDate(0, 0, -1)},
|
|
}
|
|
if err := db.Create(&uploads).Error; err != nil {
|
|
t.Fatalf("create uploads error = %v", err)
|
|
}
|
|
|
|
overview, err := NewRepository(db).listingDailyOverview(t.Context(), now, 2)
|
|
if err != nil {
|
|
t.Fatalf("listingDailyOverview() error = %v", err)
|
|
}
|
|
if len(overview.TodayUploaders) != 2 {
|
|
t.Fatalf("today uploaders = %#v, want 2 rows", overview.TodayUploaders)
|
|
}
|
|
counts := make(map[string]int64)
|
|
for _, item := range overview.TodayUploaders {
|
|
counts[item.UploaderName] = item.UploadCount
|
|
}
|
|
if counts["小晴"] != 2 || counts["小美"] != 1 {
|
|
t.Fatalf("today uploader counts = %#v, want 小晴=2, 小美=1", counts)
|
|
}
|
|
if len(overview.UploaderTrend) != 3 {
|
|
t.Fatalf("uploader trend = %#v, want 3 rows", overview.UploaderTrend)
|
|
}
|
|
}
|
|
|
|
func TestTradingDailyOverviewCountsPremiumAndInsurance(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
if err := database.AutoMigrateIfMissing(db, &model.RentalOrder{}); err != nil {
|
|
t.Fatalf("AutoMigrate() error = %v", err)
|
|
}
|
|
loc := timeutil.ShanghaiLocation()
|
|
now := time.Date(2026, 7, 25, 12, 0, 0, 0, loc)
|
|
|
|
orders := []model.RentalOrder{
|
|
// 今日:极品 + 保险8%,成交
|
|
{OrderNo: "T1", ListingID: 1, AccountID: 1, OwnerID: 1, RenterID: 1, Status: "pending_handoff", IsPremium: true, CompensationPlan: "insurance", InsuranceFeeBps: 800, CreatedAt: now},
|
|
// 今日:极品 + 保险4%,成交
|
|
{OrderNo: "T2", ListingID: 2, AccountID: 2, OwnerID: 2, RenterID: 2, Status: "renting", IsPremium: true, CompensationPlan: "insurance", InsuranceFeeBps: 400, CreatedAt: now.Add(time.Hour)},
|
|
// 今日:非极品 + 保险4%,成交
|
|
{OrderNo: "T3", ListingID: 3, AccountID: 3, OwnerID: 3, RenterID: 3, Status: "completed", CompensationPlan: "insurance", InsuranceFeeBps: 400, CreatedAt: now.Add(2 * time.Hour)},
|
|
// 今日:待支付,不计入成交
|
|
{OrderNo: "T4", ListingID: 4, AccountID: 4, OwnerID: 4, RenterID: 4, Status: "pending_payment", IsPremium: true, CompensationPlan: "insurance", InsuranceFeeBps: 400, CreatedAt: now.Add(3 * time.Hour)},
|
|
// 今日:已取消,不计入成交
|
|
{OrderNo: "T5", ListingID: 5, AccountID: 5, OwnerID: 5, RenterID: 5, Status: "cancelled", IsPremium: true, CompensationPlan: "insurance", InsuranceFeeBps: 800, CreatedAt: now.Add(4 * time.Hour)},
|
|
// 昨日:极品 + 保险8%,成交
|
|
{OrderNo: "T6", ListingID: 6, AccountID: 6, OwnerID: 6, RenterID: 6, Status: "completed", IsPremium: true, CompensationPlan: "insurance", InsuranceFeeBps: 800, CreatedAt: now.AddDate(0, 0, -1)},
|
|
// 前日:普通赔付,不计入保险
|
|
{OrderNo: "T7", ListingID: 7, AccountID: 7, OwnerID: 7, RenterID: 7, Status: "completed", CompensationPlan: "normal", CreatedAt: now.AddDate(0, 0, -2)},
|
|
}
|
|
if err := db.Create(&orders).Error; err != nil {
|
|
t.Fatalf("create orders error = %v", err)
|
|
}
|
|
|
|
overview, err := NewRepository(db).tradingDailyOverview(t.Context(), now, 7)
|
|
if err != nil {
|
|
t.Fatalf("tradingDailyOverview() error = %v", err)
|
|
}
|
|
if overview.Days != 7 || len(overview.Trend) != 7 {
|
|
t.Fatalf("trend = %#v, want 7 days", overview.Trend)
|
|
}
|
|
if overview.Today.Date != "2026-07-25" {
|
|
t.Fatalf("today date = %q, want 2026-07-25", overview.Today.Date)
|
|
}
|
|
if overview.Today.PremiumOrderCount != 2 ||
|
|
overview.Today.Insurance8OrderCount != 1 ||
|
|
overview.Today.Insurance4OrderCount != 2 {
|
|
t.Fatalf("today stats = %#v, want premium=2 insurance8=1 insurance4=2", overview.Today)
|
|
}
|
|
|
|
yesterday := overview.Trend[len(overview.Trend)-2]
|
|
if yesterday.PremiumOrderCount != 1 || yesterday.Insurance8OrderCount != 1 || yesterday.Insurance4OrderCount != 0 {
|
|
t.Fatalf("yesterday stats = %#v, want premium=1 insurance8=1 insurance4=0", yesterday)
|
|
}
|
|
}
|