65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"sundynix-micro-go/app/user/model"
|
|
"sundynix-micro-go/app/user/rpc/internal/svc"
|
|
"sundynix-micro-go/app/user/rpc/user"
|
|
|
|
"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 *user.UpdateUserReq) (*user.CommonResp, error) {
|
|
var u model.SundynixUser
|
|
if err := l.svcCtx.DB.Where("id = ?", in.Id).First(&u).Error; err != nil {
|
|
return nil, status.Error(codes.NotFound, "用户不存在")
|
|
}
|
|
|
|
updates := map[string]interface{}{}
|
|
if in.Name != "" {
|
|
updates["name"] = in.Name
|
|
}
|
|
if in.Account != "" {
|
|
updates["account"] = in.Account
|
|
}
|
|
if in.Phone != "" {
|
|
updates["phone"] = in.Phone
|
|
}
|
|
if in.AvatarId != "" {
|
|
updates["avatar_id"] = in.AvatarId
|
|
}
|
|
if in.NickName != "" {
|
|
updates["nick_name"] = in.NickName
|
|
}
|
|
|
|
if len(updates) > 0 {
|
|
if err := l.svcCtx.DB.Model(&u).Updates(updates).Error; err != nil {
|
|
l.Errorf("更新用户失败: %v", err)
|
|
return nil, status.Error(codes.Internal, "更新用户失败")
|
|
}
|
|
}
|
|
|
|
return &user.CommonResp{
|
|
Code: 200,
|
|
Msg: "更新成功",
|
|
}, nil
|
|
}
|