feat: add creator report overview export

This commit is contained in:
2026-01-13 11:44:29 +08:00
parent 4e35dff05c
commit 19b15bf20a
9 changed files with 1010 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"testing"
"time"
"quyun/v2/app/commands/testx"
creator_dto "quyun/v2/app/http/v1/dto"
@@ -384,3 +385,128 @@ func (s *CreatorTestSuite) Test_Refund() {
})
})
}
func (s *CreatorTestSuite) Test_ReportOverview() {
Convey("ReportOverview", s.T(), func() {
ctx := s.T().Context()
database.Truncate(ctx, s.DB,
models.TableNameTenant,
models.TableNameUser,
models.TableNameContent,
models.TableNameOrder,
)
owner := &models.User{Username: "owner_r", Phone: "13900001011"}
models.UserQuery.WithContext(ctx).Create(owner)
tenant := &models.Tenant{
Name: "Tenant Report",
UserID: owner.ID,
Status: consts.TenantStatusVerified,
}
models.TenantQuery.WithContext(ctx).Create(tenant)
models.ContentQuery.WithContext(ctx).Create(&models.Content{
TenantID: tenant.ID,
UserID: owner.ID,
Title: "Content A",
Status: consts.ContentStatusPublished,
Views: 100,
})
now := time.Now()
inRangePaidAt := now.Add(-12 * time.Hour)
outRangePaidAt := now.Add(-10 * 24 * time.Hour)
models.OrderQuery.WithContext(ctx).Create(
&models.Order{
TenantID: tenant.ID,
UserID: owner.ID,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusPaid,
AmountPaid: 1000,
PaidAt: inRangePaidAt,
},
&models.Order{
TenantID: tenant.ID,
UserID: owner.ID,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusPaid,
AmountPaid: 2000,
PaidAt: outRangePaidAt,
},
&models.Order{
TenantID: tenant.ID,
UserID: owner.ID,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusRefunded,
AmountPaid: 500,
UpdatedAt: now.Add(-6 * time.Hour),
},
)
start := now.Add(-24 * time.Hour).Format(time.RFC3339)
end := now.Format(time.RFC3339)
report, err := Creator.ReportOverview(ctx, tenant.ID, owner.ID, &creator_dto.ReportOverviewFilter{
StartAt: &start,
EndAt: &end,
})
So(err, ShouldBeNil)
So(report.Summary.TotalViews, ShouldEqual, 100)
So(report.Summary.PaidOrders, ShouldEqual, 1)
So(report.Summary.PaidAmount, ShouldEqual, 10.0)
So(report.Summary.RefundOrders, ShouldEqual, 1)
So(report.Summary.RefundAmount, ShouldEqual, 5.0)
var paidSum, refundSum int64
for _, item := range report.Items {
paidSum += item.PaidOrders
refundSum += item.RefundOrders
}
So(paidSum, ShouldEqual, report.Summary.PaidOrders)
So(refundSum, ShouldEqual, report.Summary.RefundOrders)
})
}
func (s *CreatorTestSuite) Test_ExportReport() {
Convey("ExportReport", s.T(), func() {
ctx := s.T().Context()
database.Truncate(ctx, s.DB,
models.TableNameTenant,
models.TableNameUser,
models.TableNameContent,
models.TableNameOrder,
)
owner := &models.User{Username: "owner_e", Phone: "13900001012"}
models.UserQuery.WithContext(ctx).Create(owner)
tenant := &models.Tenant{
Name: "Tenant Export",
UserID: owner.ID,
Status: consts.TenantStatusVerified,
}
models.TenantQuery.WithContext(ctx).Create(tenant)
models.ContentQuery.WithContext(ctx).Create(&models.Content{
TenantID: tenant.ID,
UserID: owner.ID,
Title: "Content Export",
Status: consts.ContentStatusPublished,
Views: 10,
})
models.OrderQuery.WithContext(ctx).Create(&models.Order{
TenantID: tenant.ID,
UserID: owner.ID,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusPaid,
AmountPaid: 1200,
PaidAt: time.Now().Add(-2 * time.Hour),
})
form := &creator_dto.ReportExportForm{Format: "csv"}
resp, err := Creator.ExportReport(ctx, tenant.ID, owner.ID, form)
So(err, ShouldBeNil)
So(resp.Filename, ShouldNotBeBlank)
So(resp.Content, ShouldContainSubstring, "date,paid_orders,paid_amount,refund_orders,refund_amount")
})
}