getsearchlistlogic.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/service"
  8. "jyBXCore/rpc/util"
  9. "strings"
  10. "time"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. "jyBXCore/rpc/internal/svc"
  13. "jyBXCore/rpc/type/bxcore"
  14. "log"
  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. // GetSearchList 标讯搜索结果列表数据
  29. func (l *GetSearchListLogic) GetSearchList(in *bxcore.SearchReq) (*bxcore.SearchResp, error) {
  30. defer MC.Catch()
  31. res := &bxcore.SearchData{
  32. Count: 0,
  33. List: []*bxcore.SearchList{},
  34. InterceptLimit: int64(MC.IntAllDef(IC.C.KeywordsLimit, 35)),
  35. }
  36. //初始化搜索对象
  37. ks := entity.NewKeyWordsSearch()
  38. // 未登录用户只允许翻页和筛选搜索范围 其他参数无效
  39. if in.UserId == "" {
  40. in = &bxcore.SearchReq{
  41. AppId: in.AppId,
  42. PageNum: in.PageNum,
  43. PageSize: in.PageSize,
  44. SelectType: in.SelectType,
  45. KeyWords: in.KeyWords,
  46. Platform: in.Platform,
  47. UserType: in.UserType,
  48. SearchGroup: in.SearchGroup,
  49. Subtype: in.Subtype,
  50. TopType: in.TopType,
  51. }
  52. // 搜索范围只允许筛选 标题和正文 其他的过滤掉
  53. var selectType []string
  54. selectTypeSplit := strings.Split(in.SelectType, ",")
  55. // 未登录用户只能搜标题和正文
  56. for i := 0; i < len(selectTypeSplit); i++ {
  57. if selectTypeSplit[i] == "title" || selectTypeSplit[i] == "content" {
  58. selectType = append(selectType, selectTypeSplit[i])
  59. }
  60. }
  61. if len(selectType) == 0 {
  62. selectType = []string{"title", "content"}
  63. }
  64. //
  65. in.SelectType = strings.Join(selectType, ",")
  66. }
  67. isWhite, _ := service.IsOnTheWhitelist(in.UserId)
  68. //处理搜索条件
  69. heightWords := ks.SearchParamsHandle(in, isWhite)
  70. if (in.PageNum < 0 && in.PageSize < 0) || ks.IsSearch == "F" { //没有权限
  71. return &bxcore.SearchResp{
  72. Data: res,
  73. ErrMsg: "",
  74. ErrCode: 0,
  75. }, nil
  76. }
  77. //判断是否是空搜索,如果是空搜索,查缓存数据
  78. if ks.IsEmptySearch(in, isWhite) {
  79. log.Println("空查询分组", in.SearchGroup)
  80. res.List, res.Count, res.Total = ks.GetBidSearchListByCache(in)
  81. return &bxcore.SearchResp{
  82. Data: res,
  83. ErrMsg: "",
  84. ErrCode: 0,
  85. }, nil
  86. }
  87. //异常付费用户 参数不是免费fType,但是又不是付费用户;还有一种是未登录用户 稍后处理
  88. if in.UserType != "fType" && !in.IsPay {
  89. return &bxcore.SearchResp{
  90. ErrCode: -1,
  91. ErrMsg: "无权限",
  92. }, nil
  93. }
  94. t := time.Now()
  95. //招标信息有效查询
  96. res.IsLimit = 1
  97. //查询数据
  98. searchLimit := util.IsSearchLimit(strings.Split(in.SelectType, ","))
  99. //全文检索限制
  100. if searchLimit {
  101. res.IsLimit = util.IsLimited(in.LimitFlag, in.UserId, in.UserType != "fType", in.IsNew)
  102. if res.IsLimit == 1 { //没有被限制
  103. defer util.Limit()
  104. }
  105. }
  106. //无限制
  107. if res.IsLimit == 1 {
  108. //付费用户搜索优化--默认搜索5年数据,数据量太多,接口反应太慢,前两页数据 时间范围根据配置缩小查询以达到快速查询的目的。
  109. publishTime := in.PublishTime
  110. if b := util.IsOptimize(IC.C, in); b {
  111. t1 := time.Now()
  112. res.Count, res.Total, res.List = ks.GetBidSearchList(in) // util.GetBidSearchData(in)
  113. log.Println("1查询耗时:", time.Since(t1))
  114. }
  115. //如果优化查询数据量太少,和配置数据量作比较,不够的话走原始查询
  116. if res.Count < IC.C.PaySearchLimit.PageSize {
  117. t2 := time.Now()
  118. in.PublishTime = publishTime
  119. res.Count, res.Total, res.List = ks.GetBidSearchList(in) //util.GetBidSearchData(in)
  120. log.Println("2查询耗时:", time.Since(t2))
  121. }
  122. res.KeyWords = strings.Join(heightWords, " ")
  123. res.InterceptOtherWords = in.InterceptOtherWords
  124. res.InterceptKeyWords = in.InterceptKeyWords
  125. }
  126. log.Println("关键词 -全部- 查询耗时:", time.Since(t).Seconds())
  127. return &bxcore.SearchResp{
  128. Data: res,
  129. ErrMsg: "",
  130. ErrCode: 0,
  131. }, nil
  132. }