Browse Source

feat: 监控仪表盘API——GET /dashboard/monitor(PublicGroup)

lq 1 month ago
parent
commit
e203118a53

+ 18 - 0
internal/api/v1/system/sys_dashboard.go

@@ -0,0 +1,18 @@
+package system
+
+import (
+	"github.com/gin-gonic/gin"
+	"wails-app/internal/model/common/response"
+	systemService "wails-app/internal/service/system"
+)
+
+var dashboardSvc = systemService.DashboardService{}
+
+func GetMonitor(c *gin.Context) {
+	data, err := dashboardSvc.GetMonitorData()
+	if err != nil {
+		response.FailWithMessage(err.Error(), c)
+		return
+	}
+	response.OkWithData(data, c)
+}

+ 3 - 1
internal/initialize/router.go

@@ -48,6 +48,7 @@ func Routers() *gin.Engine {
 	systemRouter := router.RouterGroupApp.System
 	exampleRouter := router.RouterGroupApp.Example
 	vehicleRouter := router.RouterGroupApp.Vehicle
+	dashboardRouter := router.RouterGroupApp.Dashboard
 
 	// 如果想要不使用nginx代理前端网页,可以修改 web/.env.production 下的
 	// VUE_APP_BASE_API = /
@@ -75,7 +76,8 @@ func Routers() *gin.Engine {
 		})
 	}
 	{
-		systemRouter.InitBaseRouter(PublicGroup) // 注册基础功能路由 不做鉴权
+		systemRouter.InitBaseRouter(PublicGroup)    // 注册基础功能路由 不做鉴权
+		dashboardRouter.InitDashboardRouter(PublicGroup) // 监控仪表盘 不做鉴权
 	}
 	PrivateGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
 	PrivateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())

+ 4 - 3
internal/router/enter.go

@@ -7,9 +7,10 @@ import (
 )
 
 type RouterGroup struct {
-	System  system.RouterGroup
-	Example example.RouterGroup
-	Vehicle vehicle.RouterGroup
+	System    system.RouterGroup
+	Example   example.RouterGroup
+	Vehicle   vehicle.RouterGroup
+	Dashboard system.DashboardRouter
 }
 
 var RouterGroupApp = new(RouterGroup)

+ 13 - 0
internal/router/system/sys_dashboard.go

@@ -0,0 +1,13 @@
+package system
+
+import (
+	"wails-app/internal/api/v1/system"
+	"github.com/gin-gonic/gin"
+)
+
+type DashboardRouter struct{}
+
+func (s *DashboardRouter) InitDashboardRouter(Router *gin.RouterGroup) {
+	dashboardRouter := Router.Group("dashboard")
+	dashboardRouter.GET("monitor", system.GetMonitor)
+}

+ 50 - 0
internal/service/system/sys_dashboard.go

@@ -0,0 +1,50 @@
+package system
+
+import (
+	"time"
+	"wails-app/internal/global"
+)
+
+type DeviceStatus struct {
+	ChannelName  string `json:"channel_name"`
+	Direction    string `json:"direction"`
+	DeviceName   string `json:"device_name"`
+	DeviceStatus string `json:"device_status"`
+}
+
+type MonitorData struct {
+	TotalSpaces int64          `json:"total_spaces"`
+	UsedSpaces  int64          `json:"used_spaces"`
+	InPark      int64          `json:"in_park"`
+	TodayIncome float64        `json:"today_income"`
+	TodayEntry  int64          `json:"today_entry"`
+	TodayExit   int64          `json:"today_exit"`
+	Devices     []DeviceStatus `json:"devices"`
+}
+
+type DashboardService struct{}
+
+func (s *DashboardService) GetMonitorData() (MonitorData, error) {
+	var d MonitorData
+	today := time.Now().Format("2006-01-02")
+
+	global.GVA_DB.Raw("SELECT COALESCE(SUM(capacity), 0) FROM parking_lot").Scan(&d.TotalSpaces)
+	global.GVA_DB.Raw("SELECT COUNT(*) FROM vehicle_record WHERE exit_time IS NULL").Scan(&d.InPark)
+	d.UsedSpaces = d.InPark
+
+	global.GVA_DB.Raw("SELECT COALESCE(SUM(amount), 0) FROM payment_record WHERE paid_at >= ? AND paid_at < ?",
+		today, today+" 23:59:59").Scan(&d.TodayIncome)
+
+	global.GVA_DB.Raw("SELECT COUNT(*) FROM vehicle_record WHERE entry_time >= ? AND entry_time < ?",
+		today, today+" 23:59:59").Scan(&d.TodayEntry)
+	global.GVA_DB.Raw("SELECT COUNT(*) FROM vehicle_record WHERE exit_time >= ? AND exit_time < ?",
+		today, today+" 23:59:59").Scan(&d.TodayExit)
+
+	global.GVA_DB.Raw(`SELECT ch.channel_name, ch.direction,
+		COALESCE(uhf.device_name, '-') as device_name,
+		COALESCE(uhf.status, 'offline') as device_status
+		FROM channel ch LEFT JOIN uhf_reader uhf ON uhf.channel_id = ch.id
+		WHERE ch.deleted_at IS NULL ORDER BY ch.id`).Scan(&d.Devices)
+
+	return d, nil
+}