package chatsession

import (
	"context"
	"fmt"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"wechat-api/ent/chatsession"
	"wechat-api/internal/utils/dberrorhandler"

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

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

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

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

func (l *DeleteApiSessionLogic) DeleteApiSession(req *types.IDsReq) (*types.BaseMsgResp, error) {
	userId := l.ctx.Value("userId").(uint64)

	msg := errormsg.Success
	ids := req.Ids
	if len(ids) > 0 {
		num, err := l.svcCtx.DB.ChatSession.Delete().Where(chatsession.UserID(userId), chatsession.IDIn(ids...)).Exec(l.ctx)
		if err != nil {
			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
		}
		msg = fmt.Sprintf("delete %d rows", num)
	}

	return &types.BaseMsgResp{
		Code: 0,
		Msg:  msg,
	}, nil
}
