newestbiddinglogic.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package logic
  2. import (
  3. "context"
  4. "jyBXBase/rpc/model"
  5. "log"
  6. "sort"
  7. "time"
  8. "jyBXBase/rpc/internal/svc"
  9. "jyBXBase/rpc/type/bxbase"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type NewestBiddingLogic struct {
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. logx.Logger
  16. }
  17. func NewNewestBiddingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NewestBiddingLogic {
  18. return &NewestBiddingLogic{
  19. ctx: ctx,
  20. svcCtx: svcCtx,
  21. Logger: logx.WithContext(ctx),
  22. }
  23. }
  24. // 首页最新招标信息
  25. func (l *NewestBiddingLogic) NewestBidding(in *bxbase.NewestBiddingReq) (*bxbase.NewsetBiddingResp, error) {
  26. t := time.Now()
  27. // status : 0 -不存缓存 1-未登录用户 2-用户自己的key 3-登录用户最新标讯
  28. r, status := func(in *bxbase.NewestBiddingReq) (*bxbase.NewsetBiddingResp, int) {
  29. var res = &bxbase.NewsetBiddingResp{
  30. Data: &bxbase.NewsetBidding{
  31. List: []*bxbase.NewestList{},
  32. },
  33. }
  34. /* 1. 未登录用户 查未登录缓存 返回 没有缓存 查最新标讯存未登录返回
  35. 2. 登录用户 查count count=1 查用户缓存 缓存没数据查用户推送
  36. count =0 查最新标讯缓存 缓存没数据查最新标讯存登录缓存 显示引导用户设置关键词
  37. */
  38. // 未登录用户
  39. if in.UserId == "" {
  40. res.Data.ShowTip = 1
  41. redisKey, _ := model.GetRedisKeyTimeout(model.StatusNoLogin, in.PositionId)
  42. list, err := model.GetNewsCache(redisKey)
  43. if err == nil && list != nil && len(list) > 0 { // 缓存有数据可以直接返回
  44. res.Data.List = list
  45. res.Data.Count = int64(len(list))
  46. return res, model.StatusCache
  47. }
  48. // 没数据查最新数据返回
  49. //未登录用户访问全部信息类型 需要过滤掉 拟建和采购意向
  50. subtype := `"预告","公告","结果","其它"`
  51. query := model.NewestQuery("", "", subtype)
  52. list = model.NewestES(query)
  53. res.Data.List = list
  54. res.Data.Count = int64(len(list))
  55. return res, model.StatusNoLogin
  56. }
  57. // 登录根据用户
  58. roleNewestInfo, flag := model.GetRoleNewestInfoService(in.AppId, in.MgoUserId, in.NewUserId, in.AccountId, in.EntId, in.EntUserId, in.PositionType, in.PositionId)
  59. res.Data.SubFlag = flag
  60. // 查count判断
  61. existData := roleNewestInfo.GetPushHistoryCount()
  62. if existData > 0 { // 存在推送 查推送缓存 缓存没有数据则查 推送数据
  63. res.Data.ShowTip = 1 // count>0说明存在数据不用显示引导用户设置关键词
  64. // 查缓存
  65. redisKey, _ := model.GetRedisKeyTimeout(model.StatusLoginUser, in.PositionId)
  66. list, err := model.GetNewsCache(redisKey)
  67. if err == nil && list != nil && len(list) > 0 {
  68. res.Data.List = list
  69. res.Data.Count = int64(len(list))
  70. return res, model.StatusCache
  71. }
  72. // 查推送
  73. list = roleNewestInfo.GetPushHistory()
  74. if list != nil && len(list) > 0 {
  75. res.Data.List = list
  76. res.Data.Count = int64(len(list))
  77. return res, model.StatusLoginUser
  78. }
  79. }
  80. // 等0则说明推送里面没有数据 所以去查最新标讯信息缓存 返回 缓存没有则查最新标讯信息 存到缓存里面
  81. res.Data.ShowTip = 2 // 显示
  82. // 查缓存
  83. redisKey, _ := model.GetRedisKeyTimeout(model.StatusLogin, in.PositionId)
  84. list, err := model.GetNewsCache(redisKey)
  85. if err == nil && list != nil && len(list) > 0 {
  86. res.Data.List = list
  87. res.Data.Count = int64(len(list))
  88. return res, model.StatusCache
  89. }
  90. // 查最新标讯
  91. query := model.NewestQuery("", "", "")
  92. list = model.NewestES(query)
  93. res.Data.List = list
  94. res.Data.Count = int64(len(list))
  95. return res, model.StatusLogin
  96. }(in)
  97. if r.Data.Count == 0 {
  98. return r, nil
  99. }
  100. // status : 0 -拿到的是缓存 不用再处理也不用存缓存 1-存到未登录用户 2-存到用户自己的key 3-存到登录用户最新标讯
  101. if status != model.StatusCache {
  102. //排序
  103. sort.Slice(r.Data.List, func(i, j int) bool {
  104. return r.Data.List[i].PublishTime > r.Data.List[j].PublishTime
  105. })
  106. redisKey, timeout := model.GetRedisKeyTimeout(status, in.PositionId)
  107. go model.PutNewsCache(redisKey, timeout, r.Data.List)
  108. }
  109. model.MakeCollection(in.UserId, r.Data.List)
  110. log.Println("接口耗时:", time.Since(t).Seconds())
  111. return r, nil
  112. }