getsearchlistlogic.go 5.2 KB

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