getsearchlistlogic.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. InterceptLimit: int64(MC.IntAllDef(IC.C.KeywordsLimit, 35)),
  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. _, res.InterceptWord, in.KeyWords = util.InterceptSearchKW(in.KeyWords, MC.IntAllDef(IC.C.KeywordsLimit, 35), 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. t1 := time.Now()
  95. if b := util.IsOptimize(IC.C, in); b {
  96. count, list = util.GetBidSearchData(in)
  97. count += 1 //避免刚好50条 无法加载下一页数据
  98. }
  99. logx.Info("1查询耗时", time.Since(t1))
  100. t2 := time.Now()
  101. //分词后 第二页数据请求 先获取全部数据 再切割
  102. if in.SplitKeywords != "" && strings.Contains(in.SplitKeywords, "+&&&") && in.PageNum == 2 {
  103. in.KeyWords = strings.ReplaceAll(in.KeyWords, "+&&&", "")
  104. in.PageNum = 1
  105. }
  106. //不够配置条 走原始查询
  107. if count < IC.C.PaySearchLimit.PageSize {
  108. in.PublishTime = publishTime
  109. count, list = util.GetBidSearchData(in)
  110. }
  111. logx.Info("2查询耗时:", time.Since(t2))
  112. res.KeyWords = in.KeyWords
  113. //二次搜索- 一次搜索结果少于一页数据;关键词长度大于三;第一,二页请求;搜索范围包括title;四个条件
  114. if len([]rune(in.KeyWords)) > 3 && int(count) < util.SearchPageSize && in.PageNum < 3 && strings.Contains(in.SelectType, "title") {
  115. if iksk := util.HttpEs(in.KeyWords, "ik_smart", IC.DB.Es.Addr); iksk != "" {
  116. t3 := time.Now()
  117. iksk_temp := in.KeyWords
  118. in.KeyWords = iksk
  119. in.SelectType = "title"
  120. //最多查两页数据
  121. in.PageSize = 2 * in.PageSize
  122. _, secondList := util.GetBidSearchData(in)
  123. //数据合并 去重 排序
  124. list = util.DelRepeatSearchData(list, secondList)
  125. count = int64(len(list))
  126. switch {
  127. case count > util.SearchPageSize:
  128. count = MC.If(count > int64(util.SearchPageSize*2), int64(util.SearchPageSize*2), count).(int64)
  129. list = list[:util.SearchPageSize]
  130. if in.SplitKeywords != "" {
  131. list = list[util.SearchPageSize:count]
  132. }
  133. case count <= util.SearchPageSize:
  134. list = list[:count]
  135. }
  136. var kbool = map[string]bool{}
  137. var karr = []string{}
  138. for _, v := range strings.Split(fmt.Sprintf("%s+%s", iksk_temp, iksk), "+") {
  139. if kbool[v] {
  140. continue
  141. }
  142. karr = append(karr, v)
  143. kbool[v] = true
  144. }
  145. //+&&& 作为二次搜索第二页数据请求的标识(临时处理)
  146. res.KeyWords = strings.Join(karr, "+") + "+&&&"
  147. logx.Info("3查询耗时:", time.Since(t3))
  148. }
  149. }
  150. limitCount := MC.If(in.UserType != "fType", int64(util.SearchPageSize*util.SearchMaxPageNum_PAYED), int64(util.SearchPageSize*util.SearchMaxPageNum)).(int64)
  151. if count > limitCount {
  152. count = limitCount
  153. }
  154. //是否收藏
  155. //util.MakeCollection(in.UserId, list)
  156. res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
  157. res.Count = count
  158. res.List = list
  159. }
  160. logx.Info("关键词 -0- 查询耗时:", time.Since(t).Seconds())
  161. } else if in.Platform == "PC" {
  162. var count int64 = 0
  163. var list = []*bxcore.SearchList{}
  164. redisDataKey := fmt.Sprintf("PC_SearchDataCache_%s_%d", in.Platform, in.PageNum)
  165. sCache, err := redis.GetNewBytes(util.RedisName, redisDataKey)
  166. if err == nil {
  167. redisCountKey := fmt.Sprintf("PC_SearchCountCache_%s", in.Platform)
  168. count = int64(redis.GetInt(util.RedisName, redisCountKey))
  169. if sCache != nil && len(*sCache) > 0 {
  170. err = json.Unmarshal(*sCache, &list)
  171. if err != nil {
  172. return &bxcore.SearchResp{
  173. ErrCode: -1,
  174. ErrMsg: "缓存数据序列化异常:" + err.Error(),
  175. }, nil
  176. }
  177. } else {
  178. pcstime := IC.C.PCSTime * 60 * 60
  179. //缓存数据
  180. _in := &bxcore.SearchReq{
  181. PageNum: in.PageNum,
  182. PageSize: in.PageSize,
  183. }
  184. count, list = util.SearchCahcheData(_in)
  185. limitCount := int64(util.SearchPageSize * util.SearchMaxPageNum)
  186. if count > limitCount {
  187. count = limitCount
  188. }
  189. if len(list) > 0 {
  190. redis.Put(util.RedisName, redisCountKey, count, pcstime)
  191. b, err := json.Marshal(list)
  192. if err == nil {
  193. redis.PutBytes(util.RedisName, redisDataKey, &b, pcstime)
  194. } else {
  195. return &bxcore.SearchResp{
  196. ErrCode: -1,
  197. ErrMsg: "缓存数据 转化异常:" + err.Error(),
  198. }, nil
  199. }
  200. }
  201. }
  202. //是否收藏
  203. util.MakeCollection(in.UserId, list)
  204. res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
  205. res.Count = count
  206. res.List = list
  207. } else {
  208. return &bxcore.SearchResp{
  209. ErrCode: -1,
  210. ErrMsg: "查询redis缓存异常:" + err.Error(),
  211. }, nil
  212. }
  213. }
  214. logx.Info("关键词 -全部- 查询耗时:", time.Since(t).Seconds())
  215. return &bxcore.SearchResp{
  216. Data: res,
  217. ErrMsg: "",
  218. ErrCode: 0,
  219. }, nil
  220. }