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

59 lines
3.8 KiB

  1. package egg_energy
  2. import (
  3. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  4. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  5. egg_system_rules "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils"
  6. "fmt"
  7. "github.com/shopspring/decimal"
  8. )
  9. // CalcPriceIncreaseFormula 计算涨价公式(【用户资金 ÷(用户资金+星球价值)÷ 现行数量】 = 用户获得蛋蛋能量个数)
  10. func CalcPriceIncreaseFormula(userAmountValue string, eggEnergyCoreData *model.EggEnergyCoreData) (err error, resp md.CalcPriceIncreaseFormulaResp) {
  11. userAmount, _ := decimal.NewFromString(userAmountValue) //用户资金
  12. nowPrice, _ := decimal.NewFromString(eggEnergyCoreData.NowPrice) //当前价格
  13. nowEnergyTotalNums, _ := decimal.NewFromString(eggEnergyCoreData.NowEnergyTotalNums) //现行数量
  14. planetTotalValue, _ := decimal.NewFromString(eggEnergyCoreData.PlanetTotalValue) //星球价值
  15. afterPrice := (planetTotalValue.Add(userAmount)).Div(nowEnergyTotalNums).Truncate(16) //涨价后的价格
  16. fmt.Println("userAmount:::", userAmount.String())
  17. fmt.Println("nowEnergyTotalNums:::", nowEnergyTotalNums.String())
  18. fmt.Println("planetTotalValue:::", planetTotalValue.String())
  19. fmt.Println("afterPrice:::", afterPrice.String())
  20. getEggEnergyNumsValue := userAmount.Div(afterPrice).Truncate(8) //用户得到绿色能量个数
  21. afterEnergyTotalNumsValue := nowEnergyTotalNums.Add(getEggEnergyNumsValue) //变更后-现行数量
  22. afterPlanetTotalValue := afterEnergyTotalNumsValue.Mul(afterPrice) //变更后-星球价值
  23. fmt.Println("afterPlanetTotalValue:::", afterPlanetTotalValue.String())
  24. resp.GetEggEnergyNums = getEggEnergyNumsValue.String()
  25. resp.BeforePrice = nowPrice.String()
  26. resp.AfterPrice = afterPrice.String()
  27. resp.BeforePlanetTotalValue = eggEnergyCoreData.PlanetTotalValue
  28. resp.AfterPlanetTotalValue = afterPlanetTotalValue.Truncate(8).String()
  29. resp.BeforeEnergyTotalNums = eggEnergyCoreData.NowEnergyTotalNums
  30. resp.AfterEnergyTotalNums = afterEnergyTotalNumsValue.Truncate(8).String()
  31. egg_system_rules.FilePutContents("CalcPriceIncreaseFormula", egg_system_rules.SerializeStr(resp))
  32. return
  33. }
  34. // CalcPriceReductionFormula 计算降价公式(【用户需兑换蛋蛋能量数量*{星球价值 ÷(用户需兑换蛋蛋能量数量+现行数量} *(1 - 扣比例50% ~ 23%) = 用户获得钱)
  35. func CalcPriceReductionFormula(userExchangeNumsValue string, eggEnergyCoreData *model.EggEnergyCoreData) (err error, resp md.CalcPriceReductionFormulaResp) {
  36. userExchangeNums, _ := decimal.NewFromString(userExchangeNumsValue) //用户兑换绿色能量
  37. nowEnergyTotalNums, _ := decimal.NewFromString(eggEnergyCoreData.NowEnergyTotalNums) //现行数量
  38. planetTotalValue, _ := decimal.NewFromString(eggEnergyCoreData.PlanetTotalValue) //星球价值
  39. afterPrice := planetTotalValue.Div(userExchangeNums.Add(nowEnergyTotalNums)) //降价后的价格
  40. greenEnergyValues := userExchangeNums.Mul(afterPrice) //用户获得的钱
  41. afterEnergyTotalNumsValue := nowEnergyTotalNums.Sub(userExchangeNums) //变更后-现行数量
  42. afterPlanetTotalValue := afterEnergyTotalNumsValue.Mul(afterPrice) //变更后-星球价值
  43. resp.GetEggEnergyAmount = greenEnergyValues.Truncate(16).String()
  44. resp.BeforePrice = eggEnergyCoreData.NowPrice
  45. resp.AfterPrice = afterPrice.String()
  46. resp.BeforePlanetTotalValue = eggEnergyCoreData.PlanetTotalValue
  47. resp.AfterPlanetTotalValue = afterPlanetTotalValue.String()
  48. resp.BeforeEnergyTotalNums = eggEnergyCoreData.NowEnergyTotalNums
  49. resp.AfterEnergyTotalNums = afterEnergyTotalNumsValue.String()
  50. egg_system_rules.FilePutContents("CalcPriceReductionFormula", egg_system_rules.SerializeStr(resp))
  51. return
  52. }