Second upload
This commit is contained in:
parent
6669a9bf3c
commit
3964510f8f
@ -33,13 +33,13 @@ namespace WebApi_data_value.Controllers
|
|||||||
return BadRequest(new { message = "請求的模型無效。" });
|
return BadRequest(new { message = "請求的模型無效。" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
_context.ParkingLogs.Add(parkingLog);
|
_context.ParkingLogs.Add(parkingLog);
|
||||||
|
|
||||||
// 儲存變更到資料庫
|
// 儲存變更到資料庫
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
|
||||||
return CreatedAtAction(nameof(GetParkingLogById), new { id = parkingLog.Id }, parkingLog);
|
return CreatedAtAction(nameof(GetParkingLogById), new { id = parkingLog.Id }, parkingLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,10 +60,10 @@ namespace WebApi_data_value.Controllers
|
|||||||
[HttpGet("DownloadCurrentStatusExcel")]
|
[HttpGet("DownloadCurrentStatusExcel")]
|
||||||
public async Task<IActionResult> DownloadCurrentStatusExcel()
|
public async Task<IActionResult> DownloadCurrentStatusExcel()
|
||||||
{
|
{
|
||||||
|
|
||||||
DateTime currentTime = DateTime.Now;
|
DateTime currentTime = DateTime.Now;
|
||||||
|
|
||||||
|
|
||||||
var parkingLog = await _context.ParkingLogs
|
var parkingLog = await _context.ParkingLogs
|
||||||
.OrderByDescending(p => p.Timestamp)
|
.OrderByDescending(p => p.Timestamp)
|
||||||
.FirstOrDefaultAsync(p => p.Timestamp <= currentTime);
|
.FirstOrDefaultAsync(p => p.Timestamp <= currentTime);
|
||||||
@ -77,30 +77,30 @@ namespace WebApi_data_value.Controllers
|
|||||||
{
|
{
|
||||||
var worksheet = workbook.Worksheets.Add("即時車位資訊");
|
var worksheet = workbook.Worksheets.Add("即時車位資訊");
|
||||||
worksheet.Cell(1, 1).Value = "時間";
|
worksheet.Cell(1, 1).Value = "時間";
|
||||||
worksheet.Cell(1, 2).Value = "星期";
|
worksheet.Cell(1, 2).Value = "星期";
|
||||||
worksheet.Cell(1, 3).Value = "總車位";
|
worksheet.Cell(1, 3).Value = "總車位";
|
||||||
worksheet.Cell(1, 4).Value = "剩餘車位";
|
worksheet.Cell(1, 4).Value = "剩餘車位";
|
||||||
worksheet.Cell(1, 5).Value = "月租車位";
|
worksheet.Cell(1, 5).Value = "月租車位";
|
||||||
worksheet.Cell(1, 6).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, 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, 2).Value = DateTime.Now.ToString("dddd", new CultureInfo("zh-TW"));
|
||||||
worksheet.Cell(2, 3).Value = parkingLog.TotalParkingSpaces;
|
worksheet.Cell(2, 3).Value = parkingLog.TotalParkingSpaces;
|
||||||
worksheet.Cell(2, 4).Value = parkingLog.RemainingSpaces;
|
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, 6).Value = parkingLog.TemporaryRentSpaces;
|
||||||
worksheet.Cell(2, 7).Value = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss dddd");
|
worksheet.Cell(2, 7).Value = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss dddd");
|
||||||
|
|
||||||
// 設定欄位寬度
|
// 設定欄位寬度
|
||||||
worksheet.Column(1).Width = 25;
|
worksheet.Column(1).Width = 25;
|
||||||
worksheet.Column(2).Width = 10;
|
worksheet.Column(2).Width = 10;
|
||||||
worksheet.Column(3).Width = 15;
|
worksheet.Column(3).Width = 15;
|
||||||
worksheet.Column(4).Width = 15;
|
worksheet.Column(4).Width = 15;
|
||||||
worksheet.Column(5).Width = 15;
|
worksheet.Column(5).Width = 15;
|
||||||
worksheet.Column(6).Width = 15;
|
worksheet.Column(6).Width = 15;
|
||||||
worksheet.Column(7).Width = 25;
|
worksheet.Column(7).Width = 25;
|
||||||
|
|
||||||
using (var stream = new MemoryStream())
|
using (var stream = new MemoryStream())
|
||||||
{
|
{
|
||||||
@ -159,7 +159,7 @@ namespace WebApi_data_value.Controllers
|
|||||||
for (int i = 0; i < records.Count; i++)
|
for (int i = 0; i < records.Count; i++)
|
||||||
{
|
{
|
||||||
var log = records[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, 2).Value = log.DayOfWeek;
|
||||||
worksheet.Cell(i + 2, 3).Value = log.TotalParkingSpaces;
|
worksheet.Cell(i + 2, 3).Value = log.TotalParkingSpaces;
|
||||||
worksheet.Cell(i + 2, 4).Value = log.RemainingSpaces;
|
worksheet.Cell(i + 2, 4).Value = log.RemainingSpaces;
|
||||||
@ -170,10 +170,10 @@ namespace WebApi_data_value.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
worksheet.Column(1).Width = 25;
|
worksheet.Column(1).Width = 25;
|
||||||
worksheet.Column(2).Width = 10;
|
worksheet.Column(2).Width = 10;
|
||||||
worksheet.Column(3).Width = 15;
|
worksheet.Column(3).Width = 15;
|
||||||
worksheet.Column(4).Width = 15;
|
worksheet.Column(4).Width = 15;
|
||||||
worksheet.Column(5).Width = 15;
|
worksheet.Column(5).Width = 15;
|
||||||
worksheet.Column(6).Width = 15;
|
worksheet.Column(6).Width = 15;
|
||||||
worksheet.Column(7).Width = 25;
|
worksheet.Column(7).Width = 25;
|
||||||
@ -183,9 +183,9 @@ namespace WebApi_data_value.Controllers
|
|||||||
workbook.SaveAs(stream);
|
workbook.SaveAs(stream);
|
||||||
var content = stream.ToArray();
|
var content = stream.ToArray();
|
||||||
|
|
||||||
|
|
||||||
string currentDate = DateTime.Now.ToString("yyyy-MM-dd");
|
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";
|
string fileName = $"停車紀錄-{currentDate}-{weekDay}.xlsx";
|
||||||
|
|
||||||
@ -198,25 +198,25 @@ namespace WebApi_data_value.Controllers
|
|||||||
// 獲取停車紀錄
|
// 獲取停車紀錄
|
||||||
private async Task<IActionResult> GetParkingLogs(DateTime startDate, DateTime endDate, int pageNumber, int pageSize)
|
private async Task<IActionResult> GetParkingLogs(DateTime startDate, DateTime endDate, int pageNumber, int pageSize)
|
||||||
{
|
{
|
||||||
|
|
||||||
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 = "結束日期必須晚於開始日期。" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var parkingLogs = await _context.ParkingLogs
|
var parkingLogs = await _context.ParkingLogs
|
||||||
.AsNoTracking()
|
.AsNoTracking()
|
||||||
.Where(p => p.Timestamp >= startDate && p.Timestamp <= endDate)
|
.Where(p => p.Timestamp >= startDate && p.Timestamp <= endDate)
|
||||||
.OrderByDescending(p => p.Timestamp)
|
.OrderByDescending(p => p.Timestamp)
|
||||||
.Skip((pageNumber - 1) * pageSize)
|
.Skip((pageNumber - 1) * pageSize)
|
||||||
.Take(pageSize)
|
.Take(pageSize)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
if (!parkingLogs.Any())
|
if (!parkingLogs.Any())
|
||||||
@ -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,9 +268,8 @@ 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();
|
|
||||||
|
|
||||||
return Ok(new
|
return Ok(new
|
||||||
{
|
{
|
||||||
@ -285,26 +281,29 @@ 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);
|
||||||
{
|
|
||||||
formattedLogs.Add(new
|
if (log != null)
|
||||||
{
|
{
|
||||||
Timestamp = new DateTime(logGroup.Key.Year, logGroup.Key.Month, logGroup.Key.Day, logGroup.Key.HourGroup * hourInterval, 0, 0),
|
formattedLogs.Add(new
|
||||||
DayOfWeek = logGroup.First().Timestamp.ToString("dddd", new CultureInfo("zh-TW")),
|
{
|
||||||
TotalParkingSpaces = logGroup.First().TotalParkingSpaces,
|
Timestamp = log.Timestamp,
|
||||||
RemainingSpaces = logGroup.First().RemainingSpaces,
|
DayOfWeek = log.Timestamp.ToString("dddd", new CultureInfo("zh-TW")),
|
||||||
MonthlyRentSpaces = logGroup.First().MonthlyRentSpaces,
|
log.TotalParkingSpaces,
|
||||||
TemporaryRentSpaces = logGroup.First().TemporaryRentSpaces
|
log.RemainingSpaces,
|
||||||
});
|
log.MonthlyRentSpaces,
|
||||||
|
log.TemporaryRentSpaces
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!formattedLogs.Any())
|
if (!formattedLogs.Any())
|
||||||
@ -313,8 +312,8 @@ namespace WebApi_data_value.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 分頁處理
|
// 分頁處理
|
||||||
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();
|
||||||
|
|
||||||
return Ok(new
|
return Ok(new
|
||||||
{
|
{
|
||||||
@ -323,7 +322,6 @@ namespace WebApi_data_value.Controllers
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user