sys_dashboard.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. var spaces struct {
  28. TotalSpaces int64
  29. UsedSpaces int64
  30. }
  31. if err := global.GVA_DB.Raw(`SELECT
  32. COALESCE(SUM(capacity), 0) AS total_spaces,
  33. COALESCE(SUM(occupied), 0) AS used_spaces
  34. FROM parking_lot WHERE deleted_at IS NULL`).Scan(&spaces).Error; err != nil {
  35. return d, err
  36. }
  37. d.TotalSpaces = spaces.TotalSpaces
  38. d.UsedSpaces = spaces.UsedSpaces
  39. d.InPark = d.UsedSpaces
  40. if err := global.GVA_DB.Raw(`SELECT COALESCE(SUM(amount), 0) FROM payment_record
  41. WHERE deleted_at IS NULL AND paid_at >= ? AND paid_at < ?`,
  42. today, today+" 23:59:59").Scan(&d.TodayIncome).Error; err != nil {
  43. return d, err
  44. }
  45. if err := global.GVA_DB.Raw(`SELECT COUNT(*) FROM vehicle_record
  46. WHERE deleted_at IS NULL AND entry_time >= ? AND entry_time < ?`,
  47. today, today+" 23:59:59").Scan(&d.TodayEntry).Error; err != nil {
  48. return d, err
  49. }
  50. if err := global.GVA_DB.Raw(`SELECT COUNT(*) FROM vehicle_record
  51. WHERE deleted_at IS NULL AND exit_time >= ? AND exit_time < ?`,
  52. today, today+" 23:59:59").Scan(&d.TodayExit).Error; err != nil {
  53. return d, err
  54. }
  55. if err := global.GVA_DB.Raw(`SELECT ch.channel_name, ch.direction,
  56. COALESCE(uhf.device_code, '') as device_code,
  57. COALESCE(uhf.device_name, '-') as device_name,
  58. 'unbound' as device_status
  59. FROM channel ch LEFT JOIN uhf_reader uhf ON uhf.channel_id = ch.id AND uhf.deleted_at IS NULL
  60. WHERE ch.deleted_at IS NULL ORDER BY ch.id`).Scan(&d.Devices).Error; err != nil {
  61. return d, err
  62. }
  63. for i := range d.Devices {
  64. device := &d.Devices[i]
  65. if device.DeviceCode == "" {
  66. device.DeviceStatus = "unbound"
  67. continue
  68. }
  69. status := parkingService.GetGateRuntimeStatus(device.DeviceCode)
  70. switch {
  71. case status.Simulated && status.Connected:
  72. device.DeviceStatus = "simulated"
  73. case status.Connected:
  74. device.DeviceStatus = "online"
  75. default:
  76. device.DeviceStatus = "offline"
  77. }
  78. }
  79. return d, nil
  80. }