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;
|
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-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 抓取所有違規車輛
|
2024-02-05 22:09:54 +08:00
|
|
|
|
/// <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()
|
|
|
|
|
{
|
2024-02-05 22:09:54 +08:00
|
|
|
|
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 透過區域名稱抓取違規車輛
|
2024-02-05 22:09:54 +08:00
|
|
|
|
/// <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 抓出指定違規車輛
|
2024-02-05 22:09:54 +08:00
|
|
|
|
/// <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 更新指定違規車輛
|
2024-02-05 22:09:54 +08:00
|
|
|
|
/// <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
|
|
|
|
|
|
2024-02-05 22:09:54 +08:00
|
|
|
|
#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
|
|
|
|
|
2024-02-05 22:09:54 +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
|
|
|
|
{
|
|
|
|
|
|
2024-02-05 22:09:54 +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
|
|
|
|
|
|
|
|
|
|
2024-02-05 22:09:54 +08:00
|
|
|
|
return in_car_table;
|
2024-02-01 13:48:38 +08:00
|
|
|
|
}
|
2024-02-05 22:09:54 +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 新增違規車輛
|
2024-02-05 22:09:54 +08:00
|
|
|
|
/// <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 刪除違規車輛
|
2024-02-05 22:09:54 +08:00
|
|
|
|
/// <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
|
|
|
|
|
|
|
|
|
|
private bool Violation_car_tableExists(string id)
|
|
|
|
|
{
|
|
|
|
|
return (_context.violation_car_table?.Any(e => e.violation_location_name == id)).GetValueOrDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|