|
- package egg_energy
-
- import (
- "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
- egg_system_rules "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/shopspring/decimal"
- )
-
- // CalcPriceIncreaseFormula 计算涨价公式(【用户资金 ÷(用户资金+星球价值)÷ 现行数量】 = 用户获得蛋蛋能量个数)
- func CalcPriceIncreaseFormula(userAmountValue string, eggEnergyCoreData *model.EggEnergyCoreData) (err error, resp md.CalcPriceIncreaseFormulaResp) {
- userAmount, _ := decimal.NewFromString(userAmountValue) //用户资金
- nowPrice, _ := decimal.NewFromString(eggEnergyCoreData.NowPrice) //当前价格
- nowEnergyTotalNums, _ := decimal.NewFromString(eggEnergyCoreData.NowEnergyTotalNums) //现行数量
- planetTotalValue, _ := decimal.NewFromString(eggEnergyCoreData.PlanetTotalValue) //星球价值
- afterPrice := (planetTotalValue.Add(userAmount)).Div(nowEnergyTotalNums).Truncate(16) //涨价后的价格
- fmt.Println("userAmount:::", userAmount.String())
- fmt.Println("nowEnergyTotalNums:::", nowEnergyTotalNums.String())
- fmt.Println("planetTotalValue:::", planetTotalValue.String())
- fmt.Println("afterPrice:::", afterPrice.String())
- getEggEnergyNumsValue := userAmount.Div(afterPrice).Truncate(8) //用户得到绿色能量个数
- afterEnergyTotalNumsValue := nowEnergyTotalNums.Add(getEggEnergyNumsValue) //变更后-现行数量
- afterPlanetTotalValue := afterEnergyTotalNumsValue.Mul(afterPrice) //变更后-星球价值
- fmt.Println("afterPlanetTotalValue:::", afterPlanetTotalValue.String())
-
- resp.GetEggEnergyNums = getEggEnergyNumsValue.String()
- resp.BeforePrice = nowPrice.String()
- resp.AfterPrice = afterPrice.String()
- resp.BeforePlanetTotalValue = eggEnergyCoreData.PlanetTotalValue
- resp.AfterPlanetTotalValue = afterPlanetTotalValue.Truncate(8).String()
- resp.BeforeEnergyTotalNums = eggEnergyCoreData.NowEnergyTotalNums
- resp.AfterEnergyTotalNums = afterEnergyTotalNumsValue.Truncate(8).String()
- egg_system_rules.FilePutContents("CalcPriceIncreaseFormula", egg_system_rules.SerializeStr(resp))
- return
- }
-
- // CalcPriceReductionFormula 计算降价公式(【用户需兑换蛋蛋能量数量*{星球价值 ÷(用户需兑换蛋蛋能量数量+现行数量} *(1 - 扣比例50% ~ 23%) = 用户获得钱)
- func CalcPriceReductionFormula(userExchangeNumsValue string, eggEnergyCoreData *model.EggEnergyCoreData, levelId string, eggEnergyBasicSetting model.EggEnergyBasicSetting) (err error, resp md.CalcPriceReductionFormulaResp) {
- exchangeAccountBalanceFee := decimal.NewFromInt(0) //兑换手续费
- userExchangeNums, _ := decimal.NewFromString(userExchangeNumsValue) //用户兑换绿色能量
- if levelId != "" {
- var vipEquitySetting []*md.VipEquitySettingStruct
- err = json.Unmarshal([]byte(eggEnergyBasicSetting.VipEquitySetting), &vipEquitySetting)
- if err != nil {
- fmt.Println("err:::::", err)
- return
- }
- var exchangeAccountBalanceFeeValue string
- for _, v := range vipEquitySetting {
- if v.VipLevelId == levelId {
- exchangeAccountBalanceFeeValue = v.ExchangeAccountBalanceFee
- }
- }
- if exchangeAccountBalanceFeeValue == "" {
- err = errors.New("未查询到当前会员等级兑换余额手续费")
- return
- }
- exchangeAccountBalanceFee, _ = decimal.NewFromString(exchangeAccountBalanceFeeValue) //兑换手续费
- }
-
- decimalRate := decimal.NewFromInt(100) //百分比
- nowEnergyTotalNums, _ := decimal.NewFromString(eggEnergyCoreData.NowEnergyTotalNums) //现行数量
- planetTotalValue, _ := decimal.NewFromString(eggEnergyCoreData.PlanetTotalValue) //星球价值
- afterPrice := planetTotalValue.Div(userExchangeNums.Add(nowEnergyTotalNums)).Truncate(16) //降价后的价格
-
- greenEnergyValues := userExchangeNums.Mul(afterPrice) //能量价值
- greenEnergyFeeValues := greenEnergyValues.Mul(exchangeAccountBalanceFee.Div(decimalRate)) //能量个数扣除手续费价值
- resp.GetEggEnergyAmount = greenEnergyValues.Sub(greenEnergyFeeValues).Truncate(2).String() //用户实际得到的钱
- amountFee := userExchangeNums.Mul(exchangeAccountBalanceFee.Div(decimalRate)) //能量手续费
- resp.AmountFee = amountFee.Truncate(8).String()
-
- afterEnergyTotalNumsValue := nowEnergyTotalNums.Sub(userExchangeNums.Sub(amountFee)) //变更后-现行数量
- afterPlanetTotalValue := afterEnergyTotalNumsValue.Mul(afterPrice) //变更后-星球价值
-
- resp.BeforePrice = eggEnergyCoreData.NowPrice
- resp.AfterPrice = afterPrice.String()
- resp.BeforePlanetTotalValue = eggEnergyCoreData.PlanetTotalValue
- resp.AfterPlanetTotalValue = afterPlanetTotalValue.String()
- resp.BeforeEnergyTotalNums = eggEnergyCoreData.NowEnergyTotalNums
- resp.AfterEnergyTotalNums = afterEnergyTotalNumsValue.String()
-
- egg_system_rules.FilePutContents("CalcPriceReductionFormula", egg_system_rules.SerializeStr(resp))
- return
- }
|