|
- package egg_energy
-
- import (
- "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
- "github.com/shopspring/decimal"
- "time"
- "xorm.io/xorm"
- )
-
- // CalcTodayGreenEnergyPriceRises 计算绿色能量今日价格涨幅
- func CalcTodayGreenEnergyPriceRises(engine *xorm.Engine, eggEnergyCoreData *model.EggEnergyCoreData) (err error, rises float64, isRises bool) {
- now := time.Now()
- if eggEnergyCoreData == nil {
- //1、查找 `egg_energy_core_data` 核心数据
- eggEnergyCoreDataDb := implement.NewEggEnergyCoreDataDb(engine)
- eggEnergyCoreData, err = eggEnergyCoreDataDb.EggEnergyCoreDataGet()
- if err != nil {
- return
- }
- }
-
- //2、查找昨日价格
- var m model.EggEnergyPrice
- if has, err1 := engine.Where("date =?", now.AddDate(0, 0, -1).Format("2006-01-02")).And("hour = 23").Get(&m); err1 != nil || has == false {
- err = err1
- return
- }
- yesterdayPrice, _ := decimal.NewFromString(m.Price)
- todayPrice, _ := decimal.NewFromString(eggEnergyCoreData.NowPrice)
- if todayPrice.GreaterThanOrEqual(yesterdayPrice) {
- isRises = true
- rises, _ = todayPrice.Sub(yesterdayPrice).Div(yesterdayPrice).Float64()
- } else {
- rises, _ = yesterdayPrice.Sub(todayPrice).Div(todayPrice).Float64()
- }
- return
- }
|