sys_dashboard.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package system
  2. import (
  3. "time"
  4. "wails-app/internal/global"
  5. parkingService "wails-app/internal/service/parking"
  6. )
  7. type DeviceStatus struct {
  8. ChannelName string `json:"channel_name"`
  9. Direction string `json:"direction"`
  10. DeviceCode string `json:"device_code"`
  11. DeviceName string `json:"device_name"`
  12. DeviceStatus string `json:"device_status"`
  13. }
  14. type MonitorData struct {
  15. TotalSpaces int64 `json:"total_spaces"`
  16. UsedSpaces int64 `json:"used_spaces"`
  17. InPark int64 `json:"in_park"`
  18. TodayIncome float64 `json:"today_income"`
  19. TodayEntry int64 `json:"today_entry"`
  20. TodayExit int64 `json:"today_exit"`
  21. Devices []DeviceStatus `json:"devices"`
  22. }
  23. type DashboardService struct{}
  24. func (s *DashboardService) GetMonitorData() (MonitorData, error) {
  25. var d MonitorData
  26. today := time.Now().Format("2006-01-02")
  27. global.GVA_DB.Raw("SELECT COALESCE(SUM(capacity), 0) FROM parking_lot").Scan(&d.TotalSpaces)
  28. global.GVA_DB.Raw("SELECT COUNT(*) FROM vehicle_record WHERE exit_time IS NULL").Scan(&d.InPark)
  29. d.UsedSpaces = d.InPark
  30. global.GVA_DB.Raw("SELECT COALESCE(SUM(amount), 0) FROM payment_record WHERE paid_at >= ? AND paid_at < ?",
  31. today, today+" 23:59:59").Scan(&d.TodayIncome)
  32. global.GVA_DB.Raw("SELECT COUNT(*) FROM vehicle_record WHERE entry_time >= ? AND entry_time < ?",
  33. today, today+" 23:59:59").Scan(&d.TodayEntry)
  34. global.GVA_DB.Raw("SELECT COUNT(*) FROM vehicle_record WHERE exit_time >= ? AND exit_time < ?",
  35. today, today+" 23:59:59").Scan(&d.TodayExit)
  36. global.GVA_DB.Raw(`SELECT ch.channel_name, ch.direction,
  37. COALESCE(uhf.device_code, '') as device_code,
  38. COALESCE(uhf.device_name, '-') as device_name,
  39. 'unbound' as device_status
  40. FROM channel ch LEFT JOIN uhf_reader uhf ON uhf.channel_id = ch.id
  41. WHERE ch.deleted_at IS NULL ORDER BY ch.id`).Scan(&d.Devices)
  42. for i := range d.Devices {
  43. device := &d.Devices[i]
  44. if device.DeviceCode == "" {
  45. device.DeviceStatus = "unbound"
  46. continue
  47. }
  48. status := parkingService.GetGateRuntimeStatus(device.DeviceCode)
  49. switch {
  50. case status.Simulated && status.Connected:
  51. device.DeviceStatus = "simulated"
  52. case status.Connected:
  53. device.DeviceStatus = "online"
  54. default:
  55. device.DeviceStatus = "offline"
  56. }
  57. }
  58. return d, nil
  59. }