getsearchlistlogic.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. "log"
  11. "strings"
  12. "time"
  13. "jyBXCore/rpc/internal/svc"
  14. "jyBXCore/rpc/type/bxcore"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. type GetSearchListLogic struct {
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. logx.Logger
  21. }
  22. func NewGetSearchListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSearchListLogic {
  23. return &GetSearchListLogic{
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. Logger: logx.WithContext(ctx),
  27. }
  28. }
  29. // 标讯搜索结果列表数据
  30. func (l *GetSearchListLogic) GetSearchList(in *bxcore.SearchReq) (*bxcore.SearchResp, error) {
  31. defer MC.Catch()
  32. t := time.Now()
  33. res := &bxcore.SearchData{
  34. Count: 0,
  35. List: []*bxcore.SearchList{},
  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. _, _, in.KeyWords = util.InterceptSearchKW(in.KeyWords, false, len(in.Industry) == 0)
  76. }
  77. //查询数据
  78. if in.KeyWords != "" || in.Industry != "" {
  79. //查询数据
  80. searchLimit := util.IsSearchLimit(strings.Split(in.SelectType, ","))
  81. //全文检索限制
  82. if searchLimit {
  83. res.IsLimit = util.IsLimited(in.UserId, in.UserType != "fType")
  84. if res.IsLimit == 1 { //没有被限制
  85. defer util.Limit()
  86. }
  87. }
  88. //无限制
  89. if res.IsLimit == 1 {
  90. var count int64
  91. var list = []*bxcore.SearchList{}
  92. //付费用户搜索优化
  93. publishTime := in.PublishTime
  94. if b := util.IsOptimize(IC.C, in); b {
  95. log.Println(in.PublishTime, "----b:", b)
  96. count, list = util.GetBidSearchData(in)
  97. }
  98. //分词后 第二页数据请求 先获取全部数据 再切割
  99. if in.SplitKeywords != "" && strings.Contains(in.SplitKeywords, "+&&&") && in.PageNum == 2 {
  100. in.KeyWords = strings.ReplaceAll(in.KeyWords, "+&&&", "")
  101. in.PageNum = 1
  102. }
  103. //不够五十条 走原始查询
  104. if count < util.SearchPageSize {
  105. in.PublishTime = publishTime
  106. count, list = util.GetBidSearchData(in)
  107. }
  108. res.KeyWords = in.KeyWords
  109. //二次搜索- 一次搜索结果少于一页数据;关键词长度大于三;第一,二页请求;搜索范围包括title;四个条件
  110. if len([]rune(in.KeyWords)) > 3 && int(count) < util.SearchPageSize && in.PageNum < 3 && strings.Contains(in.SelectType, "title") {
  111. if iksk := util.HttpEs(in.KeyWords, "ik_smart", IC.DB.Es.Addr); iksk != "" {
  112. iksk_temp := in.KeyWords
  113. in.KeyWords = iksk
  114. in.SelectType = "title"
  115. //最多查两页数据
  116. in.PageSize = 2 * in.PageSize
  117. _, secondList := util.GetBidSearchData(in)
  118. //数据合并 去重 排序
  119. list = util.DelRepeatSearchData(list, secondList)
  120. count = int64(len(list))
  121. switch {
  122. case count > util.SearchPageSize:
  123. count = MC.If(count > int64(util.SearchPageSize*2), int64(util.SearchPageSize*2), count).(int64)
  124. list = list[:util.SearchPageSize]
  125. if in.SplitKeywords != "" {
  126. list = list[util.SearchPageSize:count]
  127. }
  128. case count <= util.SearchPageSize:
  129. list = list[:count]
  130. }
  131. var kbool = map[string]bool{}
  132. var karr = []string{}
  133. for _, v := range strings.Split(fmt.Sprintf("%s+%s", iksk_temp, iksk), "+") {
  134. if kbool[v] {
  135. continue
  136. }
  137. karr = append(karr, v)
  138. kbool[v] = true
  139. }
  140. //+&&& 作为二次搜索第二页数据请求的标识(临时处理)
  141. res.KeyWords = strings.Join(karr, "+") + "+&&&"
  142. }
  143. }
  144. limitCount := MC.If(in.UserType != "fType", int64(util.SearchPageSize*util.SearchMaxPageNum_PAYED), int64(util.SearchPageSize*util.SearchMaxPageNum)).(int64)
  145. if count > limitCount {
  146. count = limitCount
  147. }
  148. //是否收藏
  149. //util.MakeCollection(in.UserId, list)
  150. res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
  151. res.Count = count
  152. res.List = list
  153. }
  154. logx.Info("关键词 -0- 查询耗时:", time.Since(t).Seconds())
  155. } else if in.Platform == "PC" {
  156. var count int64 = 0
  157. var list = []*bxcore.SearchList{}
  158. redisDataKey := fmt.Sprintf("PC_SearchDataCache_%s_%d", in.Platform, in.PageNum)
  159. sCache, err := redis.GetNewBytes(util.RedisName, redisDataKey)
  160. if err == nil {
  161. redisCountKey := fmt.Sprintf("PC_SearchCountCache_%s", in.Platform)
  162. count = int64(redis.GetInt(util.RedisName, redisCountKey))
  163. if sCache != nil && len(*sCache) > 0 {
  164. err = json.Unmarshal(*sCache, &list)
  165. if err != nil {
  166. return &bxcore.SearchResp{
  167. ErrCode: -1,
  168. ErrMsg: "缓存数据序列化异常:" + err.Error(),
  169. }, nil
  170. }
  171. } else {
  172. pcstime := IC.C.PCSTime * 60 * 60
  173. //缓存数据
  174. _in := &bxcore.SearchReq{
  175. PageNum: in.PageNum,
  176. PageSize: in.PageSize,
  177. }
  178. count, list = util.SearchCahcheData(_in)
  179. limitCount := int64(util.SearchPageSize * util.SearchMaxPageNum)
  180. if count > limitCount {
  181. count = limitCount
  182. }
  183. if len(list) > 0 {
  184. redis.Put(util.RedisName, redisCountKey, count, pcstime)
  185. b, err := json.Marshal(list)
  186. if err == nil {
  187. redis.PutBytes(util.RedisName, redisDataKey, &b, pcstime)
  188. } else {
  189. return &bxcore.SearchResp{
  190. ErrCode: -1,
  191. ErrMsg: "缓存数据 转化异常:" + err.Error(),
  192. }, nil
  193. }
  194. }
  195. }
  196. //是否收藏
  197. util.MakeCollection(in.UserId, list)
  198. res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
  199. res.Count = count
  200. res.List = list
  201. } else {
  202. return &bxcore.SearchResp{
  203. ErrCode: -1,
  204. ErrMsg: "查询redis缓存异常:" + err.Error(),
  205. }, nil
  206. }
  207. }
  208. logx.Info("关键词 -全部- 查询耗时:", time.Since(t).Seconds())
  209. return &bxcore.SearchResp{
  210. Data: res,
  211. ErrMsg: "",
  212. ErrCode: 0,
  213. }, nil
  214. }