You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.5 KiB
65 lines
1.5 KiB
package ai
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/youruser/base/internal/svc"
|
|
"github.com/youruser/base/internal/types"
|
|
"github.com/youruser/base/model"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type AiQuotaListLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewAiQuotaListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AiQuotaListLogic {
|
|
return &AiQuotaListLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *AiQuotaListLogic) AiQuotaList(req *types.AIQuotaListRequest) (resp *types.AIQuotaListResponse, err error) {
|
|
quotas, total, err := model.AIUserQuotaFindList(l.ctx, l.svcCtx.DB, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Build user ID list for batch username lookup
|
|
userIds := make([]int64, len(quotas))
|
|
for i, q := range quotas {
|
|
userIds[i] = q.UserId
|
|
}
|
|
|
|
// Lookup usernames
|
|
usernameMap := make(map[int64]string)
|
|
if len(userIds) > 0 {
|
|
var users []model.User
|
|
l.svcCtx.DB.WithContext(l.ctx).Where("id IN ?", userIds).Select("id, username").Find(&users)
|
|
for _, u := range users {
|
|
usernameMap[u.Id] = u.Username
|
|
}
|
|
}
|
|
|
|
list := make([]types.AIQuotaUserInfo, len(quotas))
|
|
for i, q := range quotas {
|
|
list[i] = types.AIQuotaUserInfo{
|
|
UserId: q.UserId,
|
|
Username: usernameMap[q.UserId],
|
|
Balance: q.Balance,
|
|
TotalRecharged: q.TotalRecharged,
|
|
TotalConsumed: q.TotalConsumed,
|
|
FrozenAmount: q.FrozenAmount,
|
|
}
|
|
}
|
|
|
|
return &types.AIQuotaListResponse{
|
|
List: list,
|
|
Total: total,
|
|
}, nil
|
|
}
|
|
|