蛋蛋星球-客户端
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

49 linhas
1.6 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, "0", "0"
  20. }
  21. initialPrice = m.Price
  22. if initialPrice == "" {
  23. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  24. basicSetting, err1 := settingDb.EggEnergyBasicSettingGetOne()
  25. if err1 != nil {
  26. return err1, 0, false, "0", "0"
  27. }
  28. initialPrice = basicSetting.InitialPrice
  29. }
  30. yesterdayPrice, _ := decimal.NewFromString(initialPrice)
  31. coreDataDb := implement.NewEggEnergyCoreDataDb(db.Db)
  32. coreData, err := coreDataDb.EggEnergyCoreDataGet()
  33. if err != nil {
  34. return err, 0, false, "0", "0"
  35. }
  36. nowPrice = coreData.NowPrice
  37. todayPrice, _ := decimal.NewFromString(coreData.NowPrice)
  38. if todayPrice.GreaterThanOrEqual(yesterdayPrice) {
  39. isRises = true
  40. rises, _ = todayPrice.Sub(yesterdayPrice).Div(yesterdayPrice).Float64()
  41. } else {
  42. rises, _ = yesterdayPrice.Sub(todayPrice).Div(todayPrice).Float64()
  43. }
  44. return nil, rises, isRises, nowPrice, initialPrice
  45. }