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

svc_deal_fund_data.go 5.5 KiB

1 month ago
1 month ago
1 month ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package svc
  2. import (
  3. "applet/app/utils"
  4. "applet/app/utils/cache"
  5. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  6. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  7. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  8. "fmt"
  9. "github.com/shopspring/decimal"
  10. "time"
  11. "xorm.io/xorm"
  12. )
  13. const DealFundDataKey = "deal_fund_data_key"
  14. func DealFundData(eg *xorm.Engine, dbName string, ch *rabbit.Channel) {
  15. fmt.Println("deal_fund_data...")
  16. defer func() {
  17. if err := recover(); err != nil {
  18. fmt.Println(err)
  19. return
  20. }
  21. }()
  22. // 悲观锁防止串行
  23. getString, _ := cache.GetString(DealFundDataKey)
  24. if getString != "" {
  25. fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次处理未执行完")
  26. return
  27. }
  28. cache.SetEx(DealFundDataKey, "running", 60*30) //30分钟
  29. // 查询所有未被执行完的数据
  30. fundDataDb := implement.NewEggEnergyFundDataDb(eg)
  31. fundDataList, err := fundDataDb.EggEnergyFundDataFindNotFinish()
  32. if err != nil {
  33. fmt.Println("DealFundData_ERR:::::", err.Error())
  34. return
  35. }
  36. now := time.Now()
  37. fundDataRecordsDb := implement.NewEggEnergyFundDataRecordsDb(eg)
  38. for _, data := range fundDataList {
  39. // 1、判断是否是第一次执行
  40. lastRecord, err1 := fundDataRecordsDb.EggEnergyFundDataRecordsGetLast(data.Id)
  41. if err1 != nil {
  42. fmt.Println("DealFundData_ERR:::::", err1.Error())
  43. continue
  44. }
  45. if lastRecord == nil {
  46. // 2.1 首次执行 判断当前是否可以执行
  47. if utils.TimeParseStd(data.CreateAt).Add(time.Duration(data.Frequency) * time.Minute).After(now) {
  48. continue
  49. }
  50. } else {
  51. // 2.2 已经被执行过
  52. if utils.TimeParseStd(lastRecord.CreateAt).Add(time.Duration(data.Frequency) * time.Minute).After(now) {
  53. continue
  54. }
  55. }
  56. // 3、计算总执行次数、每次更新金额 (每次更新金额 = 金额 / 总执行次数 总执行次数 = 时间 / 频次)
  57. frequency := decimal.NewFromInt(int64(data.Frequency))
  58. times := decimal.NewFromInt(int64(data.Hours * 60)).Div(frequency).RoundFloor(8)
  59. totalAmount, err2 := decimal.NewFromString(data.TotalAmount)
  60. if err2 != nil {
  61. fmt.Println("DealFundData_ERR:::::", err2.Error())
  62. continue
  63. }
  64. amount := totalAmount.Div(times)
  65. // 4、推入rabbitmq 异步处理
  66. ch.Publish(md2.EggEnergyExchange, md2.EggEnergyStructForEggEnergyFundData{
  67. ID: data.Id,
  68. Amount: amount.String(),
  69. }, md2.EggEnergyRoutKeyForEggEnergyFundData)
  70. //session := eg.NewSession()
  71. //session.Begin()
  72. //
  73. //eggEnergyCoreData, cb, err2 := svc.GetEggEnergyCoreData(eg)
  74. //if err2 != nil {
  75. // _ = logx.Error(err2)
  76. //}
  77. //if cb != nil {
  78. // defer cb() // 释放锁
  79. //}
  80. ////计算涨价公式
  81. //err3, calcPriceIncreaseFormulaResp := egg_energy.CalcPriceIncreaseFormula(utils.AnyToString(amount), eggEnergyCoreData)
  82. //if err3 != nil {
  83. // _ = logx.Error(err3)
  84. // _ = session.Rollback()
  85. // session.Close()
  86. // continue
  87. //}
  88. //
  89. //dealAvailableEggEnergyCoinReq := md2.DealAvailableEggEnergyCoinReq{
  90. // Amount: calcPriceIncreaseFormulaResp.GetEggEnergyNums,
  91. // AmountFee: "",
  92. // BeforePrice: calcPriceIncreaseFormulaResp.BeforePrice,
  93. // AfterPrice: calcPriceIncreaseFormulaResp.AfterPrice,
  94. // BeforePlanetTotalValue: calcPriceIncreaseFormulaResp.BeforePlanetTotalValue,
  95. // AfterPlanetTotalValue: calcPriceIncreaseFormulaResp.AfterPlanetTotalValue,
  96. // BeforeEnergyTotalNums: calcPriceIncreaseFormulaResp.BeforeEnergyTotalNums,
  97. // AfterEnergyTotalNums: calcPriceIncreaseFormulaResp.AfterEnergyTotalNums,
  98. //}
  99. //
  100. //err4 := egg_energy.DealAvailableEggEnergyCoin(session, int(enum.CapitalInjection), eggEnergyCoreData, dealAvailableEggEnergyCoinReq)
  101. //if err4 != nil {
  102. // fmt.Println("ActivityCoinAutoExchangeEggPersonEnergy:::::err111:::", err4)
  103. // _ = session.Rollback()
  104. // session.Close()
  105. // continue
  106. //}
  107. //
  108. //// 插入资金数据详细数据
  109. //record := model.EggEnergyFundDataRecords{
  110. // RecordsId: data.Id,
  111. // TotalAmount: data.TotalAmount,
  112. // BalanceAmount: utils.AnyToString(utils.AnyToFloat64(data.BalanceAmount) - amount),
  113. // BalanceTimes: data.BalanceTimes - 1,
  114. // BeforePrice: calcPriceIncreaseFormulaResp.BeforePrice,
  115. // AfterPrice: calcPriceIncreaseFormulaResp.AfterPrice,
  116. // BeforePlanetTotalValue: calcPriceIncreaseFormulaResp.BeforePlanetTotalValue,
  117. // AfterPlanetTotalValue: calcPriceIncreaseFormulaResp.AfterPlanetTotalValue,
  118. // CreateAt: now.Format("2006-01-02 15:04:05"),
  119. // UpdateAt: now.Format("2006-01-02 15:04:05"),
  120. //}
  121. //_, err5 := fundDataRecordsDb.EggEnergyFundDataRecordsInsertBySession(session, record)
  122. //if err5 != nil {
  123. // _ = session.Rollback()
  124. // continue
  125. //}
  126. //
  127. //// 更新当前数据
  128. //fundData := model.EggEnergyFundData{
  129. // Id: data.Id,
  130. // Kind: data.Kind,
  131. // TotalAmount: data.TotalAmount,
  132. // BalanceAmount: utils.AnyToString(utils.AnyToFloat64(data.BalanceAmount) - amount),
  133. // Hours: data.Hours,
  134. // BalanceTimes: data.BalanceTimes - 1,
  135. // Frequency: data.Frequency,
  136. // Memo: data.Memo,
  137. // CreateAt: data.CreateAt,
  138. // UpdateAt: now.Format("2006-01-02 15:04:05"),
  139. //}
  140. //
  141. //forceColumns := []string{
  142. // "balance_times",
  143. // "balance_amount",
  144. //}
  145. //_, err6 := fundDataDb.EggEnergyFundDataUpdateBySession(session, fundData, forceColumns...)
  146. //if err6 != nil {
  147. // _ = logx.Error(err6)
  148. // _ = session.Rollback()
  149. // continue
  150. //}
  151. }
  152. cache.Del(DealFundDataKey)
  153. }