|
1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- package svc
-
- import (
- "applet/app/db"
- "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"
- )
-
- // CalcTodayEggEnergyPriceRises 计算蛋蛋能量今日涨跌幅
- func CalcTodayEggEnergyPriceRises(engine *xorm.Engine, now time.Time) (err error, rises float64, isRises bool, nowPrice string, initialPrice string) {
-
- //1、查找昨日价格、今日价格 计算涨跌幅
- var m *model.EggEnergyPrice
- dateStr := now.AddDate(0, 0, -1).Format("2006-01-02")
- hourStr := now.AddDate(0, 0, -1).Hour()
- priceDb := implement.NewEggEnergyPriceDb(db.Db)
- m, err = priceDb.EggEnergyPriceGetOneByParams(dateStr, hourStr)
- if err != nil {
- return err, 0, false, "", ""
- }
- initialPrice = m.Price
- // todo 有数据后不需要此判断
- if initialPrice == "" {
- initialPrice = "1"
- }
- yesterdayPrice, _ := decimal.NewFromString(initialPrice)
- coreDataDb := implement.NewEggEnergyCoreDataDb(db.Db)
- coreData, err := coreDataDb.EggEnergyCoreDataGet()
- if err != nil {
- return err, 0, false, "", ""
- }
- nowPrice = coreData.NowPrice
- todayPrice, _ := decimal.NewFromString(coreData.NowPrice)
- if todayPrice.GreaterThanOrEqual(yesterdayPrice) {
- isRises = true
- rises, _ = todayPrice.Sub(yesterdayPrice).Div(yesterdayPrice).Float64()
- } else {
- rises, _ = yesterdayPrice.Sub(todayPrice).Div(todayPrice).Float64()
- }
- return nil, rises, isRises, nowPrice, initialPrice
- }
|