getsearchlistlogic.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package logic
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "context"
  5. "jyBXCore/rpc/entity"
  6. IC "jyBXCore/rpc/init"
  7. "jyBXCore/rpc/util"
  8. "strings"
  9. "time"
  10. "jyBXCore/rpc/internal/svc"
  11. "jyBXCore/rpc/type/bxcore"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type GetSearchListLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewGetSearchListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSearchListLogic {
  20. return &GetSearchListLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. // GetSearchList 标讯搜索结果列表数据
  27. func (l *GetSearchListLogic) GetSearchList(in *bxcore.SearchReq) (*bxcore.SearchResp, error) {
  28. defer MC.Catch()
  29. //用户身份 是否登录
  30. if in.UserId == "" {
  31. return &bxcore.SearchResp{
  32. ErrCode: -1,
  33. ErrMsg: "用户信息异常",
  34. }, nil
  35. }
  36. res := &bxcore.SearchData{
  37. Count: 0,
  38. List: []*bxcore.SearchList{},
  39. InterceptLimit: int64(MC.IntAllDef(IC.C.KeywordsLimit, 35)),
  40. }
  41. //初始化搜索对象
  42. ks := entity.NewKeyWordsSearch()
  43. //处理搜索条件
  44. heightWords := ks.SearchParamsHandle(in)
  45. //判断是否是空搜索,如果是空搜索,查缓存数据
  46. if ks.IsEmptySearch(in) {
  47. res.List, res.Count, res.Total = ks.GetBidSearchListByCache(in)
  48. return &bxcore.SearchResp{
  49. Data: res,
  50. ErrMsg: "",
  51. ErrCode: 0,
  52. }, nil
  53. }
  54. //异常付费用户 参数不是免费fType,但是又不是付费用户;还有一种是未登录用户 稍后处理
  55. if in.UserType != "fType" && !in.IsPay {
  56. return &bxcore.SearchResp{
  57. ErrCode: -1,
  58. ErrMsg: "无权限",
  59. }, nil
  60. }
  61. t := time.Now()
  62. //招标信息有效查询
  63. res.IsLimit = 1
  64. <<<<<<< HEAD
  65. //以后可能会出现 关键词 C++ 等带+的关键词
  66. if in.KeyWords != "" {
  67. kwLimit := MC.IntAllDef(IC.C.KeywordsLimit, 35)
  68. if in.UserId == "" {
  69. kwLimit = 25
  70. }
  71. res.InterceptKeywords, res.InterceptWord, in.KeyWords = util.InterceptSearchKW(in.KeyWords, kwLimit, len(in.Industry) == 0)
  72. }
  73. if in.UserId == "" {
  74. //未登录用户访信息类型 需要过滤掉 拟建和采购意向问全部
  75. if in.Subtype == "" {
  76. in.Subtype = "招标预告,招标公告,招标结果,招标信用信息"
  77. }
  78. }
  79. //查询数据
  80. if in.KeyWords != "" || in.Industry != "" {
  81. //查询数据
  82. searchLimit := util.IsSearchLimit(strings.Split(in.SelectType, ","))
  83. //未登录用户标题、正文都限制,已登录用户只限制正文
  84. if in.UserId == "" {
  85. searchLimit = true
  86. }
  87. //全文检索限制
  88. if searchLimit {
  89. res.IsLimit = util.IsLimited(in.LimitFlag, in.UserId, in.UserType != "fType", in.IsNew)
  90. if res.IsLimit == 1 { //没有被限制
  91. defer util.Limit()
  92. }
  93. =======
  94. //查询数据
  95. searchLimit := util.IsSearchLimit(strings.Split(in.SelectType, ","))
  96. //全文检索限制
  97. if searchLimit {
  98. res.IsLimit = util.IsLimited(in.UserId, in.UserType != "fType")
  99. if res.IsLimit == 1 { //没有被限制
  100. defer util.Limit()
  101. >>>>>>> master
  102. }
  103. }
  104. //无限制
  105. if res.IsLimit == 1 {
  106. //付费用户搜索优化--默认搜索5年数据,数据量太多,接口反应太慢,前两页数据 时间范围根据配置缩小查询以达到快速查询的目的。
  107. publishTime := in.PublishTime
  108. if b := util.IsOptimize(IC.C, in); b {
  109. t1 := time.Now()
  110. res.Count, res.Total, res.List = ks.GetBidSearchList(in) // util.GetBidSearchData(in)
  111. logx.Info("1查询耗时:", time.Since(t1))
  112. }
  113. //如果优化查询数据量太少,和配置数据量作比较,不够的话走原始查询
  114. if res.Count < IC.C.PaySearchLimit.PageSize {
  115. t2 := time.Now()
  116. in.PublishTime = publishTime
  117. res.Count, res.Total, res.List = ks.GetBidSearchList(in) //util.GetBidSearchData(in)
  118. logx.Info("2查询耗时:", time.Since(t2))
  119. <<<<<<< HEAD
  120. res.KeyWords = in.KeyWords
  121. //二次搜索- 一次搜索结果少于一页数据;关键词长度大于三;第一,二页请求;搜索范围包括title;四个条件
  122. if len([]rune(in.KeyWords)) > 3 && int(count) < util.SearchPageSize && in.PageNum < 3 && strings.Contains(in.SelectType, "title") {
  123. if iksk := util.HttpEs(in.KeyWords, "ik_smart", IC.DB.Es.Addr); iksk != "" {
  124. t3 := time.Now()
  125. iksk_temp := in.KeyWords
  126. in.KeyWords = iksk
  127. in.SelectType = "title"
  128. //最多查两页数据
  129. in.PageSize = 2 * in.PageSize
  130. _, secondList := util.GetBidSearchData(in)
  131. //数据合并 去重 排序
  132. list = util.DelRepeatSearchData(list, secondList)
  133. count = int64(len(list))
  134. switch {
  135. case count > util.SearchPageSize:
  136. count = MC.If(count > int64(util.SearchPageSize*2), int64(util.SearchPageSize*2), count).(int64)
  137. list = list[:util.SearchPageSize]
  138. if in.SplitKeywords != "" {
  139. list = list[util.SearchPageSize:count]
  140. }
  141. case count <= util.SearchPageSize:
  142. list = list[:count]
  143. }
  144. var kbool = map[string]bool{}
  145. var karr = []string{}
  146. for _, v := range strings.Split(fmt.Sprintf("%s+%s", iksk_temp, iksk), "+") {
  147. if kbool[v] {
  148. continue
  149. }
  150. karr = append(karr, v)
  151. kbool[v] = true
  152. }
  153. //+&&& 作为二次搜索第二页数据请求的标识(临时处理)
  154. res.KeyWords = strings.Join(karr, "+") + "+&&&"
  155. logx.Info("3查询耗时:", time.Since(t3))
  156. }
  157. }
  158. limitCount := MC.If(in.UserType != "fType", int64(util.SearchPageSize*util.SearchMaxPageNum_PAYED), int64(util.SearchPageSize*util.SearchMaxPageNum)).(int64)
  159. if in.UserId == "" {
  160. limitCount = util.SearchMaxPageCount_NOLOGIN
  161. }
  162. if count > limitCount {
  163. count = limitCount
  164. }
  165. //是否收藏
  166. //util.MakeCollection(in.UserId, list)
  167. res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
  168. res.Count = count
  169. res.List = list
  170. }
  171. logx.Info("关键词 -0- 查询耗时:", time.Since(t).Seconds())
  172. } else if in.Platform == "PC" {
  173. var count int64 = 0
  174. var list = []*bxcore.SearchList{}
  175. redisDataKey := fmt.Sprintf("PC_SearchDataCache_%s_%d", in.Platform, in.PageNum)
  176. sCache, err := redis.GetNewBytes(util.RedisName, redisDataKey)
  177. if err == nil {
  178. redisCountKey := fmt.Sprintf("PC_SearchCountCache_%s", in.Platform)
  179. count = int64(redis.GetInt(util.RedisName, redisCountKey))
  180. if sCache != nil && len(*sCache) > 0 {
  181. err = json.Unmarshal(*sCache, &list)
  182. if err != nil {
  183. return &bxcore.SearchResp{
  184. ErrCode: -1,
  185. ErrMsg: "缓存数据序列化异常:" + err.Error(),
  186. }, nil
  187. }
  188. } else {
  189. pcstime := IC.C.PCSTime * 60 * 60
  190. //缓存数据
  191. _in := &bxcore.SearchReq{
  192. PageNum: in.PageNum,
  193. PageSize: in.PageSize,
  194. }
  195. count, list = util.SearchCahcheData(_in)
  196. limitCount := int64(util.SearchPageSize * util.SearchMaxPageNum)
  197. if in.UserId == "" {
  198. limitCount = util.SearchMaxPageCount_NOLOGIN
  199. }
  200. if count > limitCount {
  201. count = limitCount
  202. }
  203. if len(list) > 0 {
  204. redis.Put(util.RedisName, redisCountKey, count, pcstime)
  205. b, err := json.Marshal(list)
  206. if err == nil {
  207. redis.PutBytes(util.RedisName, redisDataKey, &b, pcstime)
  208. } else {
  209. return &bxcore.SearchResp{
  210. ErrCode: -1,
  211. ErrMsg: "缓存数据 转化异常:" + err.Error(),
  212. }, nil
  213. }
  214. }
  215. }
  216. //是否收藏
  217. util.MakeCollection(in.UserId, list)
  218. res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
  219. res.Count = count
  220. res.List = list
  221. } else {
  222. return &bxcore.SearchResp{
  223. ErrCode: -1,
  224. ErrMsg: "查询redis缓存异常:" + err.Error(),
  225. }, nil
  226. =======
  227. >>>>>>> master
  228. }
  229. res.KeyWords = strings.Join(heightWords, " ")
  230. res.InterceptOtherWords = in.InterceptOtherWords
  231. res.InterceptKeyWords = in.InterceptKeyWords
  232. }
  233. logx.Info("关键词 -全部- 查询耗时:", time.Since(t).Seconds())
  234. return &bxcore.SearchResp{
  235. Data: res,
  236. ErrMsg: "",
  237. ErrCode: 0,
  238. }, nil
  239. }