蛋蛋星球-制度模式
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.

119 line
3.7 KiB

  1. package egg_energy
  2. import (
  3. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  4. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  5. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  6. "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  7. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  8. "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/cache"
  9. zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx"
  10. "errors"
  11. "fmt"
  12. "github.com/shopspring/decimal"
  13. "time"
  14. "xorm.io/xorm"
  15. )
  16. const LockKey = "auto_release_exchange_team_egg_energy_lock_key"
  17. // AutoReleaseExchangeTeamEggEnergy 团队蛋蛋能量自动释放成可用绿色能量
  18. func AutoReleaseExchangeTeamEggEnergy(engine *xorm.Engine, masterId string) (err error) {
  19. now := time.Now()
  20. fmt.Println(now.Hour())
  21. if !(now.Hour() > 1 && now.Hour() < 8) {
  22. //TODO::只在凌晨一点 ~ 凌晨 8 点运行
  23. return errors.New("非运行时间")
  24. }
  25. //TODO::增加“悲观锁”防止串行
  26. getString, _ := cache.GetString(LockKey)
  27. //if err != nil {
  28. // return err
  29. //}
  30. if getString != "" {
  31. fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完")
  32. return errors.New("上一次结算未执行完")
  33. }
  34. cache.SetEx(LockKey, "running", 3600*8) //8小时
  35. //1、查找`egg_energy_basic_setting` 基础设置
  36. eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(engine)
  37. eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  38. "key": "is_open",
  39. "value": 1,
  40. })
  41. if err != nil {
  42. return
  43. }
  44. if eggEnergyBasicSetting.SettlementQuantity == 0 {
  45. err = errors.New("结算百分比未设置!")
  46. return
  47. }
  48. settlementQuantity := eggEnergyBasicSetting.SettlementQuantity
  49. fmt.Println("settlementQuantity>>>>>>>>>>>>", settlementQuantity)
  50. //2、当前 "可用"绿色能量可以兑换的用户数据
  51. var list1 []model.UserVirtualAmount
  52. err = engine.Where("coin_id = ?", eggEnergyBasicSetting.TeamEggEnergyCoinId).And("amount > ?", 0).Find(&list1)
  53. if err != nil {
  54. fmt.Println("err:::::1111", err)
  55. return
  56. }
  57. //4、处理释放
  58. settlementQuantityRadio := decimal.NewFromFloat(100)
  59. settlementQuantityValue := decimal.NewFromInt(int64(settlementQuantity)).Div(settlementQuantityRadio)
  60. for _, v := range list1 {
  61. userAmount, _ := decimal.NewFromString(v.Amount)
  62. settlementQuantityAmount, _ := userAmount.Mul(settlementQuantityValue).Float64()
  63. if settlementQuantityAmount > 0 {
  64. session := engine.NewSession()
  65. defer func() {
  66. session.Close()
  67. if err := recover(); err != nil {
  68. _ = zhios_order_relate_logx.Error(err)
  69. }
  70. }()
  71. session.Begin()
  72. //4.2给相应的用户减少团队蛋蛋能量
  73. err = rule.DealUserVirtualCoin(session, md.DealUserVirtualCoinReq{
  74. Kind: "sub",
  75. Title: enum.TeamEggEnergyReleaseToPersonEggEnergy.String(),
  76. TransferType: int(enum.TeamEggEnergyReleaseToPersonEggEnergy),
  77. CoinId: eggEnergyBasicSetting.TeamEggEnergyCoinId,
  78. Uid: v.Uid,
  79. Amount: settlementQuantityAmount,
  80. })
  81. if err != nil {
  82. _ = session.Rollback()
  83. fmt.Println("err:::::33333", err)
  84. continue
  85. }
  86. //4.3给相应的用户增加个人的蛋蛋能量
  87. err = rule.DealUserVirtualCoin(session, md.DealUserVirtualCoinReq{
  88. Kind: "add",
  89. Title: enum.PersonEggGetByTeamEggEnergyRelease.String(),
  90. TransferType: int(enum.PersonEggGetByTeamEggEnergyRelease),
  91. CoinId: eggEnergyBasicSetting.PersonEggEnergyCoinId,
  92. Uid: v.Uid,
  93. Amount: settlementQuantityAmount,
  94. })
  95. if err != nil {
  96. _ = session.Rollback()
  97. fmt.Println("err:::::22222", err)
  98. continue
  99. }
  100. err = session.Commit()
  101. if err != nil {
  102. _ = session.Rollback()
  103. continue
  104. }
  105. }
  106. }
  107. return
  108. }