package token

import (
	"context"
	"github.com/zeromicro/go-zero/core/errorx"
	"wechat-api/internal/utils/dberrorhandler"

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

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

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

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

func (l *ClearTokenByIdLogic) ClearTokenById(req *types.IDReq) (*types.BaseMsgResp, error) {
	isAdmin := l.ctx.Value("isAdmin").(bool)

	resp := types.BaseMsgResp{
		Code: 0,
		Msg:  "操作成功",
	}

	if isAdmin {
		_, err := l.svcCtx.DB.Token.UpdateOneID(req.Id).SetMAC("").Save(l.ctx)
		if err != nil {
			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
		}
	} else {
		return nil, errorx.NewInvalidArgumentError("暂无权限")
	}

	return &resp, nil
}
