box.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package services
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "goiot-cronapi/models"
  5. )
  6. // GetBoxIdList 获取所有在线的盒子列表
  7. func GetBoxIdList(db *gorm.DB) []string {
  8. var boxList []models.BoxBase
  9. sql := "SELECT box_id FROM btk_box WHERE box_is_delete = 0 AND box_state = 1"
  10. db.Raw(sql).Find(&boxList)
  11. var boxIds = make([]string, 0)
  12. for _, box := range boxList {
  13. if len(box.BoxId) > 0 {
  14. boxIds = append(boxIds, box.BoxId)
  15. }
  16. }
  17. return boxIds
  18. }
  19. // GetAreaList 获取所有在线的盒子对应的地区
  20. func GetAreaList(db *gorm.DB) models.AreaList {
  21. var list models.AreaList
  22. sql := `SELECT p.province, p.city, p.area, p.district, p.city_id FROM btk_box b
  23. JOIN btk_project p ON p.project_auto_id = b.project_auto_id
  24. WHERE b.box_is_delete = 0 AND b.box_state = 1 AND p.city_id > 0
  25. GROUP BY p.city_id`
  26. db.Raw(sql).Find(&list)
  27. return list
  28. }
  29. // 获取每个盒子对应的运行总时长
  30. func GetBoxRunTime(db *gorm.DB) map[string]int64 {
  31. var (
  32. list []models.BoxTime
  33. timeMap = make(map[string]int64)
  34. )
  35. sql := "SELECT box_id, SUM(count) as time FROM btk_run_time GROUP BY box_id"
  36. db.Raw(sql).Find(&list)
  37. for _, value := range list {
  38. timeMap[value.BoxId] = value.Time
  39. }
  40. return timeMap
  41. }