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

88 lines
2.6 KiB

  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 EggEnergyDealFundDataKey = "egg_energy_deal_fund_data_key"
  14. // EggEnergyDealFundData 处理价值投入
  15. func EggEnergyDealFundData(eg *xorm.Engine) {
  16. fmt.Println("egg_energy_deal_fund_data...")
  17. defer func() {
  18. if err := recover(); err != nil {
  19. fmt.Println(err)
  20. return
  21. }
  22. }()
  23. // 悲观锁防止串行
  24. getString, _ := cache.GetString(EggEnergyDealFundDataKey)
  25. if getString != "" {
  26. fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次处理未执行完")
  27. return
  28. }
  29. cache.SetEx(EggEnergyDealFundDataKey, "running", 60*30) //30分钟
  30. defer cache.Del(EggEnergyDealFundDataKey)
  31. // 查询所有未被执行完的数据
  32. fundDataDb := implement.NewEggEnergyFundDataDb(eg)
  33. fundDataList, err := fundDataDb.EggEnergyFundDataFindNotFinish()
  34. if err != nil {
  35. fmt.Println("DealFundData_ERR:::::", err.Error())
  36. return
  37. }
  38. ch, err := rabbit.Cfg.Pool.GetChannel()
  39. if err != nil {
  40. return
  41. }
  42. defer ch.Release()
  43. now := time.Now()
  44. fundDataRecordsDb := implement.NewEggEnergyFundDataRecordsDb(eg)
  45. for _, data := range fundDataList {
  46. // 1、判断是否是第一次执行
  47. lastRecord, err1 := fundDataRecordsDb.EggEnergyFundDataRecordsGetLast(data.Id)
  48. if err1 != nil {
  49. fmt.Println("DealFundData_ERR:::::", err1.Error())
  50. continue
  51. }
  52. if lastRecord == nil {
  53. // 2.1 首次执行 判断当前是否可以执行
  54. if utils.TimeParseStd(data.CreateAt).Add(time.Duration(data.Frequency) * time.Minute).After(now) {
  55. continue
  56. }
  57. } else {
  58. // 2.2 已经被执行过
  59. if utils.TimeParseStd(lastRecord.CreateAt).Add(time.Duration(data.Frequency) * time.Minute).After(now) {
  60. continue
  61. }
  62. }
  63. // 3、计算总执行次数、每次更新金额 (每次更新金额 = 金额 / 总执行次数 总执行次数 = 时间 / 频次)
  64. frequency := decimal.NewFromInt(int64(data.Frequency))
  65. times := decimal.NewFromInt(int64(data.Hours * 60)).Div(frequency).RoundFloor(8)
  66. totalAmount, err2 := decimal.NewFromString(data.TotalAmount)
  67. if err2 != nil {
  68. fmt.Println("DealFundData_ERR:::::", err2.Error())
  69. continue
  70. }
  71. amount := totalAmount.Div(times)
  72. // 4、推入rabbitmq 异步处理
  73. ch.Publish(md2.EggEnergyExchange, md2.EggEnergyStructForEggEnergyFundData{
  74. ID: data.Id,
  75. Amount: amount.String(),
  76. }, md2.EggEnergyRoutKeyForEggEnergyFundData)
  77. }
  78. }