feat: 移除“租户管理员为用户充值 / 每租户一套余额”能力:余额统一为全局用户余额
This commit is contained in:
@@ -198,131 +198,6 @@ func (s *order) AdminOrderExportCSV(
|
||||
}, 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
|
||||
}
|
||||
|
||||
// PurchaseContentParams 定义“租户内使用余额购买内容”的入参。
|
||||
type PurchaseContentParams struct {
|
||||
// TenantID 租户 ID(多租户隔离范围)。
|
||||
@@ -349,7 +224,7 @@ type PurchaseContentResult struct {
|
||||
AmountPaid int64
|
||||
}
|
||||
|
||||
// order 提供订单域能力(购买、充值、退款、查询等)。
|
||||
// order 提供订单域能力(购买、退款、查询等)。
|
||||
//
|
||||
// @provider
|
||||
type order struct {
|
||||
@@ -394,123 +269,6 @@ func (s *order) enqueueOrderRefundJob(args jobs_args.OrderRefundJob) error {
|
||||
return s.job.Add(args)
|
||||
}
|
||||
|
||||
// AdminTopupUser 租户管理员给租户成员充值(增加该租户下的可用余额)。
|
||||
func (s *order) AdminTopupUser(
|
||||
ctx context.Context,
|
||||
tenantID, operatorUserID, targetUserID, amount int64,
|
||||
idempotencyKey, reason string,
|
||||
now time.Time,
|
||||
) (*models.Order, error) {
|
||||
if tenantID <= 0 || operatorUserID <= 0 || targetUserID <= 0 {
|
||||
return nil, errorx.ErrInvalidParameter.WithMsg("tenant_id/operator_user_id/target_user_id must be > 0")
|
||||
}
|
||||
if amount <= 0 {
|
||||
return nil, errorx.ErrInvalidParameter.WithMsg("amount must be > 0")
|
||||
}
|
||||
if now.IsZero() {
|
||||
now = time.Now()
|
||||
}
|
||||
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"tenant_id": tenantID,
|
||||
"operator_user": operatorUserID,
|
||||
"target_user": targetUserID,
|
||||
"amount": amount,
|
||||
"idempotency_key": idempotencyKey,
|
||||
}).Info("services.order.admin.topup_user")
|
||||
|
||||
var out models.Order
|
||||
|
||||
err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
// 关键前置条件:目标用户必须属于该租户(同时加行锁,避免并发余额写入冲突)。
|
||||
var tu models.TenantUser
|
||||
if err := tx.
|
||||
Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("tenant_id = ? AND user_id = ?", tenantID, targetUserID).
|
||||
First(&tu).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errorx.ErrPreconditionFailed.WithMsg("目标用户不属于该租户")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 充值幂等:按 orders(tenant_id,user_id,idempotency_key) 去重,避免重复入账。
|
||||
if idempotencyKey != "" {
|
||||
var existing models.Order
|
||||
if err := tx.Where(
|
||||
"tenant_id = ? AND user_id = ? AND idempotency_key = ?",
|
||||
tenantID, targetUserID, idempotencyKey,
|
||||
).First(&existing).Error; err == nil {
|
||||
out = existing
|
||||
return nil
|
||||
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 先落订单(paid),再写入账本(credit_topup),确保“订单可追溯 + 账本可对账”。
|
||||
snapshot := newOrderSnapshot(consts.OrderTypeTopup, &fields.OrdersTopupSnapshot{
|
||||
OperatorUserID: operatorUserID,
|
||||
TargetUserID: targetUserID,
|
||||
Amount: amount,
|
||||
Currency: consts.CurrencyCNY,
|
||||
Reason: reason,
|
||||
IdempotencyKey: idempotencyKey,
|
||||
TopupAt: now,
|
||||
})
|
||||
orderModel := models.Order{
|
||||
TenantID: tenantID,
|
||||
UserID: targetUserID,
|
||||
Type: consts.OrderTypeTopup,
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountOriginal: amount,
|
||||
AmountDiscount: 0,
|
||||
AmountPaid: amount,
|
||||
Snapshot: snapshot,
|
||||
IdempotencyKey: idempotencyKey,
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := tx.Create(&orderModel).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 账本幂等键固定使用 topup:<orderID>,保证同一订单不会重复入账。
|
||||
ledgerKey := fmt.Sprintf("topup:%d", orderModel.ID)
|
||||
remark := reason
|
||||
if remark == "" {
|
||||
remark = fmt.Sprintf("topup by tenant_admin:%d", operatorUserID)
|
||||
}
|
||||
if _, err := s.ledger.CreditTopupTx(ctx, tx, tenantID, operatorUserID, targetUserID, orderModel.ID, amount, ledgerKey, remark, now); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
out = orderModel
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"tenant_id": tenantID,
|
||||
"operator_user": operatorUserID,
|
||||
"target_user": targetUserID,
|
||||
"amount": amount,
|
||||
"idempotency_key": idempotencyKey,
|
||||
}).WithError(err).Warn("services.order.admin.topup_user.failed")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"tenant_id": tenantID,
|
||||
"target_user": targetUserID,
|
||||
"order_id": out.ID,
|
||||
"amount": amount,
|
||||
}).Info("services.order.admin.topup_user.ok")
|
||||
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
// MyOrderPage 分页查询当前用户在租户内的订单。
|
||||
func (s *order) MyOrderPage(
|
||||
ctx context.Context,
|
||||
|
||||
Reference in New Issue
Block a user