feat: enhance order processing and tenant management services

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
2026-02-08 18:30:42 +08:00
parent 9bf3a87b32
commit 590662964a
4 changed files with 183 additions and 24 deletions

View File

@@ -594,6 +594,9 @@ func (s *common) GetAssetURL(objectKey string) string {
if objectKey == "" {
return ""
}
if strings.HasPrefix(objectKey, "http://") || strings.HasPrefix(objectKey, "https://") {
return objectKey
}
url, _ := s.storage.SignURL("GET", objectKey, 1*time.Hour)
return url

View File

@@ -263,12 +263,35 @@ func (s *order) payWithBalance(ctx context.Context, o *models.Order) (*transacti
return &transaction_dto.OrderPayResponse{Status: string(consts.OrderStatusPaid)}, nil
}
func (s *order) settleRechargeOrder(ctx context.Context, order *models.Order) error {
func (s *order) settleRechargeOrder(ctx context.Context, tx *models.Query, order *models.Order) error {
if order == nil {
return errorx.ErrInvalidParameter.WithMsg("充值订单不存在")
}
if tx == nil {
return errorx.ErrInvalidParameter.WithMsg("事务上下文缺失")
}
return s.settleOrder(ctx, order, "recharge")
_, err := tx.User.WithContext(ctx).
Where(tx.User.ID.Eq(order.UserID)).
Update(tx.User.Balance, gorm.Expr("balance + ?", order.AmountPaid))
if err != nil {
return err
}
now := time.Now()
info, err := tx.Order.WithContext(ctx).Where(tx.Order.ID.Eq(order.ID)).Updates(&models.Order{
Status: consts.OrderStatusPaid,
PaidAt: now,
UpdatedAt: now,
})
if err != nil {
return err
}
if info.RowsAffected == 0 {
return errorx.ErrRecordNotFound.WithMsg("充值订单不存在")
}
return nil
}
func (s *order) settleOrder(ctx context.Context, o *models.Order, method string) error {
@@ -314,22 +337,36 @@ func (s *order) settleOrder(ctx context.Context, o *models.Order, method string)
// 3. Grant Content Access
items, _ := tx.OrderItem.WithContext(ctx).Where(tx.OrderItem.OrderID.Eq(o.ID)).Find()
for _, item := range items {
// Check if access already exists (idempotency)
exists, _ := tx.ContentAccess.WithContext(ctx).
Where(tx.ContentAccess.UserID.Eq(o.UserID), tx.ContentAccess.ContentID.Eq(item.ContentID)).
Exists()
if exists {
tblAccess, qAccess := tx.ContentAccess.QueryContext(ctx)
existingAccess, err := qAccess.Where(
tblAccess.UserID.Eq(o.UserID),
tblAccess.ContentID.Eq(item.ContentID),
).First()
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
access := &models.ContentAccess{
TenantID: item.TenantID,
UserID: o.UserID,
ContentID: item.ContentID,
OrderID: o.ID,
Status: consts.ContentAccessStatusActive,
}
if err := qAccess.Create(access); err != nil {
return err
}
continue
}
access := &models.ContentAccess{
_, err = qAccess.Where(tblAccess.ID.Eq(existingAccess.ID)).Updates(&models.ContentAccess{
TenantID: item.TenantID,
UserID: o.UserID,
ContentID: item.ContentID,
OrderID: o.ID,
Status: consts.ContentAccessStatusActive,
}
if err := tx.ContentAccess.WithContext(ctx).Save(access); err != nil {
RevokedAt: time.Time{},
UpdatedAt: time.Now(),
})
if err != nil {
return err
}
}