蛋蛋星球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.
 
 
 
 
 
 

112 lines
3.2 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. typesPrefix := "incentive_eggSmash"
  64. callbackDb := implement.NewAdvertisingCallbackDb(db.Db)
  65. count, err := callbackDb.AdvertisingCallbackCount(utils2.Int64ToStr(msg.AssistanceUid), msg.SignStart, msg.SignEnd, typesPrefix)
  66. if err != nil {
  67. return err
  68. }
  69. // 3. 变更数据
  70. assistanceDb := implement.NewEggEnergyTeamAssistanceDb(db.Db)
  71. now := time.Now()
  72. date := now.Format("2006-01-02")
  73. teamAssistance, err := assistanceDb.EggEnergyTeamAssistanceGetOne(msg.Uid, date)
  74. if err != nil {
  75. return err
  76. }
  77. if teamAssistance == nil {
  78. m := model.EggEnergyTeamAssistance{
  79. Uid: msg.Uid,
  80. AssistedNum: 1,
  81. AssistedTimes: 1,
  82. RewardScore: utils2.Float64ToStr(msg.AssistanceValue),
  83. Date: date,
  84. CreateAt: now.Format("2006-01-02 15:04:05"),
  85. UpdateAt: now.Format("2006-01-02 15:04:05"),
  86. }
  87. _, err = assistanceDb.EggEnergyTeamAssistanceInsert(&m)
  88. if err != nil {
  89. return err
  90. }
  91. } else {
  92. teamAssistance.RewardScore = utils2.Float64ToStr(msg.AssistanceValue + utils2.StrToFloat64(teamAssistance.RewardScore))
  93. // 如果是第一条说明还没有被记录过, 助力人数 + 1
  94. if count == 1 {
  95. teamAssistance.AssistedNum++
  96. }
  97. teamAssistance.AssistedTimes++
  98. _, err = assistanceDb.EggEnergyTeamAssistanceUpdate(teamAssistance.Id, teamAssistance, "assisted_num", "reward_score", "assisted_times")
  99. if err != nil {
  100. return err
  101. }
  102. }
  103. return nil
  104. }