box.go 1.3 KB

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