蛋蛋星球RabbitMq消费项目
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

111 行
3.1 KiB

  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. utils2 "applet/app/utils"
  6. "applet/app/utils/logx"
  7. "applet/consume/md"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  10. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  11. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  12. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  13. "encoding/json"
  14. "errors"
  15. "fmt"
  16. "github.com/streadway/amqp"
  17. "time"
  18. )
  19. func EggEnergyTeamAssistanceConsume(queue md.MqQueue) {
  20. fmt.Println(">>>>>>>>>>>>EggEnergyTeamAssistanceConsume>>>>>>>>>>>>")
  21. ch, err := rabbit.Cfg.Pool.GetChannel()
  22. if err != nil {
  23. logx.Error(err)
  24. return
  25. }
  26. defer ch.Release()
  27. //1、将自己绑定到交换机上
  28. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  29. //2、取出数据进行消费
  30. ch.Qos(1)
  31. delivery := ch.Consume(queue.Name, false)
  32. egg_system_rules.Init(cfg.RedisAddr)
  33. var res amqp.Delivery
  34. var ok bool
  35. for {
  36. res, ok = <-delivery
  37. if ok == true {
  38. err = handleEggEnergyTeamAssistanceDataConsume(res.Body)
  39. if err != nil {
  40. fmt.Println("EggEnergyTeamAssistanceConsume_ERR:::::", err.Error())
  41. utils2.FilePutContents("EggEnergyTeamAssistance_ERR", utils2.SerializeStr(map[string]interface{}{
  42. "body": res.Body,
  43. "err": err.Error(),
  44. }))
  45. }
  46. //_ = res.Reject(false)
  47. err = res.Ack(true)
  48. fmt.Println("err ::: ", err)
  49. } else {
  50. panic(errors.New("error getting message"))
  51. }
  52. }
  53. }
  54. func handleEggEnergyTeamAssistanceDataConsume(msgData []byte) error {
  55. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  56. // 1.解析mq中queue的数据结构体
  57. var msg *md2.EggEnergyTeamAssistanceReq
  58. err := json.Unmarshal(msgData, &msg)
  59. if err != nil {
  60. return err
  61. }
  62. // 2.查询用户是否已经助力过
  63. callbackDb := implement.NewAdvertisingCallbackDb(db.Db)
  64. count, err := callbackDb.AdvertisingCallbackCount(utils2.Int64ToStr(msg.AssistanceUid), msg.SignStart, msg.SignEnd, "")
  65. if err != nil {
  66. return err
  67. }
  68. // 3. 变更数据
  69. assistanceDb := implement.NewEggEnergyTeamAssistanceDb(db.Db)
  70. now := time.Now()
  71. date := now.Format("2006-01-02")
  72. teamAssistance, err := assistanceDb.EggEnergyTeamAssistanceGetOne(msg.Uid, date)
  73. if err != nil {
  74. return err
  75. }
  76. if teamAssistance == nil {
  77. m := model.EggEnergyTeamAssistance{
  78. Uid: msg.Uid,
  79. AssistedNum: 1,
  80. AssistedTimes: 1,
  81. RewardScore: utils2.Float64ToStr(msg.AssistanceValue),
  82. Date: date,
  83. CreateAt: now.Format("2006-01-02 15:04:05"),
  84. UpdateAt: now.Format("2006-01-02 15:04:05"),
  85. }
  86. _, err = assistanceDb.EggEnergyTeamAssistanceInsert(&m)
  87. if err != nil {
  88. return err
  89. }
  90. } else {
  91. teamAssistance.RewardScore = utils2.Float64ToStr(msg.AssistanceValue + utils2.StrToFloat64(teamAssistance.RewardScore))
  92. // 如果是第一条说明还没有被记录过, 助力人数 + 1
  93. if count == 1 {
  94. teamAssistance.AssistedNum++
  95. }
  96. teamAssistance.AssistedTimes++
  97. _, err = assistanceDb.EggEnergyTeamAssistanceUpdate(teamAssistance.Id, teamAssistance, "assisted_num", "reward_score", "assisted_times")
  98. if err != nil {
  99. return err
  100. }
  101. }
  102. return nil
  103. }