package svc import ( "applet/app/cfg" "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" "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/svc" "errors" "fmt" "time" "xorm.io/xorm" ) // EggEnergyAutoRecordPrices 蛋蛋能量-自动记录价格 func EggEnergyAutoRecordPrices(engine *xorm.Engine) { fmt.Println("egg_energy_auto_record_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("EggEnergyAutoRecordPrices_ERR:::::", err.Error()) return } // 2. 查询当前价格 egg_system_rules.Init(cfg.RedisAddr) eggEnergyCoreData, cb, err1 := svc.GetEggEnergyCoreData(engine) if err1 != nil { fmt.Println("EggEnergyAutoRecordPrices_ERR:::::", err1.Error()) return } if cb != nil { defer cb() // 释放锁 } if has { // 3. 当前时间已经有记录 更新价格 if lastPrice.Date == dateNowStr && lastPrice.Hour == hourNowStr { lastPrice.Price = eggEnergyCoreData.NowPrice affected, err2 := priceDb.EggEnergyPriceUpdate(lastPrice) if err2 != nil { fmt.Println("SaveEggEnergyPrice_ERR:::::", err2.Error()) return } if affected == 0 { fmt.Println("SaveEggEnergyPrice_ERR:::::", errors.New(fmt.Sprintf("%s 新增价格失败, 当前价格 %s", now.Format("2006-01-02 15:04:05"), eggEnergyCoreData.NowPrice))) return } } } else { // 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 }