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

129 lines
3.7 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. "strconv"
  23. "time"
  24. )
  25. // EggRestoreDataConsume 恢复返还用户流水
  26. func EggRestoreDataConsume(queue md.MqQueue) {
  27. fmt.Println(">>>>>>>>>>>>EggRestoreDataConsume>>>>>>>>>>>>")
  28. ch, err := rabbit.Cfg.Pool.GetChannel()
  29. if err != nil {
  30. logx.Error(err)
  31. return
  32. }
  33. defer ch.Release()
  34. //1、将自己绑定到交换机上
  35. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  36. //2、取出数据进行消费
  37. ch.Qos(1)
  38. delivery := ch.Consume(queue.Name, false)
  39. egg_system_rules.Init(cfg.RedisAddr)
  40. var res amqp.Delivery
  41. var ok bool
  42. for {
  43. res, ok = <-delivery
  44. if ok == true {
  45. err = handleEggRestoreDataConsume(res.Body)
  46. if err != nil {
  47. fmt.Println("EggRestoreDataConsume_ERR:::::", err.Error())
  48. utils2.FilePutContents("EggRestoreDataConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  49. "body": res.Body,
  50. "err": err.Error(),
  51. }))
  52. }
  53. //_ = res.Reject(false)
  54. err = res.Ack(true)
  55. fmt.Println("err ::: ", err)
  56. } else {
  57. panic(errors.New("error getting message"))
  58. }
  59. }
  60. }
  61. func handleEggRestoreDataConsume(msgData []byte) error {
  62. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  63. // 1.解析mq中queue的数据结构体
  64. var msg *md2.EggRoutKeyForRestoreRecordData
  65. err := json.Unmarshal(msgData, &msg)
  66. if err != nil {
  67. return err
  68. }
  69. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  70. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  71. if err != nil {
  72. return err
  73. }
  74. //1、分布式锁阻拦
  75. requestIdPrefix1 := fmt.Sprintf(md2.DealUserCoinRequestIdPrefix, setting.PersonEggEnergyCoinId, msg.Uid)
  76. cb1, err := svc.HandleDistributedLock(zhios_order_relate_utils.Int64ToStr(msg.Uid), strconv.Itoa(setting.PersonEggEnergyCoinId), requestIdPrefix1)
  77. if err != nil {
  78. return err
  79. }
  80. if cb1 != nil {
  81. defer cb1() // 释放锁
  82. }
  83. session := db.Db.NewSession()
  84. defer session.Close()
  85. session.Begin()
  86. var personalDel model.UserVirtualCoinFlow
  87. has1, err := session.Table("user_virtual_coin_flow").
  88. Where("transfer_type = ?", int(enum.EggEnergyMaliciousAccountFlashingDeductionRecoverPersonal)).
  89. And("uid = ?", msg.Uid).Get(&personalDel)
  90. if err != nil {
  91. session.Rollback()
  92. return err
  93. }
  94. if has1 {
  95. err = rule.DealUserVirtualCoinMinus(session, md3.DealUserVirtualCoinReq{
  96. Kind: "sub",
  97. Title: enum.EggEnergyMaliciousAccountFlashingDeductionRestorePersonal.String(),
  98. TransferType: int(enum.EggEnergyMaliciousAccountFlashingDeductionRestorePersonal),
  99. CoinId: setting.PersonEggEnergyCoinId,
  100. Uid: msg.Uid,
  101. Amount: utils2.StrToFloat64(personalDel.Amount),
  102. })
  103. if err != nil {
  104. session.Rollback()
  105. return err
  106. }
  107. }
  108. _, err = session.Table("punishment_record").
  109. Where("uid = ?", msg.Uid).Cols("complete").
  110. Update(model.PunishmentRecord{
  111. Complete: 1,
  112. })
  113. if err != nil {
  114. return err
  115. }
  116. session.Commit()
  117. fmt.Println("handleEggRestoreDataConsume_succeed ====>", msg.Uid)
  118. return nil
  119. }