123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package services
- import (
- "github.com/jinzhu/gorm"
- "goiot-cronapi/models"
- "goiot-cronapi/utils"
- )
- // GetBoxIdList 获取所有在线的盒子列表
- func GetBoxIdList(db *gorm.DB) []string {
- var boxList []models.BoxBase
- sql := "SELECT box_id FROM btk_box WHERE box_is_delete = 0 AND box_state = 1"
- db.Raw(sql).Find(&boxList)
- var boxIds = make([]string, 0)
- for _, box := range boxList {
- if len(box.BoxId) > 0 {
- boxIds = append(boxIds, box.BoxId)
- }
- }
- return boxIds
- }
- // GetAreaList 获取所有在线的盒子对应的地区
- func GetAreaList() models.AreaList {
- db, err := utils.OpenConnection()
- if err != nil {
- //common.ReturnSuccess(c, "db.err is:"+err.Error())
- //return
- }
- defer db.Close()
- var list models.AreaList
- sql := `SELECT p.province, p.city, p.area, p.district, p.city_id FROM btk_box b
- JOIN btk_project p ON p.project_auto_id = b.project_auto_id
- WHERE b.box_is_delete = 0 AND b.box_state = 1 AND p.city_id > 0
- GROUP BY p.city_id`
- db.Raw(sql).Find(&list)
- return list
- }
- // 获取每个盒子对应的运行总时长
- func GetBoxRunTime(db *gorm.DB) map[string]int64 {
- var (
- list []models.BoxTime
- timeMap = make(map[string]int64)
- )
- sql := "SELECT box_id, SUM(count) as time FROM btk_run_time GROUP BY box_id"
- db.Raw(sql).Find(&list)
- for _, value := range list {
- timeMap[value.BoxId] = value.Time
- }
- return timeMap
- }
|