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

egg_energy_platform_revenue_data.go 5.4 KiB

1 month ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 EggEnergyDealPlatformRevenueDataConsume(queue md.MqQueue) {
  24. fmt.Println(">>>>>>>>>>>>EggEnergyDealPlatformRevenueDataConsume>>>>>>>>>>>>")
  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 = handleEggEnergyDealPlatformRevenueDataConsume(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 handleEggEnergyDealPlatformRevenueDataConsume(msgData []byte) error {
  59. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  60. // 1.解析mq中queue的数据结构体
  61. var msg *md2.EggEnergyStructForPlatformRevenueData
  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. 获取 platform_revenue_data 数据
  71. revenueDataDb := implement.NewPlatformRevenueDataDb(engine)
  72. dataRecordsDb := implement.NewPlatformRevenueDataRecordsDb(engine)
  73. data, err := revenueDataDb.PlatformRevenueDataGetOneByParams(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.PlatformRevenueDataRecords{
  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 := dataRecordsDb.PlatformRevenueDataRecordsInsertBySession(session, record)
  131. if err5 != nil {
  132. _ = session.Rollback()
  133. return err5
  134. }
  135. // 8. 更新当前数据
  136. PlatformRevenueData := model.PlatformRevenueData{
  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 := revenueDataDb.PlatformRevenueDataUpdateBySession(session, PlatformRevenueData, forceColumns...)
  153. if err6 != nil {
  154. _ = session.Rollback()
  155. return err6
  156. }
  157. return session.Commit()
  158. }