新增違規判斷功能

This commit is contained in:
威勝 張 2024-03-26 12:49:54 +08:00
parent 17a3de6643
commit 4ccfd9cb78
2 changed files with 171 additions and 2 deletions

View File

@ -10,6 +10,8 @@ using Parking_space_WebAPI.Services;
using Parking_space_WebAPI.ViewModel;
using Parking_space_WebAPI.Authorization;
using Mysqlx;
using OfficeOpenXml;
using System.Net;
namespace Parking_space_WebAPI.Controllers
{
@ -317,6 +319,7 @@ namespace Parking_space_WebAPI.Controllers
violation_car_table.create_data_time = DateTime.Now;
var location_name = violation_car_table.violation_location_name;
var license_plate_number = violation_car_table.license_plate_number;
var in_car_img = violation_car_table.car_start_img;
// 取得現在時間
DateTime time = DateTime.Now;
// 取得 10秒前的時間
@ -328,7 +331,7 @@ namespace Parking_space_WebAPI.Controllers
//判斷10秒內有多少資料
if (Data_list.Count == 1) //只有1筆 若與全景判斷的車牌不同就替換車牌
{
if (Data_list[0].license_plate_number != license_plate_number)
if (Data_list[0].license_plate_number != license_plate_number && time.Hour <= 17)
{
var data = Data_list[0];
_context.violation_car_table.Remove(Data_list[0]);
@ -343,6 +346,24 @@ namespace Parking_space_WebAPI.Controllers
return Ok();
}
if (Data_list[0].license_plate_number != license_plate_number && time.Hour > 17)
{
var data = Data_list[0];
_context.violation_car_table.Remove(Data_list[0]);
await _context.SaveChangesAsync();
data.license_plate_number = license_plate_number;
data.car_start_img = in_car_img;
_context.violation_car_table.Add(data);
await _context.SaveChangesAsync();
return Ok();
}
else
{
return Ok();
}
}
if (Data_list.Count > 1)//超過2筆
{
@ -369,6 +390,153 @@ namespace Parking_space_WebAPI.Controllers
}
#endregion
#region EXCEL
/// <summary>
/// 生成全部違規車輛EXCEL
/// </summary>
/// <returns></returns>
[HttpGet("export-all_excel")]
public async Task<IActionResult> GenerateExcel()
{
var violation_car_table = await(from c in _context.violation_car_table
orderby c.create_data_time descending
select new Violation_car_table
{
license_plate_number = c.license_plate_number,
create_data_time = c.create_data_time,
violation_location_name = c.violation_location_name,
}).ToListAsync();
// 創建ExcelPackage對象
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("ViolationCarTable");
// 添加標題行
worksheet.Cells["A1"].Value = "違規位置";
worksheet.Cells["B1"].Value = "車牌號碼";
worksheet.Cells["C1"].Value = "違規時間";
// 添加資料
int row = 2;
foreach (var item in violation_car_table)
{
worksheet.Cells["A" + row].Value = item.violation_location_name;
worksheet.Cells["B" + row].Value = item.license_plate_number;
worksheet.Cells["C" + row].Value = item.create_data_time.ToString(); // 可以自行調整日期時間的格式
row++;
}
// 將ExcelPackage保存到內存流中
MemoryStream stream = new MemoryStream(package.GetAsByteArray());
// 設置HttpResponseMessage的內容
Response.Headers.Add("Content-Disposition", "attachment; filename=test.xlsx");
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
}
#endregion
#region EXCEL
/// <summary>
/// 生成指定車輛EXCEL
/// </summary>
/// <returns></returns>
[HttpGet("export-excel_license_plate_number-{id}")]
public async Task<IActionResult> Getlicense_plate_number_excel(string id)
{
var violation_car_table = await (from c in _context.violation_car_table
where c.license_plate_number == id
orderby c.create_data_time descending
select new Single_violation_car_table
{
violation_location_name = c.violation_location_name,
license_plate_number = c.license_plate_number,
create_data_time = c.create_data_time,
}).ToListAsync();
// 創建ExcelPackage對象
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("ViolationCarTable");
// 添加標題行
worksheet.Cells["A1"].Value = "違規位置";
worksheet.Cells["B1"].Value = "車牌號碼";
worksheet.Cells["C1"].Value = "違規時間";
// 添加資料
int row = 2;
foreach (var item in violation_car_table)
{
worksheet.Cells["A" + row].Value = item.violation_location_name;
worksheet.Cells["B" + row].Value = item.license_plate_number;
worksheet.Cells["C" + row].Value = item.create_data_time.ToString(); // 可以自行調整日期時間的格式
row++;
}
// 將ExcelPackage保存到內存流中
MemoryStream stream = new MemoryStream(package.GetAsByteArray());
// 設置HttpResponseMessage的內容
Response.Headers.Add("Content-Disposition", "attachment; filename=test.xlsx");
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
}
#endregion
#region EXCEL
/// <summary>
/// 生成指定時間內EXCEL
/// </summary>
/// <returns></returns>
[HttpGet("export-excel_location_name_1_-{id}-start_time-{start_time}-end_time-{end_time}")]
public async Task<IActionResult> Getviolation_car_table_date_excel(string id, DateTime start_time, DateTime end_time)
{
var violation_car_table = await (from c in _context.violation_car_table
where c.violation_location_name == id
where c.create_data_time >= start_time
where c.create_data_time <= end_time
select new Single_violation_car_table
{
violation_location_name = c.violation_location_name,
license_plate_number = c.license_plate_number,
create_data_time = c.create_data_time,
}).ToListAsync();
// 創建ExcelPackage對象
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("ViolationCarTable");
// 添加標題行
worksheet.Cells["A1"].Value = "違規位置";
worksheet.Cells["B1"].Value = "車牌號碼";
worksheet.Cells["C1"].Value = "違規時間";
// 添加資料
int row = 2;
foreach (var item in violation_car_table)
{
worksheet.Cells["A" + row].Value = item.violation_location_name;
worksheet.Cells["B" + row].Value = item.license_plate_number;
worksheet.Cells["C" + row].Value = item.create_data_time.ToString(); // 可以自行調整日期時間的格式
row++;
}
// 將ExcelPackage保存到內存流中
MemoryStream stream = new MemoryStream(package.GetAsByteArray());
// 設置HttpResponseMessage的內容
Response.Headers.Add("Content-Disposition", "attachment; filename=test.xlsx");
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
}
#endregion
private bool Violation_car_tableExists(string id)
{
return (_context.violation_car_table?.Any(e => e.violation_location_name == id)).GetValueOrDefault();

View File

@ -20,6 +20,7 @@
<ItemGroup>
<PackageReference Include="Emgu.CV" Version="4.8.0.5324" />
<PackageReference Include="EPPlus" Version="7.0.10" />
<PackageReference Include="FFmpeg.AutoGen" Version="6.0.0.2" />
<PackageReference Include="JWT" Version="10.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.7" />