蛋蛋星球RabbitMq消费项目
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

video_reward_consume.go 4.7 KiB

1 tydzień temu
1 tydzień temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/e"
  6. utils2 "applet/app/utils"
  7. "applet/app/utils/cache"
  8. "applet/app/utils/logx"
  9. "applet/consume/md"
  10. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  11. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  12. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  13. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  14. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  15. "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc"
  16. "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc/sys_cfg"
  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 VideoRewardConsume(queue md.MqQueue) {
  26. fmt.Println(">>>>>>>>>>>>VideoRewardConsume>>>>>>>>>>>>")
  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 = handleVideoRewardConsume(ch, res.Body)
  45. if err != nil {
  46. fmt.Println("VideoRewardConsume_ERR:::::", err.Error())
  47. utils2.FilePutContents("VideoRewardConsume_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.VideoReward
  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 handleVideoRewardConsume(ch *rabbit.Channel, msgData []byte) error {
  68. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  69. // 1.解析mq中queue的数据结构体
  70. var msg *md.VideoReward
  71. err := json.Unmarshal(msgData, &msg)
  72. if err != nil {
  73. return err
  74. }
  75. //1、分布式锁阻拦
  76. requestIdPrefix := fmt.Sprintf("video.reward.lock:%s", msg.Uid)
  77. cb, err := svc.HandleDistributedLockForComm(msg.Uid, "video.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. sysCfgDb := sys_cfg.NewSysCfgDb(db.Db)
  86. videoBase := sysCfgDb.SysCfgGetWithDb("video_base")
  87. sess := eg.NewSession()
  88. defer sess.Close()
  89. sess.Begin()
  90. NewVideoTotalDb := implement.NewVideoTotalDb(db.Db)
  91. total, _ := NewVideoTotalDb.GetVideoTotalSess(sess, msg.Uid, time.Now().Format("20060102"))
  92. if total == nil {
  93. total = &model.VideoTotal{
  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(videoBase, "total").String()) - total.Total
  105. if Leave-1 < 0 {
  106. return nil
  107. }
  108. total.Total++
  109. update, err2 := sess.Where("id=?", total.Id).Cols("total").Update(total)
  110. if update == 0 || err2 != nil {
  111. sess.Rollback()
  112. return e.NewErr(400, "获取奖励失败")
  113. }
  114. sess.Commit()
  115. numKey := "video.num:" + time.Now().Format("20060102") + "." + msg.Uid
  116. todayRange := utils2.GetTimeRange("today")
  117. cache.SetEx(numKey, utils2.IntToStr(total.Total), int(todayRange["end"]-time.Now().Unix()))
  118. NewEggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  119. eggData, _ := NewEggEnergyBasicSettingDb.EggEnergyBasicSettingGetOne()
  120. err = ch.PublishV2(md2.EggEnergyExchange, md2.EggEnergyStructForDealUserVirtualCoinData{
  121. Kind: "add",
  122. Title: enum.EggEnergyVideoRewardPersonalActiveCoin.String(),
  123. TransferType: int(enum.EggEnergyVideoRewardPersonalActiveCoin),
  124. CoinId: eggData.PersonEggPointsCoinId,
  125. Uid: utils2.StrToInt64(msg.Uid),
  126. Amount: utils2.StrToFloat64(gjson.Get(videoBase, "reward").String()),
  127. }, md2.EggEnergyRoutKeyForDealUserVirtualCoinData)
  128. if err != nil {
  129. ch.PublishV2(md2.EggEnergyExchange, md2.EggEnergyStructForDealUserVirtualCoinData{
  130. Kind: "add",
  131. Title: enum.EggEnergyVideoRewardPersonalActiveCoin.String(),
  132. TransferType: int(enum.EggEnergyVideoRewardPersonalActiveCoin),
  133. CoinId: eggData.PersonEggPointsCoinId,
  134. Uid: utils2.StrToInt64(msg.Uid),
  135. Amount: utils2.StrToFloat64(gjson.Get(videoBase, "reward").String()),
  136. }, md2.EggEnergyRoutKeyForDealUserVirtualCoinData)
  137. }
  138. return nil
  139. }