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
});
}
//獲取前端要查詢的時間 利用interval == "none" 不需要分間格 interval != 分間隔
[HttpGet("GetHourlyParkingLogs")]
public async Task<IActionResult> 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,7 +268,6 @@ namespace WebApi_data_value.Controllers
log.TemporaryRentSpaces
}).ToList();
var totalRecords = formattedLogs.Count;
var pagedLogs = formattedLogs.Skip((page - 1) * recordsPerPage).Take(recordsPerPage).ToList();
@ -285,27 +281,30 @@ namespace WebApi_data_value.Controllers
{
// 將 interval 轉換為整數
int hourInterval = int.Parse(interval);
// 轉換時間格式為可讀的格式,並獲取每小時的紀錄
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,
log.Timestamp.Day,
HourGroup = log.Timestamp.Hour / hourInterval
}))
// 查找該時間的停車紀錄
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)
{
formattedLogs.Add(new
{
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
Timestamp = log.Timestamp,
DayOfWeek = log.Timestamp.ToString("dddd", new CultureInfo("zh-TW")),
log.TotalParkingSpaces,
log.RemainingSpaces,
log.MonthlyRentSpaces,
log.TemporaryRentSpaces
});
}
}
if (!formattedLogs.Any())
{
@ -323,7 +322,6 @@ namespace WebApi_data_value.Controllers
});
}
}
}
}