parking-webapi/WebApi_data_value/Controllers/Violation_car_tableController.cs

543 lines
23 KiB
C#
Raw Normal View History

2024-02-01 13:48:38 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2024-03-05 19:49:58 +08:00
using Parking_space_WebAPI.Models;
using Parking_space_WebAPI.Services;
using Parking_space_WebAPI.ViewModel;
using Parking_space_WebAPI.Authorization;
using Mysqlx;
2024-03-26 12:49:54 +08:00
using OfficeOpenXml;
using System.Net;
2024-02-01 13:48:38 +08:00
2024-03-05 19:49:58 +08:00
namespace Parking_space_WebAPI.Controllers
2024-02-01 13:48:38 +08:00
{
[Route("api/[controller]")]
[ApiController]
2024-02-09 16:44:45 +08:00
[Authorize]
2024-04-24 16:33:49 +08:00
[ApiExplorerSettings(GroupName = "校園外網")]
2024-02-01 13:48:38 +08:00
public class Violation_car_tableController : ControllerBase
{
private readonly SqlContext _context;
public Violation_car_tableController(SqlContext context)
{
_context = context;
}
#region
/// <summary>
/// 抓取所有違規車輛
/// </summary>
/// <returns></returns>
2024-02-01 13:48:38 +08:00
// GET: api/Violation_car_table
[HttpGet]
public async Task<ActionResult<IEnumerable<Violation_car_table>>> Getviolation_car_table()
{
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();
return violation_car_table;
2024-02-01 13:48:38 +08:00
}
#endregion
#region
/// <summary>
/// 透過區域名稱抓取違規車輛
/// </summary>
/// <param name="id">違規區域</param>
/// <returns></returns>
2024-02-01 13:48:38 +08:00
// GET: api/Violation_car_table/5
[HttpGet("violation_location_name-{id}")]
public async Task<IEnumerable<Single_violation_car_table>> GetViolation_car_table(string id)
{
var violation_car_table = await(from c in _context.violation_car_table
where c.violation_location_name == 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();
return violation_car_table;
}
#endregion
#region
/// <summary>
/// 抓出指定違規車輛
/// </summary>
/// <param name="id">違規區域</param>
/// <param name="time">資料新增時間</param>
/// <returns></returns>
2024-02-01 13:48:38 +08:00
[HttpGet("location_nam-{id}-time-{time}")]
public async Task<ActionResult<Violation_car_table>> violation_location_name(string id, DateTime time)
{
var violation_car_table = await (from c in _context.violation_car_table
where c.violation_location_name == id
where c.create_data_time == time
select new Violation_car_table
{
violation_location_name = c.violation_location_name,
license_plate_number = c.license_plate_number,
create_data_time = c.create_data_time,
car_start_img = c.car_start_img,
car_end_img = c.car_end_img,
}).FirstAsync();
return violation_car_table;
}
#endregion
#region
/// <summary>
/// 更新指定違規車輛
/// </summary>
/// <param name="id">區域</param>
/// <param name="time">資料新增時間</param>
/// <returns></returns>
2024-02-01 13:48:38 +08:00
[HttpPut("location_nam-{id}-time-{time}")]
public async Task<IActionResult> PutViolation_car_table(string id, DateTime time,Violation_car_table violation_car_table)
{
var violation_car_table_1 = await (from c in _context.violation_car_table
where c.violation_location_name == id
where c.create_data_time == time
select c).FirstAsync();
_context.violation_car_table.Remove(violation_car_table_1);
await _context.SaveChangesAsync();
_context.violation_car_table.Add(violation_car_table);
await _context.SaveChangesAsync();
return NoContent();
}
#endregion
#region
/// <summary>
/// 獲取時間內的車輛
/// </summary>
/// <param name="id">位置名稱</param>
/// <param name="start_time">起始時間</param>
/// <param name="end_time">結束時間</param>
/// <returns></returns>
[HttpGet("location_name_1_-{id}-start_time-{start_time}-end_time-{end_time}")]
public async Task<IEnumerable<Single_violation_car_table>> GetYuntech_in_car_table_date(string id, DateTime start_time, DateTime end_time)
{
2024-02-01 13:48:38 +08:00
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();
return violation_car_table;
}
#endregion
#region
/// <summary>
/// 透過車牌號碼搜尋所有資料
/// </summary>
/// <param name="id">車牌號碼 ex:ABC4321</param>
/// <returns></returns>
[HttpGet("license_plate_number-{id}")]
public async Task<IEnumerable<Single_violation_car_table>> Getlicense_plate_number(string id)
2024-02-01 13:48:38 +08:00
{
var in_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();
2024-02-01 13:48:38 +08:00
return in_car_table;
2024-02-01 13:48:38 +08:00
}
#endregion
2024-02-01 13:48:38 +08:00
2024-02-08 23:44:13 +08:00
#region
2024-02-06 00:34:29 +08:00
/// <summary>
/// 尋找違規次數最多
/// </summary>
/// <returns></returns>
[HttpGet("recidivists")]
public async Task<IEnumerable<object>> GetRecidivists()
{
var plateNumberOccurrences = await _context.violation_car_table
.GroupBy(c => c.license_plate_number)
.Select(group => new
{
LicensePlateNumber = group.Key,
Occurrences = group.Count()
})
.OrderByDescending(x => x.Occurrences)
.ToListAsync();
return plateNumberOccurrences;
}
#endregion
2024-02-08 23:44:13 +08:00
#region
/// <summary>
/// 尋找規定時間內每日違規人數
/// </summary>
/// <returns></returns>
[HttpGet("get_day_violation-{day}")]
public async Task<IEnumerable<object>> Getdayvalue(int day)
{
// 取得今日日期
DateTime today = DateTime.Today;
// 取得 30 天前的日期
DateTime DaysAgo = today.AddDays(-day);
var dateValue = new List<object>();
// 使用 for 迴圈逐日計算次數
for (int i = 1; i <= day; i++)
{
DateTime date_1 = DaysAgo;
date_1 = date_1.AddDays(i);
DateTime date_2 = DaysAgo;
date_2 = date_2.AddDays(i + 1);
var dailyCount = await _context.violation_car_table
.Where(c => c.create_data_time >= date_1.Date) // 只選擇指定日期的資料
.Where(c => c.create_data_time <= date_2.Date) // 只選擇指定日期的資料
.CountAsync(); // 計算該日期的次數
dateValue.Add(new { Date = date_1.Date, Occurrences = dailyCount });
}
return dateValue;
}
#endregion
2024-02-01 13:48:38 +08:00
#region
/// <summary>
/// 新增違規車輛
/// </summary>
/// <param name="violation_car_table"></param>
/// <returns></returns>
2024-02-01 13:48:38 +08:00
// POST: api/Violation_car_table
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<Violation_car_table>> PostViolation_car_table(Violation_car_table violation_car_table)
{
if (_context.violation_car_table == null)
{
return Problem("Entity set 'SqlContext.violation_car_table' is null.");
}
violation_car_table.create_data_time= DateTime.Now;
_context.violation_car_table.Add(violation_car_table);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (Violation_car_tableExists(violation_car_table.violation_location_name))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction("GetViolation_car_table", new { id = violation_car_table.violation_location_name }, violation_car_table);
}
#endregion
#region
/// <summary>
/// 刪除違規車輛
/// </summary>
/// <param name="id"></param>
/// <param name="time"></param>
/// <returns></returns>
2024-02-01 13:48:38 +08:00
// DELETE: api/Violation_car_table/5
[HttpDelete("location_name-{id}-time-{time}")]
public async Task<IActionResult> DeleteViolation_car_table(string id, DateTime time)
{
var violation_car_table = await (from c in _context.violation_car_table
where c.violation_location_name == id
where c.create_data_time == time
select c).FirstAsync();
_context.violation_car_table.Remove(violation_car_table);
await _context.SaveChangesAsync();
return NoContent();
}
#endregion
#region 10
/// <summary>
/// 新增違規資料並比對10秒內
/// </summary>
/// <param name="violation_car_table"></param>
/// <returns></returns>
// POST: api/Violation_car_table
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[AllowAnonymous]
[HttpPost("post_10s")]
public async Task<ActionResult<Violation_car_table>> PostViolation_car_table_1(Violation_car_table violation_car_table)
{
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;
2024-03-26 12:49:54 +08:00
var in_car_img = violation_car_table.car_start_img;
// 取得現在時間
DateTime time = DateTime.Now;
// 取得 10秒前的時間
DateTime time_10s_ago = time.AddSeconds(-10);
var Data_list = await _context.violation_car_table
.Where(c => c.violation_location_name == location_name)
.Where(c => c.create_data_time >= time_10s_ago) // 只選擇指定日期的資料
.ToListAsync();
//判斷10秒內有多少資料
if (Data_list.Count == 1) //只有1筆 若與全景判斷的車牌不同就替換車牌
{
if (Data_list[0].license_plate_number != license_plate_number)
{
var data = Data_list[0];
_context.violation_car_table.Remove(Data_list[0]);
await _context.SaveChangesAsync();
data.license_plate_number = license_plate_number;
if (time.Hour <= 17)
{
// 如果时间早于或等于17:00则只更新车牌号码
_context.violation_car_table.Add(data);
}
else
{
// 如果时间晚于17:00则同时更新车牌号码和车辆入场图片
data.car_start_img = in_car_img;
_context.violation_car_table.Add(data);
}
2024-03-26 12:49:54 +08:00
await _context.SaveChangesAsync();
return Ok();
}
else
{
return Ok();
}
}
if (Data_list.Count > 1)//超過2筆
{
for (int i = 0; i < Data_list.Count; i++)
{
if (Data_list[i].license_plate_number != license_plate_number)
{
if (Data_list[i].create_data_time > time.AddSeconds(-10) && Data_list[i].create_data_time < time.AddSeconds(-3))
{
var data = Data_list[i];
_context.violation_car_table.Remove(Data_list[0]);
await _context.SaveChangesAsync();
data.license_plate_number = license_plate_number;
_context.violation_car_table.Add(data);
await _context.SaveChangesAsync();
return Ok();
}
}
}
}
_context.violation_car_table.Add(violation_car_table);
await _context.SaveChangesAsync();
return Ok();
}
#endregion
2024-03-26 12:49:54 +08:00
#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
2024-02-01 13:48:38 +08:00
private bool Violation_car_tableExists(string id)
{
return (_context.violation_car_table?.Any(e => e.violation_location_name == id)).GetValueOrDefault();
}
}
}