蛋蛋星球RabbitMq消费项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

102 lines
2.5 KiB

  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/utils/logx"
  6. "applet/consume/md"
  7. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  8. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  9. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy"
  10. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  11. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  12. "encoding/json"
  13. "errors"
  14. "fmt"
  15. "github.com/streadway/amqp"
  16. "time"
  17. )
  18. func AdvertisingSignConsume(queue md.MqQueue) {
  19. fmt.Println(">>>>>>>>>>>>JpushRecordConsume>>>>>>>>>>>>")
  20. ch, err := rabbit.Cfg.Pool.GetChannel()
  21. if err != nil {
  22. logx.Error(err)
  23. return
  24. }
  25. defer ch.Release()
  26. //1、将自己绑定到交换机上
  27. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  28. //2、取出数据进行消费
  29. ch.Qos(1)
  30. delivery := ch.Consume(queue.Name, false)
  31. egg_system_rules.Init(cfg.RedisAddr)
  32. var res amqp.Delivery
  33. var ok bool
  34. for {
  35. res, ok = <-delivery
  36. if ok == true {
  37. err = handleAdvertisingSignConsume(res.Body)
  38. err = res.Ack(true)
  39. fmt.Println("err ::: ", err)
  40. } else {
  41. panic(errors.New("error getting message"))
  42. }
  43. }
  44. fmt.Println("get msg done")
  45. }
  46. func handleAdvertisingSignConsume(msgData []byte) error {
  47. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  48. // 1.解析mq中queue的数据结构体
  49. var msg *md.AdvertisingWatch
  50. err := json.Unmarshal(msgData, &msg)
  51. if err != nil {
  52. return err
  53. }
  54. var data model.AdvertisingCallback
  55. db.Db.Where("id=? ", msg.Id).Get(&data)
  56. if data.Id == 0 {
  57. return errors.New("记录不存在")
  58. }
  59. if data.IsRun == 1 {
  60. return nil
  61. }
  62. req := md2.HomePageStartSignInReq{
  63. UID: int64(data.Uid),
  64. }
  65. err = egg_energy.HomePageStartSignIn(db.Db, req)
  66. if err != nil {
  67. return err
  68. }
  69. UpdateUserTime(int64(data.Uid), "sign")
  70. data.IsRun = 1
  71. db.Db.Where("id=?", data.Id).Cols("is_run").Update(&data)
  72. return nil
  73. }
  74. func UpdateUserTime(uid int64, types string) {
  75. count, _ := db.Db.Where("uid=?", uid).Count(&model.UserNoticeTime{})
  76. if count == 0 {
  77. tmp := &model.UserNoticeTime{Uid: int(uid)}
  78. if types == "login" {
  79. tmp.LoginTime = int(time.Now().Unix())
  80. } else {
  81. tmp.SignTime = int(time.Now().Unix())
  82. }
  83. db.Db.Insert(tmp)
  84. } else {
  85. tmp := &model.UserNoticeTime{}
  86. str := ""
  87. if types == "login" {
  88. str = "login_time"
  89. tmp.LoginTime = int(time.Now().Unix())
  90. } else {
  91. str = "sign_time"
  92. tmp.SignTime = int(time.Now().Unix())
  93. }
  94. db.Db.Where("uid=?", uid).Cols(str).Update(tmp)
  95. }
  96. }