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

auto_adjust_prices.go 4.8 KiB

1 month ago
1 week ago
1 month ago
1 month ago
1 week ago
1 month ago
1 month ago
1 week ago
1 month ago
1 month ago
1 week ago
1 month ago
1 month ago
1 month ago
1 month ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. marketplaceMerchantsFundExchangeMarketplaceMerchantsNumsValue, _ := decimal.NewFromString(priceSettingStruct.MarketplaceMerchantsFundExchangeMarketplaceMerchantsNumsValue)
  59. priceBelowValue, _ := decimal.NewFromString(priceSettingStruct.PriceBelowValue)
  60. nowPriceValue, _ := decimal.NewFromString(eggEnergyCoreData.NowPrice)
  61. if nowPriceValue.GreaterThan(priceHigherThanValue) {
  62. //当价格涨到设置某个价格时,自动执行 市商数量 兑换 市商资金 (降价公式)
  63. err1, resp := CalcPriceReductionFormula(marketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue.String(), eggEnergyCoreData)
  64. if err1 != nil {
  65. fmt.Println("err1111:::", err1)
  66. _ = session.Rollback()
  67. return err1
  68. }
  69. // 市商数量 减少、市商资金 增加
  70. err = DealAvailableEggEnergyCoin(session, int(enum.MarketplaceMerchantNumsAutoExchangeMarketplaceMerchantFunds), eggEnergyCoreData, md2.DealAvailableEggEnergyCoinReq{
  71. Amount: resp.GetEggEnergyAmount,
  72. AmountFee: marketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue.String(),
  73. BeforePrice: resp.BeforePrice,
  74. AfterPrice: resp.AfterPrice,
  75. BeforePlanetTotalValue: resp.BeforePlanetTotalValue,
  76. AfterPlanetTotalValue: resp.AfterPlanetTotalValue,
  77. BeforeEnergyTotalNums: resp.BeforeEnergyTotalNums,
  78. AfterEnergyTotalNums: resp.AfterEnergyTotalNums,
  79. })
  80. if err != nil {
  81. fmt.Println("err:::::22222", err)
  82. _ = session.Rollback()
  83. return err
  84. }
  85. }
  86. if nowPriceValue.LessThan(priceBelowValue) {
  87. //当价格降到设置某个价格时,自动执行 市商资金 兑换 市商数量 (涨价公式)
  88. err1, resp := CalcPriceIncreaseFormula(marketplaceMerchantsFundExchangeMarketplaceMerchantsNumsValue.String(), eggEnergyCoreData)
  89. if err1 != nil {
  90. fmt.Println("err1111:::", err1)
  91. _ = session.Rollback()
  92. return err1
  93. }
  94. // 市商数量 增加、市商资金 减少
  95. err = DealAvailableEggEnergyCoin(session, int(enum.MarketplaceMerchantFundsAutoExchangeMarketplaceMerchantNums), eggEnergyCoreData, md2.DealAvailableEggEnergyCoinReq{
  96. Amount: resp.GetEggEnergyNums,
  97. AmountFee: marketplaceMerchantsFundExchangeMarketplaceMerchantsNumsValue.String(),
  98. BeforePrice: resp.BeforePrice,
  99. AfterPrice: resp.AfterPrice,
  100. BeforePlanetTotalValue: resp.BeforePlanetTotalValue,
  101. AfterPlanetTotalValue: resp.AfterPlanetTotalValue,
  102. BeforeEnergyTotalNums: resp.BeforeEnergyTotalNums,
  103. AfterEnergyTotalNums: resp.AfterEnergyTotalNums,
  104. })
  105. if err != nil {
  106. fmt.Println("err:::::22222", err)
  107. _ = session.Rollback()
  108. return err
  109. }
  110. }
  111. err = session.Commit()
  112. if err != nil {
  113. _ = session.Rollback()
  114. return errors.New("事务提交失败")
  115. }
  116. return
  117. }