package egg_energy import ( "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum" md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/svc" zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx" "encoding/json" "errors" "fmt" "github.com/shopspring/decimal" "time" "xorm.io/xorm" ) // AutoAdjustPrice 自动调整价格 func AutoAdjustPrice(engine *xorm.Engine) (err error) { now := time.Now() fmt.Println(now.Hour()) //1、查找 `egg_energy_core_data` && `egg_energy_basic_setting` eggEnergyCoreData, cb, err := svc.GetEggEnergyCoreData(engine) if err != nil { return } if cb != nil { defer cb() // 释放锁 } eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(engine) eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{ "key": "is_open", "value": 1, }) if err != nil { return } if eggEnergyBasicSetting == nil { err = errors.New("蛋蛋能量设置未开启!") return } var priceSettingStruct *md2.PriceSettingStruct err = json.Unmarshal([]byte(eggEnergyBasicSetting.PriceSetting), &priceSettingStruct) if err != nil { return } if priceSettingStruct.PriceHigherThanValue == "" || priceSettingStruct.MarketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue == "" || priceSettingStruct.PriceBelowValue == "" || priceSettingStruct.MarketplaceMerchantsFundExchangeMarketplaceMerchantsNumsValue == "" { err = errors.New("价格设置未完全!") return } session := engine.NewSession() defer func() { session.Close() if err := recover(); err != nil { _ = zhios_order_relate_logx.Error(err) } }() session.Begin() priceHigherThanValue, _ := decimal.NewFromString(priceSettingStruct.PriceHigherThanValue) marketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue, _ := decimal.NewFromString(priceSettingStruct.MarketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue) marketplaceMerchantsFundExchangeMarketplaceMerchantsNumsValue, _ := decimal.NewFromString(priceSettingStruct.MarketplaceMerchantsFundExchangeMarketplaceMerchantsNumsValue) priceBelowValue, _ := decimal.NewFromString(priceSettingStruct.PriceBelowValue) nowPriceValue, _ := decimal.NewFromString(eggEnergyCoreData.NowPrice) if nowPriceValue.GreaterThan(priceHigherThanValue) { //当价格涨到设置某个价格时,自动执行 市商数量 兑换 市商资金 (降价公式) err1, resp := CalcPriceReductionFormula(marketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue.String(), eggEnergyCoreData, "", *eggEnergyBasicSetting) if err1 != nil { fmt.Println("err1111:::", err1) _ = session.Rollback() return err1 } // 市商数量 减少、市商资金 增加 err = DealAvailableEggEnergyCoin(session, int(enum.MarketplaceMerchantNumsAutoExchangeMarketplaceMerchantFunds), eggEnergyCoreData, md2.DealAvailableEggEnergyCoinReq{ Amount: marketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue.String(), AmountValue: resp.GetEggEnergyAmount, AmountFee: resp.AmountFee, BeforePrice: resp.BeforePrice, AfterPrice: resp.AfterPrice, BeforePlanetTotalValue: resp.BeforePlanetTotalValue, AfterPlanetTotalValue: resp.AfterPlanetTotalValue, BeforeEnergyTotalNums: resp.BeforeEnergyTotalNums, AfterEnergyTotalNums: resp.AfterEnergyTotalNums, }) if err != nil { fmt.Println("err:::::22222", err) _ = session.Rollback() return err } } if nowPriceValue.LessThan(priceBelowValue) { //当价格降到设置某个价格时,自动执行 市商资金 兑换 市商数量 (涨价公式) err1, resp := CalcPriceIncreaseFormula(marketplaceMerchantsFundExchangeMarketplaceMerchantsNumsValue.String(), eggEnergyCoreData) if err1 != nil { fmt.Println("err1111:::", err1) _ = session.Rollback() return err1 } // 市商数量 增加、市商资金 减少 err = DealAvailableEggEnergyCoin(session, int(enum.MarketplaceMerchantFundsAutoExchangeMarketplaceMerchantNums), eggEnergyCoreData, md2.DealAvailableEggEnergyCoinReq{ Amount: resp.GetEggEnergyNums, AmountValue: marketplaceMerchantsFundExchangeMarketplaceMerchantsNumsValue.String(), AmountFee: "", BeforePrice: resp.BeforePrice, AfterPrice: resp.AfterPrice, BeforePlanetTotalValue: resp.BeforePlanetTotalValue, AfterPlanetTotalValue: resp.AfterPlanetTotalValue, BeforeEnergyTotalNums: resp.BeforeEnergyTotalNums, AfterEnergyTotalNums: resp.AfterEnergyTotalNums, }) if err != nil { fmt.Println("err:::::22222", err) _ = session.Rollback() return err } } err = session.Commit() if err != nil { _ = session.Rollback() return errors.New("事务提交失败") } return }