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

172 line
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. }
  58. func handleEggEnergyDealFundDataConsume(msgData []byte) error {
  59. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  60. // 1.解析mq中queue的数据结构体
  61. var msg *md2.EggEnergyStructForEggEnergyFundData
  62. err := json.Unmarshal(msgData, &msg)
  63. if err != nil {
  64. return err
  65. }
  66. now := time.Now()
  67. engine := db.Db
  68. session := engine.NewSession()
  69. session.Begin()
  70. // 2. 获取 fund_data 数据
  71. fundDataDb := implement.NewEggEnergyFundDataDb(engine)
  72. fundDataRecordsDb := implement.NewEggEnergyFundDataRecordsDb(engine)
  73. data, err := fundDataDb.EggEnergyFundDataGetOneByParams(map[string]interface{}{
  74. "key": "id",
  75. "value": msg.ID,
  76. })
  77. if err != nil {
  78. return err
  79. }
  80. // 3. 获取核心数据
  81. eggEnergyCoreData, cb, err2 := svc.GetEggEnergyCoreData(engine)
  82. if err2 != nil {
  83. return err2
  84. }
  85. if cb != nil {
  86. defer cb() // 释放锁
  87. }
  88. // 4. 计算涨价公式
  89. err3, calcPriceIncreaseFormulaResp := egg_energy.CalcPriceIncreaseFormula(utils2.AnyToString(msg.Amount), eggEnergyCoreData)
  90. if err3 != nil {
  91. _ = session.Rollback()
  92. session.Close()
  93. return err3
  94. }
  95. // 5. 处理可用能量
  96. dealAvailableEggEnergyCoinReq := md2.DealAvailableEggEnergyCoinReq{
  97. Amount: calcPriceIncreaseFormulaResp.GetEggEnergyNums,
  98. AmountFee: "",
  99. BeforePrice: calcPriceIncreaseFormulaResp.BeforePrice,
  100. AfterPrice: calcPriceIncreaseFormulaResp.AfterPrice,
  101. BeforePlanetTotalValue: calcPriceIncreaseFormulaResp.BeforePlanetTotalValue,
  102. AfterPlanetTotalValue: calcPriceIncreaseFormulaResp.AfterPlanetTotalValue,
  103. BeforeEnergyTotalNums: calcPriceIncreaseFormulaResp.BeforeEnergyTotalNums,
  104. AfterEnergyTotalNums: calcPriceIncreaseFormulaResp.AfterEnergyTotalNums,
  105. }
  106. err4 := egg_energy.DealAvailableEggEnergyCoin(session, int(enum.CapitalInjection), eggEnergyCoreData, dealAvailableEggEnergyCoinReq)
  107. if err4 != nil {
  108. fmt.Println("ActivityCoinAutoExchangeEggPersonEnergy:::::err111:::", err4)
  109. _ = session.Rollback()
  110. session.Close()
  111. return err4
  112. }
  113. // 6. 计算余额
  114. balanceAmount := decimal.NewFromFloat(utils2.AnyToFloat64(data.BalanceAmount)).
  115. Sub(decimal.NewFromFloat(utils2.AnyToFloat64(data.BalanceAmount))).
  116. String()
  117. // 7. 插入资金数据详细数据
  118. record := model.EggEnergyFundDataRecords{
  119. RecordsId: data.Id,
  120. TotalAmount: data.TotalAmount,
  121. BalanceAmount: balanceAmount,
  122. BalanceTimes: data.BalanceTimes - 1,
  123. BeforePrice: calcPriceIncreaseFormulaResp.BeforePrice,
  124. AfterPrice: calcPriceIncreaseFormulaResp.AfterPrice,
  125. BeforePlanetTotalValue: calcPriceIncreaseFormulaResp.BeforePlanetTotalValue,
  126. AfterPlanetTotalValue: calcPriceIncreaseFormulaResp.AfterPlanetTotalValue,
  127. CreateAt: now.Format("2006-01-02 15:04:05"),
  128. UpdateAt: now.Format("2006-01-02 15:04:05"),
  129. }
  130. _, err5 := fundDataRecordsDb.EggEnergyFundDataRecordsInsertBySession(session, record)
  131. if err5 != nil {
  132. _ = session.Rollback()
  133. return err5
  134. }
  135. // 8. 更新当前数据
  136. fundData := model.EggEnergyFundData{
  137. Id: data.Id,
  138. Kind: data.Kind,
  139. TotalAmount: data.TotalAmount,
  140. BalanceAmount: balanceAmount,
  141. Hours: data.Hours,
  142. BalanceTimes: data.BalanceTimes - 1,
  143. Frequency: data.Frequency,
  144. Memo: data.Memo,
  145. CreateAt: data.CreateAt,
  146. UpdateAt: now.Format("2006-01-02 15:04:05"),
  147. }
  148. forceColumns := []string{
  149. "balance_times",
  150. "balance_amount",
  151. }
  152. _, err6 := fundDataDb.EggEnergyFundDataUpdateBySession(session, fundData, forceColumns...)
  153. if err6 != nil {
  154. _ = session.Rollback()
  155. return err6
  156. }
  157. return session.Commit()
  158. }