getsearchlistlogic.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. }
  36. if in.UserId != "" {
  37. //历史记录
  38. history_str := redis.GetStr("other", "s_"+in.UserId)
  39. arrs := util.SearchHistory(history_str, in.KeyWords)
  40. //redis.Del("other", "s_"+in.UserId)
  41. if len(arrs) > 0 {
  42. redis.Put("other", "s_"+in.UserId, strings.Join(arrs, ","), -1)
  43. //res.HistoryKeys = arrs
  44. }
  45. }
  46. //
  47. if in.SelectType == "" {
  48. in.SelectType = "title,content"
  49. }
  50. in.Industry = strings.TrimSpace(in.Industry)
  51. userInfo := util.GetVipState(IC.MainMysql, IC.Mgo, in.UserId)
  52. //付费用户
  53. if in.UserType != "fType" && !userInfo.IsPayedUser() {
  54. return &bxcore.SearchResp{
  55. ErrCode: -1,
  56. ErrMsg: "无权限",
  57. }, nil
  58. }
  59. //搜索范围
  60. queryItems := userInfo.GetQueryItems(in.SelectType, IC.C.BidSearchOldUserLimit)
  61. if in.UserType == "fType" {
  62. in.BuyerClass = ""
  63. in.BuyerTel = ""
  64. in.WinnerTel = ""
  65. in.ExclusionWords = ""
  66. in.City = ""
  67. }
  68. in.KeyWords = strings.TrimSpace(in.KeyWords)
  69. searchLimit := util.IsSearchLimit(strings.Split(in.SelectType, ","))
  70. in.SelectType = strings.Join(queryItems, ",")
  71. //b_word, s_word := "", ""
  72. res.IsLimit = 1
  73. if in.KeyWords != "" {
  74. _, _, in.KeyWords = util.InterceptSearchKW(in.KeyWords, false, len(in.Industry) == 0)
  75. }
  76. //查询数据
  77. if in.KeyWords != "" || in.Industry != "" {
  78. //查询数据
  79. //全文检索限制
  80. if searchLimit {
  81. res.IsLimit = util.IsLimited(in.UserId, in.UserType != "fType")
  82. if res.IsLimit == 1 { //没有被限制
  83. defer util.Limit()
  84. }
  85. }
  86. //无限制
  87. if res.IsLimit == 1 {
  88. var count int64 = 0
  89. var list = []*bxcore.SearchList{}
  90. if !in.SecondSearch {
  91. count, list = util.GetBidSearchData(in)
  92. }
  93. //二次搜索- 一次搜索结果少于一页数据;关键词长度大于三;第一,二页请求;搜索范围包括title;四个条件
  94. if len([]rune(in.KeyWords)) > 3 && int(count) < util.SearchPageSize && in.PageNum < 3 && strings.Contains(in.SelectType, "title") {
  95. if in.KeyWords = util.HttpEs(in.KeyWords, "ik_smart", IC.DB.Es.Addr); in.KeyWords != "" {
  96. in.SelectType = "title"
  97. count, list = util.GetBidSearchData(in)
  98. //最多两页数据
  99. if count > util.SearchPageSize*2 {
  100. count = util.SearchPageSize * 2
  101. }
  102. res.KeyWords = in.KeyWords
  103. if in.PageNum == 1 {
  104. res.SecondSearch = true
  105. }
  106. }
  107. }
  108. limitCount := MC.If(in.UserType != "fType", int64(util.SearchPageSize*util.SearchMaxPageNum_PAYED), int64(util.SearchPageSize*util.SearchMaxPageNum)).(int64)
  109. if count > limitCount {
  110. count = limitCount
  111. }
  112. //是否收藏
  113. //util.MakeCollection(in.UserId, list)
  114. res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
  115. res.Count = count
  116. res.List = list
  117. }
  118. logx.Info("关键词 -0- 查询耗时:", time.Since(t).Seconds())
  119. } else if in.Platform == "PC" {
  120. var count int64 = 0
  121. var list = []*bxcore.SearchList{}
  122. redisDataKey := fmt.Sprintf("PC_SearchDataCache_%s_%d", in.Platform, in.PageNum)
  123. sCache, err := redis.GetNewBytes(util.RedisName, redisDataKey)
  124. if err == nil {
  125. redisCountKey := fmt.Sprintf("PC_SearchCountCache_%s", in.Platform)
  126. count = int64(redis.GetInt(util.RedisName, redisCountKey))
  127. if sCache != nil && len(*sCache) > 0 {
  128. err = json.Unmarshal(*sCache, &list)
  129. if err != nil {
  130. return &bxcore.SearchResp{
  131. ErrCode: -1,
  132. ErrMsg: "缓存数据序列化异常:" + err.Error(),
  133. }, nil
  134. }
  135. } else {
  136. pcstime := IC.C.PCSTime * 60 * 60
  137. //缓存数据
  138. _in := &bxcore.SearchReq{
  139. PageNum: in.PageNum,
  140. PageSize: in.PageSize,
  141. }
  142. count, list = util.SearchCahcheData(_in)
  143. limitCount := int64(util.SearchPageSize * util.SearchMaxPageNum)
  144. if count > limitCount {
  145. count = limitCount
  146. }
  147. if len(list) > 0 {
  148. redis.Put(util.RedisName, redisCountKey, count, pcstime)
  149. b, err := json.Marshal(list)
  150. if err == nil {
  151. redis.PutBytes(util.RedisName, redisDataKey, &b, pcstime)
  152. } else {
  153. return &bxcore.SearchResp{
  154. ErrCode: -1,
  155. ErrMsg: "缓存数据 转化异常:" + err.Error(),
  156. }, nil
  157. }
  158. }
  159. }
  160. //是否收藏
  161. util.MakeCollection(in.UserId, list)
  162. res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
  163. res.Count = count
  164. res.List = list
  165. } else {
  166. return &bxcore.SearchResp{
  167. ErrCode: -1,
  168. ErrMsg: "查询redis缓存异常:" + err.Error(),
  169. }, nil
  170. }
  171. }
  172. logx.Info("关键词 -全部- 查询耗时:", time.Since(t).Seconds())
  173. return &bxcore.SearchResp{
  174. Data: res,
  175. ErrMsg: "",
  176. ErrCode: 0,
  177. }, nil
  178. }