getsearchlistlogic.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "github.com/zeromicro/go-zero/core/logx"
  11. "jyBXCore/rpc/internal/svc"
  12. "jyBXCore/rpc/type/bxcore"
  13. "log"
  14. )
  15. type GetSearchListLogic struct {
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. logx.Logger
  19. }
  20. func NewGetSearchListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSearchListLogic {
  21. return &GetSearchListLogic{
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. Logger: logx.WithContext(ctx),
  25. }
  26. }
  27. // GetSearchList 标讯搜索结果列表数据
  28. func (l *GetSearchListLogic) GetSearchList(in *bxcore.SearchReq) (*bxcore.SearchResp, error) {
  29. defer MC.Catch()
  30. res := &bxcore.SearchData{
  31. Count: 0,
  32. List: []*bxcore.SearchList{},
  33. InterceptLimit: int64(MC.IntAllDef(IC.C.KeywordsLimit, 35)),
  34. }
  35. //初始化搜索对象
  36. ks := entity.NewKeyWordsSearch()
  37. // 未登录用户只允许翻页和筛选搜索范围 其他参数无效
  38. if in.UserId == "" {
  39. in = &bxcore.SearchReq{
  40. AppId: in.AppId,
  41. PageNum: in.PageNum,
  42. PageSize: in.PageSize,
  43. SelectType: in.SelectType,
  44. KeyWords: in.KeyWords,
  45. Platform: in.Platform,
  46. UserType: in.UserType,
  47. SearchGroup: in.SearchGroup,
  48. Subtype: in.Subtype,
  49. TopType: in.TopType,
  50. }
  51. // 搜索范围只允许筛选 标题和正文 其他的过滤掉
  52. var selectType []string
  53. selectTypeSplit := strings.Split(in.SelectType, ",")
  54. // 未登录用户只能搜标题和正文
  55. for i := 0; i < len(selectTypeSplit); i++ {
  56. if selectTypeSplit[i] == "title" || selectTypeSplit[i] == "content" {
  57. selectType = append(selectType, selectTypeSplit[i])
  58. }
  59. }
  60. if len(selectType) == 0 {
  61. selectType = []string{"title", "content"}
  62. }
  63. //
  64. in.SelectType = strings.Join(selectType, ",")
  65. }
  66. //处理搜索条件
  67. heightWords := ks.SearchParamsHandle(in)
  68. if in.PageNum < 0 && in.PageSize < 0 {
  69. return &bxcore.SearchResp{
  70. Data: res,
  71. ErrMsg: "",
  72. ErrCode: 0,
  73. }, nil
  74. }
  75. //判断是否是空搜索,如果是空搜索,查缓存数据
  76. if ks.IsEmptySearch(in) {
  77. log.Println("空查询分组", in.SearchGroup)
  78. res.List, res.Count, res.Total = ks.GetBidSearchListByCache(in)
  79. return &bxcore.SearchResp{
  80. Data: res,
  81. ErrMsg: "",
  82. ErrCode: 0,
  83. }, nil
  84. }
  85. //异常付费用户 参数不是免费fType,但是又不是付费用户;还有一种是未登录用户 稍后处理
  86. if in.UserType != "fType" && !in.IsPay {
  87. return &bxcore.SearchResp{
  88. ErrCode: -1,
  89. ErrMsg: "无权限",
  90. }, nil
  91. }
  92. t := time.Now()
  93. //招标信息有效查询
  94. res.IsLimit = 1
  95. //查询数据
  96. searchLimit := util.IsSearchLimit(strings.Split(in.SelectType, ","))
  97. //全文检索限制
  98. if searchLimit {
  99. res.IsLimit = util.IsLimited(in.LimitFlag, in.UserId, in.UserType != "fType", in.IsNew)
  100. if res.IsLimit == 1 { //没有被限制
  101. defer util.Limit()
  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. log.Println("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. log.Println("2查询耗时:", time.Since(t2))
  119. }
  120. res.KeyWords = strings.Join(heightWords, " ")
  121. res.InterceptOtherWords = in.InterceptOtherWords
  122. res.InterceptKeyWords = in.InterceptKeyWords
  123. }
  124. log.Println("关键词 -全部- 查询耗时:", time.Since(t).Seconds())
  125. return &bxcore.SearchResp{
  126. Data: res,
  127. ErrMsg: "",
  128. ErrCode: 0,
  129. }, nil
  130. }