package logic

import (
	"context"
	"jyBXBase/rpc/bxbase"
	IC "jyBXBase/rpc/init"
	"jyBXBase/rpc/util"
	"log"
	"strings"
	"time"

	"jyBXBase/rpc/internal/svc"

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

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

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

// 添加搜索列表
func (l *AddSearchLogic) AddSearch(in *bxbase.AddSearchReq) (res *bxbase.CommonRes, err error) {
	res = new(bxbase.CommonRes)
	newData := make(map[string]interface{})
	if in.UserId == "" {
		res.ErrCode = 1
		res.ErrMsg = "用户未登录"
		return
	}
	if in.InKey == "" {
		res.ErrCode = 1
		res.ErrMsg = "保存失败"
		return
	}
	log.Println("添加搜索列表:", in)
	in.Keywords = strings.Replace(in.Keywords, " ", ",", -1)
	switch in.SearchGroup {
	case util.SearchGroupBidding:
		if ValueSort(util.TopTypesBidding) == in.Subtype {
			in.Subtype = ""
		}
	case util.SearchGroupLeadingProject:
		if ValueSort(util.TopTypesLeadingProject) == in.Subtype {
			in.Subtype = ""
		}
	}
	newData["keywords"] = in.Keywords
	newData["type"] = in.Type
	newData["publish_time"] = in.PublishTime
	newData["area"] = in.Area
	newData["city"] = in.City
	newData["subtype"] = in.Subtype
	newData["min_price"] = in.MinPrice
	newData["max_price"] = in.MaxPrice
	newData["industry"] = in.Industry
	newData["select_type"] = in.SelectType
	newData["buyer_class"] = in.BuyerClass
	newData["buyer_tel"] = in.HasBuyerTel
	newData["winner_tel"] = in.HasWinnerTel
	newData["file_exists"] = in.FileExists
	newData["not_key"] = in.NotKey
	newData["creation_time"] = time.Now().Unix()
	newData["user_id"] = in.UserId
	newData["in_key"] = in.InKey
	newData["searchGroup"] = in.SearchGroup
	newData["searchMode"] = in.SearchMode
	newData["wordsMode"] = in.WordsMode
	newData["additionalWords"] = in.AdditionalWords
	IC.Mgo.Save("search_condition", newData)
	return
}
