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

126 lines
4.5 KiB

  1. package egg_energy
  2. import (
  3. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  4. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum"
  5. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  6. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/svc"
  7. zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx"
  8. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "github.com/shopspring/decimal"
  12. "time"
  13. "xorm.io/xorm"
  14. )
  15. // AutoAdjustPrice 自动调整价格
  16. func AutoAdjustPrice(engine *xorm.Engine) (err error) {
  17. now := time.Now()
  18. fmt.Println(now.Hour())
  19. //1、查找 `egg_energy_core_data` && `egg_energy_basic_setting`
  20. eggEnergyCoreData, cb, err := svc.GetEggEnergyCoreData(engine)
  21. if err != nil {
  22. return
  23. }
  24. if cb != nil {
  25. defer cb() // 释放锁
  26. }
  27. eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(engine)
  28. eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  29. "key": "is_open",
  30. "value": 1,
  31. })
  32. if err != nil {
  33. return
  34. }
  35. if eggEnergyBasicSetting == nil {
  36. err = errors.New("蛋蛋能量设置未开启!")
  37. return
  38. }
  39. var priceSettingStruct *md2.PriceSettingStruct
  40. err = json.Unmarshal([]byte(eggEnergyBasicSetting.PriceSetting), &priceSettingStruct)
  41. if err != nil {
  42. return
  43. }
  44. if priceSettingStruct.PriceHigherThanValue == "" || priceSettingStruct.MarketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue == "" || priceSettingStruct.PriceBelowValue == "" || priceSettingStruct.MarketplaceMerchantsFundExchangeMarketplaceMerchantsNumsValue == "" {
  45. err = errors.New("价格设置未完全!")
  46. return
  47. }
  48. session := engine.NewSession()
  49. defer func() {
  50. session.Close()
  51. if err := recover(); err != nil {
  52. _ = zhios_order_relate_logx.Error(err)
  53. }
  54. }()
  55. session.Begin()
  56. priceHigherThanValue, _ := decimal.NewFromString(priceSettingStruct.PriceHigherThanValue)
  57. marketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue, _ := decimal.NewFromString(priceSettingStruct.MarketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue)
  58. priceBelowValue, _ := decimal.NewFromString(priceSettingStruct.PriceBelowValue)
  59. nowPriceValue, _ := decimal.NewFromString(eggEnergyCoreData.NowPrice)
  60. if nowPriceValue.GreaterThan(priceHigherThanValue) {
  61. //当价格涨到设置某个价格时,自动执行 市商数量 兑换 市商资金 (降价公式)
  62. err1, resp := CalcPriceReductionFormula(marketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue.String(), eggEnergyCoreData)
  63. if err1 != nil {
  64. fmt.Println("err1111:::", err1)
  65. _ = session.Rollback()
  66. return err1
  67. }
  68. // 市商数量 减少、市商资金 增加
  69. err = DealAvailableEggEnergyCoin(session, int(enum.MarketplaceMerchantNumsAutoExchangeMarketplaceMerchantFunds), eggEnergyCoreData, md2.DealAvailableEggEnergyCoinReq{
  70. Amount: resp.GetEggEnergyAmount,
  71. AmountFee: "",
  72. BeforePrice: resp.BeforePrice,
  73. AfterPrice: resp.AfterPrice,
  74. BeforePlanetTotalValue: resp.BeforePlanetTotalValue,
  75. AfterPlanetTotalValue: resp.AfterPlanetTotalValue,
  76. BeforeEnergyTotalNums: resp.BeforeEnergyTotalNums,
  77. AfterEnergyTotalNums: resp.AfterEnergyTotalNums,
  78. })
  79. if err != nil {
  80. fmt.Println("err:::::22222", err)
  81. _ = session.Rollback()
  82. return err
  83. }
  84. }
  85. if nowPriceValue.LessThan(priceBelowValue) {
  86. //当价格降到设置某个价格时,自动执行 市商资金 兑换 市商数量 (涨价公式)
  87. err1, resp := CalcPriceIncreaseFormula(marketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue.String(), eggEnergyCoreData)
  88. if err1 != nil {
  89. fmt.Println("err1111:::", err1)
  90. _ = session.Rollback()
  91. return err1
  92. }
  93. // 市商数量 增加、市商资金 减少
  94. err = DealAvailableEggEnergyCoin(session, int(enum.MarketplaceMerchantFundsAutoExchangeMarketplaceMerchantNums), eggEnergyCoreData, md2.DealAvailableEggEnergyCoinReq{
  95. Amount: resp.GetEggEnergyNums,
  96. AmountFee: "",
  97. BeforePrice: resp.BeforePrice,
  98. AfterPrice: resp.AfterPrice,
  99. BeforePlanetTotalValue: resp.BeforePlanetTotalValue,
  100. AfterPlanetTotalValue: resp.AfterPlanetTotalValue,
  101. BeforeEnergyTotalNums: resp.BeforeEnergyTotalNums,
  102. AfterEnergyTotalNums: resp.AfterEnergyTotalNums,
  103. })
  104. if err != nil {
  105. fmt.Println("err:::::22222", err)
  106. _ = session.Rollback()
  107. return err
  108. }
  109. }
  110. err = session.Commit()
  111. if err != nil {
  112. _ = session.Rollback()
  113. return errors.New("事务提交失败")
  114. }
  115. return
  116. }