package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"jyBXBase/rpc/bxbase"
	IC "jyBXBase/rpc/init"
	"jyBXBase/rpc/internal/svc"
	"log"
	"strings"
)

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

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

// 删除搜索列表
func (l *DelSearchLogic) DelSearch(in *bxbase.DelSearchReq) (res *bxbase.CommonRes, err error) {
	// todo: add your logic here and delete this line
	res = new(bxbase.CommonRes)
	delId := strings.Split(in.Id, ",")
	if len(delId) < 1 {
		return
	}
	log.Println("删除搜索列表:", in)
	var allDelId []primitive.ObjectID
	for _, v := range delId {
		ss, _ := primitive.ObjectIDFromHex(v)
		allDelId = append(allDelId, ss)
	}

	query := map[string]interface{}{
		"user_id": in.UserId,
		"_id":     map[string]interface{}{"$in": allDelId},
	}
	delCount := IC.Mgo.Delete("search_condition", query)
	if delCount == 0 {
		res.ErrCode = 1
		res.ErrMsg = "删除失败"
	}
	return
}
