feat: add superadmin wallet view
This commit is contained in:
@@ -294,6 +294,94 @@ func (s *super) GetUser(ctx context.Context, id int64) (*super_dto.UserItem, err
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *super) GetUserWallet(ctx context.Context, userID int64) (*super_dto.SuperWalletResponse, error) {
|
||||
if userID == 0 {
|
||||
return nil, errorx.ErrBadRequest.WithMsg("用户ID不能为空")
|
||||
}
|
||||
|
||||
// 查询用户余额。
|
||||
userTbl, userQuery := models.UserQuery.QueryContext(ctx)
|
||||
u, err := userQuery.Where(userTbl.ID.Eq(userID)).First()
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errorx.ErrRecordNotFound
|
||||
}
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
// 仅返回最近交易记录,避免超管页面加载过重。
|
||||
orderTbl, orderQuery := models.OrderQuery.QueryContext(ctx)
|
||||
orders, err := orderQuery.
|
||||
Where(orderTbl.UserID.Eq(userID), orderTbl.Status.Eq(consts.OrderStatusPaid)).
|
||||
Order(orderTbl.CreatedAt.Desc()).
|
||||
Limit(20).
|
||||
Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
// 补齐订单对应的租户信息。
|
||||
tenantIDMap := make(map[int64]struct{})
|
||||
for _, o := range orders {
|
||||
if o.TenantID > 0 {
|
||||
tenantIDMap[o.TenantID] = struct{}{}
|
||||
}
|
||||
}
|
||||
tenantIDs := make([]int64, 0, len(tenantIDMap))
|
||||
for id := range tenantIDMap {
|
||||
tenantIDs = append(tenantIDs, id)
|
||||
}
|
||||
tenantMap := make(map[int64]*models.Tenant)
|
||||
if len(tenantIDs) > 0 {
|
||||
tenantTbl, tenantQuery := models.TenantQuery.QueryContext(ctx)
|
||||
tenants, err := tenantQuery.Where(tenantTbl.ID.In(tenantIDs...)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
for _, tenant := range tenants {
|
||||
tenantMap[tenant.ID] = tenant
|
||||
}
|
||||
}
|
||||
|
||||
txs := make([]super_dto.SuperWalletTransaction, 0, len(orders))
|
||||
for _, o := range orders {
|
||||
txType := "expense"
|
||||
switch o.Type {
|
||||
case consts.OrderTypeRecharge:
|
||||
txType = "income"
|
||||
case consts.OrderTypeWithdrawal:
|
||||
txType = "expense"
|
||||
case consts.OrderTypeContentPurchase:
|
||||
txType = "expense"
|
||||
}
|
||||
|
||||
tenant := tenantMap[o.TenantID]
|
||||
tenantCode := ""
|
||||
tenantName := ""
|
||||
if tenant != nil {
|
||||
tenantCode = tenant.Code
|
||||
tenantName = tenant.Name
|
||||
}
|
||||
txs = append(txs, super_dto.SuperWalletTransaction{
|
||||
ID: o.ID,
|
||||
OrderType: o.Type,
|
||||
Title: o.Type.Description(),
|
||||
Amount: o.AmountPaid,
|
||||
Type: txType,
|
||||
Date: o.CreatedAt.Format(time.RFC3339),
|
||||
TenantID: o.TenantID,
|
||||
TenantCode: tenantCode,
|
||||
TenantName: tenantName,
|
||||
})
|
||||
}
|
||||
|
||||
return &super_dto.SuperWalletResponse{
|
||||
Balance: u.Balance,
|
||||
BalanceFrozen: u.BalanceFrozen,
|
||||
Transactions: txs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *super) UpdateUserStatus(ctx context.Context, id int64, form *super_dto.UserStatusUpdateForm) error {
|
||||
tbl, q := models.UserQuery.QueryContext(ctx)
|
||||
_, err := q.Where(tbl.ID.Eq(id)).Update(tbl.Status, consts.UserStatus(form.Status))
|
||||
|
||||
Reference in New Issue
Block a user