search.go 9.9 KB

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