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

88 lines
5.0 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. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "github.com/shopspring/decimal"
  10. )
  11. // CalcPriceIncreaseFormula 计算涨价公式(【用户资金 ÷(用户资金+星球价值)÷ 现行数量】 = 用户获得蛋蛋能量个数)
  12. func CalcPriceIncreaseFormula(userAmountValue string, eggEnergyCoreData *model.EggEnergyCoreData) (err error, resp md.CalcPriceIncreaseFormulaResp) {
  13. userAmount, _ := decimal.NewFromString(userAmountValue) //用户资金
  14. nowPrice, _ := decimal.NewFromString(eggEnergyCoreData.NowPrice) //当前价格
  15. nowEnergyTotalNums, _ := decimal.NewFromString(eggEnergyCoreData.NowEnergyTotalNums) //现行数量
  16. planetTotalValue, _ := decimal.NewFromString(eggEnergyCoreData.PlanetTotalValue) //星球价值
  17. afterPrice := (planetTotalValue.Add(userAmount)).Div(nowEnergyTotalNums).Truncate(16) //涨价后的价格
  18. fmt.Println("userAmount:::", userAmount.String())
  19. fmt.Println("nowEnergyTotalNums:::", nowEnergyTotalNums.String())
  20. fmt.Println("planetTotalValue:::", planetTotalValue.String())
  21. fmt.Println("afterPrice:::", afterPrice.String())
  22. getEggEnergyNumsValue := userAmount.Div(afterPrice).Truncate(8) //用户得到绿色能量个数
  23. afterEnergyTotalNumsValue := nowEnergyTotalNums.Add(getEggEnergyNumsValue) //变更后-现行数量
  24. afterPlanetTotalValue := afterEnergyTotalNumsValue.Mul(afterPrice) //变更后-星球价值
  25. fmt.Println("afterPlanetTotalValue:::", afterPlanetTotalValue.String())
  26. resp.GetEggEnergyNums = getEggEnergyNumsValue.String()
  27. resp.BeforePrice = nowPrice.String()
  28. resp.AfterPrice = afterPrice.String()
  29. resp.BeforePlanetTotalValue = eggEnergyCoreData.PlanetTotalValue
  30. resp.AfterPlanetTotalValue = afterPlanetTotalValue.Truncate(8).String()
  31. resp.BeforeEnergyTotalNums = eggEnergyCoreData.NowEnergyTotalNums
  32. resp.AfterEnergyTotalNums = afterEnergyTotalNumsValue.Truncate(8).String()
  33. egg_system_rules.FilePutContents("CalcPriceIncreaseFormula", egg_system_rules.SerializeStr(resp))
  34. return
  35. }
  36. // CalcPriceReductionFormula 计算降价公式(【用户需兑换蛋蛋能量数量*{星球价值 ÷(用户需兑换蛋蛋能量数量+现行数量} *(1 - 扣比例50% ~ 23%) = 用户获得钱)
  37. func CalcPriceReductionFormula(userExchangeNumsValue string, eggEnergyCoreData *model.EggEnergyCoreData, levelId string, eggEnergyBasicSetting model.EggEnergyBasicSetting) (err error, resp md.CalcPriceReductionFormulaResp) {
  38. exchangeAccountBalanceFee := decimal.NewFromInt(0) //兑换手续费
  39. userExchangeNums, _ := decimal.NewFromString(userExchangeNumsValue) //用户兑换绿色能量
  40. if levelId != "" {
  41. var vipEquitySetting []*md.VipEquitySettingStruct
  42. err = json.Unmarshal([]byte(eggEnergyBasicSetting.VipEquitySetting), &vipEquitySetting)
  43. if err != nil {
  44. fmt.Println("err:::::", err)
  45. return
  46. }
  47. var exchangeAccountBalanceFeeValue string
  48. for _, v := range vipEquitySetting {
  49. if v.VipLevelId == levelId {
  50. exchangeAccountBalanceFeeValue = v.ExchangeAccountBalanceFee
  51. }
  52. }
  53. if exchangeAccountBalanceFeeValue == "" {
  54. err = errors.New("未查询到当前会员等级兑换余额手续费")
  55. return
  56. }
  57. exchangeAccountBalanceFee, _ = decimal.NewFromString(exchangeAccountBalanceFeeValue) //兑换手续费
  58. }
  59. decimalRate := decimal.NewFromInt(100) //百分比
  60. nowEnergyTotalNums, _ := decimal.NewFromString(eggEnergyCoreData.NowEnergyTotalNums) //现行数量
  61. planetTotalValue, _ := decimal.NewFromString(eggEnergyCoreData.PlanetTotalValue) //星球价值
  62. afterPrice := planetTotalValue.Div(userExchangeNums.Add(nowEnergyTotalNums)).Truncate(16) //降价后的价格
  63. greenEnergyValues := userExchangeNums.Mul(afterPrice) //能量价值
  64. greenEnergyFeeValues := greenEnergyValues.Mul(exchangeAccountBalanceFee.Div(decimalRate)) //能量个数扣除手续费价值
  65. resp.GetEggEnergyAmount = greenEnergyValues.Sub(greenEnergyFeeValues).Truncate(8).String() //用户实际得到的钱
  66. amountFee := userExchangeNums.Mul(exchangeAccountBalanceFee.Div(decimalRate)) //能量手续费
  67. resp.AmountFee = amountFee.String()
  68. afterEnergyTotalNumsValue := nowEnergyTotalNums.Sub(userExchangeNums.Sub(amountFee)) //变更后-现行数量
  69. afterPlanetTotalValue := afterEnergyTotalNumsValue.Mul(afterPrice) //变更后-星球价值
  70. resp.BeforePrice = eggEnergyCoreData.NowPrice
  71. resp.AfterPrice = afterPrice.String()
  72. resp.BeforePlanetTotalValue = eggEnergyCoreData.PlanetTotalValue
  73. resp.AfterPlanetTotalValue = afterPlanetTotalValue.String()
  74. resp.BeforeEnergyTotalNums = eggEnergyCoreData.NowEnergyTotalNums
  75. resp.AfterEnergyTotalNums = afterEnergyTotalNumsValue.String()
  76. egg_system_rules.FilePutContents("CalcPriceReductionFormula", egg_system_rules.SerializeStr(resp))
  77. return
  78. }