package logic import ( "context" "strconv" "lone-services/pkg/modelbase" "lone-services/pkg/utils" "lone-services/pkg/validate" user "lone-services/rpc/user/pb" "lone-services/services/user/internal/dao" "lone-services/services/user/internal/model" "lone-services/services/user/internal/svc" "lone-services/services/user/validator" "github.com/zeromicro/go-zero/core/logx" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) type UsersByIdsLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewUsersByIdsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UsersByIdsLogic { return &UsersByIdsLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } func (l *UsersByIdsLogic) UsersByIds(in *user.UsersByIdsReq) (*user.UsersByIdsData, error) { var req validator.UsersByIdsValidator if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty { return nil, status.Error(codes.InvalidArgument, msg) } ids := make([]string, 0, len(req.Ids)) seen := make(map[int64]struct{}, len(req.Ids)) for _, id := range req.Ids { if id < utils.NumberOne { continue } if _, ok := seen[id]; ok { continue } seen[id] = struct{}{} ids = append(ids, strconv.FormatInt(id, utils.NumberTen)) } if len(ids) < utils.NumberOne { return &user.UsersByIdsData{Items: []*user.UserBriefItem{}}, nil } var rows []dao.UserRow userModel := model.UserModel{}.Init() if err := userModel.Items(modelbase.Params{ In: map[string][]string{"id": ids}, }, &rows); err != nil { l.Errorf("users by ids: %v", err) return nil, status.Error(codes.Internal, utils.Fail.Msg) } items := make([]*user.UserBriefItem, 0, len(rows)) for i := range rows { items = append(items, &user.UserBriefItem{ Id: rows[i].Id, Mobile: rows[i].Mobile, Name: rows[i].Name, Avatar: rows[i].Avatar, Gender: uint32(rows[i].Gender), Birthday: rows[i].Birthday.DateString(), Status: uint32(rows[i].Status), }) } return &user.UsersByIdsData{Items: items}, nil }