newestbiddinglogic.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "jyBXBase/rpc/init"
  9. "jyBXBase/rpc/model"
  10. "log"
  11. "strings"
  12. "time"
  13. "jyBXBase/rpc/internal/svc"
  14. "jyBXBase/rpc/type/bxbase"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. type NewestBiddingLogic struct {
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. logx.Logger
  21. }
  22. func NewNewestBiddingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NewestBiddingLogic {
  23. return &NewestBiddingLogic{
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. Logger: logx.WithContext(ctx),
  27. }
  28. }
  29. // 首页最新招标信息
  30. func (l *NewestBiddingLogic) NewestBidding(in *bxbase.NewestBiddingReq) (*bxbase.NewsetBiddingResp, error) {
  31. t := time.Now()
  32. r := func(in *bxbase.NewestBiddingReq) *bxbase.NewsetBiddingResp {
  33. var res = &bxbase.NewsetBiddingResp{
  34. Data: &bxbase.NewsetBidding{
  35. List: []*bxbase.NewestList{},
  36. },
  37. }
  38. rks := MC.If(in.UserId != "", in.UserId, in.City).(string)
  39. redisByte, err := redis.GetBytes("other", "p1_indexMessage_new_"+rks)
  40. if err == nil && redisByte != nil && len(*redisByte) > 0 {
  41. err := json.Unmarshal(*redisByte, res.Data)
  42. if err != nil {
  43. res.ErrCode = -1
  44. res.ErrMsg = fmt.Sprintf("读取缓存 序列化异常,err:%s", err.Error())
  45. }
  46. return res
  47. }
  48. //登录用户
  49. if in.UserId != "" {
  50. //获取订阅信息
  51. userMap, ok := IC.Mgo.FindById("user", rks, `{"o_jy":1,"o_vipjy":1,"i_vip_status":1}`)
  52. if !ok || userMap == nil || len(*userMap) == 0 {
  53. //查询出错
  54. res.ErrCode = -1
  55. res.ErrMsg = fmt.Errorf("未查询到用户信息").Error()
  56. return res
  57. }
  58. res.Data.History = strings.Split(redis.GetStr("other", "s_"+in.UserId), ",")
  59. //根据订阅词获取查询语句
  60. vipStatus := MC.IntAll((*userMap)["i_vip_status"])
  61. //vip用户
  62. if vipStatus > 0 {
  63. o_msgset := MC.ObjToMap((*userMap)["o_vipjy"])
  64. vip_items, ok := (*o_msgset)["a_items"].([]interface{})
  65. //vip查询推送历史
  66. result := model.GetPushHistory(rks)
  67. res.Data.IsVip = true
  68. res.Data.HasSubKeys = ok && len(vip_items) > 0
  69. res.Data.Count = int64(len(result))
  70. res.Data.List = result
  71. return res
  72. }
  73. //普通用户用户- 有关键词
  74. o_msgset := MC.ObjToMap((*userMap)["o_jy"])
  75. items, ok := (*o_msgset)["a_key"].([]interface{})
  76. if ok && len(items) > 0 {
  77. //普通用户查询推送历史
  78. result := model.GetPushHistory(rks)
  79. res.Data.IsVip = false
  80. res.Data.HasSubKeys = ok && len(items) > 0
  81. res.Data.Count = int64(len(result))
  82. res.Data.List = result
  83. return res
  84. }
  85. //搜索历史-关键词
  86. hKeys := redis.GetStr("other", fmt.Sprintf("s_%", in.UserId))
  87. if hKeys != "" && len(strings.Split(hKeys, ",")) > 0 {
  88. query := model.NewestQuery("", hKeys)
  89. result := model.NewestES(query)
  90. res.Data.IsVip = false
  91. res.Data.HasSubKeys = false
  92. res.Data.Count = int64(len(result))
  93. res.Data.List = result
  94. return res
  95. }
  96. }
  97. if in.IsSearch == 2 { //定位查询
  98. query := model.NewestQuery(rks, "")
  99. result := model.NewestES(query)
  100. res.Data.IsVip = false
  101. res.Data.HasSubKeys = false
  102. res.Data.Count = int64(len(result))
  103. res.Data.List = result
  104. return res
  105. }
  106. return res
  107. }(in)
  108. if r.Data.Count > 0 {
  109. rks := MC.If(in.UserId != "", in.UserId, in.City).(string)
  110. b, err := json.Marshal(r.Data)
  111. if err != nil {
  112. r.ErrCode = -1
  113. r.ErrMsg = fmt.Sprintf("保存缓存 序列化异常,err:%s", err.Error())
  114. } else {
  115. if err := redis.PutBytes("other", "p1_indexMessage_new_"+rks, &b, 2*60*60); err != nil {
  116. r.ErrCode = -1
  117. r.ErrMsg = fmt.Sprintf("保存缓存 redis 异常,err:%s", err.Error())
  118. }
  119. }
  120. }
  121. model.MakeCollection(in.UserId, r.Data.List)
  122. log.Println("接口耗时:", time.Since(t).Seconds())
  123. return r, nil
  124. }