6157b2c571
* 修复(#23): 修复用户订阅列表未回显用户级限速 Co-authored-by: multica-agent <github@multica.ai> * 修复(#23): 移除 PR 草稿文件 Co-authored-by: multica-agent <github@multica.ai> --------- Co-authored-by: multica-agent <github@multica.ai>
83 lines
2.4 KiB
Go
83 lines
2.4 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/perfect-panel/server/internal/model/group"
|
|
"github.com/perfect-panel/server/internal/model/user"
|
|
"github.com/perfect-panel/server/internal/svc"
|
|
"github.com/perfect-panel/server/internal/types"
|
|
"github.com/perfect-panel/server/pkg/logger"
|
|
"github.com/perfect-panel/server/pkg/tool"
|
|
"github.com/perfect-panel/server/pkg/xerr"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type GetUserSubscribeLogic struct {
|
|
logger.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// Get user subcribe
|
|
func NewGetUserSubscribeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserSubscribeLogic {
|
|
return &GetUserSubscribeLogic{
|
|
Logger: logger.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *GetUserSubscribeLogic) GetUserSubscribe(req *types.GetUserSubscribeListRequest) (resp *types.GetUserSubscribeListResponse, err error) {
|
|
data, err := l.svcCtx.UserModel.QueryUserSubscribe(l.ctx, req.UserId, 0, 1, 2, 3, 4)
|
|
if err != nil {
|
|
l.Errorw("[GetUserSubscribeLogs] Get User Subscribe Error:", logger.Field("err", err.Error()))
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "Get User Subscribe Error")
|
|
}
|
|
|
|
resp = &types.GetUserSubscribeListResponse{
|
|
List: make([]types.UserSubscribe, 0),
|
|
Total: int64(len(data)),
|
|
}
|
|
|
|
// 收集所有 node_group_id,批量查分组名
|
|
groupIdSet := make(map[int64]struct{})
|
|
for _, item := range data {
|
|
if item.NodeGroupId > 0 {
|
|
groupIdSet[item.NodeGroupId] = struct{}{}
|
|
}
|
|
}
|
|
groupNames := make(map[int64]string)
|
|
if len(groupIdSet) > 0 {
|
|
ids := make([]int64, 0, len(groupIdSet))
|
|
for id := range groupIdSet {
|
|
ids = append(ids, id)
|
|
}
|
|
var groups []group.NodeGroup
|
|
if err := l.svcCtx.DB.WithContext(l.ctx).Where("id IN ?", ids).Find(&groups).Error; err == nil {
|
|
for _, g := range groups {
|
|
groupNames[g.Id] = g.Name
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, item := range data {
|
|
sub := buildUserSubscribeListItem(item, groupNames[item.NodeGroupId])
|
|
resp.List = append(resp.List, sub)
|
|
}
|
|
return
|
|
}
|
|
|
|
func buildUserSubscribeListItem(item *user.SubscribeDetails, groupName string) types.UserSubscribe {
|
|
var sub types.UserSubscribe
|
|
tool.DeepCopy(&sub, item)
|
|
sub.Short, _ = tool.FixedUniqueString(item.Token, 8, "")
|
|
sub.NodeGroupId = item.NodeGroupId
|
|
sub.NodeGroupName = groupName
|
|
if item.TrafficLimit != nil && *item.TrafficLimit != "" {
|
|
_ = json.Unmarshal([]byte(*item.TrafficLimit), &sub.TrafficLimit)
|
|
}
|
|
return sub
|
|
}
|