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

172 líneas
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. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  11. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy"
  12. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum"
  13. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  14. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/svc"
  15. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "github.com/shopspring/decimal"
  20. "github.com/streadway/amqp"
  21. "time"
  22. )
  23. func EggEnergyDealFundDataConsume(queue md.MqQueue) {
  24. fmt.Println(">>>>>>>>>>>>EggEnergyDealFundDataConsume>>>>>>>>>>>>")
  25. ch, err := rabbit.Cfg.Pool.GetChannel()
  26. if err != nil {
  27. logx.Error(err)
  28. return
  29. }
  30. defer ch.Release()
  31. //1、将自己绑定到交换机上
  32. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  33. //2、取出数据进行消费
  34. ch.Qos(1)
  35. delivery := ch.Consume(queue.Name, false)
  36. egg_system_rules.Init(cfg.RedisAddr)
  37. var res amqp.Delivery
  38. var ok bool
  39. for {
  40. res, ok = <-delivery
  41. if ok == true {
  42. err = handleEggEnergyDealFundDataConsume(res.Body)
  43. if err != nil {
  44. fmt.Println("EggEnergyDealFundDataConsume_ERR:::::", err.Error())
  45. utils2.FilePutContents("EggEnergyDealFundDataConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  46. "body": res.Body,
  47. "err": err.Error(),
  48. }))
  49. }
  50. //_ = res.Reject(false)
  51. err = res.Ack(true)
  52. fmt.Println("err ::: ", err)
  53. } else {
  54. panic(errors.New("error getting message"))
  55. }
  56. }
  57. fmt.Println("get msg done")
  58. }
  59. func handleEggEnergyDealFundDataConsume(msgData []byte) error {
  60. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  61. // 1.解析mq中queue的数据结构体
  62. var msg *md2.EggEnergyStructForEggEnergyFundData
  63. err := json.Unmarshal(msgData, &msg)
  64. if err != nil {
  65. return err
  66. }
  67. now := time.Now()
  68. engine := db.Db
  69. session := engine.NewSession()
  70. session.Begin()
  71. // 2. 获取 fund_data 数据
  72. fundDataDb := implement.NewEggEnergyFundDataDb(engine)
  73. fundDataRecordsDb := implement.NewEggEnergyFundDataRecordsDb(engine)
  74. data, err := fundDataDb.EggEnergyFundDataGetOneByParams(map[string]interface{}{
  75. "key": "id",
  76. "value": msg.ID,
  77. })
  78. if err != nil {
  79. return err
  80. }
  81. // 3. 获取核心数据
  82. eggEnergyCoreData, cb, err2 := svc.GetEggEnergyCoreData(engine)
  83. if err2 != nil {
  84. return err2
  85. }
  86. if cb != nil {
  87. defer cb() // 释放锁
  88. }
  89. // 4. 计算涨价公式
  90. err3, calcPriceIncreaseFormulaResp := egg_energy.CalcPriceIncreaseFormula(utils2.AnyToString(msg.Amount), eggEnergyCoreData)
  91. if err3 != nil {
  92. _ = session.Rollback()
  93. session.Close()
  94. return err3
  95. }
  96. // 5. 处理可用能量
  97. dealAvailableEggEnergyCoinReq := md2.DealAvailableEggEnergyCoinReq{
  98. Amount: calcPriceIncreaseFormulaResp.GetEggEnergyNums,
  99. AmountFee: "",
  100. BeforePrice: calcPriceIncreaseFormulaResp.BeforePrice,
  101. AfterPrice: calcPriceIncreaseFormulaResp.AfterPrice,
  102. BeforePlanetTotalValue: calcPriceIncreaseFormulaResp.BeforePlanetTotalValue,
  103. AfterPlanetTotalValue: calcPriceIncreaseFormulaResp.AfterPlanetTotalValue,
  104. BeforeEnergyTotalNums: calcPriceIncreaseFormulaResp.BeforeEnergyTotalNums,
  105. AfterEnergyTotalNums: calcPriceIncreaseFormulaResp.AfterEnergyTotalNums,
  106. }
  107. err4 := egg_energy.DealAvailableEggEnergyCoin(session, int(enum.CapitalInjection), eggEnergyCoreData, dealAvailableEggEnergyCoinReq)
  108. if err4 != nil {
  109. fmt.Println("ActivityCoinAutoExchangeEggPersonEnergy:::::err111:::", err4)
  110. _ = session.Rollback()
  111. session.Close()
  112. return err4
  113. }
  114. // 6. 计算余额
  115. balanceAmount := decimal.NewFromFloat(utils2.AnyToFloat64(data.BalanceAmount)).
  116. Sub(decimal.NewFromFloat(utils2.AnyToFloat64(data.BalanceAmount))).
  117. String()
  118. // 7. 插入资金数据详细数据
  119. record := model.EggEnergyFundDataRecords{
  120. RecordsId: data.Id,
  121. TotalAmount: data.TotalAmount,
  122. BalanceAmount: balanceAmount,
  123. BalanceTimes: data.BalanceTimes - 1,
  124. BeforePrice: calcPriceIncreaseFormulaResp.BeforePrice,
  125. AfterPrice: calcPriceIncreaseFormulaResp.AfterPrice,
  126. BeforePlanetTotalValue: calcPriceIncreaseFormulaResp.BeforePlanetTotalValue,
  127. AfterPlanetTotalValue: calcPriceIncreaseFormulaResp.AfterPlanetTotalValue,
  128. CreateAt: now.Format("2006-01-02 15:04:05"),
  129. UpdateAt: now.Format("2006-01-02 15:04:05"),
  130. }
  131. _, err5 := fundDataRecordsDb.EggEnergyFundDataRecordsInsertBySession(session, record)
  132. if err5 != nil {
  133. _ = session.Rollback()
  134. return err5
  135. }
  136. // 8. 更新当前数据
  137. fundData := model.EggEnergyFundData{
  138. Id: data.Id,
  139. Kind: data.Kind,
  140. TotalAmount: data.TotalAmount,
  141. BalanceAmount: balanceAmount,
  142. Hours: data.Hours,
  143. BalanceTimes: data.BalanceTimes - 1,
  144. Frequency: data.Frequency,
  145. Memo: data.Memo,
  146. CreateAt: data.CreateAt,
  147. UpdateAt: now.Format("2006-01-02 15:04:05"),
  148. }
  149. forceColumns := []string{
  150. "balance_times",
  151. "balance_amount",
  152. }
  153. _, err6 := fundDataDb.EggEnergyFundDataUpdateBySession(session, fundData, forceColumns...)
  154. if err6 != nil {
  155. _ = session.Rollback()
  156. return err6
  157. }
  158. return session.Commit()
  159. }