蛋蛋星球RabbitMq消费项目
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

playlet_reward_consume.go 4.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/e"
  6. utils2 "applet/app/utils"
  7. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  8. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  9. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  10. "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc"
  11. "applet/app/utils/cache"
  12. "applet/app/utils/logx"
  13. "applet/consume/md"
  14. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  15. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  16. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  17. "encoding/json"
  18. "errors"
  19. "fmt"
  20. "github.com/streadway/amqp"
  21. "github.com/tidwall/gjson"
  22. "time"
  23. )
  24. func PlayletRewardConsume(queue md.MqQueue) {
  25. fmt.Println(">>>>>>>>>>>>PlayletRewardConsume>>>>>>>>>>>>")
  26. ch, err := rabbit.Cfg.Pool.GetChannel()
  27. if err != nil {
  28. logx.Error(err)
  29. return
  30. }
  31. defer ch.Release()
  32. //1、将自己绑定到交换机上
  33. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  34. //2、取出数据进行消费
  35. ch.Qos(100)
  36. delivery := ch.Consume(queue.Name, false)
  37. egg_system_rules.Init(cfg.RedisAddr)
  38. var res amqp.Delivery
  39. var ok bool
  40. for {
  41. res, ok = <-delivery
  42. if ok == true {
  43. err = handlePlayletRewardConsume(ch, res.Body)
  44. if err != nil {
  45. fmt.Println("PlayletRewardConsume_ERR:::::", err.Error())
  46. utils2.FilePutContents("PlayletRewardConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  47. "body": res.Body,
  48. "err": err.Error(),
  49. }))
  50. _ = res.Reject(false)
  51. //TODO::重新推回队列末尾,避免造成队列堵塞
  52. var msg *md.PlayletReward
  53. json.Unmarshal(res.Body, &msg)
  54. ch.Publish(queue.ExchangeName, msg, queue.RoutKey)
  55. } else {
  56. //_ = res.Reject(false)
  57. err = res.Ack(true)
  58. }
  59. fmt.Println("err ::: ", err)
  60. } else {
  61. panic(errors.New("error getting message"))
  62. }
  63. }
  64. fmt.Println("get msg done")
  65. }
  66. func handlePlayletRewardConsume(ch *rabbit.Channel, msgData []byte) error {
  67. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  68. // 1.解析mq中queue的数据结构体
  69. var msg *md.PlayletReward
  70. err := json.Unmarshal(msgData, &msg)
  71. if err != nil {
  72. return err
  73. }
  74. //1、分布式锁阻拦
  75. requestIdPrefix := fmt.Sprintf("playlet.reward.lock:%s", msg.Uid)
  76. cb, err := svc.HandleDistributedLockForComm(msg.Uid, "playlet.reward.lock.update:%s", requestIdPrefix)
  77. if err != nil {
  78. return err
  79. }
  80. if cb != nil {
  81. defer cb() // 释放锁
  82. }
  83. eg := db.Db
  84. redisConn := cache.GetPool().Get()
  85. sysCfgDb := implement.NewSysCfgDb(eg, redisConn)
  86. playletBase := sysCfgDb.SysCfgGetWithDb("playlet_base")
  87. sess := eg.NewSession()
  88. defer sess.Close()
  89. sess.Begin()
  90. NewPlayletTotalDb := implement.NewPlayletTotalDb(db.Db)
  91. total, _ := NewPlayletTotalDb.GetPlayletTotalSess(sess, msg.Uid, time.Now().Format("20060102"))
  92. if total == nil {
  93. total = &model.PlayletTotal{
  94. Uid: utils2.StrToInt(msg.Uid),
  95. Date: utils2.StrToInt(time.Now().Format("20060102")),
  96. Time: time.Now(),
  97. }
  98. insert, err := sess.Insert(total)
  99. if insert == 0 || err != nil {
  100. sess.Rollback()
  101. return e.NewErr(400, "获取奖励失败")
  102. }
  103. }
  104. Leave := utils2.StrToInt(gjson.Get(playletBase, "total").String()) - total.Total
  105. if Leave-1 < 0 {
  106. sess.Rollback()
  107. return nil
  108. }
  109. total.Total++
  110. update, err2 := sess.Where("id=?", total.Id).Cols("total").Update(total)
  111. if update == 0 || err2 != nil {
  112. sess.Rollback()
  113. return e.NewErr(400, "获取奖励失败")
  114. }
  115. sess.Commit()
  116. numKey := "playlet.num:" + time.Now().Format("20060102") + "." + msg.Uid
  117. todayRange := utils2.GetTimeRange("today")
  118. cache.SetEx(numKey, utils2.IntToStr(total.Total), int(todayRange["end"]-time.Now().Unix()))
  119. NewEggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  120. eggData, _ := NewEggEnergyBasicSettingDb.EggEnergyBasicSettingGetOne()
  121. err = ch.PublishV2(md2.EggEnergyExchange, md2.EggEnergyStructForDealUserVirtualCoinData{
  122. Kind: "add",
  123. Title: enum.EggEnergyPlayletRewardPersonalActiveCoin.String(),
  124. TransferType: int(enum.EggEnergyPlayletRewardPersonalActiveCoin),
  125. CoinId: eggData.PersonEggPointsCoinId,
  126. Uid: utils2.StrToInt64(msg.Uid),
  127. Amount: utils2.StrToFloat64(gjson.Get(playletBase, "reward").String()),
  128. }, md2.EggEnergyRoutKeyForDealUserVirtualCoinData)
  129. if err != nil {
  130. ch.PublishV2(md2.EggEnergyExchange, md2.EggEnergyStructForDealUserVirtualCoinData{
  131. Kind: "add",
  132. Title: enum.EggEnergyPlayletRewardPersonalActiveCoin.String(),
  133. TransferType: int(enum.EggEnergyPlayletRewardPersonalActiveCoin),
  134. CoinId: eggData.PersonEggPointsCoinId,
  135. Uid: utils2.StrToInt64(msg.Uid),
  136. Amount: utils2.StrToFloat64(gjson.Get(playletBase, "reward").String()),
  137. }, md2.EggEnergyRoutKeyForDealUserVirtualCoinData)
  138. }
  139. return nil
  140. }