search.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package entity
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/redis"
  5. "app.yhyue.com/moapp/jypkg/common/src/qfw/util/jy"
  6. "bp.jydev.jianyu360.cn/BaseService/powerCheckCenter/rpc/pb"
  7. "encoding/json"
  8. "fmt"
  9. IC "jyBXCore/rpc/init"
  10. "jyBXCore/rpc/service"
  11. "jyBXCore/rpc/type/bxcore"
  12. "jyBXCore/rpc/util"
  13. "log"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. var (
  19. SearchCacheKey = "searchDataCache_%d_%s_%s_%s"
  20. SearchCacheNoLoginKey = "searchDataCacheNoLogin_%d_%s_%s_%s"
  21. SearchCacheCount = "searchCountCache_%d_%s_%s_%s"
  22. SearchCacheCountNoLogin = "searchCountCacheNoLogin_%d_%s_%s_%s"
  23. )
  24. type KeyWordsSearch struct{}
  25. func NewKeyWordsSearch() *KeyWordsSearch {
  26. return &KeyWordsSearch{}
  27. }
  28. // IsEmptySearch 是否是空搜索,如果是空搜索查缓存数据
  29. func (kws *KeyWordsSearch) IsEmptySearch(in *bxcore.SearchReq) bool {
  30. //有主关键词 或 选择了行业,都不是空搜索
  31. //P492招标采购搜索匹配采购单位等优化--采购单位 || 中标企业 || 招标代理机构 有任何一个都可以进行搜索
  32. if strings.TrimSpace(in.KeyWords) != "" || strings.TrimSpace(in.Industry) != "" || strings.TrimSpace(in.AdditionalWords) != "" || strings.TrimSpace(in.Winner) != "" || strings.TrimSpace(in.Buyer) != "" || strings.TrimSpace(in.Agency) != "" {
  33. return false
  34. }
  35. //所有的未登录空搜索 缓存都最多查500条数据
  36. return true
  37. }
  38. // GetBidSearchListByCache 查询缓存数据
  39. // 未登录用户默认搜索和关键词搜索改成500条和免费用户保持一致--需求调整P260来自产品经理杨蘭20220116
  40. func (kws *KeyWordsSearch) GetBidSearchListByCache(in *bxcore.SearchReq) (list []*bxcore.SearchList, count, total int64) {
  41. //缓存数据 最大量是5000条 100页数据
  42. l, c := func(in *bxcore.SearchReq) (list []*bxcore.SearchList, count int64) {
  43. //缓存数据总量 - 当前平台
  44. redisCountKey := fmt.Sprintf(SearchCacheCount, in.SearchGroup, MC.If(in.IsPay, "v", "f").(string), MC.If(in.BidField != "", in.BidField, "n").(string), in.Platform)
  45. //缓存数据: SearchGroup-全部;招标信息;超前项目信息;kws.PageNum-当前页 免费用户 or 付费用户
  46. redisDataKey := fmt.Sprintf(SearchCacheKey, in.SearchGroup, MC.If(in.IsPay, "v", "f").(string), MC.If(in.BidField != "", in.BidField, "n").(string), in.Platform)
  47. if in.UserId == "" {
  48. // 未登录用户查的不是一个库 缓存也分开
  49. //缓存数据总量 - 当前平台
  50. redisCountKey = fmt.Sprintf(SearchCacheCountNoLogin, in.SearchGroup, MC.If(in.IsPay, "v", "f").(string), MC.If(in.BidField != "", in.BidField, "n").(string), in.Platform)
  51. //缓存数据: SearchGroup-全部;招标信息;超前项目信息;kws.PageNum-当前页 免费用户 or 付费用户
  52. redisDataKey = fmt.Sprintf(SearchCacheNoLoginKey, in.SearchGroup, MC.If(in.IsPay, "v", "f").(string), MC.If(in.BidField != "", in.BidField, "n").(string), in.Platform)
  53. }
  54. count = int64(redis.GetInt(util.RedisNameNew, redisCountKey))
  55. sCache, err := redis.GetNewBytes(util.RedisNameNew, redisDataKey)
  56. log.Println("-------------------------redisDataKey--------------------------------:", redisDataKey)
  57. if err == nil {
  58. if sCache != nil && len(*sCache) > 0 {
  59. err = json.Unmarshal(*sCache, &list)
  60. if err == nil {
  61. return
  62. } else {
  63. log.Println("缓存序列化异常")
  64. }
  65. }
  66. }
  67. //无缓存数据 或 缓存数据查询异常
  68. //查库>存redis缓存
  69. //查询缓存数据 参数初始化
  70. kws.DefaultSearchParamsAuto(in)
  71. //缓存数据
  72. count, list = service.GetBidSearchData(in, true)
  73. if len(list) > 0 {
  74. redis.Put(util.RedisNameNew, redisCountKey, count, MC.If(IC.C.DefaultSearchCacheTime > 0, IC.C.DefaultSearchCacheTime*60*60, 24*60*60).(int))
  75. b, err := json.Marshal(list)
  76. if err == nil {
  77. redis.PutBytes(util.RedisNameNew, redisDataKey, &b, MC.If(IC.C.DefaultSearchCacheTime > 0, IC.C.DefaultSearchCacheTime*60*60, 24*60*60).(int))
  78. } else {
  79. log.Println("默认搜索查询结果保存redis缓存异常")
  80. }
  81. } else {
  82. log.Println("默认搜索 暂无数据")
  83. }
  84. return
  85. }(in)
  86. if len(l) > 0 {
  87. total = c
  88. limitCount := int64(util.SearchPageSize * MC.If(in.IsPay, util.SearchMaxPageNum_PAYED, util.SearchMaxPageNum).(int))
  89. log.Println(in.IsPay, limitCount, "调试日志")
  90. count = c
  91. if count > limitCount {
  92. count = limitCount
  93. }
  94. list = l[(in.PageNum-1)*in.PageSize : in.PageNum*in.PageSize]
  95. //是否收藏
  96. util.MakeCollection(in.UserId, list)
  97. }
  98. return
  99. }
  100. // DefaultSearchParamsAuto 缓存查询条件初始化
  101. func (kws *KeyWordsSearch) DefaultSearchParamsAuto(in *bxcore.SearchReq) {
  102. in.TopType = ""
  103. in.City = ""
  104. in.Industry = ""
  105. in.FileExists = ""
  106. in.WinnerTel = ""
  107. in.BuyerTel = ""
  108. in.BuyerClass = ""
  109. in.Price = ""
  110. in.SelectType = "title"
  111. in.Province = ""
  112. }
  113. // SaveKeyWordsToHistory 保存历史记录
  114. func (kws *KeyWordsSearch) SaveKeyWordsToHistory(in *bxcore.SearchReq) {
  115. if in.KeyWords != "" {
  116. //历史记录
  117. history := redis.GetStr("other", "s_"+in.UserId)
  118. keys := util.SearchHistory(history, in.KeyWords, in.AdditionalWords)
  119. if len(keys) > 0 {
  120. if b := redis.Put("other", "s_"+in.UserId, strings.Join(keys, ","), -1); !b {
  121. log.Println("保存搜索记录异常,用户id:", in.UserId)
  122. }
  123. }
  124. }
  125. }
  126. // GetSearchKeyWordsQueryStr 关键词和附加词处理 获取关键词查询条件;是否进行分词查询
  127. func (kws *KeyWordsSearch) GetSearchKeyWordsQueryStr(in *bxcore.SearchReq) (searchWords []string) {
  128. // in.SearchMode 搜索模式:0:精准搜索;1:模糊搜索
  129. // 精准搜索:不分词,完全匹配;(中间带空格的关键词组自动分词)
  130. // 模糊搜索:对用户输入的单个关键词进行分词处理,但必须都存在;
  131. //主关键词词组
  132. if in.KeyWords != "" {
  133. if in.SearchMode == 1 {
  134. if ikWords := util.HttpEs(in.KeyWords, "ik_smart", IC.DB.Es.Addr); ikWords != "" {
  135. in.KeyWords = jy.KeywordsProcessing(ikWords, IC.C.JYKeyMark)
  136. }
  137. }
  138. searchWords = append(searchWords, in.KeyWords)
  139. }
  140. //多组附加词,每组间,号隔开。每组内如果关键词中间有空格,自动分词
  141. if in.AdditionalWords != "" {
  142. if in.SearchMode == 1 {
  143. var (
  144. addWords []string
  145. )
  146. for _, awv := range strings.Split(in.AdditionalWords, ",") {
  147. if strings.TrimSpace(awv) != "" {
  148. if ikWords := util.HttpEs(awv, "ik_smart", IC.DB.Es.Addr); ikWords != "" {
  149. addWords = append(addWords, jy.KeywordsProcessing(ikWords, IC.C.JYKeyMark))
  150. }
  151. }
  152. }
  153. if len(addWords) > 0 {
  154. in.AdditionalWords = strings.Join(addWords, ",")
  155. }
  156. }
  157. searchWords = append(searchWords, strings.Split(in.AdditionalWords, ",")...)
  158. }
  159. return
  160. }
  161. // SearchParamsHandle 搜索条件 处理
  162. func (kws *KeyWordsSearch) SearchParamsHandle(in *bxcore.SearchReq) []string {
  163. baseUserId, _ := strconv.ParseInt(in.NewUserId, 10, 64)
  164. accountId, _ := strconv.ParseInt(in.AccountId, 10, 64)
  165. positionType, _ := strconv.ParseInt(in.PositionType, 10, 64)
  166. positionId, _ := strconv.ParseInt(in.PositionId, 10, 64)
  167. userInfo := &pb.CheckResp{}
  168. if in.UserId != "" {
  169. //判断用户身份
  170. userInfo = IC.Middleground.PowerCheckCenter.Check(in.AppId, in.MgoUserId, baseUserId, accountId, in.EntId, positionType, positionId)
  171. } else {
  172. userInfo = &pb.CheckResp{Free: &pb.Free{IsFree: true}}
  173. }
  174. //是否是付费用户
  175. in.IsPay = !userInfo.Free.IsFree
  176. //默认搜索范围
  177. if in.SelectType == "" {
  178. in.SelectType = "title,content"
  179. }
  180. queryItems := util.GetQueryItems(in.SelectType, IC.C.BidSearchOldUserLimit, userInfo.Free.Registedate, in.IsPay)
  181. in.SelectType = strings.Join(queryItems, ",")
  182. // in.SearchGroup 搜索分组 搜索分组:默认0:全部;1:招标采购公告;2:超前项目
  183. // 详情页判断是否能使用超前项目 老版超级订阅、大会员、商机管理有权限
  184. if in.SearchGroup < 0 || in.SearchGroup > 2 {
  185. in.SearchGroup = 1
  186. }
  187. //信息类型
  188. if in.Subtype == "" && in.TopType == "" {
  189. //(免费用户和新版超级订阅用户 有搜索权限,但是没有查看权限)免费用户与未登录用户支持 拟建,采购意向搜索
  190. if in.SearchGroup > 0 && len(IC.C.DefaultTopTypes) >= int(in.SearchGroup) {
  191. in.Subtype = IC.C.DefaultTopTypes[in.SearchGroup-1]
  192. }
  193. }
  194. // in.SearchMode 搜索模式 搜索模式:0:精准搜索;1:模糊搜索
  195. // 精准搜索:不分词,完全匹配;(中间带空格的关键词组自动分词)
  196. // 模糊搜索:对用户输入的单个关键词进行分词处理,但必须都存在;
  197. if in.SearchMode < 0 || in.SearchMode > 1 {
  198. in.SearchMode = 0
  199. }
  200. // in.WordsMode 搜索关键词模式;默认0:包含所有,1:包含任意
  201. if in.WordsMode < 0 || in.WordsMode > 1 {
  202. in.WordsMode = 0
  203. }
  204. //查询时间publishTime
  205. if in.PublishTime == "" {
  206. //付费用户最新5年;免费用户||未登录用户最新1年
  207. in.PublishTime = fmt.Sprintf("%d-%d", time.Now().AddDate(-1, 0, 0).Unix(), time.Now().Unix())
  208. if in.IsPay {
  209. in.PublishTime = fmt.Sprintf("%d-%d", time.Now().AddDate(-5, 0, 0).Unix(), time.Now().Unix())
  210. }
  211. }
  212. //默认每页数据量
  213. if in.PageSize <= 0 {
  214. in.PageSize = 50
  215. }
  216. //第一页
  217. if in.PageNum <= 0 {
  218. in.PageNum = 1
  219. }
  220. count := MC.If(in.IsPay, IC.C.DefaultBidInfo.PayCount, IC.C.DefaultBidInfo.Count).(int)
  221. if in.PageNum > int64(count)/in.PageSize {
  222. in.PageNum = -1
  223. in.PageSize = -1
  224. }
  225. //行业格式化
  226. if in.Industry != "" {
  227. in.Industry = strings.TrimSpace(in.Industry)
  228. }
  229. //免费用户:高级筛选 采购单位类型、采购单位联系方式、中标企业联系方式、排除词、城市
  230. if userInfo.Free.IsFree {
  231. in.BuyerClass = ""
  232. in.BuyerTel = ""
  233. in.WinnerTel = ""
  234. in.ExclusionWords = ""
  235. in.City = ""
  236. in.District = ""
  237. in.ExclusionWords = ""
  238. }
  239. //判断是否有关键词
  240. if in.KeyWords != "" {
  241. //关键词处理
  242. in.KeyWords = strings.TrimSpace(in.KeyWords)
  243. in.InterceptKeyWords, in.InterceptOtherWords, in.KeyWords = util.InterceptSearchKW(in.KeyWords, MC.IntAllDef(IC.C.KeywordsLimit, 35), true) // len(in.Industry) == 0
  244. }
  245. //附加词 每组附加词不能超过15个字符
  246. if in.AdditionalWords != "" {
  247. var additionalWords []string
  248. for _, ak := range strings.Split(in.AdditionalWords, ",") {
  249. if len([]rune(ak)) > 15 {
  250. additionalWords = append(additionalWords, string([]rune(ak)[:15]))
  251. } else {
  252. additionalWords = append(additionalWords, ak)
  253. }
  254. }
  255. in.AdditionalWords = strings.Join(additionalWords, ",") //分组不变
  256. }
  257. //更新关键词搜索历史记录
  258. go kws.SaveKeyWordsToHistory(in)
  259. //排除词 每组排除词不能超过15个字符
  260. if in.ExclusionWords != "" {
  261. var exclusionWords []string
  262. for _, ak := range strings.Split(in.ExclusionWords, ",") {
  263. if len([]rune(ak)) > 15 {
  264. exclusionWords = append(exclusionWords, string([]rune(ak)[:15]))
  265. } else {
  266. exclusionWords = append(exclusionWords, ak)
  267. }
  268. }
  269. in.ExclusionWords = strings.Join(exclusionWords, IC.C.JYKeyMark) //util.MatchSpace.ReplaceAllString(in.ExclusionWords, IC.C.JYKeyMark)
  270. }
  271. return kws.GetSearchKeyWordsQueryStr(in) //格式化关键词
  272. }
  273. // GetBidSearchList 非空搜索 查询
  274. func (kws *KeyWordsSearch) GetBidSearchList(in *bxcore.SearchReq) (count, total int64, list []*bxcore.SearchList) {
  275. //排除异常in.PageNum参数
  276. count, list = service.GetBidSearchData(in, false)
  277. util.MakeCollection(in.UserId, list)
  278. total = count //返回数据总量提示信息
  279. limitCount := MC.If(in.IsPay, int64(util.SearchPageSize*util.SearchMaxPageNum_PAYED), int64(util.SearchPageSize*util.SearchMaxPageNum)).(int64)
  280. if count > limitCount {
  281. count = limitCount //付费用户count 最多5000条,100页数据,每页50条;免费用户count 最多500条,10页数据,每页50条。
  282. }
  283. return
  284. }
  285. // 聚合搜索
  286. func (kws *KeyWordsSearch) PolymerizeSearch(in *bxcore.PolymerizeSearchReq) *bxcore.SearchReturn {
  287. data := &bxcore.SearchReturn{}
  288. //powerCheck := IC.Middleground.PowerCheckCenter.Check(in.AppId, in.UserId, in.NewUserId, in.AccountId, in.EntId, in.PositionType, in.PositionId)
  289. //企业搜索
  290. now1 := time.Now().Unix()
  291. entList := &bxcore.SearchMap{}
  292. entList.Data, entList.Count = service.EntSearch(in.SearchCode)
  293. data.EntList = entList
  294. now2 := time.Now().Unix()
  295. //采购单位搜搜索
  296. procureList := &bxcore.SearchMap{}
  297. procureList.Data, procureList.Count = service.ProcureSearch(in.SearchCode)
  298. data.ProcureList = procureList
  299. now3 := time.Now().Unix()
  300. log.Println("企业查询耗时", now2-now1)
  301. log.Println("采购单位搜搜索", now3-now2)
  302. /*if in.AccountId > 0 {
  303. //菜单搜索
  304. data.MenuList = service.MenuSearch(in)
  305. //标讯搜索
  306. now4 := time.Now().Unix()
  307. subscribeList := &bxcore.SearchMap{}
  308. subscribeList.Data = service.SubscribeSearch(in.SearchCode, powerCheck)
  309. data.SubscribeList = subscribeList
  310. now4 := time.Now().Unix()
  311. log.Println("菜单搜索", now4-now3)
  312. }*/
  313. return data
  314. }