tenant: admin batch topup
This commit is contained in:
@@ -180,6 +180,131 @@ func (s *order) AdminOrderExportCSV(ctx context.Context, tenantID int64, filter
|
||||
}, nil
|
||||
}
|
||||
|
||||
// AdminBatchTopupUsers 租户管理员批量为成员充值(逐条幂等,允许部分失败)。
|
||||
func (s *order) AdminBatchTopupUsers(
|
||||
ctx context.Context,
|
||||
tenantID, operatorUserID int64,
|
||||
form *dto.AdminBatchTopupForm,
|
||||
now time.Time,
|
||||
) (*dto.AdminBatchTopupResponse, error) {
|
||||
if tenantID <= 0 || operatorUserID <= 0 {
|
||||
return nil, errorx.ErrInvalidParameter.WithMsg("tenant_id/operator_user_id must be > 0")
|
||||
}
|
||||
if form == nil {
|
||||
return nil, errorx.ErrInvalidParameter.WithMsg("form is required")
|
||||
}
|
||||
if strings.TrimSpace(form.BatchIdempotencyKey) == "" {
|
||||
return nil, errorx.ErrInvalidParameter.WithMsg("batch_idempotency_key is required")
|
||||
}
|
||||
if len(form.Items) == 0 {
|
||||
return nil, errorx.ErrInvalidParameter.WithMsg("items is required")
|
||||
}
|
||||
if now.IsZero() {
|
||||
now = time.Now()
|
||||
}
|
||||
|
||||
// 批量充值属于高敏感操作:限制单次条数,避免拖垮系统。
|
||||
const maxItems = 200
|
||||
if len(form.Items) > maxItems {
|
||||
return nil, errorx.ErrInvalidParameter.WithMsg("items too many")
|
||||
}
|
||||
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"tenant_id": tenantID,
|
||||
"operator_user_id": operatorUserID,
|
||||
"batch_idempotency_key": form.BatchIdempotencyKey,
|
||||
"total": len(form.Items),
|
||||
}).Info("services.order.admin.batch_topup")
|
||||
|
||||
out := &dto.AdminBatchTopupResponse{
|
||||
Total: len(form.Items),
|
||||
Items: make([]*dto.AdminBatchTopupResultItem, 0, len(form.Items)),
|
||||
}
|
||||
|
||||
for idx, item := range form.Items {
|
||||
if item == nil {
|
||||
out.Failed++
|
||||
out.Items = append(out.Items, &dto.AdminBatchTopupResultItem{
|
||||
OK: false,
|
||||
ErrorCode: int(errorx.CodeInvalidParameter),
|
||||
ErrorMessage: "item is nil",
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
idemKey := strings.TrimSpace(item.IdempotencyKey)
|
||||
if idemKey == "" {
|
||||
// 关键语义:为空时用批次幂等键派生,确保批次重试不会重复入账。
|
||||
idemKey = fmt.Sprintf("batch_topup:%s:%d:%d", strings.TrimSpace(form.BatchIdempotencyKey), item.UserID, idx)
|
||||
}
|
||||
|
||||
resultItem := &dto.AdminBatchTopupResultItem{
|
||||
UserID: item.UserID,
|
||||
Amount: item.Amount,
|
||||
IdempotencyKey: idemKey,
|
||||
OK: false,
|
||||
}
|
||||
|
||||
// 单条参数校验:失败只影响该条,不影响整批(便于运营侧修正后重试)。
|
||||
if item.UserID <= 0 {
|
||||
resultItem.ErrorCode = int(errorx.CodeInvalidParameter)
|
||||
resultItem.ErrorMessage = "user_id must be > 0"
|
||||
out.Failed++
|
||||
out.Items = append(out.Items, resultItem)
|
||||
continue
|
||||
}
|
||||
if item.Amount <= 0 {
|
||||
resultItem.ErrorCode = int(errorx.CodeInvalidParameter)
|
||||
resultItem.ErrorMessage = "amount must be > 0"
|
||||
out.Failed++
|
||||
out.Items = append(out.Items, resultItem)
|
||||
continue
|
||||
}
|
||||
|
||||
// 逐条调用单用户充值逻辑:保持“订单 + 账本 + 余额”一致性与幂等语义一致。
|
||||
orderModel, err := s.AdminTopupUser(
|
||||
ctx,
|
||||
tenantID,
|
||||
operatorUserID,
|
||||
item.UserID,
|
||||
item.Amount,
|
||||
idemKey,
|
||||
item.Reason,
|
||||
now,
|
||||
)
|
||||
if err != nil {
|
||||
// 错误收敛为可展示结构:便于前端逐条展示与导出审计。
|
||||
var appErr *errorx.AppError
|
||||
if errors.As(err, &appErr) {
|
||||
resultItem.ErrorCode = int(appErr.Code)
|
||||
resultItem.ErrorMessage = appErr.Message
|
||||
} else {
|
||||
resultItem.ErrorCode = int(errorx.CodeInternalError)
|
||||
resultItem.ErrorMessage = err.Error()
|
||||
}
|
||||
out.Failed++
|
||||
out.Items = append(out.Items, resultItem)
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"tenant_id": tenantID,
|
||||
"operator_user_id": operatorUserID,
|
||||
"user_id": item.UserID,
|
||||
"amount": item.Amount,
|
||||
"idempotency_key": idemKey,
|
||||
}).WithError(err).Warn("services.order.admin.batch_topup.item_failed")
|
||||
continue
|
||||
}
|
||||
|
||||
resultItem.OK = true
|
||||
if orderModel != nil {
|
||||
resultItem.OrderID = orderModel.ID
|
||||
}
|
||||
out.Success++
|
||||
out.Items = append(out.Items, resultItem)
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// PurchaseOrderSnapshot 为“内容购买订单”的下单快照(用于历史展示与争议审计)。
|
||||
type PurchaseOrderSnapshot struct {
|
||||
// ContentID 内容ID。
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -42,7 +42,8 @@ func (t *tenant) AdminTenantUsersPage(ctx context.Context, tenantID int64, filte
|
||||
conds = append(conds, tbl.UserID.Eq(*filter.UserID))
|
||||
}
|
||||
if filter.Role != nil && *filter.Role != "" {
|
||||
conds = append(conds, tbl.Role.Contains(string(*filter.Role)))
|
||||
// role 字段为 PostgreSQL text[]:使用数组参数才能正确生成 `@> '{"tenant_admin"}'` 语义。
|
||||
conds = append(conds, tbl.Role.Contains(types.NewArray([]consts.TenantUserRole{*filter.Role})))
|
||||
}
|
||||
if filter.Status != nil && *filter.Status != "" {
|
||||
conds = append(conds, tbl.Status.Eq(*filter.Status))
|
||||
@@ -225,6 +226,19 @@ func (t *tenant) Pager(ctx context.Context, filter *dto.TenantFilter) (*requests
|
||||
}
|
||||
|
||||
func (t *tenant) TenantUserCountMapping(ctx context.Context, tenantIds []int64) (map[int64]int64, error) {
|
||||
// 关键语义:返回值必须包含入参中的所有 tenant_id。
|
||||
// 即便该租户当前没有成员,也应返回 count=0,便于调用方直接取值而无需额外补全逻辑。
|
||||
result := make(map[int64]int64, len(tenantIds))
|
||||
for _, id := range tenantIds {
|
||||
if id <= 0 {
|
||||
continue
|
||||
}
|
||||
result[id] = 0
|
||||
}
|
||||
if len(result) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
tbl, query := models.TenantUserQuery.QueryContext(ctx)
|
||||
|
||||
var items []struct {
|
||||
@@ -243,7 +257,6 @@ func (t *tenant) TenantUserCountMapping(ctx context.Context, tenantIds []int64)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(map[int64]int64)
|
||||
for _, item := range items {
|
||||
result[item.TenantID] = item.Count
|
||||
}
|
||||
@@ -252,6 +265,19 @@ func (t *tenant) TenantUserCountMapping(ctx context.Context, tenantIds []int64)
|
||||
|
||||
// TenantUserBalanceMapping
|
||||
func (t *tenant) TenantUserBalanceMapping(ctx context.Context, tenantIds []int64) (map[int64]int64, error) {
|
||||
// 关键语义:返回值必须包含入参中的所有 tenant_id。
|
||||
// 即便该租户当前没有成员,也应返回 balance=0,保持调用方逻辑一致。
|
||||
result := make(map[int64]int64, len(tenantIds))
|
||||
for _, id := range tenantIds {
|
||||
if id <= 0 {
|
||||
continue
|
||||
}
|
||||
result[id] = 0
|
||||
}
|
||||
if len(result) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
tbl, query := models.TenantUserQuery.QueryContext(ctx)
|
||||
|
||||
var items []struct {
|
||||
@@ -270,7 +296,6 @@ func (t *tenant) TenantUserBalanceMapping(ctx context.Context, tenantIds []int64
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(map[int64]int64)
|
||||
for _, item := range items {
|
||||
result[item.TenantID] = item.Balance
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user