61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
|
|
package logic
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
|
||
|
|
"muyu-apiserver/model"
|
||
|
|
"muyu-apiserver/rpc/system/internal/svc"
|
||
|
|
"muyu-apiserver/rpc/system/pb"
|
||
|
|
|
||
|
|
"github.com/zeromicro/go-zero/core/logx"
|
||
|
|
"google.golang.org/grpc/codes"
|
||
|
|
"google.golang.org/grpc/status"
|
||
|
|
)
|
||
|
|
|
||
|
|
type UpdateUserLogic struct {
|
||
|
|
ctx context.Context
|
||
|
|
svcCtx *svc.ServiceContext
|
||
|
|
logx.Logger
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
|
||
|
|
return &UpdateUserLogic{
|
||
|
|
ctx: ctx,
|
||
|
|
svcCtx: svcCtx,
|
||
|
|
Logger: logx.WithContext(ctx),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *UpdateUserLogic) UpdateUser(in *pb.UpdateUserReq) (*pb.Empty, error) {
|
||
|
|
user, err := l.svcCtx.UserModel.FindOneByUserId(l.ctx, in.UserId)
|
||
|
|
if err != nil {
|
||
|
|
if errors.Is(err, model.ErrNotFound) {
|
||
|
|
return nil, status.Error(codes.NotFound, "user not found")
|
||
|
|
}
|
||
|
|
return nil, status.Error(codes.Internal, err.Error())
|
||
|
|
}
|
||
|
|
|
||
|
|
user.RealName = in.RealName
|
||
|
|
user.Phone = in.Phone
|
||
|
|
user.Email = in.Email
|
||
|
|
if in.Avatar != "" {
|
||
|
|
user.Avatar = in.Avatar
|
||
|
|
}
|
||
|
|
|
||
|
|
err = l.svcCtx.UserModel.Update(l.ctx, user)
|
||
|
|
if err != nil {
|
||
|
|
return nil, status.Error(codes.Internal, err.Error())
|
||
|
|
}
|
||
|
|
|
||
|
|
if in.RoleId != "" {
|
||
|
|
_ = l.svcCtx.UserRoleModel.DeleteByUserId(l.ctx, in.UserId)
|
||
|
|
_, _ = l.svcCtx.UserRoleModel.Insert(l.ctx, &model.SysUserRole{
|
||
|
|
UserId: in.UserId,
|
||
|
|
RoleId: in.RoleId,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
return &pb.Empty{}, nil
|
||
|
|
}
|