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

39 lines
1.2 KiB

  1. package egg_energy
  2. import (
  3. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  4. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  5. "github.com/shopspring/decimal"
  6. "time"
  7. "xorm.io/xorm"
  8. )
  9. // CalcTodayGreenEnergyPriceRises 计算绿色能量今日价格涨幅
  10. func CalcTodayGreenEnergyPriceRises(engine *xorm.Engine, eggEnergyCoreData *model.EggEnergyCoreData) (err error, rises float64, isRises bool) {
  11. now := time.Now()
  12. if eggEnergyCoreData == nil {
  13. //1、查找 `egg_energy_core_data` 核心数据
  14. eggEnergyCoreDataDb := implement.NewEggEnergyCoreDataDb(engine)
  15. eggEnergyCoreData, err = eggEnergyCoreDataDb.EggEnergyCoreDataGet()
  16. if err != nil {
  17. return
  18. }
  19. }
  20. //2、查找昨日价格
  21. var m model.EggEnergyPrice
  22. if has, err1 := engine.Where("date =?", now.AddDate(0, 0, -1).Format("2006-01-02")).And("hour = 23").Get(&m); err1 != nil || has == false {
  23. err = err1
  24. return
  25. }
  26. yesterdayPrice, _ := decimal.NewFromString(m.Price)
  27. todayPrice, _ := decimal.NewFromString(eggEnergyCoreData.NowPrice)
  28. if todayPrice.GreaterThanOrEqual(yesterdayPrice) {
  29. isRises = true
  30. rises, _ = todayPrice.Sub(yesterdayPrice).Div(yesterdayPrice).Float64()
  31. } else {
  32. rises, _ = yesterdayPrice.Sub(todayPrice).Div(todayPrice).Float64()
  33. }
  34. return
  35. }