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

egg_energy_team_assistance_consume.go 3.9 KiB

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