Second upload

This commit is contained in:
qi_0527 2024-10-03 18:09:51 +08:00
parent 6669a9bf3c
commit 3964510f8f

View File

@ -230,17 +230,15 @@ namespace WebApi_data_value.Controllers
records = parkingLogs records = parkingLogs
}); });
} }
//獲取前端要查詢的時間 利用interval == "none" 不需要分間格 interval != 分間隔
[HttpGet("GetHourlyParkingLogs")] [HttpGet("GetHourlyParkingLogs")]
public async Task<IActionResult> GetHourlyParkingLogs(DateTime startDate, DateTime endDate, string interval, int page = 1, int recordsPerPage = 20) public async Task<IActionResult> GetHourlyParkingLogs(DateTime startDate, DateTime endDate, string interval, int page = 1, int recordsPerPage = 20)
{ {
if (startDate == default || endDate == default) if (startDate == default || endDate == default)
{ {
return BadRequest(new { message = "日期無效。" }); return BadRequest(new { message = "日期無效。" });
} }
if (startDate >= endDate) if (startDate >= endDate)
{ {
return BadRequest(new { message = "結束日期必須晚於開始日期。" }); return BadRequest(new { message = "結束日期必須晚於開始日期。" });
@ -260,7 +258,6 @@ namespace WebApi_data_value.Controllers
// 設定時間間隔 // 設定時間間隔
if (interval == "none") if (interval == "none")
{ {
var formattedLogs = parkingLogs.Select(log => new var formattedLogs = parkingLogs.Select(log => new
{ {
log.Timestamp, log.Timestamp,
@ -271,7 +268,6 @@ namespace WebApi_data_value.Controllers
log.TemporaryRentSpaces log.TemporaryRentSpaces
}).ToList(); }).ToList();
var totalRecords = formattedLogs.Count; var totalRecords = formattedLogs.Count;
var pagedLogs = formattedLogs.Skip((page - 1) * recordsPerPage).Take(recordsPerPage).ToList(); var pagedLogs = formattedLogs.Skip((page - 1) * recordsPerPage).Take(recordsPerPage).ToList();
@ -285,27 +281,30 @@ namespace WebApi_data_value.Controllers
{ {
// 將 interval 轉換為整數 // 將 interval 轉換為整數
int hourInterval = int.Parse(interval); int hourInterval = int.Parse(interval);
// 轉換時間格式為可讀的格式,並獲取每小時的紀錄
var formattedLogs = new List<object>(); var formattedLogs = new List<object>();
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, var log = parkingLogs.FirstOrDefault(p => p.Timestamp.Year == currentTime.Year &&
log.Timestamp.Day, p.Timestamp.Month == currentTime.Month &&
HourGroup = log.Timestamp.Hour / hourInterval p.Timestamp.Day == currentTime.Day &&
})) p.Timestamp.Hour == currentTime.Hour);
if (log != null)
{ {
formattedLogs.Add(new formattedLogs.Add(new
{ {
Timestamp = new DateTime(logGroup.Key.Year, logGroup.Key.Month, logGroup.Key.Day, logGroup.Key.HourGroup * hourInterval, 0, 0), Timestamp = log.Timestamp,
DayOfWeek = logGroup.First().Timestamp.ToString("dddd", new CultureInfo("zh-TW")), DayOfWeek = log.Timestamp.ToString("dddd", new CultureInfo("zh-TW")),
TotalParkingSpaces = logGroup.First().TotalParkingSpaces, log.TotalParkingSpaces,
RemainingSpaces = logGroup.First().RemainingSpaces, log.RemainingSpaces,
MonthlyRentSpaces = logGroup.First().MonthlyRentSpaces, log.MonthlyRentSpaces,
TemporaryRentSpaces = logGroup.First().TemporaryRentSpaces log.TemporaryRentSpaces
}); });
} }
}
if (!formattedLogs.Any()) if (!formattedLogs.Any())
{ {
@ -323,7 +322,6 @@ namespace WebApi_data_value.Controllers
}); });
} }
} }
} }
} }