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 := db.AutoMigrate(&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 := db.AutoMigrate(&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) } }