蛋蛋星球RabbitMq消费项目
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

195 řádky
5.3 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. zhios_order_relate_utils "code.fnuoos.com/EggPlanet/egg_models.git/utils"
  11. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  12. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  13. md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  14. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  15. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  16. "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc"
  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. "math"
  23. "strconv"
  24. "time"
  25. )
  26. // EggRecoverDataConsume 返还用户流水
  27. func EggRecoverDataConsume(queue md.MqQueue) {
  28. fmt.Println(">>>>>>>>>>>>EggPublishDataConsume>>>>>>>>>>>>")
  29. ch, err := rabbit.Cfg.Pool.GetChannel()
  30. if err != nil {
  31. logx.Error(err)
  32. return
  33. }
  34. defer ch.Release()
  35. //1、将自己绑定到交换机上
  36. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  37. //2、取出数据进行消费
  38. ch.Qos(1)
  39. delivery := ch.Consume(queue.Name, false)
  40. egg_system_rules.Init(cfg.RedisAddr)
  41. var res amqp.Delivery
  42. var ok bool
  43. for {
  44. res, ok = <-delivery
  45. if ok == true {
  46. err = handleEggRecoverDataConsume(res.Body)
  47. if err != nil {
  48. fmt.Println("EggRecoverDataConsume_ERR:::::", err.Error())
  49. utils2.FilePutContents("EggRecoverDataConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  50. "body": res.Body,
  51. "err": err.Error(),
  52. }))
  53. }
  54. //_ = res.Reject(false)
  55. err = res.Ack(true)
  56. fmt.Println("err ::: ", err)
  57. } else {
  58. panic(errors.New("error getting message"))
  59. }
  60. }
  61. }
  62. func handleEggRecoverDataConsume(msgData []byte) error {
  63. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  64. // 1.解析mq中queue的数据结构体
  65. var msg *md2.EggRoutKeyForRecoverRecordData
  66. err := json.Unmarshal(msgData, &msg)
  67. if err != nil {
  68. return err
  69. }
  70. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  71. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  72. if err != nil {
  73. return err
  74. }
  75. session := db.Db.NewSession()
  76. defer session.Close()
  77. session.Begin()
  78. var personalDel []model.UserVirtualCoinFlow
  79. var teamDel []model.UserVirtualCoinFlow
  80. err = session.Table("user_virtual_coin_flow").
  81. In("transfer_type", 17, 19, 21).
  82. Where("uid = ?", msg.Uid).
  83. And("coin_id = ?", setting.PersonEggEnergyCoinId).
  84. Find(&personalDel)
  85. if err != nil {
  86. session.Rollback()
  87. return err
  88. }
  89. var personalAmount float64
  90. for _, flow := range personalDel {
  91. if flow.Direction == 1 {
  92. personalAmount += utils2.StrToFloat64(flow.Amount)
  93. }
  94. if flow.Direction == 2 {
  95. personalAmount -= utils2.StrToFloat64(flow.Amount)
  96. }
  97. }
  98. err = session.Table("user_virtual_coin_flow").
  99. In("transfer_type", 18, 20).
  100. Where("uid = ?", msg.Uid).
  101. And("coin_id = ?", setting.TeamEggEnergyCoinId).
  102. Find(&teamDel)
  103. if err != nil {
  104. session.Rollback()
  105. return err
  106. }
  107. var teamAmount float64
  108. for _, flow := range teamDel {
  109. if flow.Direction == 1 {
  110. teamAmount += utils2.StrToFloat64(flow.Amount)
  111. }
  112. if flow.Direction == 2 {
  113. teamAmount -= utils2.StrToFloat64(flow.Amount)
  114. }
  115. }
  116. if personalAmount != 0 {
  117. requestIdPrefix1 := fmt.Sprintf(md2.DealUserCoinRequestIdPrefix, setting.PersonEggEnergyCoinId, msg.Uid)
  118. cb1, err := svc.HandleDistributedLock(zhios_order_relate_utils.Int64ToStr(msg.Uid), strconv.Itoa(setting.PersonEggEnergyCoinId), requestIdPrefix1)
  119. if err != nil {
  120. return err
  121. }
  122. if cb1 != nil {
  123. defer cb1() // 释放锁
  124. }
  125. var kind string
  126. if personalAmount > 0 {
  127. kind = "sub"
  128. } else {
  129. kind = "add"
  130. }
  131. err = rule.DealUserVirtualCoinMinus(session, md3.DealUserVirtualCoinReq{
  132. Kind: kind,
  133. Title: enum.EggEnergyMaliciousAccountFlashingDeductionRecoverPersonal.String(),
  134. TransferType: int(enum.EggEnergyMaliciousAccountFlashingDeductionRecoverPersonal),
  135. CoinId: setting.PersonEggEnergyCoinId,
  136. Uid: msg.Uid,
  137. Amount: math.Abs(personalAmount - 0),
  138. })
  139. if err != nil {
  140. session.Rollback()
  141. return err
  142. }
  143. }
  144. if teamAmount != 0 {
  145. //1、分布式锁阻拦
  146. requestIdPrefix2 := fmt.Sprintf(md2.DealUserCoinRequestIdPrefix, setting.TeamEggEnergyCoinId, msg.Uid)
  147. cb2, err := svc.HandleDistributedLock(zhios_order_relate_utils.Int64ToStr(msg.Uid), strconv.Itoa(setting.TeamEggEnergyCoinId), requestIdPrefix2)
  148. if err != nil {
  149. return err
  150. }
  151. if cb2 != nil {
  152. defer cb2() // 释放锁
  153. }
  154. var kind string
  155. if teamAmount > 0 {
  156. kind = "sub"
  157. } else {
  158. kind = "add"
  159. }
  160. err = rule.DealUserVirtualCoinMinus(session, md3.DealUserVirtualCoinReq{
  161. Kind: kind,
  162. Title: enum.EggEnergyMaliciousAccountFlashingDeductionRecoverTeam.String(),
  163. TransferType: int(enum.EggEnergyMaliciousAccountFlashingDeductionRecoverTeam),
  164. CoinId: setting.TeamEggEnergyCoinId,
  165. Uid: msg.Uid,
  166. Amount: math.Abs(teamAmount - 0),
  167. })
  168. if err != nil {
  169. session.Rollback()
  170. return err
  171. }
  172. }
  173. _, err = session.Table("punishment_record").
  174. Where("uid = ?", msg.Uid).Cols("complete").
  175. Update(model.PunishmentRecord{
  176. Complete: 1,
  177. })
  178. if err != nil {
  179. return err
  180. }
  181. session.Commit()
  182. fmt.Println("handleEggRecoverDataConsume_succeed ====>", msg.Uid)
  183. return nil
  184. }