jobs.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package jobs
  2. import (
  3. sysModel "cold-delivery/app/admin/model"
  4. "cold-delivery/common/global"
  5. "cold-delivery/common/nats/nats_server"
  6. "cold-delivery/db"
  7. "fmt"
  8. "github.com/gin-gonic/gin"
  9. log "gogs.baozhida.cn/zoie/OAuth-core/logger"
  10. "sort"
  11. "time"
  12. )
  13. // 需要将定义的struct 添加到字典中;
  14. // 字典 key 可以配置到 自动任务 调用目标 中;
  15. func InitJob() {
  16. jobList = map[string]JobsExec{
  17. //"ExamplesOne": ExamplesOne{},
  18. "CountIceRaftRecordFreezeDuration": CountIceRaftRecordFreezeDuration{},
  19. "UpdateCoolerBoxMonitorStatus": UpdateCoolerBoxMonitorStatus{},
  20. // ...
  21. }
  22. }
  23. // 新添加的job 必须按照以下格式定义,并实现Exec函数
  24. type ExamplesOne struct {
  25. }
  26. func (t ExamplesOne) Exec(arg interface{}) error {
  27. str := time.Now().Format(timeFormat) + " [INFO] JobCore ExamplesOne exec success"
  28. // TODO: 这里需要注意 Examples 传入参数是 string 所以 arg.(string);请根据对应的类型进行转化;
  29. switch arg.(type) {
  30. case string:
  31. if arg.(string) != "" {
  32. fmt.Println("string", arg.(string))
  33. fmt.Println(str, arg.(string))
  34. } else {
  35. fmt.Println("arg is nil")
  36. fmt.Println(str, "arg is nil")
  37. }
  38. break
  39. }
  40. return nil
  41. }
  42. // 统计冰排冷冻时长
  43. type CountIceRaftRecordFreezeDuration struct {
  44. }
  45. // 更新保温箱监控状态
  46. type UpdateCoolerBoxMonitorStatus struct {
  47. }
  48. // 统计冰排冷冻时长
  49. func (t CountIceRaftRecordFreezeDuration) Exec(arg interface{}) error {
  50. iceRaftRecordList := make([]sysModel.IceRaftRecord, 0)
  51. // GetOrm 获取orm连接
  52. orm, _ := db.GetOrm(&gin.Context{})
  53. err := orm.Where("status = ? or status = ? ", sysModel.IceRaftRecordStatusFreezing, sysModel.IceRaftRecordStatusWaitUse).
  54. Find(&iceRaftRecordList).Error
  55. if err != nil {
  56. log.Errorf("db error: %s", err)
  57. return global.GetFailedErr
  58. }
  59. //冰排统计时长
  60. for _, record := range iceRaftRecordList {
  61. sort.Slice(record.FreezeClaim, func(i, j int) bool {
  62. return record.FreezeClaim[i] < record.FreezeClaim[j]
  63. })
  64. inTime := record.InStorageTime.Local()
  65. //if inTime.Add(time.Hour * time.Duration(record.FreezeClaim)).After(time.Now()) {
  66. if inTime.Add(time.Duration(time.Hour * time.Duration(record.FreezeClaim[0]))).After(time.Now()) {
  67. // 未达到冷冻时长 不修改状态 只修改时间
  68. record.FreezeDuration = int(time.Now().Sub(inTime).Minutes())
  69. } else {
  70. record.FreezeDuration = int(time.Now().Sub(inTime).Minutes())
  71. record.Status = sysModel.IceRaftRecordStatusWaitUse
  72. }
  73. if err := orm.Save(&record).Error; err != nil {
  74. log.Errorf("db error: %s", err)
  75. return global.UpdateFailedErr
  76. }
  77. }
  78. return nil
  79. }
  80. // Exec 更新保温箱监控状态
  81. func (t UpdateCoolerBoxMonitorStatus) Exec(arg interface{}) error {
  82. // GetOrm 获取orm连接
  83. orm, _ := db.GetOrm(&gin.Context{})
  84. var coolerBox []sysModel.CoolerBox
  85. err := orm.Model(&sysModel.CoolerBox{}).Find(&coolerBox).Error
  86. if err != nil {
  87. log.Errorf("获取保温箱状态失败: %s", err)
  88. return global.GetFailedErr
  89. }
  90. for i, _ := range coolerBox {
  91. data, _ := nats_server.Read_Device_ByT_sn(coolerBox[i].Sn)
  92. if len(data.T_sn) != 0 {
  93. //只查询设备在线并且处于监控状态下的设备
  94. if data.T_monitor != coolerBox[i].MonitorStatus {
  95. err := orm.Model(&sysModel.CoolerBox{}).Where("id = ?", coolerBox[i].Id).Update("monitor_status", data.T_monitor).Error
  96. if err != nil {
  97. log.Errorf("更新保温箱监控状态失败: %s", err)
  98. return global.UpdateFailedErr
  99. }
  100. }
  101. }
  102. }
  103. return nil
  104. }