package service

import (
	MC "app.yhyue.com/moapp/jybase/common"
	IC "jyBXCore/rpc/init"
	"jyBXCore/rpc/model/es"
	"jyBXCore/rpc/type/bxcore"
	"jyBXCore/rpc/util"
	"log"
	"strings"
	"time"
)

// GetBidSearchData  默认查询缓存数据 只查标题
// 登录用户默认搜索500条数据，付费用户字段和免费用户字段不同，未登录用户查询5000条。
// 标信息搜索 isCache:是否是获取缓存信息
func GetBidSearchData(in *bxcore.SearchReq, isCache bool) (count int64, list []*bxcore.SearchList) {
	var start = int((in.PageNum - 1) * in.PageSize)
	if start >= 0 {
		t := time.Now()
		fields := MC.If(in.IsPay, es.BidSearchFieldOfVip, es.BidSearchFieldBase).(string)
		//in.BidField（医疗）领域化字段
		if in.BidField != "" {
			fields = es.BidSearchDomainField
		}
		//IC.C.FileSignBool列表是否显示附件开关
		if IC.C.FileSignBool {
			fields = fields + es.BidSearchFieldFile
		}
		biddingSearch := es.SearchByES{
			Index:      es.INDEX,
			IType:      es.TYPE,
			Query:      es.GetSearchQuery(in, es.GetBidSearchQuery(in)),
			FindFields: MC.If(isCache, "title", "detail").(string),
			Order:      es.BidSearchSort,
			Fields:     fields,
			Start:      MC.If(isCache, 0, start).(int),
			Limit:      MC.If(isCache, MC.If(in.IsPay, IC.C.DefaultBidInfo.PayCount, IC.C.DefaultBidInfo.Count).(int), int(in.PageSize)).(int),
			Count:      MC.If(strings.Contains(in.SelectType, "detail"), 115, 0).(int),       //高亮正文数量
			HighLight:  MC.If(strings.Contains(in.SelectType, "detail"), true, false).(bool), //是否高亮正文
		}
		var repl *[]map[string]interface{}
		count, repl = biddingSearch.GetAllByNgramWithCount()
		if repl != nil && *repl != nil && len(*repl) > 0 {
			//格式化查询结果
			list = util.SearchListFormat(in.UserId, in.Industry, repl, strings.Contains(in.SelectType, "detail"))
		} else {
			log.Println("查询数据异常")
		}
		log.Println(in.KeyWords, "关键词 -1- 查询耗时：", time.Since(t).Seconds())
	}
	return
}
