sys_dashboard.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package system
  2. import (
  3. "time"
  4. "wails-app/internal/global"
  5. )
  6. type DeviceStatus struct {
  7. ChannelName string `json:"channel_name"`
  8. Direction string `json:"direction"`
  9. DeviceName string `json:"device_name"`
  10. DeviceStatus string `json:"device_status"`
  11. }
  12. type MonitorData struct {
  13. TotalSpaces int64 `json:"total_spaces"`
  14. UsedSpaces int64 `json:"used_spaces"`
  15. InPark int64 `json:"in_park"`
  16. TodayIncome float64 `json:"today_income"`
  17. TodayEntry int64 `json:"today_entry"`
  18. TodayExit int64 `json:"today_exit"`
  19. Devices []DeviceStatus `json:"devices"`
  20. }
  21. type DashboardService struct{}
  22. func (s *DashboardService) GetMonitorData() (MonitorData, error) {
  23. var d MonitorData
  24. today := time.Now().Format("2006-01-02")
  25. global.GVA_DB.Raw("SELECT COALESCE(SUM(capacity), 0) FROM parking_lot").Scan(&d.TotalSpaces)
  26. global.GVA_DB.Raw("SELECT COUNT(*) FROM vehicle_record WHERE exit_time IS NULL").Scan(&d.InPark)
  27. d.UsedSpaces = d.InPark
  28. global.GVA_DB.Raw("SELECT COALESCE(SUM(amount), 0) FROM payment_record WHERE paid_at >= ? AND paid_at < ?",
  29. today, today+" 23:59:59").Scan(&d.TodayIncome)
  30. global.GVA_DB.Raw("SELECT COUNT(*) FROM vehicle_record WHERE entry_time >= ? AND entry_time < ?",
  31. today, today+" 23:59:59").Scan(&d.TodayEntry)
  32. global.GVA_DB.Raw("SELECT COUNT(*) FROM vehicle_record WHERE exit_time >= ? AND exit_time < ?",
  33. today, today+" 23:59:59").Scan(&d.TodayExit)
  34. global.GVA_DB.Raw(`SELECT ch.channel_name, ch.direction,
  35. COALESCE(uhf.device_name, '-') as device_name,
  36. COALESCE(uhf.status, 'offline') as device_status
  37. FROM channel ch LEFT JOIN uhf_reader uhf ON uhf.channel_id = ch.id
  38. WHERE ch.deleted_at IS NULL ORDER BY ch.id`).Scan(&d.Devices)
  39. return d, nil
  40. }