diff --git a/WebApi_data_value/Controllers/ParkingLogsController.cs b/WebApi_data_value/Controllers/ParkingLogsController.cs index 0c861bd..8f8fff4 100644 --- a/WebApi_data_value/Controllers/ParkingLogsController.cs +++ b/WebApi_data_value/Controllers/ParkingLogsController.cs @@ -33,13 +33,13 @@ namespace WebApi_data_value.Controllers return BadRequest(new { message = "請求的模型無效。" }); } - + _context.ParkingLogs.Add(parkingLog); // 儲存變更到資料庫 await _context.SaveChangesAsync(); - + return CreatedAtAction(nameof(GetParkingLogById), new { id = parkingLog.Id }, parkingLog); } @@ -60,10 +60,10 @@ namespace WebApi_data_value.Controllers [HttpGet("DownloadCurrentStatusExcel")] public async Task DownloadCurrentStatusExcel() { - + DateTime currentTime = DateTime.Now; - + var parkingLog = await _context.ParkingLogs .OrderByDescending(p => p.Timestamp) .FirstOrDefaultAsync(p => p.Timestamp <= currentTime); @@ -77,30 +77,30 @@ namespace WebApi_data_value.Controllers { var worksheet = workbook.Worksheets.Add("即時車位資訊"); worksheet.Cell(1, 1).Value = "時間"; - worksheet.Cell(1, 2).Value = "星期"; + worksheet.Cell(1, 2).Value = "星期"; worksheet.Cell(1, 3).Value = "總車位"; worksheet.Cell(1, 4).Value = "剩餘車位"; worksheet.Cell(1, 5).Value = "月租車位"; worksheet.Cell(1, 6).Value = "臨停車位"; - worksheet.Cell(1, 7).Value = "下載日期"; + worksheet.Cell(1, 7).Value = "下載日期"; - - worksheet.Cell(2, 1).Value = parkingLog.Timestamp.ToString("yyyy/MM/dd HH:mm:ss"); - worksheet.Cell(2, 2).Value = DateTime.Now.ToString("dddd", new CultureInfo("zh-TW")); - worksheet.Cell(2, 3).Value = parkingLog.TotalParkingSpaces; + + worksheet.Cell(2, 1).Value = parkingLog.Timestamp.ToString("yyyy/MM/dd HH:mm:ss"); + worksheet.Cell(2, 2).Value = DateTime.Now.ToString("dddd", new CultureInfo("zh-TW")); + worksheet.Cell(2, 3).Value = parkingLog.TotalParkingSpaces; worksheet.Cell(2, 4).Value = parkingLog.RemainingSpaces; - worksheet.Cell(2, 5).Value = parkingLog.MonthlyRentSpaces; + worksheet.Cell(2, 5).Value = parkingLog.MonthlyRentSpaces; worksheet.Cell(2, 6).Value = parkingLog.TemporaryRentSpaces; worksheet.Cell(2, 7).Value = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss dddd"); // 設定欄位寬度 - worksheet.Column(1).Width = 25; - worksheet.Column(2).Width = 10; - worksheet.Column(3).Width = 15; - worksheet.Column(4).Width = 15; - worksheet.Column(5).Width = 15; - worksheet.Column(6).Width = 15; - worksheet.Column(7).Width = 25; + worksheet.Column(1).Width = 25; + worksheet.Column(2).Width = 10; + worksheet.Column(3).Width = 15; + worksheet.Column(4).Width = 15; + worksheet.Column(5).Width = 15; + worksheet.Column(6).Width = 15; + worksheet.Column(7).Width = 25; using (var stream = new MemoryStream()) { @@ -159,7 +159,7 @@ namespace WebApi_data_value.Controllers for (int i = 0; i < records.Count; i++) { var log = records[i]; - worksheet.Cell(i + 2, 1).Value = log.Timestamp.ToString("yyyy/MM/dd HH:mm:ss"); + worksheet.Cell(i + 2, 1).Value = log.Timestamp.ToString("yyyy/MM/dd HH:mm:ss"); worksheet.Cell(i + 2, 2).Value = log.DayOfWeek; worksheet.Cell(i + 2, 3).Value = log.TotalParkingSpaces; worksheet.Cell(i + 2, 4).Value = log.RemainingSpaces; @@ -170,10 +170,10 @@ namespace WebApi_data_value.Controllers } - worksheet.Column(1).Width = 25; - worksheet.Column(2).Width = 10; - worksheet.Column(3).Width = 15; - worksheet.Column(4).Width = 15; + worksheet.Column(1).Width = 25; + worksheet.Column(2).Width = 10; + worksheet.Column(3).Width = 15; + worksheet.Column(4).Width = 15; worksheet.Column(5).Width = 15; worksheet.Column(6).Width = 15; worksheet.Column(7).Width = 25; @@ -183,9 +183,9 @@ namespace WebApi_data_value.Controllers workbook.SaveAs(stream); var content = stream.ToArray(); - + string currentDate = DateTime.Now.ToString("yyyy-MM-dd"); - string weekDay = DateTime.Now.ToString("dddd", new CultureInfo("zh-TW")); + string weekDay = DateTime.Now.ToString("dddd", new CultureInfo("zh-TW")); string fileName = $"停車紀錄-{currentDate}-{weekDay}.xlsx"; @@ -198,25 +198,25 @@ namespace WebApi_data_value.Controllers // 獲取停車紀錄 private async Task GetParkingLogs(DateTime startDate, DateTime endDate, int pageNumber, int pageSize) { - + if (startDate == default || endDate == default) { return BadRequest(new { message = "日期無效。" }); } - + if (startDate >= endDate) { return BadRequest(new { message = "結束日期必須晚於開始日期。" }); } - + var parkingLogs = await _context.ParkingLogs .AsNoTracking() .Where(p => p.Timestamp >= startDate && p.Timestamp <= endDate) - .OrderByDescending(p => p.Timestamp) - .Skip((pageNumber - 1) * pageSize) - .Take(pageSize) + .OrderByDescending(p => p.Timestamp) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) .ToListAsync(); if (!parkingLogs.Any()) @@ -230,17 +230,15 @@ namespace WebApi_data_value.Controllers records = parkingLogs }); } - //獲取前端要查詢的時間 利用interval == "none" 不需要分間格 interval != 分間隔 + [HttpGet("GetHourlyParkingLogs")] public async Task GetHourlyParkingLogs(DateTime startDate, DateTime endDate, string interval, int page = 1, int recordsPerPage = 20) { - if (startDate == default || endDate == default) { return BadRequest(new { message = "日期無效。" }); } - if (startDate >= endDate) { return BadRequest(new { message = "結束日期必須晚於開始日期。" }); @@ -260,7 +258,6 @@ namespace WebApi_data_value.Controllers // 設定時間間隔 if (interval == "none") { - var formattedLogs = parkingLogs.Select(log => new { log.Timestamp, @@ -271,9 +268,8 @@ namespace WebApi_data_value.Controllers log.TemporaryRentSpaces }).ToList(); - - var totalRecords = formattedLogs.Count; - var pagedLogs = formattedLogs.Skip((page - 1) * recordsPerPage).Take(recordsPerPage).ToList(); + var totalRecords = formattedLogs.Count; + var pagedLogs = formattedLogs.Skip((page - 1) * recordsPerPage).Take(recordsPerPage).ToList(); return Ok(new { @@ -285,26 +281,29 @@ namespace WebApi_data_value.Controllers { // 將 interval 轉換為整數 int hourInterval = int.Parse(interval); - - // 轉換時間格式為可讀的格式,並獲取每小時的紀錄 var formattedLogs = new List(); - foreach (var logGroup in parkingLogs.GroupBy(log => new + + // 從 startDate 開始,每次加 hourInterval 小時,直到 endDate + for (DateTime currentTime = startDate; currentTime <= endDate; currentTime = currentTime.AddHours(hourInterval)) { - log.Timestamp.Year, - log.Timestamp.Month, - log.Timestamp.Day, - HourGroup = log.Timestamp.Hour / hourInterval - })) - { - formattedLogs.Add(new + // 查找該時間的停車紀錄 + var log = parkingLogs.FirstOrDefault(p => p.Timestamp.Year == currentTime.Year && + p.Timestamp.Month == currentTime.Month && + p.Timestamp.Day == currentTime.Day && + p.Timestamp.Hour == currentTime.Hour); + + if (log != null) { - Timestamp = new DateTime(logGroup.Key.Year, logGroup.Key.Month, logGroup.Key.Day, logGroup.Key.HourGroup * hourInterval, 0, 0), - DayOfWeek = logGroup.First().Timestamp.ToString("dddd", new CultureInfo("zh-TW")), - TotalParkingSpaces = logGroup.First().TotalParkingSpaces, - RemainingSpaces = logGroup.First().RemainingSpaces, - MonthlyRentSpaces = logGroup.First().MonthlyRentSpaces, - TemporaryRentSpaces = logGroup.First().TemporaryRentSpaces - }); + formattedLogs.Add(new + { + Timestamp = log.Timestamp, + DayOfWeek = log.Timestamp.ToString("dddd", new CultureInfo("zh-TW")), + log.TotalParkingSpaces, + log.RemainingSpaces, + log.MonthlyRentSpaces, + log.TemporaryRentSpaces + }); + } } if (!formattedLogs.Any()) @@ -313,8 +312,8 @@ namespace WebApi_data_value.Controllers } // 分頁處理 - var totalRecords = formattedLogs.Count; - var pagedLogs = formattedLogs.Skip((page - 1) * recordsPerPage).Take(recordsPerPage).ToList(); + var totalRecords = formattedLogs.Count; + var pagedLogs = formattedLogs.Skip((page - 1) * recordsPerPage).Take(recordsPerPage).ToList(); return Ok(new { @@ -323,7 +322,6 @@ namespace WebApi_data_value.Controllers }); } } - } }