From dc6649bed54ccc273fef2b90c5a654b70eb07923 Mon Sep 17 00:00:00 2001 From: shenjiachi Date: Fri, 15 Nov 2024 10:41:17 +0800 Subject: [PATCH] add save price --- app/task/svc/svc_save_egg_energy_price.go | 69 +++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 app/task/svc/svc_save_egg_energy_price.go diff --git a/app/task/svc/svc_save_egg_energy_price.go b/app/task/svc/svc_save_egg_energy_price.go new file mode 100644 index 0000000..5303094 --- /dev/null +++ b/app/task/svc/svc_save_egg_energy_price.go @@ -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 +}