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

155 行
4.9 KiB

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