蛋蛋星球RabbitMq消费项目
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

130 рядки
3.6 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(2)
  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(10) * 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. detailDb := implement.NewEggEnergyAssistanceDetailDb(db.Db)
  64. exist, err := detailDb.AssistanceDetailExist(msg.Uid, msg.AssistanceUid, msg.SignId)
  65. if err != nil {
  66. return err
  67. }
  68. now := time.Now()
  69. if !exist {
  70. //插入助力数据
  71. detail := model.EggEnergyAssistanceDetail{
  72. Uid: msg.Uid,
  73. AssistedUid: msg.AssistanceUid,
  74. SignId: msg.SignId,
  75. CreatedAt: now.Format("2006-01-02 15:04:05"),
  76. UpdatedAt: now.Format("2006-01-02 15:04:05"),
  77. }
  78. _, err = detailDb.AssistanceDetailInsert(&detail)
  79. if err != nil {
  80. return err
  81. }
  82. }
  83. // 3. 变更数据
  84. assistanceDb := implement.NewEggEnergyTeamAssistanceDb(db.Db)
  85. date := now.Format("2006-01-02")
  86. session := db.Db.Where("")
  87. teamAssistance, err := assistanceDb.EggEnergyTeamAssistanceGetOneByParamsBySession(session, map[string]interface{}{
  88. "key": "sign_id",
  89. "value": msg.SignId,
  90. })
  91. if err != nil {
  92. return err
  93. }
  94. if teamAssistance == nil {
  95. m := model.EggEnergyTeamAssistance{
  96. Uid: msg.Uid,
  97. AssistedNum: 1,
  98. AssistedTimes: 1,
  99. RewardScore: utils2.Float64ToStr(msg.AssistanceValue),
  100. Date: date,
  101. SignId: msg.SignId,
  102. CreateAt: now.Format("2006-01-02 15:04:05"),
  103. UpdateAt: now.Format("2006-01-02 15:04:05"),
  104. }
  105. _, err = assistanceDb.EggEnergyTeamAssistanceInsert(&m)
  106. if err != nil {
  107. return err
  108. }
  109. } else {
  110. teamAssistance.RewardScore = utils2.Float64ToStr(msg.AssistanceValue + utils2.StrToFloat64(teamAssistance.RewardScore))
  111. // 如果未助力过, 助力人数 + 1 添加助力信息
  112. if !exist {
  113. teamAssistance.AssistedNum++
  114. }
  115. teamAssistance.AssistedTimes++
  116. _, err = assistanceDb.EggEnergyTeamAssistanceUpdate(teamAssistance.Id, teamAssistance, "assisted_num", "reward_score", "assisted_times")
  117. if err != nil {
  118. return err
  119. }
  120. }
  121. return nil
  122. }