getsearchlistlogic.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package logic
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/redis"
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. IC "jyBXCore/rpc/init"
  9. "jyBXCore/rpc/util"
  10. "strings"
  11. "time"
  12. "jyBXCore/rpc/internal/svc"
  13. "jyBXCore/rpc/type/bxcore"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type GetSearchListLogic struct {
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. logx.Logger
  20. }
  21. func NewGetSearchListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSearchListLogic {
  22. return &GetSearchListLogic{
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. Logger: logx.WithContext(ctx),
  26. }
  27. }
  28. // 标讯搜索结果列表数据
  29. func (l *GetSearchListLogic) GetSearchList(in *bxcore.SearchReq) (*bxcore.SearchResp, error) {
  30. defer MC.Catch()
  31. t := time.Now()
  32. res := &bxcore.SearchData{
  33. Count: 0,
  34. List: []*bxcore.SearchList{},
  35. InterceptLimit: int64(MC.IntAllDef(IC.C.KeywordsLimit, 35)),
  36. }
  37. if in.UserId != "" {
  38. //历史记录
  39. history_str := redis.GetStr("other", "s_"+in.UserId)
  40. arrs := util.SearchHistory(history_str, in.KeyWords)
  41. //redis.Del("other", "s_"+in.UserId)
  42. if len(arrs) > 0 {
  43. redis.Put("other", "s_"+in.UserId, strings.Join(arrs, ","), -1)
  44. //res.HistoryKeys = arrs
  45. }
  46. }
  47. //
  48. if in.SelectType == "" {
  49. in.SelectType = "title,content"
  50. }
  51. in.Industry = strings.TrimSpace(in.Industry)
  52. userInfo := util.GetVipState(IC.MainMysql, IC.Mgo, in.UserId)
  53. //付费用户
  54. if in.UserType != "fType" && !userInfo.IsPayedUser() {
  55. return &bxcore.SearchResp{
  56. ErrCode: -1,
  57. ErrMsg: "无权限",
  58. }, nil
  59. }
  60. if in.UserType == "fType" {
  61. in.BuyerClass = ""
  62. in.BuyerTel = ""
  63. in.WinnerTel = ""
  64. in.ExclusionWords = ""
  65. in.City = ""
  66. }
  67. in.KeyWords = strings.TrimSpace(in.KeyWords)
  68. //搜索范围
  69. queryItems := userInfo.GetQueryItems(in.SelectType, IC.C.BidSearchOldUserLimit)
  70. in.SelectType = strings.Join(queryItems, ",")
  71. //b_word, s_word := "", ""
  72. res.IsLimit = 1
  73. //以后可能会出现 关键词 C++ 等带+的关键词
  74. if in.KeyWords != "" {
  75. res.InterceptKeywords, res.InterceptWord, in.KeyWords = util.InterceptSearchKW(in.KeyWords, MC.IntAllDef(IC.C.KeywordsLimit, 35), len(in.Industry) == 0)
  76. }
  77. if in.UserId == "" {
  78. //未登录用户访信息类型 需要过滤掉 拟建和采购意向问全部
  79. if in.Subtype == "" {
  80. in.Subtype = "招标预告,招标公告,招标结果,招标信用信息"
  81. }
  82. }
  83. //查询数据
  84. if in.KeyWords != "" || in.Industry != "" {
  85. //查询数据
  86. searchLimit := util.IsSearchLimit(strings.Split(in.SelectType, ","))
  87. //未登录用户标题、正文都限制,已登录用户只限制正文
  88. if in.UserId == "" {
  89. searchLimit = true
  90. }
  91. //全文检索限制
  92. if searchLimit {
  93. res.IsLimit = util.IsLimited(in.LimitFlag, in.UserId, in.UserType != "fType", in.IsNew)
  94. if res.IsLimit == 1 { //没有被限制
  95. defer util.Limit()
  96. }
  97. }
  98. //无限制
  99. if res.IsLimit == 1 {
  100. var count int64
  101. var list = []*bxcore.SearchList{}
  102. //付费用户搜索优化
  103. publishTime := in.PublishTime
  104. t1 := time.Now()
  105. if b := util.IsOptimize(IC.C, in); b {
  106. count, list = util.GetBidSearchData(in)
  107. count += 1 //避免刚好50条 无法加载下一页数据
  108. }
  109. logx.Info("1查询耗时", time.Since(t1))
  110. t2 := time.Now()
  111. //分词后 第二页数据请求 先获取全部数据 再切割
  112. if in.SplitKeywords != "" && strings.Contains(in.SplitKeywords, "+&&&") && in.PageNum == 2 {
  113. in.KeyWords = strings.ReplaceAll(in.KeyWords, "+&&&", "")
  114. in.PageNum = 1
  115. }
  116. //不够配置条 走原始查询
  117. if count < IC.C.PaySearchLimit.PageSize {
  118. in.PublishTime = publishTime
  119. count, list = util.GetBidSearchData(in)
  120. }
  121. logx.Info("2查询耗时:", time.Since(t2))
  122. res.KeyWords = in.KeyWords
  123. //二次搜索- 一次搜索结果少于一页数据;关键词长度大于三;第一,二页请求;搜索范围包括title;四个条件
  124. if len([]rune(in.KeyWords)) > 3 && int(count) < util.SearchPageSize && in.PageNum < 3 && strings.Contains(in.SelectType, "title") {
  125. if iksk := util.HttpEs(in.KeyWords, "ik_smart", IC.DB.Es.Addr); iksk != "" {
  126. t3 := time.Now()
  127. iksk_temp := in.KeyWords
  128. in.KeyWords = iksk
  129. in.SelectType = "title"
  130. //最多查两页数据
  131. in.PageSize = 2 * in.PageSize
  132. _, secondList := util.GetBidSearchData(in)
  133. //数据合并 去重 排序
  134. list = util.DelRepeatSearchData(list, secondList)
  135. count = int64(len(list))
  136. switch {
  137. case count > util.SearchPageSize:
  138. count = MC.If(count > int64(util.SearchPageSize*2), int64(util.SearchPageSize*2), count).(int64)
  139. list = list[:util.SearchPageSize]
  140. if in.SplitKeywords != "" {
  141. list = list[util.SearchPageSize:count]
  142. }
  143. case count <= util.SearchPageSize:
  144. list = list[:count]
  145. }
  146. var kbool = map[string]bool{}
  147. var karr = []string{}
  148. for _, v := range strings.Split(fmt.Sprintf("%s+%s", iksk_temp, iksk), "+") {
  149. if kbool[v] {
  150. continue
  151. }
  152. karr = append(karr, v)
  153. kbool[v] = true
  154. }
  155. //+&&& 作为二次搜索第二页数据请求的标识(临时处理)
  156. res.KeyWords = strings.Join(karr, "+") + "+&&&"
  157. logx.Info("3查询耗时:", time.Since(t3))
  158. }
  159. }
  160. limitCount := MC.If(in.UserType != "fType", int64(util.SearchPageSize*util.SearchMaxPageNum_PAYED), int64(util.SearchPageSize*util.SearchMaxPageNum)).(int64)
  161. if count > limitCount {
  162. count = limitCount
  163. }
  164. //是否收藏
  165. //util.MakeCollection(in.UserId, list)
  166. res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
  167. res.Count = count
  168. res.List = list
  169. }
  170. logx.Info("关键词 -0- 查询耗时:", time.Since(t).Seconds())
  171. } else if in.Platform == "PC" {
  172. var count int64 = 0
  173. var list = []*bxcore.SearchList{}
  174. redisDataKey := fmt.Sprintf("PC_SearchDataCache_%s_%d", in.Platform, in.PageNum)
  175. sCache, err := redis.GetNewBytes(util.RedisName, redisDataKey)
  176. if err == nil {
  177. redisCountKey := fmt.Sprintf("PC_SearchCountCache_%s", in.Platform)
  178. count = int64(redis.GetInt(util.RedisName, redisCountKey))
  179. if sCache != nil && len(*sCache) > 0 {
  180. err = json.Unmarshal(*sCache, &list)
  181. if err != nil {
  182. return &bxcore.SearchResp{
  183. ErrCode: -1,
  184. ErrMsg: "缓存数据序列化异常:" + err.Error(),
  185. }, nil
  186. }
  187. } else {
  188. pcstime := IC.C.PCSTime * 60 * 60
  189. //缓存数据
  190. _in := &bxcore.SearchReq{
  191. PageNum: in.PageNum,
  192. PageSize: in.PageSize,
  193. }
  194. count, list = util.SearchCahcheData(_in)
  195. limitCount := int64(util.SearchPageSize * util.SearchMaxPageNum)
  196. if count > limitCount {
  197. count = limitCount
  198. }
  199. if len(list) > 0 {
  200. redis.Put(util.RedisName, redisCountKey, count, pcstime)
  201. b, err := json.Marshal(list)
  202. if err == nil {
  203. redis.PutBytes(util.RedisName, redisDataKey, &b, pcstime)
  204. } else {
  205. return &bxcore.SearchResp{
  206. ErrCode: -1,
  207. ErrMsg: "缓存数据 转化异常:" + err.Error(),
  208. }, nil
  209. }
  210. }
  211. }
  212. //是否收藏
  213. util.MakeCollection(in.UserId, list)
  214. res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
  215. res.Count = count
  216. res.List = list
  217. } else {
  218. return &bxcore.SearchResp{
  219. ErrCode: -1,
  220. ErrMsg: "查询redis缓存异常:" + err.Error(),
  221. }, nil
  222. }
  223. }
  224. logx.Info("关键词 -全部- 查询耗时:", time.Since(t).Seconds())
  225. return &bxcore.SearchResp{
  226. Data: res,
  227. ErrMsg: "",
  228. ErrCode: 0,
  229. }, nil
  230. }