This commit is contained in:
qi_0527 2024-11-14 16:26:16 +08:00
parent e695f63198
commit 25340124f9
2 changed files with 85 additions and 4 deletions

View File

@ -193,6 +193,86 @@ namespace WebApi_data_value.Controllers
} }
} }
} }
// 自動下載 Excel 檔案
[HttpGet("AutoDownloadExcel")]
public async Task<IActionResult> 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<IActionResult> GetParkingLogs(DateTime startDate, DateTime endDate, int pageNumber, int pageSize) private async Task<IActionResult> GetParkingLogs(DateTime startDate, DateTime endDate, int pageNumber, int pageSize)
{ {

View File

@ -181,7 +181,8 @@ namespace Parking_space_WebAPI.Controllers
out_time = c.out_time, out_time = c.out_time,
car_img = c.car_img, car_img = c.car_img,
location = c.location, location = c.location,
out_car_img = c.out_car_img out_car_img = c.out_car_img,
out_location= c.out_location,
}).FirstAsync(); }).FirstAsync();
@ -597,7 +598,7 @@ namespace Parking_space_WebAPI.Controllers
} }
var license_plate_number = yuntech_out_car_table.license_plate_number; var license_plate_number = yuntech_out_car_table.license_plate_number;
var out_car_img = yuntech_out_car_table.out_car_img; 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 var in_car_data = await (from c in _context.yuntech_in_car_table
where c.license_plate_number == license_plate_number where c.license_plate_number == license_plate_number
where c.out_time == null where c.out_time == null
@ -614,7 +615,7 @@ namespace Parking_space_WebAPI.Controllers
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
in_car_data.out_time = DateTime.Now; in_car_data.out_time = DateTime.Now;
in_car_data.out_car_img = out_car_img; 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); _context.yuntech_in_car_table.Add(in_car_data);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
@ -780,7 +781,7 @@ namespace Parking_space_WebAPI.Controllers
[HttpGet("auto_export_excel")] [HttpGet("auto_export_excel")]
public async Task<IActionResult> ExportYuntech_in_car_table_date_to_excel_auto() public async Task<IActionResult> 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 end_time = DateTime.Now;
var in_car_table = await (from c in _context.yuntech_in_car_table var in_car_table = await (from c in _context.yuntech_in_car_table
where c.in_time >= start_time where c.in_time >= start_time