getsearchlistlogic.go 6.9 KB

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