getsearchlistlogic.go 7.6 KB

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