qixiaoning
2025-07-08 84d2ef9760af0a4a4aa933937294400b3caa291d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package manage
 
import (
    "time"
    "vamicro/iotData-service/model"
    "vamicro/iotData-service/serializer"
)
 
type BasicInfoService struct {
}
 
type returnInfo struct {
    deviceAmount int64
    newDevice    int64
    todayAdded   int64
}
 
func (service *BasicInfoService) Show() serializer.Response {
    info := returnInfo{}
 
    code := 200
 
    db := model.DB.Table("devices")
 
    if err := db.Count(&info.deviceAmount).Error; err != nil {
        code = 30001
        return serializer.Response{
            Status: code,
            Msg:    "数据库错误 count",
            Error:  err.Error(),
        }
    }
    if err := db.Where("created_at BETWEEN ? AND ?", GetMonthFirst(), GetNowStr()).Count(&info.newDevice).Error; err != nil {
        code = 30001
        return serializer.Response{
            Status: code,
            Msg:    "数据库错误 count",
            Error:  err.Error(),
        }
    }
 
    //
    if err := model.DB.Model(&model.Warning{}).Where("created_at BETWEEN ? AND ?", GetCurDayStart(), GetNowStr()).Count(&info.todayAdded).Error; err != nil {
        code = 30001
        return serializer.Response{
            Status: code,
            Msg:    "数据库错误 count",
            Error:  err.Error(),
        }
    }
 
    return serializer.Response{
        Status: code,
        Data: map[string]interface{}{
            "deviceAmount": info.deviceAmount,
            "newDevice":    info.newDevice,
            "todayAdded":   info.todayAdded,
        },
    }
 
}
 
func GetMonthFirst() string {
    now := time.Now()
    y, m, _ := now.Date()
    loc := now.Location()
    firstOfMonth := time.Date(y, m, 1, 0, 0, 0, 0, loc)
    firstOfMonthStr := firstOfMonth.Format("2006-01-02 15:04:05")
    return firstOfMonthStr
}
 
func GetCurDayStart() string {
    now := time.Now()
    dayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
    str := dayStart.Format("2006-01-02 15:04:05")
    return str
}
 
func GetNowStr() string {
    str := time.Now().Format("2006-01-02 15:04:05")
    return str
}
 
func GetDayStart(day time.Time) string {
    dayStart := time.Date(day.Year(), day.Month(), day.Day(), 0, 0, 0, 0, day.Location())
    str := dayStart.Format("2006-01-02 15:04:05")
    return str
}