蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

82 lines
2.2 KiB

  1. package svc
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/utils"
  5. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  6. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  7. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  8. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/svc"
  9. "errors"
  10. "fmt"
  11. "time"
  12. "xorm.io/xorm"
  13. )
  14. // EggEnergyAutoRecordPrices 蛋蛋能量-自动记录价格
  15. func EggEnergyAutoRecordPrices(engine *xorm.Engine) {
  16. fmt.Println("egg_energy_auto_record_price...")
  17. defer func() {
  18. if err := recover(); err != nil {
  19. fmt.Println(err)
  20. return
  21. }
  22. }()
  23. now := time.Now()
  24. hourNowStr := utils.AnyToString(now.Hour())
  25. dateNowStr := now.Format("2006-01-02")
  26. // 1. 获取最后一条记录
  27. priceDb := implement.NewEggEnergyPriceDb(engine)
  28. lastPrice, has, err := priceDb.EggEnergyPriceGetLastOne()
  29. if err != nil {
  30. fmt.Println("EggEnergyAutoRecordPrices_ERR:::::", err.Error())
  31. return
  32. }
  33. // 2. 查询当前价格
  34. egg_system_rules.Init(cfg.RedisAddr)
  35. eggEnergyCoreData, cb, err1 := svc.GetEggEnergyCoreData(engine)
  36. if err1 != nil {
  37. fmt.Println("EggEnergyAutoRecordPrices_ERR:::::", err1.Error())
  38. return
  39. }
  40. if cb != nil {
  41. defer cb() // 释放锁
  42. }
  43. if has {
  44. // 3. 当前时间已经有记录 更新价格
  45. if lastPrice.Date == dateNowStr && lastPrice.Hour == hourNowStr {
  46. lastPrice.Price = eggEnergyCoreData.NowPrice
  47. affected, err2 := priceDb.EggEnergyPriceUpdate(lastPrice)
  48. if err2 != nil {
  49. fmt.Println("SaveEggEnergyPrice_ERR:::::", err2.Error())
  50. return
  51. }
  52. if affected == 0 {
  53. fmt.Println("SaveEggEnergyPrice_ERR:::::", errors.New(fmt.Sprintf("%s 新增价格失败, 当前价格 %s", now.Format("2006-01-02 15:04:05"), eggEnergyCoreData.NowPrice)))
  54. return
  55. }
  56. }
  57. } else {
  58. // 4. 保存当前价格
  59. priceData := model.EggEnergyPrice{
  60. Price: eggEnergyCoreData.NowPrice,
  61. Date: dateNowStr,
  62. Hour: hourNowStr,
  63. }
  64. id, err2 := priceDb.EggEnergyPriceInsert(&priceData)
  65. if err2 != nil {
  66. fmt.Println("SaveEggEnergyPrice_ERR:::::", err2.Error())
  67. return
  68. }
  69. if id == 0 {
  70. fmt.Println("SaveEggEnergyPrice_ERR:::::", errors.New(fmt.Sprintf("%s 新增价格失败, 当前价格 %s", now.Format("2006-01-02 15:04:05"), eggEnergyCoreData.NowPrice)))
  71. return
  72. }
  73. }
  74. return
  75. }