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。
|
||||
|
||||
Reference in New Issue
Block a user