getsearchlistlogic.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 := true
  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. //limitFlag = public.Lst.Flag
  80. //isLimit = public.Lst.IsLimited(m.Request, m.ResponseWriter, m.Session(), isPayedUser)
  81. //if isLimit == 1 { //没有被限制
  82. // defer public.Lst.Limit()
  83. //}
  84. }
  85. //无限制
  86. if res.IsLimit == 1 {
  87. var count int64 = 0
  88. var list = []*bxcore.SearchList{}
  89. if !in.SecondSearch {
  90. count, list = util.GetBidSearchData(in)
  91. }
  92. //二次搜索- 一次搜索结果少于一页数据;关键词长度大于三;第一,二页请求;搜索范围包括title;四个条件
  93. if len([]rune(in.KeyWords)) > 3 && int(count) < util.SearchPageSize && in.PageNum < 3 && strings.Contains(in.SelectType, "title") {
  94. if in.KeyWords = util.HttpEs(in.KeyWords, "ik_smart", IC.DB.Es.Addr); in.KeyWords != "" {
  95. count, list = util.GetBidSearchData(in)
  96. //最多两页数据
  97. if count > util.SearchPageSize*2 {
  98. count = util.SearchPageSize * 2
  99. }
  100. res.KeyWords = in.KeyWords
  101. if in.PageNum == 1 {
  102. res.SecondSearch = true
  103. }
  104. }
  105. }
  106. limitCount := MC.If(in.UserType != "fType", int64(util.SearchPageSize*util.SearchMaxPageNum_PAYED), int64(util.SearchPageSize*util.SearchMaxPageNum)).(int64)
  107. if count > limitCount {
  108. count = limitCount
  109. }
  110. res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
  111. res.Count = count
  112. res.List = list
  113. }
  114. } else if in.Platform == "PC" {
  115. var count int64 = 0
  116. var list = []*bxcore.SearchList{}
  117. redisDataKey := fmt.Sprintf("PC_SearchDataCache_%s_%d", in.Platform, in.PageNum)
  118. sCache, err := redis.GetNewBytes(util.RedisName, redisDataKey)
  119. if err == nil {
  120. redisCountKey := fmt.Sprintf("PC_SearchCountCache_%s", in.Platform)
  121. count = int64(redis.GetInt(util.RedisName, redisCountKey))
  122. if sCache != nil && len(*sCache) > 0 {
  123. err = json.Unmarshal(*sCache, &list)
  124. if err != nil {
  125. return &bxcore.SearchResp{
  126. ErrCode: -1,
  127. ErrMsg: "缓存数据序列化异常:" + err.Error(),
  128. }, nil
  129. }
  130. } else {
  131. pcstime := IC.C.PCSTime * 60 * 60
  132. //缓存数据
  133. _in := &bxcore.SearchReq{
  134. PageNum: in.PageNum,
  135. PageSize: in.PageSize,
  136. }
  137. count, list = util.SearchCahcheData(_in)
  138. limitCount := int64(util.SearchPageSize * util.SearchMaxPageNum)
  139. if count > limitCount {
  140. count = limitCount
  141. }
  142. if len(list) > 0 {
  143. redis.Put(util.RedisName, redisCountKey, count, pcstime)
  144. b, err := json.Marshal(list)
  145. if err == nil {
  146. redis.PutBytes(util.RedisName, redisDataKey, &b, pcstime)
  147. } else {
  148. return &bxcore.SearchResp{
  149. ErrCode: -1,
  150. ErrMsg: "缓存数据 转化异常:" + err.Error(),
  151. }, nil
  152. }
  153. }
  154. }
  155. //是否收藏
  156. util.MakeCollection(in.UserId, list)
  157. res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
  158. res.Count = count
  159. res.List = list
  160. } else {
  161. return &bxcore.SearchResp{
  162. ErrCode: -1,
  163. ErrMsg: "查询redis缓存异常:" + err.Error(),
  164. }, nil
  165. }
  166. }
  167. return &bxcore.SearchResp{
  168. Data: res,
  169. ErrMsg: "",
  170. ErrCode: 0,
  171. }, nil
  172. }