// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1

package user

import (
	"context"
	"fmt"

	"sundynix-micro-go/app/auth/api/internal/svc"
	"sundynix-micro-go/app/auth/api/internal/types"
	sysPb "sundynix-micro-go/app/system/rpc/system"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
	return &UpdateUserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateUserLogic) UpdateUser(req *types.UpdateUserReq) error {
	userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
	if userId == "" || userId == "<nil>" {
		return fmt.Errorf("用户未登录")
	}

	_, err := l.svcCtx.SystemRpc.UpdateUser(l.ctx, &sysPb.UpdateUserReq{
		Id:       userId,
		Name:     req.Name,
		Account:  req.Account,
		Phone:    req.Phone,
		AvatarId: req.AvatarId,
		NickName: req.NickName,
	})
	if err != nil {
		l.Errorf("更新用户失败: %v", err)
		return fmt.Errorf("更新用户失败")
	}

	return nil
}
