更新订单管理记录更多事情
This commit is contained in:
@@ -29,9 +29,10 @@ type AdminWriteService struct {
|
||||
events *repo.EventRepository
|
||||
apiKeys *repo.APIKeyRepository
|
||||
tokens *repo.TokenRepository
|
||||
orders *repo.OrderRepository
|
||||
}
|
||||
|
||||
func NewAdminWriteService(users *repo.UserRepository, showcase *repo.ShowcaseRepository, models *repo.ModelRepository, events *repo.EventRepository, apiKeys *repo.APIKeyRepository, tokens *repo.TokenRepository) *AdminWriteService {
|
||||
func NewAdminWriteService(users *repo.UserRepository, showcase *repo.ShowcaseRepository, models *repo.ModelRepository, events *repo.EventRepository, apiKeys *repo.APIKeyRepository, tokens *repo.TokenRepository, orders *repo.OrderRepository) *AdminWriteService {
|
||||
return &AdminWriteService{
|
||||
users: users,
|
||||
showcase: showcase,
|
||||
@@ -39,6 +40,7 @@ func NewAdminWriteService(users *repo.UserRepository, showcase *repo.ShowcaseRep
|
||||
events: events,
|
||||
apiKeys: apiKeys,
|
||||
tokens: tokens,
|
||||
orders: orders,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,6 +226,9 @@ func (s *AdminWriteService) AdjustUserCredits(ctx context.Context, userID string
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if delta != 0 {
|
||||
RecordCreditOrder(ctx, s.orders, userID, delta, "admin", "管理员调整余额")
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
@@ -234,6 +239,13 @@ func (s *AdminWriteService) SetUserCredits(ctx context.Context, userID string, v
|
||||
if value < 0 {
|
||||
value = 0
|
||||
}
|
||||
before, err := s.users.GetByID(ctx, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
user, err := s.users.SetCredits(ctx, userID, value)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -241,6 +253,9 @@ func (s *AdminWriteService) SetUserCredits(ctx context.Context, userID string, v
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if delta := user.Credits - before.Credits; delta != 0 {
|
||||
RecordCreditOrder(ctx, s.orders, userID, delta, "admin", "管理员设置余额")
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -15,13 +15,15 @@ type CDKService struct {
|
||||
cdks *repo.CDKRepository
|
||||
users *repo.UserRepository
|
||||
settings *repo.SiteSettingRepository
|
||||
orders *repo.OrderRepository
|
||||
}
|
||||
|
||||
func NewCDKService(cdks *repo.CDKRepository, users *repo.UserRepository, settings *repo.SiteSettingRepository) *CDKService {
|
||||
func NewCDKService(cdks *repo.CDKRepository, users *repo.UserRepository, settings *repo.SiteSettingRepository, orders *repo.OrderRepository) *CDKService {
|
||||
return &CDKService{
|
||||
cdks: cdks,
|
||||
users: users,
|
||||
settings: settings,
|
||||
orders: orders,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,6 +162,7 @@ func (s *CDKService) Redeem(ctx context.Context, userID, code string) (map[strin
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
RecordCreditOrder(ctx, s.orders, userID, float64(item.Amount), "cdk", "兑换码 "+item.Code)
|
||||
|
||||
return map[string]any{
|
||||
"amount": item.Amount,
|
||||
|
||||
@@ -255,7 +255,7 @@ func (s *PaymentService) ListByUser(ctx context.Context, userID, status, query s
|
||||
|
||||
// ListAll — admin order list. A search query also matches 用户名/邮箱: resolve
|
||||
// the term to user ids first so "张三" finds that user's orders.
|
||||
func (s *PaymentService) ListAll(ctx context.Context, status, query string, limit, offset int) ([]model.Order, int64, error) {
|
||||
func (s *PaymentService) ListAll(ctx context.Context, status, source, query string, limit, offset int) ([]model.Order, int64, error) {
|
||||
var userIDs []string
|
||||
if term := strings.ToLower(strings.TrimSpace(query)); term != "" {
|
||||
if users, err := s.users.List(ctx); err == nil {
|
||||
@@ -268,7 +268,36 @@ func (s *PaymentService) ListAll(ctx context.Context, status, query string, limi
|
||||
}
|
||||
}
|
||||
}
|
||||
return s.orders.List(ctx, status, query, userIDs, limit, offset)
|
||||
return s.orders.List(ctx, status, source, query, userIDs, limit, offset)
|
||||
}
|
||||
|
||||
// RecordCreditOrder persists a synthetic already-paid order for a credit grant
|
||||
// that didn't go through epay — admin manual adjustments (source="admin") and
|
||||
// CDK redemptions (source="cdk") — so 订单管理 lists them alongside recharges.
|
||||
// Best-effort: a failed insert must never fail the credit operation itself.
|
||||
func RecordCreditOrder(ctx context.Context, orders *repo.OrderRepository, userID string, points float64, source, remark string) {
|
||||
if orders == nil {
|
||||
return
|
||||
}
|
||||
prefix := "X"
|
||||
switch source {
|
||||
case "admin":
|
||||
prefix = "M"
|
||||
case "cdk":
|
||||
prefix = "C"
|
||||
}
|
||||
now := time.Now()
|
||||
_ = orders.Create(ctx, &model.Order{
|
||||
ID: prefix + strconv.FormatInt(now.Unix(), 10) + randomUpper(6),
|
||||
UserID: userID,
|
||||
Points: int(math.Round(points)),
|
||||
PayType: source,
|
||||
Status: "paid",
|
||||
Source: source,
|
||||
Remark: remark,
|
||||
ExpiresAt: now,
|
||||
PaidAt: &now,
|
||||
})
|
||||
}
|
||||
|
||||
// UserNames maps user id → display name (name, else email, else id) so the admin
|
||||
|
||||
Reference in New Issue
Block a user