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

129 lines
4.9 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. 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, "", *eggEnergyBasicSetting)
  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: marketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue.String(),
  72. AmountValue: resp.GetEggEnergyAmount,
  73. AmountFee: resp.AmountFee,
  74. BeforePrice: resp.BeforePrice,
  75. AfterPrice: resp.AfterPrice,
  76. BeforePlanetTotalValue: resp.BeforePlanetTotalValue,
  77. AfterPlanetTotalValue: resp.AfterPlanetTotalValue,
  78. BeforeEnergyTotalNums: resp.BeforeEnergyTotalNums,
  79. AfterEnergyTotalNums: resp.AfterEnergyTotalNums,
  80. })
  81. if err != nil {
  82. fmt.Println("err:::::22222", err)
  83. _ = session.Rollback()
  84. return err
  85. }
  86. }
  87. if nowPriceValue.LessThan(priceBelowValue) {
  88. //当价格降到设置某个价格时,自动执行 市商资金 兑换 市商数量 (涨价公式)
  89. err1, resp := CalcPriceIncreaseFormula(marketplaceMerchantsFundExchangeMarketplaceMerchantsNumsValue.String(), eggEnergyCoreData)
  90. if err1 != nil {
  91. fmt.Println("err1111:::", err1)
  92. _ = session.Rollback()
  93. return err1
  94. }
  95. // 市商数量 增加、市商资金 减少
  96. err = DealAvailableEggEnergyCoin(session, int(enum.MarketplaceMerchantFundsAutoExchangeMarketplaceMerchantNums), eggEnergyCoreData, md2.DealAvailableEggEnergyCoinReq{
  97. Amount: resp.GetEggEnergyNums,
  98. AmountValue: marketplaceMerchantsFundExchangeMarketplaceMerchantsNumsValue.String(),
  99. AmountFee: "",
  100. BeforePrice: resp.BeforePrice,
  101. AfterPrice: resp.AfterPrice,
  102. BeforePlanetTotalValue: resp.BeforePlanetTotalValue,
  103. AfterPlanetTotalValue: resp.AfterPlanetTotalValue,
  104. BeforeEnergyTotalNums: resp.BeforeEnergyTotalNums,
  105. AfterEnergyTotalNums: resp.AfterEnergyTotalNums,
  106. })
  107. if err != nil {
  108. fmt.Println("err:::::22222", err)
  109. _ = session.Rollback()
  110. return err
  111. }
  112. }
  113. err = session.Commit()
  114. if err != nil {
  115. _ = session.Rollback()
  116. return errors.New("事务提交失败")
  117. }
  118. return
  119. }