search.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package entity
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/redis"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. "jyBXCore/rpc/bxcore"
  9. IC "jyBXCore/rpc/init"
  10. "jyBXCore/rpc/service"
  11. "jyBXCore/rpc/util"
  12. "strings"
  13. "time"
  14. )
  15. var (
  16. SearchCacheKey = "searchDataCache_%s_%s"
  17. SearchCacheCount = "searchCountCache_%s_%s"
  18. )
  19. type KeyWordsSearch struct{}
  20. func NewKeyWordsSearch() *KeyWordsSearch {
  21. return &KeyWordsSearch{}
  22. }
  23. // IsEmptySearch 是否是空搜索,如果是空搜索查缓存数据
  24. func (kws *KeyWordsSearch) IsEmptySearch(in *bxcore.SearchReq) bool {
  25. //有主关键词 或 选择了行业,都不是空搜索
  26. if in.KeyWords != "" || in.Industry != "" {
  27. return false
  28. }
  29. return true
  30. }
  31. // GetBidSearchListByCache 查询缓存数据
  32. //未登录用户默认搜索和关键词搜索改成500条和免费用户保持一致--需求调整P260来自产品经理杨蘭20220116
  33. func (kws *KeyWordsSearch) GetBidSearchListByCache(in *bxcore.SearchReq) (list []*bxcore.SearchList, count int64) {
  34. //缓存数据 最大量是5000条 100页数据
  35. l, c := func(in *bxcore.SearchReq) (list []*bxcore.SearchList, count int64) {
  36. //缓存数据: kws.Platform-平台;kws.PageNum-当前页 免费用户 or 付费用户
  37. redisDataKey := fmt.Sprintf(SearchCacheKey, in.Platform, MC.If(in.IsPay, "V", "F").(string))
  38. //缓存数据总量 - 当前平台
  39. redisCountKey := fmt.Sprintf(SearchCacheCount, in.Platform, MC.If(in.IsPay, "V", "F").(string))
  40. sCache, err := redis.GetNewBytes(util.RedisNameNew, redisDataKey)
  41. count = int64(redis.GetInt(util.RedisNameNew, redisCountKey))
  42. if err == nil {
  43. if sCache != nil && len(*sCache) > 0 {
  44. err = json.Unmarshal(*sCache, &list)
  45. if err == nil {
  46. return
  47. }
  48. }
  49. }
  50. //无缓存数据 或 缓存数据查询异常
  51. //查库>存redis缓存
  52. //查询缓存数据 参数初始化
  53. kws.DefaultSearchParamsAuto(in)
  54. //缓存数据
  55. count, list = service.GetBidSearchData(in, true)
  56. limitCount := int64(util.SearchPageSize * MC.If(in.IsPay, util.SearchMaxPageNum_PAYED, util.SearchMaxPageNum).(int))
  57. if count > limitCount {
  58. count = limitCount
  59. }
  60. if len(list) > 0 {
  61. redis.Put(util.RedisNameNew, redisCountKey, count, MC.If(IC.C.DefaultSearchCacheTime > 0, IC.C.DefaultSearchCacheTime*60*60, 24*60*60).(int))
  62. b, err := json.Marshal(list)
  63. if err == nil {
  64. redis.PutBytes(util.RedisNameNew, redisDataKey, &b, MC.If(IC.C.DefaultSearchCacheTime > 0, IC.C.DefaultSearchCacheTime*60*60, 24*60*60).(int))
  65. } else {
  66. logx.Info("默认搜索查询结果保存redis缓存异常")
  67. }
  68. } else {
  69. logx.Info("默认搜索 暂无数据")
  70. }
  71. return
  72. }(in)
  73. if len(l) > 0 {
  74. list = l[(in.PageNum-1)*in.PageSize : in.PageNum*in.PageSize]
  75. count = c
  76. //是否收藏
  77. util.MakeCollection(in.UserId, list)
  78. }
  79. return
  80. }
  81. // DefaultSearchParamsAuto 缓存查询条件初始化
  82. func (kws *KeyWordsSearch) DefaultSearchParamsAuto(in *bxcore.SearchReq) {
  83. //缓存数据 默认查询分组 searchGroup = 1 ,
  84. //不查超前信息--招标信息类型为拟建和采购意向的信息
  85. in.SearchGroup = 1
  86. in.Subtype = IC.C.DefaultTopTypes[in.SearchGroup-1]
  87. in.TopType = ""
  88. in.City = ""
  89. in.Industry = ""
  90. in.FileExists = ""
  91. in.WinnerTel = ""
  92. in.BuyerTel = ""
  93. in.BuyerClass = ""
  94. in.Price = ""
  95. in.SelectType = "title"
  96. in.Province = ""
  97. //in.PublishTime = ""
  98. }
  99. // SaveKeyWordsToHistory 保存历史记录
  100. func (kws *KeyWordsSearch) SaveKeyWordsToHistory(in *bxcore.SearchReq) {
  101. if in.KeyWords != "" {
  102. //历史记录
  103. history := redis.GetStr("other", "s_"+in.UserId)
  104. keys := util.SearchHistory(history, in.KeyWords)
  105. if len(keys) > 0 {
  106. if b := redis.Put("other", "s_"+in.UserId, strings.Join(keys, ","), -1); !b {
  107. logx.Info("保存搜索记录异常,用户id:", in.UserId)
  108. }
  109. }
  110. }
  111. }
  112. // SearchParamsHandle 搜索条件 处理
  113. func (kws *KeyWordsSearch) SearchParamsHandle(in *bxcore.SearchReq) {
  114. //判断用户身份
  115. userInfo := util.GetVipState(IC.MainMysql, IC.Mgo, in.UserId, in.EntId)
  116. //是否是付费用户
  117. in.IsPay = userInfo.IsPayedUser()
  118. //默认搜索范围
  119. if in.SelectType == "" {
  120. in.SelectType = "title,content"
  121. }
  122. queryItems := userInfo.GetQueryItems(in.SelectType, IC.C.BidSearchOldUserLimit)
  123. in.SelectType = strings.Join(queryItems, ",")
  124. // in.SearchGroup 搜索分组 搜索分组:默认0:全部;1:招标采购公告;2:超前项目
  125. if in.SearchGroup < 0 || in.SearchGroup > 2 {
  126. in.SearchGroup = 1
  127. }
  128. //判断是否能使用超前项目 老版超级订阅、大会员、商机管理有权限
  129. if !(userInfo.IsOldVip || userInfo.BigMember > 0 || userInfo.EntMember > 0) {
  130. in.SearchGroup = 1
  131. //Subtype 包含:拟建,采购意向,则需要排除
  132. if in.Subtype != "" {
  133. var subtype []string
  134. for _, sv := range strings.Split(in.Subtype, ",") {
  135. if sv == "拟建" || sv == "采购意向" {
  136. continue
  137. }
  138. subtype = append(subtype, sv)
  139. }
  140. in.Subtype = MC.If(len(subtype) > 0, strings.Join(subtype, ","), "").(string)
  141. }
  142. }
  143. //信息类型参数为空 根据搜索分组 初始化信息类型
  144. //in.SearchGroup = 0
  145. if in.TopType == "" && in.Subtype == "" {
  146. if in.SearchGroup > 0 && len(IC.C.DefaultTopTypes) > int(in.SearchGroup) {
  147. in.Subtype = IC.C.DefaultTopTypes[in.SearchGroup-1]
  148. }
  149. }
  150. // in.SearchMode 搜索模式 搜索模式:0:精准搜索;1:模糊搜索
  151. // 精准搜索:不分词,完全匹配;(中间带空格的关键词组自动分词)
  152. // 模糊搜索:对用户输入的单个关键词进行分词处理,但必须都存在;
  153. if in.SearchMode < 0 || in.SearchMode > 1 {
  154. in.SearchMode = 0
  155. }
  156. // in.WordsMode 搜索关键词模式;默认0:包含所有,1:包含任意
  157. if in.WordsMode < 0 || in.WordsMode > 1 {
  158. in.WordsMode = 0
  159. }
  160. //查询时间publishTime
  161. if in.PublishTime == "" {
  162. //付费用户最新5年;免费用户最新3年
  163. in.PublishTime = fmt.Sprintf("%d-%d", time.Now().AddDate(-3, 0, 0).Unix(), time.Now().Unix())
  164. if userInfo.IsPayedUser() {
  165. in.PublishTime = fmt.Sprintf("%d-%d", time.Now().AddDate(-5, 0, 0).Unix(), time.Now().Unix())
  166. }
  167. }
  168. //默认每页数据量
  169. if in.PageSize <= 0 {
  170. in.PageSize = 50
  171. }
  172. //第一页
  173. if in.PageNum <= 0 {
  174. in.PageNum = 1
  175. }
  176. //行业格式化
  177. if in.Industry != "" {
  178. in.Industry = strings.TrimSpace(in.Industry)
  179. }
  180. //免费用户:高级筛选 采购单位类型、采购单位联系方式、中标企业联系方式、排除词、城市
  181. if !userInfo.IsPayedUser() {
  182. in.BuyerClass = ""
  183. in.BuyerTel = ""
  184. in.WinnerTel = ""
  185. in.ExclusionWords = ""
  186. in.City = ""
  187. in.SearchGroup = MC.If(in.SearchGroup > 1, 0, in.SearchGroup).(int64) //搜索分组:默认0:全部;1:招标采购公告;2:超前项目
  188. in.ExclusionWords = ""
  189. }
  190. //判断是否有关键词
  191. if in.KeyWords != "" {
  192. //关键词处理
  193. in.KeyWords = strings.TrimSpace(in.KeyWords)
  194. //以后可能会出现 关键词 C++ 等带+的关键词
  195. in.InterceptKeyWords, in.InterceptOtherWords, in.KeyWords = util.InterceptSearchKW(in.KeyWords, MC.IntAllDef(IC.C.KeywordsLimit, 35), len(in.Industry) == 0)
  196. //附加词在关键词的基础上 有效
  197. //附加词 每组附加词不能超过15个字符
  198. if in.AdditionalWords != "" {
  199. var additionalWords []string
  200. for _, ak := range strings.Split(in.AdditionalWords, ",") {
  201. if len([]rune(ak)) > 15 {
  202. additionalWords = append(additionalWords, string([]rune(ak)[:15]))
  203. } else {
  204. additionalWords = append(additionalWords, ak)
  205. }
  206. }
  207. in.AdditionalWords = strings.ReplaceAll(strings.Join(additionalWords, IC.C.JYKeyMark), " ", IC.C.JYKeyMark) //util.MatchSpace.ReplaceAllString(in.AdditionalWords, IC.C.JYKeyMark)
  208. }
  209. //格式化关键词
  210. in.KeyWords = util.GetSearchKeyWordsQueryStr(in)
  211. //更新关键词搜索历史记录
  212. go kws.SaveKeyWordsToHistory(in)
  213. } else {
  214. in.AdditionalWords = ""
  215. }
  216. //排除词 每组排除词不能超过15个字符
  217. if in.ExclusionWords != "" {
  218. var exclusionWords []string
  219. for _, ak := range strings.Split(in.ExclusionWords, ",") {
  220. if len([]rune(ak)) > 15 {
  221. exclusionWords = append(exclusionWords, string([]rune(ak)[:15]))
  222. } else {
  223. exclusionWords = append(exclusionWords, ak)
  224. }
  225. }
  226. in.ExclusionWords = strings.Join(exclusionWords, IC.C.JYKeyMark) //util.MatchSpace.ReplaceAllString(in.ExclusionWords, IC.C.JYKeyMark)
  227. }
  228. }
  229. // GetBidSearchList 非空搜索 查询
  230. func (kws *KeyWordsSearch) GetBidSearchList(in *bxcore.SearchReq) (count, total int64, list []*bxcore.SearchList) {
  231. //排除异常in.PageNum参数
  232. count, list = service.GetBidSearchData(in, false)
  233. util.MakeCollection(in.UserId, list)
  234. total = count //返回数据总量提示信息
  235. limitCount := MC.If(in.IsPay, int64(util.SearchPageSize*util.SearchMaxPageNum_PAYED), int64(util.SearchPageSize*util.SearchMaxPageNum)).(int64)
  236. if count > limitCount {
  237. count = limitCount //付费用户count 最多5000条,100页数据,每页50条;免费用户count 最多500条,10页数据,每页50条。
  238. }
  239. return
  240. }