using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Parking_space_WebAPI.Models;
using Parking_space_WebAPI.Services;
using Parking_space_WebAPI.ViewModel;
using Parking_space_WebAPI.Authorization;
using Mysqlx;
using OfficeOpenXml;
using System.Net;
using System.Net.Http.Headers;
namespace Parking_space_WebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
[ApiExplorerSettings(GroupName = "校園外網")]
public class Violation_car_tableController : ControllerBase
{
private readonly SqlContext _context;
public Violation_car_tableController(SqlContext context)
{
_context = context;
}
#region 抓取所有違規車輛
///
/// 抓取所有違規車輛
///
///
// GET: api/Violation_car_table
[HttpGet]
public async Task>> 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;
}
#endregion
#region 透過區域名稱抓取違規車輛
///
/// 透過區域名稱抓取違規車輛
///
/// 違規區域
///
// GET: api/Violation_car_table/5
[HttpGet("violation_location_name-{id}")]
public async Task> 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 抓出指定違規車輛
///
/// 抓出指定違規車輛
///
/// 違規區域
/// 資料新增時間
///
[HttpGet("location_nam-{id}-time-{time}")]
public async Task> 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 更新指定違規車輛
///
/// 更新指定違規車輛
///
/// 區域
/// 資料新增時間
///
[HttpPut("location_nam-{id}-time-{time}")]
public async Task 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 獲取時間內的資料
///
/// 獲取時間內的車輛
///
/// 位置名稱
/// 起始時間
/// 結束時間
///
[HttpGet("location_name_1_-{id}-start_time-{start_time}-end_time-{end_time}")]
public async Task> GetYuntech_in_car_table_date(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();
return violation_car_table;
}
#endregion
#region 透過車牌號碼搜尋所有資料
///
/// 透過車牌號碼搜尋所有資料
///
/// 車牌號碼 ex:ABC4321
///
[HttpGet("license_plate_number-{id}")]
public async Task> Getlicense_plate_number(string id)
{
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();
return in_car_table;
}
#endregion
#region 尋找違規次數最多
///
/// 尋找違規次數最多
///
///
[HttpGet("recidivists")]
public async Task> 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
#region 尋找規定時間內每日違規人數
///
/// 尋找規定時間內每日違規人數
///
///
[HttpGet("get_day_violation-{day}")]
public async Task> Getdayvalue(int day)
{
// 取得今日日期
DateTime today = DateTime.Today;
// 取得 30 天前的日期
DateTime DaysAgo = today.AddDays(-day);
var dateValue = new List