getsearchlistlogic.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package logic
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "context"
  5. "fmt"
  6. "jyBXCore/rpc/entity"
  7. IC "jyBXCore/rpc/init"
  8. "jyBXCore/rpc/util"
  9. "strings"
  10. "time"
  11. "jyBXCore/rpc/internal/svc"
  12. "jyBXCore/rpc/type/bxcore"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type GetSearchListLogic struct {
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. logx.Logger
  19. }
  20. func NewGetSearchListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSearchListLogic {
  21. return &GetSearchListLogic{
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. Logger: logx.WithContext(ctx),
  25. }
  26. }
  27. // 标讯搜索结果列表数据
  28. func (l *GetSearchListLogic) GetSearchList(in *bxcore.SearchReq) (*bxcore.SearchResp, error) {
  29. defer MC.Catch()
  30. //用户身份 是否登录
  31. if in.UserId == "" {
  32. return &bxcore.SearchResp{
  33. ErrCode: -1,
  34. ErrMsg: "用户信息异常",
  35. }, nil
  36. }
  37. res := &bxcore.SearchData{
  38. Count: 0,
  39. List: []*bxcore.SearchList{},
  40. InterceptLimit: int64(MC.IntAllDef(IC.C.KeywordsLimit, 35)),
  41. NumberMsg: IC.C.DefaultBidInfoNum,
  42. }
  43. //初始化搜索对象
  44. ks := entity.NewKeyWordsSearch(in)
  45. //判断是否是空搜索,如果是空搜索,查缓存数据
  46. if ks.IsEmptySearch() {
  47. res.List, res.Count = ks.GetBidListByCache()
  48. return &bxcore.SearchResp{
  49. Data: res,
  50. ErrMsg: "",
  51. ErrCode: 0,
  52. }, nil
  53. }
  54. //更新关键词搜索历史记录
  55. go ks.SaveKeyWordsToHistory()
  56. //判断搜索是否有查询条件
  57. //判断用户身份
  58. t := time.Now()
  59. //
  60. if in.SelectType == "" {
  61. in.SelectType = "title,content"
  62. }
  63. in.Industry = strings.TrimSpace(in.Industry)
  64. userInfo := util.GetVipState(IC.MainMysql, IC.Mgo, in.UserId)
  65. //付费用户
  66. if in.UserType != "fType" && !userInfo.IsPayedUser() {
  67. return &bxcore.SearchResp{
  68. ErrCode: -1,
  69. ErrMsg: "无权限",
  70. }, nil
  71. }
  72. if in.UserType == "fType" {
  73. in.BuyerClass = ""
  74. in.BuyerTel = ""
  75. in.WinnerTel = ""
  76. in.ExclusionWords = ""
  77. in.City = ""
  78. }
  79. in.KeyWords = strings.TrimSpace(in.KeyWords)
  80. //搜索范围
  81. queryItems := userInfo.GetQueryItems(in.SelectType, IC.C.BidSearchOldUserLimit)
  82. in.SelectType = strings.Join(queryItems, ",")
  83. //b_word, s_word := "", ""
  84. res.IsLimit = 1
  85. //以后可能会出现 关键词 C++ 等带+的关键词
  86. if in.KeyWords != "" {
  87. res.InterceptKeywords, res.InterceptWord, in.KeyWords = util.InterceptSearchKW(in.KeyWords, MC.IntAllDef(IC.C.KeywordsLimit, 35), len(in.Industry) == 0)
  88. }
  89. //查询数据
  90. if in.KeyWords != "" || in.Industry != "" {
  91. //查询数据
  92. searchLimit := util.IsSearchLimit(strings.Split(in.SelectType, ","))
  93. //全文检索限制
  94. if searchLimit {
  95. res.IsLimit = util.IsLimited(in.UserId, in.UserType != "fType")
  96. if res.IsLimit == 1 { //没有被限制
  97. defer util.Limit()
  98. }
  99. }
  100. //无限制
  101. if res.IsLimit == 1 {
  102. var count int64
  103. var list = []*bxcore.SearchList{}
  104. //付费用户搜索优化
  105. publishTime := in.PublishTime
  106. t1 := time.Now()
  107. if b := util.IsOptimize(IC.C, in); b {
  108. count, list = util.GetBidSearchData(in)
  109. count += 1 //避免刚好50条 无法加载下一页数据
  110. }
  111. logx.Info("1查询耗时", time.Since(t1))
  112. t2 := time.Now()
  113. //分词后 第二页数据请求 先获取全部数据 再切割
  114. if in.SplitKeywords != "" && strings.Contains(in.SplitKeywords, "+&&&") && in.PageNum == 2 {
  115. in.KeyWords = strings.ReplaceAll(in.KeyWords, "+&&&", "")
  116. in.PageNum = 1
  117. }
  118. //不够配置条 走原始查询
  119. if count < IC.C.PaySearchLimit.PageSize {
  120. in.PublishTime = publishTime
  121. count, list = util.GetBidSearchData(in)
  122. }
  123. logx.Info("2查询耗时:", time.Since(t2))
  124. res.KeyWords = in.KeyWords
  125. //二次搜索- 一次搜索结果少于一页数据;关键词长度大于三;第一,二页请求;搜索范围包括title;四个条件
  126. if len([]rune(in.KeyWords)) > 3 && int(count) < util.SearchPageSize && in.PageNum < 3 && strings.Contains(in.SelectType, "title") {
  127. if iksk := util.HttpEs(in.KeyWords, "ik_smart", IC.DB.Es.Addr); iksk != "" {
  128. t3 := time.Now()
  129. iksk_temp := in.KeyWords
  130. in.KeyWords = iksk
  131. in.SelectType = "title"
  132. //最多查两页数据
  133. in.PageSize = 2 * in.PageSize
  134. _, secondList := util.GetBidSearchData(in)
  135. //数据合并 去重 排序
  136. list = util.DelRepeatSearchData(list, secondList)
  137. count = int64(len(list))
  138. switch {
  139. case count > util.SearchPageSize:
  140. count = MC.If(count > int64(util.SearchPageSize*2), int64(util.SearchPageSize*2), count).(int64)
  141. list = list[:util.SearchPageSize]
  142. if in.SplitKeywords != "" {
  143. list = list[util.SearchPageSize:count]
  144. }
  145. case count <= util.SearchPageSize:
  146. list = list[:count]
  147. }
  148. var kbool = map[string]bool{}
  149. var karr = []string{}
  150. for _, v := range strings.Split(fmt.Sprintf("%s+%s", iksk_temp, iksk), "+") {
  151. if kbool[v] {
  152. continue
  153. }
  154. karr = append(karr, v)
  155. kbool[v] = true
  156. }
  157. //+&&& 作为二次搜索第二页数据请求的标识(临时处理)
  158. res.KeyWords = strings.Join(karr, "+") + "+&&&"
  159. logx.Info("3查询耗时:", time.Since(t3))
  160. }
  161. }
  162. limitCount := MC.If(in.UserType != "fType", int64(util.SearchPageSize*util.SearchMaxPageNum_PAYED), int64(util.SearchPageSize*util.SearchMaxPageNum)).(int64)
  163. if count > limitCount {
  164. count = limitCount
  165. }
  166. //是否收藏
  167. //util.MakeCollection(in.UserId, list)
  168. res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
  169. res.Count = count
  170. res.List = list
  171. }
  172. logx.Info("关键词 -0- 查询耗时:", time.Since(t).Seconds())
  173. } else if in.Platform == "PC" {
  174. }
  175. logx.Info("关键词 -全部- 查询耗时:", time.Since(t).Seconds())
  176. return &bxcore.SearchResp{
  177. Data: res,
  178. ErrMsg: "",
  179. ErrCode: 0,
  180. }, nil
  181. }