newestbiddinglogic.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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,"o_member_jy":1,"i_member_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. vipStatus := MC.IntAll((*userMap)["i_vip_status"])
  59. //vip用户
  60. if vipStatus > 0 {
  61. o_msgset := MC.ObjToMap((*userMap)["o_vipjy"])
  62. vip_items, ok := (*o_msgset)["a_items"].([]interface{})
  63. //vip查询推送历史
  64. result := model.GetPushHistory(rks)
  65. res.Data.IsVip = true
  66. res.Data.HasSubKeys = ok && len(vip_items) > 0
  67. res.Data.Count = int64(len(result))
  68. res.Data.List = result
  69. return res
  70. }
  71. //普通用户用户- 有关键词
  72. o_msgset := MC.ObjToMap((*userMap)["o_jy"])
  73. items, ok := (*o_msgset)["a_key"].([]interface{})
  74. if ok && len(items) > 0 {
  75. //普通用户查询推送历史
  76. result := model.GetPushHistory(rks)
  77. res.Data.IsVip = false
  78. res.Data.HasSubKeys = ok && len(items) > 0
  79. res.Data.Count = int64(len(result))
  80. res.Data.List = result
  81. return res
  82. }
  83. //搜索历史-关键词
  84. hKeys := redis.GetStr("other", fmt.Sprintf("s_%", in.UserId, in.UserId))
  85. if hKeys != "" && len(strings.Split(hKeys, ",")) > 0 {
  86. //历史搜索
  87. res.Data.History = strings.Split(hKeys, ",")
  88. //根据订阅词获取查询语句
  89. query := model.NewestQuery("", hKeys)
  90. result := model.NewestES(query)
  91. res.Data.IsVip = false
  92. res.Data.HasSubKeys = false
  93. res.Data.Count = int64(len(result))
  94. res.Data.List = result
  95. return res
  96. }
  97. }
  98. if in.IsSearch == 2 { //定位查询
  99. query := model.NewestQuery(rks, "")
  100. result := model.NewestES(query)
  101. res.Data.IsVip = false
  102. res.Data.HasSubKeys = false
  103. res.Data.Count = int64(len(result))
  104. res.Data.List = result
  105. return res
  106. }
  107. return res
  108. }(in)
  109. if r.Data.Count > 0 {
  110. rks := MC.If(in.UserId != "", in.UserId, in.City).(string)
  111. b, err := json.Marshal(r.Data)
  112. if err != nil {
  113. r.ErrCode = -1
  114. r.ErrMsg = fmt.Sprintf("保存缓存 序列化异常,err:%s", err.Error())
  115. } else {
  116. if err := redis.PutBytes("other", "p1_indexMessage_new_"+rks, &b, 2*60*60); err != nil {
  117. r.ErrCode = -1
  118. r.ErrMsg = fmt.Sprintf("保存缓存 redis 异常,err:%s", err.Error())
  119. }
  120. }
  121. }
  122. model.MakeCollection(in.UserId, r.Data.List)
  123. log.Println("接口耗时:", time.Since(t).Seconds())
  124. return r, nil
  125. }