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 }