蛋蛋星球-客户端
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.
 
 
 
 
 
 

44 lines
1.4 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  5. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  6. "github.com/shopspring/decimal"
  7. "time"
  8. "xorm.io/xorm"
  9. )
  10. // CalcTodayEggEnergyPriceRises 计算蛋蛋能量今日涨跌幅
  11. func CalcTodayEggEnergyPriceRises(engine *xorm.Engine, now time.Time) (err error, rises float64, isRises bool, nowPrice string, initialPrice string) {
  12. //1、查找昨日价格、今日价格 计算涨跌幅
  13. var m *model.EggEnergyPrice
  14. dateStr := now.AddDate(0, 0, -1).Format("2006-01-02")
  15. hourStr := now.AddDate(0, 0, -1).Hour()
  16. priceDb := implement.NewEggEnergyPriceDb(db.Db)
  17. m, err = priceDb.EggEnergyPriceGetOneByParams(dateStr, hourStr)
  18. if err != nil {
  19. return err, 0, false, "", ""
  20. }
  21. initialPrice = m.Price
  22. if initialPrice == "" {
  23. initialPrice = "1"
  24. }
  25. yesterdayPrice, _ := decimal.NewFromString(initialPrice)
  26. coreDataDb := implement.NewEggEnergyCoreDataDb(db.Db)
  27. coreData, err := coreDataDb.EggEnergyCoreDataGet()
  28. if err != nil {
  29. return err, 0, false, "", ""
  30. }
  31. nowPrice = coreData.NowPrice
  32. todayPrice, _ := decimal.NewFromString(coreData.NowPrice)
  33. if todayPrice.GreaterThanOrEqual(yesterdayPrice) {
  34. isRises = true
  35. rises, _ = todayPrice.Sub(yesterdayPrice).Div(yesterdayPrice).Float64()
  36. } else {
  37. rises, _ = yesterdayPrice.Sub(todayPrice).Div(todayPrice).Float64()
  38. }
  39. return nil, rises, isRises, nowPrice, initialPrice
  40. }