From 25340124f922e54d5c9eb91b29f81636e203b8cb Mon Sep 17 00:00:00 2001 From: qi_0527 Date: Thu, 14 Nov 2024 16:26:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ParkingLogsController.cs | 80 +++++++++++++++++++ .../Yuntech_in_car_tableController.cs | 9 ++- 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/WebApi_data_value/Controllers/ParkingLogsController.cs b/WebApi_data_value/Controllers/ParkingLogsController.cs index 581e806..cbfb0ec 100644 --- a/WebApi_data_value/Controllers/ParkingLogsController.cs +++ b/WebApi_data_value/Controllers/ParkingLogsController.cs @@ -193,6 +193,86 @@ namespace WebApi_data_value.Controllers } } } + + + // 自動下載 Excel 檔案 + [HttpGet("AutoDownloadExcel")] + public async Task AutoDownloadExcel() + { + string string_startDate = "2024-10-01"; + DateTime startDate = DateTime.ParseExact(string_startDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); + + var endDate = DateTime.Now; + if (startDate == default || endDate == default) + { + return BadRequest(new { message = "日期無效。" }); + } + + // 調用 GetHourlyParkingLogs 方法以獲取所有停車紀錄 + var parkingLogsResult = await GetHourlyParkingLogs(startDate, endDate, "1", 1, int.MaxValue); + + if (parkingLogsResult is NotFoundResult) + { + return NotFound(new { message = "找不到該日期範圍的停車紀錄。" }); + } + + var logs = (parkingLogsResult as OkObjectResult).Value as dynamic; + var records = logs.logs; // 獲取停車紀錄 + + using (var workbook = new XLWorkbook()) + { + var worksheet = workbook.Worksheets.Add("停車紀錄"); + worksheet.Cell(1, 1).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 = "下載日期"; + + + 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, 2).Value = log.DayOfWeek; + worksheet.Cell(i + 2, 3).Value = log.TotalParkingSpaces; + worksheet.Cell(i + 2, 4).Value = log.RemainingSpaces; + worksheet.Cell(i + 2, 5).Value = log.MonthlyRentSpaces; + worksheet.Cell(i + 2, 6).Value = log.TemporaryRentSpaces; + worksheet.Cell(i + 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; + + using (var stream = new MemoryStream()) + { + 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 fileName = $"停車紀錄-{currentDate}-{weekDay}.xlsx"; + + return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName); + } + } + } + + + + + + + // 獲取停車紀錄 private async Task GetParkingLogs(DateTime startDate, DateTime endDate, int pageNumber, int pageSize) { diff --git a/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs b/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs index 73f6fbf..25de7b0 100644 --- a/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs +++ b/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs @@ -181,7 +181,8 @@ namespace Parking_space_WebAPI.Controllers out_time = c.out_time, car_img = c.car_img, location = c.location, - out_car_img = c.out_car_img + out_car_img = c.out_car_img, + out_location= c.out_location, }).FirstAsync(); @@ -597,7 +598,7 @@ namespace Parking_space_WebAPI.Controllers } var license_plate_number = yuntech_out_car_table.license_plate_number; var out_car_img = yuntech_out_car_table.out_car_img; - var location = yuntech_out_car_table.out_location; + var out_location = yuntech_out_car_table.location; var in_car_data = await (from c in _context.yuntech_in_car_table where c.license_plate_number == license_plate_number where c.out_time == null @@ -614,7 +615,7 @@ namespace Parking_space_WebAPI.Controllers await _context.SaveChangesAsync(); in_car_data.out_time = DateTime.Now; in_car_data.out_car_img = out_car_img; - in_car_data.out_location = location; + in_car_data.out_location = out_location; _context.yuntech_in_car_table.Add(in_car_data); await _context.SaveChangesAsync(); @@ -780,7 +781,7 @@ namespace Parking_space_WebAPI.Controllers [HttpGet("auto_export_excel")] public async Task ExportYuntech_in_car_table_date_to_excel_auto() { - var start_time = DateTime.Now.AddHours(-12); + var start_time = DateTime.Now.AddHours(-24);//將資料調整成24小時內的車輛紀錄 var end_time = DateTime.Now; var in_car_table = await (from c in _context.yuntech_in_car_table where c.in_time >= start_time