tenant: admin batch topup

This commit is contained in:
2025-12-19 13:55:20 +08:00
parent 86a1a0a2cc
commit 17d51d5ed2
11 changed files with 771 additions and 3 deletions

View File

@@ -877,6 +877,123 @@ func (s *OrderTestSuite) Test_AdminOrderExportCSV() {
})
}
func (s *OrderTestSuite) Test_AdminBatchTopupUsers() {
Convey("Order.AdminBatchTopupUsers", s.T(), func() {
ctx := s.T().Context()
now := time.Now().UTC()
tenantID := int64(1)
operatorUserID := int64(10)
s.truncate(
ctx,
models.TableNameTenantLedger,
models.TableNameOrderItem,
models.TableNameOrder,
models.TableNameTenantUser,
)
Convey("参数非法应返回错误", func() {
_, err := Order.AdminBatchTopupUsers(ctx, 0, operatorUserID, &dto.AdminBatchTopupForm{}, now)
So(err, ShouldNotBeNil)
_, err = Order.AdminBatchTopupUsers(ctx, tenantID, 0, &dto.AdminBatchTopupForm{}, now)
So(err, ShouldNotBeNil)
_, err = Order.AdminBatchTopupUsers(ctx, tenantID, operatorUserID, nil, now)
So(err, ShouldNotBeNil)
_, err = Order.AdminBatchTopupUsers(ctx, tenantID, operatorUserID, &dto.AdminBatchTopupForm{BatchIdempotencyKey: ""}, now)
So(err, ShouldNotBeNil)
_, err = Order.AdminBatchTopupUsers(ctx, tenantID, operatorUserID, &dto.AdminBatchTopupForm{BatchIdempotencyKey: "b1", Items: nil}, now)
So(err, ShouldNotBeNil)
})
Convey("超过单批次最大条数应返回错误", func() {
items := make([]*dto.AdminBatchTopupItem, 0, 201)
for i := 0; i < 201; i++ {
items = append(items, &dto.AdminBatchTopupItem{UserID: int64(1000 + i), Amount: 1})
}
_, err := Order.AdminBatchTopupUsers(ctx, tenantID, operatorUserID, &dto.AdminBatchTopupForm{
BatchIdempotencyKey: "too_many",
Items: items,
}, now)
So(err, ShouldNotBeNil)
})
Convey("单条参数不合法应只影响该条并返回错误明细", func() {
s.seedTenantUser(ctx, tenantID, 20, 0, 0)
form := &dto.AdminBatchTopupForm{
BatchIdempotencyKey: "batch_invalid_item",
Items: []*dto.AdminBatchTopupItem{
nil,
{UserID: 0, Amount: 100, Reason: "bad_user_id"},
{UserID: 20, Amount: 0, Reason: "bad_amount"},
{UserID: 20, Amount: 100, Reason: "ok"},
},
}
resp, err := Order.AdminBatchTopupUsers(ctx, tenantID, operatorUserID, form, now)
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
So(resp.Total, ShouldEqual, 4)
So(resp.Success, ShouldEqual, 1)
So(resp.Failed, ShouldEqual, 3)
So(len(resp.Items), ShouldEqual, 4)
So(resp.Items[0].OK, ShouldBeFalse)
So(resp.Items[1].OK, ShouldBeFalse)
So(resp.Items[2].OK, ShouldBeFalse)
So(resp.Items[3].OK, ShouldBeTrue)
})
Convey("部分成功应返回明细结果且成功入账", func() {
// seed 2 个成员1 个非成员
s.seedTenantUser(ctx, tenantID, 20, 0, 0)
s.seedTenantUser(ctx, tenantID, 21, 0, 0)
form := &dto.AdminBatchTopupForm{
BatchIdempotencyKey: "batch_001",
Items: []*dto.AdminBatchTopupItem{
{UserID: 20, Amount: 100, Reason: "a"},
{UserID: 999, Amount: 100, Reason: "not_member"},
{UserID: 21, Amount: 200, Reason: "b"},
},
}
resp, err := Order.AdminBatchTopupUsers(ctx, tenantID, operatorUserID, form, now)
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
So(resp.Total, ShouldEqual, 3)
So(resp.Success, ShouldEqual, 2)
So(resp.Failed, ShouldEqual, 1)
So(len(resp.Items), ShouldEqual, 3)
So(resp.Items[0].OK, ShouldBeTrue)
So(resp.Items[0].OrderID, ShouldBeGreaterThan, 0)
So(resp.Items[1].OK, ShouldBeFalse)
So(resp.Items[2].OK, ShouldBeTrue)
var tu20 models.TenantUser
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, int64(20)).First(&tu20).Error, ShouldBeNil)
So(tu20.Balance, ShouldEqual, 100)
var tu21 models.TenantUser
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, int64(21)).First(&tu21).Error, ShouldBeNil)
So(tu21.Balance, ShouldEqual, 200)
Convey("同一批次重复调用应幂等,不重复入账", func() {
resp2, err := Order.AdminBatchTopupUsers(ctx, tenantID, operatorUserID, form, now.Add(time.Second))
So(err, ShouldBeNil)
So(resp2.Success, ShouldEqual, 2)
var tu20b models.TenantUser
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, int64(20)).First(&tu20b).Error, ShouldBeNil)
So(tu20b.Balance, ShouldEqual, 100)
var tu21b models.TenantUser
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, int64(21)).First(&tu21b).Error, ShouldBeNil)
So(tu21b.Balance, ShouldEqual, 200)
})
})
})
}
func (s *OrderTestSuite) Test_AdminRefundOrder() {
Convey("Order.AdminRefundOrder", s.T(), func() {
ctx := s.T().Context()