package aliyun_avatar

import (
	"context"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"
	"wechat-api/internal/utils/dberrorhandler"

	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/zeromicro/go-zero/core/logx"
)

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

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

func (l *UpdateAliyunAvatarLogic) UpdateAliyunAvatar(req *types.AliyunAvatarInfo) (*types.BaseMsgResp, error) {
	err := l.svcCtx.DB.AliyunAvatar.UpdateOneID(*req.Id).
		SetNotNilUserID(req.UserId).
		SetNotNilBizID(req.BizId).
		SetNotNilAccessKeyID(req.AccessKeyId).
		SetNotNilAccessKeySecret(req.AccessKeySecret).
		SetNotNilAppID(req.AppId).
		SetNotNilTenantID(req.TenantId).
		SetNotNilResponse(req.Response).
		SetNotNilToken(req.Token).
		SetNotNilSessionID(req.SessionId).
		Exec(l.ctx)

	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
}
