feat: 添加 GetUsersByIds 批量用户查询接口

This commit is contained in:
zetaloop
2026-04-24 06:44:44 +08:00
parent 809dbf2cac
commit 1936533244
6 changed files with 422 additions and 226 deletions
@@ -0,0 +1,38 @@
package logic
import (
"context"
"juwan-backend/app/users/rpc/internal/models/users"
"juwan-backend/app/users/rpc/internal/svc"
"juwan-backend/app/users/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type GetUsersByIdsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetUsersByIdsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUsersByIdsLogic {
return &GetUsersByIdsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetUsersByIdsLogic) GetUsersByIds(in *pb.GetUsersByIdsReq) (*pb.GetUsersByIdsResp, error) {
if len(in.Ids) == 0 {
return &pb.GetUsersByIdsResp{}, nil
}
userList, err := l.svcCtx.UsersModelRO.Users.Query().Where(users.IDIn(in.Ids...)).All(l.ctx)
if err != nil {
return nil, err
}
return &pb.GetUsersByIdsResp{Users: ConvertEntUsersToProto(userList)}, nil
}
@@ -1,5 +1,5 @@
// Code generated by goctl. DO NOT EDIT. // Code generated by goctl. DO NOT EDIT.
// goctl 1.9.2 // goctl 1.10.1
// Source: users.proto // Source: users.proto
package server package server
@@ -44,6 +44,11 @@ func (s *UsercenterServer) GetUsersById(ctx context.Context, in *pb.GetUsersById
return l.GetUsersById(in) return l.GetUsersById(in)
} }
func (s *UsercenterServer) GetUsersByIds(ctx context.Context, in *pb.GetUsersByIdsReq) (*pb.GetUsersByIdsResp, error) {
l := logic.NewGetUsersByIdsLogic(ctx, s.svcCtx)
return l.GetUsersByIds(in)
}
func (s *UsercenterServer) SearchUsers(ctx context.Context, in *pb.SearchUsersReq) (*pb.SearchUsersResp, error) { func (s *UsercenterServer) SearchUsers(ctx context.Context, in *pb.SearchUsersReq) (*pb.SearchUsersResp, error) {
l := logic.NewSearchUsersLogic(ctx, s.svcCtx) l := logic.NewSearchUsersLogic(ctx, s.svcCtx)
return l.SearchUsers(in) return l.SearchUsers(in)
File diff suppressed because it is too large Load Diff
+39 -1
View File
@@ -1,7 +1,7 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT. // Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions: // versions:
// - protoc-gen-go-grpc v1.6.1 // - protoc-gen-go-grpc v1.6.1
// - protoc v5.29.6 // - protoc v7.34.1
// source: users.proto // source: users.proto
package pb package pb
@@ -23,6 +23,7 @@ const (
Usercenter_UpdateUsers_FullMethodName = "/pb.usercenter/UpdateUsers" Usercenter_UpdateUsers_FullMethodName = "/pb.usercenter/UpdateUsers"
Usercenter_DelUsers_FullMethodName = "/pb.usercenter/DelUsers" Usercenter_DelUsers_FullMethodName = "/pb.usercenter/DelUsers"
Usercenter_GetUsersById_FullMethodName = "/pb.usercenter/GetUsersById" Usercenter_GetUsersById_FullMethodName = "/pb.usercenter/GetUsersById"
Usercenter_GetUsersByIds_FullMethodName = "/pb.usercenter/GetUsersByIds"
Usercenter_SearchUsers_FullMethodName = "/pb.usercenter/SearchUsers" Usercenter_SearchUsers_FullMethodName = "/pb.usercenter/SearchUsers"
Usercenter_GetUserByUsername_FullMethodName = "/pb.usercenter/GetUserByUsername" Usercenter_GetUserByUsername_FullMethodName = "/pb.usercenter/GetUserByUsername"
Usercenter_Login_FullMethodName = "/pb.usercenter/Login" Usercenter_Login_FullMethodName = "/pb.usercenter/Login"
@@ -53,6 +54,7 @@ type UsercenterClient interface {
UpdateUsers(ctx context.Context, in *UpdateUsersReq, opts ...grpc.CallOption) (*UpdateUsersResp, error) UpdateUsers(ctx context.Context, in *UpdateUsersReq, opts ...grpc.CallOption) (*UpdateUsersResp, error)
DelUsers(ctx context.Context, in *DelUsersReq, opts ...grpc.CallOption) (*DelUsersResp, error) DelUsers(ctx context.Context, in *DelUsersReq, opts ...grpc.CallOption) (*DelUsersResp, error)
GetUsersById(ctx context.Context, in *GetUsersByIdReq, opts ...grpc.CallOption) (*GetUsersByIdResp, error) GetUsersById(ctx context.Context, in *GetUsersByIdReq, opts ...grpc.CallOption) (*GetUsersByIdResp, error)
GetUsersByIds(ctx context.Context, in *GetUsersByIdsReq, opts ...grpc.CallOption) (*GetUsersByIdsResp, error)
SearchUsers(ctx context.Context, in *SearchUsersReq, opts ...grpc.CallOption) (*SearchUsersResp, error) SearchUsers(ctx context.Context, in *SearchUsersReq, opts ...grpc.CallOption) (*SearchUsersResp, error)
GetUserByUsername(ctx context.Context, in *GetUserByUsernameReq, opts ...grpc.CallOption) (*GetUserByUsernameResp, error) GetUserByUsername(ctx context.Context, in *GetUserByUsernameReq, opts ...grpc.CallOption) (*GetUserByUsernameResp, error)
Login(ctx context.Context, in *LoginReq, opts ...grpc.CallOption) (*LoginResp, error) Login(ctx context.Context, in *LoginReq, opts ...grpc.CallOption) (*LoginResp, error)
@@ -124,6 +126,16 @@ func (c *usercenterClient) GetUsersById(ctx context.Context, in *GetUsersByIdReq
return out, nil return out, nil
} }
func (c *usercenterClient) GetUsersByIds(ctx context.Context, in *GetUsersByIdsReq, opts ...grpc.CallOption) (*GetUsersByIdsResp, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(GetUsersByIdsResp)
err := c.cc.Invoke(ctx, Usercenter_GetUsersByIds_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *usercenterClient) SearchUsers(ctx context.Context, in *SearchUsersReq, opts ...grpc.CallOption) (*SearchUsersResp, error) { func (c *usercenterClient) SearchUsers(ctx context.Context, in *SearchUsersReq, opts ...grpc.CallOption) (*SearchUsersResp, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(SearchUsersResp) out := new(SearchUsersResp)
@@ -323,6 +335,7 @@ type UsercenterServer interface {
UpdateUsers(context.Context, *UpdateUsersReq) (*UpdateUsersResp, error) UpdateUsers(context.Context, *UpdateUsersReq) (*UpdateUsersResp, error)
DelUsers(context.Context, *DelUsersReq) (*DelUsersResp, error) DelUsers(context.Context, *DelUsersReq) (*DelUsersResp, error)
GetUsersById(context.Context, *GetUsersByIdReq) (*GetUsersByIdResp, error) GetUsersById(context.Context, *GetUsersByIdReq) (*GetUsersByIdResp, error)
GetUsersByIds(context.Context, *GetUsersByIdsReq) (*GetUsersByIdsResp, error)
SearchUsers(context.Context, *SearchUsersReq) (*SearchUsersResp, error) SearchUsers(context.Context, *SearchUsersReq) (*SearchUsersResp, error)
GetUserByUsername(context.Context, *GetUserByUsernameReq) (*GetUserByUsernameResp, error) GetUserByUsername(context.Context, *GetUserByUsernameReq) (*GetUserByUsernameResp, error)
Login(context.Context, *LoginReq) (*LoginResp, error) Login(context.Context, *LoginReq) (*LoginResp, error)
@@ -366,6 +379,9 @@ func (UnimplementedUsercenterServer) DelUsers(context.Context, *DelUsersReq) (*D
func (UnimplementedUsercenterServer) GetUsersById(context.Context, *GetUsersByIdReq) (*GetUsersByIdResp, error) { func (UnimplementedUsercenterServer) GetUsersById(context.Context, *GetUsersByIdReq) (*GetUsersByIdResp, error) {
return nil, status.Error(codes.Unimplemented, "method GetUsersById not implemented") return nil, status.Error(codes.Unimplemented, "method GetUsersById not implemented")
} }
func (UnimplementedUsercenterServer) GetUsersByIds(context.Context, *GetUsersByIdsReq) (*GetUsersByIdsResp, error) {
return nil, status.Error(codes.Unimplemented, "method GetUsersByIds not implemented")
}
func (UnimplementedUsercenterServer) SearchUsers(context.Context, *SearchUsersReq) (*SearchUsersResp, error) { func (UnimplementedUsercenterServer) SearchUsers(context.Context, *SearchUsersReq) (*SearchUsersResp, error) {
return nil, status.Error(codes.Unimplemented, "method SearchUsers not implemented") return nil, status.Error(codes.Unimplemented, "method SearchUsers not implemented")
} }
@@ -516,6 +532,24 @@ func _Usercenter_GetUsersById_Handler(srv interface{}, ctx context.Context, dec
return interceptor(ctx, in, info, handler) return interceptor(ctx, in, info, handler)
} }
func _Usercenter_GetUsersByIds_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetUsersByIdsReq)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(UsercenterServer).GetUsersByIds(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: Usercenter_GetUsersByIds_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(UsercenterServer).GetUsersByIds(ctx, req.(*GetUsersByIdsReq))
}
return interceptor(ctx, in, info, handler)
}
func _Usercenter_SearchUsers_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { func _Usercenter_SearchUsers_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(SearchUsersReq) in := new(SearchUsersReq)
if err := dec(in); err != nil { if err := dec(in); err != nil {
@@ -881,6 +915,10 @@ var Usercenter_ServiceDesc = grpc.ServiceDesc{
MethodName: "GetUsersById", MethodName: "GetUsersById",
Handler: _Usercenter_GetUsersById_Handler, Handler: _Usercenter_GetUsersById_Handler,
}, },
{
MethodName: "GetUsersByIds",
Handler: _Usercenter_GetUsersByIds_Handler,
},
{ {
MethodName: "SearchUsers", MethodName: "SearchUsers",
Handler: _Usercenter_SearchUsers_Handler, Handler: _Usercenter_SearchUsers_Handler,
+9 -1
View File
@@ -1,5 +1,5 @@
// Code generated by goctl. DO NOT EDIT. // Code generated by goctl. DO NOT EDIT.
// goctl 1.9.2 // goctl 1.10.1
// Source: users.proto // Source: users.proto
package usercenter package usercenter
@@ -36,6 +36,8 @@ type (
GetUserPreferencesByIdResp = pb.GetUserPreferencesByIdResp GetUserPreferencesByIdResp = pb.GetUserPreferencesByIdResp
GetUsersByIdReq = pb.GetUsersByIdReq GetUsersByIdReq = pb.GetUsersByIdReq
GetUsersByIdResp = pb.GetUsersByIdResp GetUsersByIdResp = pb.GetUsersByIdResp
GetUsersByIdsReq = pb.GetUsersByIdsReq
GetUsersByIdsResp = pb.GetUsersByIdsResp
LoginReq = pb.LoginReq LoginReq = pb.LoginReq
LoginResp = pb.LoginResp LoginResp = pb.LoginResp
LogoutReq = pb.LogoutReq LogoutReq = pb.LogoutReq
@@ -70,6 +72,7 @@ type (
UpdateUsers(ctx context.Context, in *UpdateUsersReq, opts ...grpc.CallOption) (*UpdateUsersResp, error) UpdateUsers(ctx context.Context, in *UpdateUsersReq, opts ...grpc.CallOption) (*UpdateUsersResp, error)
DelUsers(ctx context.Context, in *DelUsersReq, opts ...grpc.CallOption) (*DelUsersResp, error) DelUsers(ctx context.Context, in *DelUsersReq, opts ...grpc.CallOption) (*DelUsersResp, error)
GetUsersById(ctx context.Context, in *GetUsersByIdReq, opts ...grpc.CallOption) (*GetUsersByIdResp, error) GetUsersById(ctx context.Context, in *GetUsersByIdReq, opts ...grpc.CallOption) (*GetUsersByIdResp, error)
GetUsersByIds(ctx context.Context, in *GetUsersByIdsReq, opts ...grpc.CallOption) (*GetUsersByIdsResp, error)
SearchUsers(ctx context.Context, in *SearchUsersReq, opts ...grpc.CallOption) (*SearchUsersResp, error) SearchUsers(ctx context.Context, in *SearchUsersReq, opts ...grpc.CallOption) (*SearchUsersResp, error)
GetUserByUsername(ctx context.Context, in *GetUserByUsernameReq, opts ...grpc.CallOption) (*GetUserByUsernameResp, error) GetUserByUsername(ctx context.Context, in *GetUserByUsernameReq, opts ...grpc.CallOption) (*GetUserByUsernameResp, error)
Login(ctx context.Context, in *LoginReq, opts ...grpc.CallOption) (*LoginResp, error) Login(ctx context.Context, in *LoginReq, opts ...grpc.CallOption) (*LoginResp, error)
@@ -125,6 +128,11 @@ func (m *defaultUsercenter) GetUsersById(ctx context.Context, in *GetUsersByIdRe
return client.GetUsersById(ctx, in, opts...) return client.GetUsersById(ctx, in, opts...)
} }
func (m *defaultUsercenter) GetUsersByIds(ctx context.Context, in *GetUsersByIdsReq, opts ...grpc.CallOption) (*GetUsersByIdsResp, error) {
client := pb.NewUsercenterClient(m.cli.Conn())
return client.GetUsersByIds(ctx, in, opts...)
}
func (m *defaultUsercenter) SearchUsers(ctx context.Context, in *SearchUsersReq, opts ...grpc.CallOption) (*SearchUsersResp, error) { func (m *defaultUsercenter) SearchUsers(ctx context.Context, in *SearchUsersReq, opts ...grpc.CallOption) (*SearchUsersResp, error) {
client := pb.NewUsercenterClient(m.cli.Conn()) client := pb.NewUsercenterClient(m.cli.Conn())
return client.SearchUsers(ctx, in, opts...) return client.SearchUsers(ctx, in, opts...)
+9
View File
@@ -84,6 +84,14 @@ message GetUsersByIdResp {
Users users = 1; //users Users users = 1; //users
} }
message GetUsersByIdsReq {
repeated int64 ids = 1; //ids
}
message GetUsersByIdsResp {
repeated Users users = 1; //users
}
message SearchUsersReq { message SearchUsersReq {
optional int64 offset = 1; //offset optional int64 offset = 1; //offset
optional int64 limit = 2; //limit optional int64 limit = 2; //limit
@@ -327,6 +335,7 @@ service usercenter {
rpc UpdateUsers(UpdateUsersReq) returns (UpdateUsersResp); rpc UpdateUsers(UpdateUsersReq) returns (UpdateUsersResp);
rpc DelUsers(DelUsersReq) returns (DelUsersResp); rpc DelUsers(DelUsersReq) returns (DelUsersResp);
rpc GetUsersById(GetUsersByIdReq) returns (GetUsersByIdResp); rpc GetUsersById(GetUsersByIdReq) returns (GetUsersByIdResp);
rpc GetUsersByIds(GetUsersByIdsReq) returns (GetUsersByIdsResp);
rpc SearchUsers(SearchUsersReq) returns (SearchUsersResp); rpc SearchUsers(SearchUsersReq) returns (SearchUsersResp);
rpc GetUserByUsername(GetUserByUsernameReq) returns (GetUserByUsernameResp); rpc GetUserByUsername(GetUserByUsernameReq) returns (GetUserByUsernameResp);