Browse Source

add save price

master
shenjiachi 1 month ago
parent
commit
dc6649bed5
1 changed files with 69 additions and 0 deletions
  1. +69
    -0
      app/task/svc/svc_save_egg_energy_price.go

+ 69
- 0
app/task/svc/svc_save_egg_energy_price.go View File

@@ -0,0 +1,69 @@
package svc

import (
"applet/app/utils"
"code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
"code.fnuoos.com/EggPlanet/egg_models.git/src/model"
"code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/svc"
"errors"
"fmt"
"time"
"xorm.io/xorm"
)

// SaveEggEnergyPrice 记录蛋蛋能量价格
func SaveEggEnergyPrice(engine *xorm.Engine, dbName string) {
fmt.Println("save_egg_energy_price...")
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
return
}
}()

now := time.Now()
hourNowStr := utils.AnyToString(now.Hour())
dateNowStr := now.Format("2006-01-02")

// 1. 获取最后一条记录
priceDb := implement.NewEggEnergyPriceDb(engine)
lastPrice, has, err := priceDb.EggEnergyPriceGetLastOne()
if err != nil {
fmt.Println("SaveEggEnergyPrice_ERR:::::", err.Error())
return
}
if has {
// 2. 判断最后一条记录是否间隔一个小时 不能在同一天的同一小时
if lastPrice.Date == dateNowStr && lastPrice.Hour == hourNowStr {
fmt.Println("当前小时已记录过价格")
return
}
}

// 3. 查询当前价格
eggEnergyCoreData, cb, err1 := svc.GetEggEnergyCoreData(engine)
if err1 != nil {
fmt.Println("SaveEggEnergyPrice_ERR:::::", err1.Error())
return
}
if cb != nil {
defer cb() // 释放锁
}

// 4. 保存当前价格
priceData := model.EggEnergyPrice{
Price: eggEnergyCoreData.NowPrice,
Date: dateNowStr,
Hour: hourNowStr,
}
id, err2 := priceDb.EggEnergyPriceInsert(&priceData)
if err2 != nil {
fmt.Println("SaveEggEnergyPrice_ERR:::::", err2.Error())
return
}
if id == 0 {
fmt.Println("SaveEggEnergyPrice_ERR:::::", errors.New(fmt.Sprintf("%s 新增价格失败, 当前价格 %s", now.Format("2006-01-02 15:04:05"), eggEnergyCoreData.NowPrice)))
return
}
return
}

Loading…
Cancel
Save