package logic

import (
	"context"

	plantModel "sundynix-micro-go/app/plant/model"
	"sundynix-micro-go/app/plant/rpc/internal/svc"
	"sundynix-micro-go/app/plant/rpc/plant"

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

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

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

// 更新用户资料
func (l *UpdateUserProfileLogic) UpdateUserProfile(in *plant.UpdateProfileReq) (*plant.CommonResp, error) {
	updateMap := map[string]interface{}{}
	if in.NickName != "" {
		updateMap["nick_name"] = in.NickName
	}
	if in.AvatarId != "" {
		updateMap["avatar_id"] = in.AvatarId
	}
	if len(updateMap) == 0 {
		return &plant.CommonResp{Code: 0, Msg: "nothing to update"}, nil
	}
	if err := l.svcCtx.DB.Model(&plantModel.UserProfile{}).Where("user_id = ?", in.UserId).
		Updates(updateMap).Error; err != nil {
		return nil, err
	}
	return &plant.CommonResp{Code: 0, Msg: "ok"}, nil
}
