mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-30 04:41:25 +08:00
【feat】首页面板数据
This commit is contained in:
@@ -24,10 +24,25 @@ import (
|
||||
|
||||
type DeviceApi struct {
|
||||
DeviceApp services.DeviceModel
|
||||
DeviceAlarmApp services.DeviceAlarmModel
|
||||
ProductApp services.ProductModel
|
||||
ProductTemplateApp services.ProductTemplateModel
|
||||
}
|
||||
|
||||
func (p *DeviceApi) GetDevicePanel(rc *restfulx.ReqCtx) {
|
||||
get, err := global.Cache.ComputeIfAbsent("panel", func(k any) (any, error) {
|
||||
var data entity.DeviceTotalOutput
|
||||
data.DeviceInfo = p.DeviceApp.FindDeviceCount()
|
||||
data.DeviceLinkStatusInfo = p.DeviceApp.FindDeviceCountGroupByLinkStatus()
|
||||
data.DeviceCountType = p.DeviceApp.FindDeviceCountGroupByType()
|
||||
data.AlarmInfo = p.DeviceAlarmApp.FindAlarmCount()
|
||||
data.ProductInfo = p.ProductApp.FindProductCount()
|
||||
return data, nil
|
||||
})
|
||||
biz.ErrIsNil(err, "获取面板数据失败")
|
||||
rc.ResData = get
|
||||
}
|
||||
|
||||
// GetDeviceList Device列表数据
|
||||
func (p *DeviceApi) GetDeviceList(rc *restfulx.ReqCtx) {
|
||||
data := entity.Device{}
|
||||
|
||||
@@ -50,3 +50,18 @@ func (DeviceCmdLog) TableName() string {
|
||||
type DeviceStatistics struct {
|
||||
Time time.Time `gorm:"comment:时间" json:"time"`
|
||||
}
|
||||
|
||||
type DeviceCount struct {
|
||||
Total int64 `json:"total"`
|
||||
Today int64 `json:"todayAdd"`
|
||||
}
|
||||
|
||||
type DeviceCountLinkStatus struct {
|
||||
Total int64 `json:"deviceTotal"`
|
||||
LinkStatus string `json:"linkStatus"`
|
||||
}
|
||||
|
||||
type DeviceCountType struct {
|
||||
Total int64 `json:"deviceTotal"`
|
||||
DeviceType string `json:"deviceType"`
|
||||
}
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
package entity
|
||||
|
||||
type DeviceTotalOutput struct {
|
||||
DeviceInfo DeviceCount `json:"deviceInfo"`
|
||||
DeviceLinkStatusInfo []DeviceCountLinkStatus `json:"deviceLinkStatusInfo"`
|
||||
DeviceCountType []DeviceCountType `json:"deviceCountType"`
|
||||
ProductInfo DeviceCount `json:"productInfo"`
|
||||
AlarmInfo DeviceCount `json:"alarmInfo"`
|
||||
}
|
||||
|
||||
type DeviceStatusVo struct {
|
||||
Name string `json:"name"`
|
||||
Key string `json:"key"`
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
func InitDeviceRouter(container *restful.Container) {
|
||||
s := &api.DeviceApi{
|
||||
DeviceApp: services.DeviceModelDao,
|
||||
DeviceAlarmApp: services.DeviceAlarmModelDao,
|
||||
ProductApp: services.ProductModelDao,
|
||||
ProductTemplateApp: services.ProductTemplateModelDao,
|
||||
}
|
||||
@@ -22,6 +23,14 @@ func InitDeviceRouter(container *restful.Container) {
|
||||
ws.Path("/device").Produces(restful.MIME_JSON)
|
||||
tags := []string{"device"}
|
||||
|
||||
ws.Route(ws.GET("/panel").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithLog("获取DevicePanel").Handle(s.GetDevicePanel)
|
||||
}).
|
||||
Doc("获取DevicePanel").
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||
Writes(entity.DeviceTotalOutput{}).
|
||||
Returns(200, "OK", entity.DeviceTotalOutput{}))
|
||||
|
||||
ws.Route(ws.GET("/list").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithLog("获取Device分页列表").Handle(s.GetDeviceList)
|
||||
}).
|
||||
|
||||
@@ -19,6 +19,9 @@ type (
|
||||
Update(data entity.Device) *entity.Device
|
||||
UpdateStatus(id, linkStatus string)
|
||||
Delete(ids []string)
|
||||
FindDeviceCount() entity.DeviceCount
|
||||
FindDeviceCountGroupByLinkStatus() []entity.DeviceCountLinkStatus
|
||||
FindDeviceCountGroupByType() []entity.DeviceCountType
|
||||
}
|
||||
|
||||
deviceModelImpl struct {
|
||||
@@ -209,3 +212,26 @@ func GetDeviceToken(data *entity.Device) (*tool.DeviceAuth, error) {
|
||||
err := etoken.CreateDeviceToken(data.Id)
|
||||
return etoken, err
|
||||
}
|
||||
|
||||
// 获取设备数量统计
|
||||
func (m *deviceModelImpl) FindDeviceCount() (result entity.DeviceCount) {
|
||||
sql := `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM devices WHERE DATE(create_time) = CURDATE()) AS today FROM devices`
|
||||
err := global.Db.Raw(sql).Scan(&result).Error
|
||||
biz.ErrIsNil(err, "获取设备统计总数失败")
|
||||
return result
|
||||
}
|
||||
|
||||
// 获取设备类型数量统计
|
||||
func (m *deviceModelImpl) FindDeviceCountGroupByLinkStatus() (count []entity.DeviceCountLinkStatus) {
|
||||
sql := `SELECT link_status, COUNT(*) AS total FROM devices GROUP BY link_status`
|
||||
err := global.Db.Raw(sql, m.table).Scan(&count).Error
|
||||
biz.ErrIsNil(err, "获取通过设备在线状态的设备统计总数失败")
|
||||
return
|
||||
}
|
||||
|
||||
func (m *deviceModelImpl) FindDeviceCountGroupByType() (count []entity.DeviceCountType) {
|
||||
sql := `SELECT device_type, COUNT(*) AS total FROM devices GROUP BY device_type`
|
||||
err := global.Db.Raw(sql, m.table).Scan(&count).Error
|
||||
biz.ErrIsNil(err, "获取通过设备类型的设备统计总数失败")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ type (
|
||||
FindListPage(page, pageSize int, data entity.DeviceAlarmForm) (*[]entity.DeviceAlarm, int64)
|
||||
Update(data entity.DeviceAlarm) error
|
||||
Delete(ids []string)
|
||||
FindAlarmCount() entity.DeviceCount
|
||||
}
|
||||
|
||||
alarmModelImpl struct {
|
||||
@@ -93,3 +94,11 @@ func (m *alarmModelImpl) Update(data entity.DeviceAlarm) error {
|
||||
func (m *alarmModelImpl) Delete(id []string) {
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.DeviceAlarm{}, "id in (?)", id).Error, "删除设备告警失败")
|
||||
}
|
||||
|
||||
// 获取告警数量统计
|
||||
func (m *alarmModelImpl) FindAlarmCount() (count entity.DeviceCount) {
|
||||
sql := `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM device_alarms WHERE DATE(time) = CURDATE()) AS today FROM device_alarms`
|
||||
err := global.Db.Raw(sql).Scan(&count).Error
|
||||
biz.ErrIsNil(err, "获取告警统计总数失败")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ type (
|
||||
FindList(data entity.Product) *[]entity.ProductRes
|
||||
Update(data entity.Product) *entity.Product
|
||||
Delete(ids []string)
|
||||
FindProductCount() entity.DeviceCount
|
||||
}
|
||||
|
||||
productModelImpl struct {
|
||||
@@ -148,3 +149,11 @@ func deleteDeviceStable(productId string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取产品数量统计
|
||||
func (m *productModelImpl) FindProductCount() (count entity.DeviceCount) {
|
||||
sql := `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM products WHERE DATE(create_time) = CURDATE()) AS today FROM products`
|
||||
err := global.Db.Raw(sql).Scan(&count).Error
|
||||
biz.ErrIsNil(err, "获取产品统计总数失败")
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user