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

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