107 lines
2.4 KiB
Go
107 lines
2.4 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
user "lone-services/rpc/user/pb"
|
|
"lone-services/services/percutaneous/internal/dao"
|
|
"lone-services/services/percutaneous/internal/model"
|
|
"lone-services/services/percutaneous/validator"
|
|
"strconv"
|
|
|
|
"lone-services/rpc/percutaneous/pb"
|
|
"lone-services/services/percutaneous/internal/svc"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type UserItemsLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
func NewUserItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserItemsLogic {
|
|
return &UserItemsLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *UserItemsLogic) UserItems(in *percutaneous.UserItemsRequest) (*percutaneous.Response, error) {
|
|
var v validator.GroupItemsValidator
|
|
if fail := l.checkParams(in, &v); fail != nil {
|
|
return fail, nil
|
|
}
|
|
adminInfo := utils.GetUserFromCtx(l.ctx)
|
|
if adminInfo.ID < utils.NumberOne {
|
|
return l.fail(utils.ErrorNoLoginInfo)
|
|
}
|
|
|
|
w := modelbase.Params{
|
|
Eq: map[string]string{
|
|
"host_user_id": strconv.FormatInt(adminInfo.ID, utils.NumberTen),
|
|
"host_code": in.HostCode,
|
|
"status": utils.StringStatusOk,
|
|
},
|
|
Page: int(in.Page),
|
|
Size: int(in.Size),
|
|
Order: "id desc",
|
|
}
|
|
var info []dao.GroupInfo
|
|
items, err := model.GroupModel{}.Init().Page(w, &info)
|
|
if err != nil {
|
|
logx.Errorf("get group err: %v", err)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
var ids []int64
|
|
for _, item := range info {
|
|
ids = append(ids, item.UserId)
|
|
}
|
|
l.setUserItems(&info, ids)
|
|
|
|
items.Items = info
|
|
return l.ok(items)
|
|
}
|
|
|
|
func (l *UserItemsLogic) setUserItems(info *[]dao.GroupInfo, ids []int64) {
|
|
if len(ids) == utils.NumberZero {
|
|
return
|
|
}
|
|
|
|
cli, err := svc.GetRpcClient(l.svcCtx.UserName)
|
|
if err != nil {
|
|
logx.Errorf("get user err: %v", err)
|
|
return
|
|
}
|
|
|
|
client := user.NewUserClient(cli.Conn())
|
|
|
|
data := user.InternalUsersByIdsReq{
|
|
Ids: ids,
|
|
}
|
|
res, err := client.InternalUsersByIds(context.Background(), &data)
|
|
if err != nil {
|
|
logx.Errorf("get order err: %v", err)
|
|
return
|
|
}
|
|
|
|
type UserItem struct {
|
|
Name string `json:"name"`
|
|
Avatar string `json:"avatar"`
|
|
}
|
|
names := make(map[int64]UserItem)
|
|
for _, item := range res.Items {
|
|
names[item.Id] = UserItem{Name: item.Name, Avatar: item.Avatar}
|
|
}
|
|
for key, item := range *info {
|
|
(*info)[key].UserName = names[item.UserId].Name
|
|
(*info)[key].UserAvatar = names[item.UserId].Avatar
|
|
}
|
|
|
|
return
|
|
}
|