659 lines
30 KiB
C#
659 lines
30 KiB
C#
|
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 抓取所有違規車輛
|
|||
|
/// <summary>
|
|||
|
/// 抓取所有違規車輛
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
// 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;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
|
|||
|
#region 透過區域名稱抓取違規車輛
|
|||
|
/// <summary>
|
|||
|
/// 透過區域名稱抓取違規車輛
|
|||
|
/// </summary>
|
|||
|
/// <param name="id">違規區域</param>
|
|||
|
/// <returns></returns>
|
|||
|
// 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>
|
|||
|
[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>
|
|||
|
[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)
|
|||
|
{
|
|||
|
|
|||
|
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)
|
|||
|
{
|
|||
|
|
|||
|
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 尋找違規次數最多
|
|||
|
/// <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
|
|||
|
|
|||
|
|
|||
|
#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
|
|||
|
|
|||
|
#region 新增違規車輛
|
|||
|
/// <summary>
|
|||
|
/// 新增違規車輛
|
|||
|
/// </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
|
|||
|
[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);
|
|||
|
|
|||
|
// 比對名單,若與vip125名單相符合則回傳ok
|
|||
|
var in_vip125_data = await _context.el125_car_table.FindAsync(violation_car_table.license_plate_number);
|
|||
|
if (in_vip125_data != null)
|
|||
|
{
|
|||
|
return Ok();
|
|||
|
}
|
|||
|
|
|||
|
// 車位數量+1
|
|||
|
var yuntech_parking = await _context.yuntech_parking.FirstOrDefaultAsync();
|
|||
|
if (yuntech_parking != null)
|
|||
|
{
|
|||
|
string now_num_str = yuntech_parking.now_num;
|
|||
|
int now_num_int;
|
|||
|
Int32.TryParse(now_num_str, out now_num_int);
|
|||
|
now_num_int = now_num_int + 1;
|
|||
|
now_num_str = now_num_int.ToString();
|
|||
|
yuntech_parking.now_num = now_num_str;
|
|||
|
// 保存更改
|
|||
|
await _context.SaveChangesAsync();
|
|||
|
}
|
|||
|
// 比對名單,若與名單相符合則上傳至其他API
|
|||
|
var in_list_data = await _context.yuntech_parking_user_list.FindAsync(violation_car_table.license_plate_number);
|
|||
|
if (in_list_data != null)
|
|||
|
{
|
|||
|
//傳送line
|
|||
|
HttpClient httpClient = new HttpClient();
|
|||
|
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
|
|||
|
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "ykmUs2ZECQ9LDiNR4waT8w6NLXjWuYE9P8safG5xTZ6");
|
|||
|
var content = new Dictionary<string, string>();
|
|||
|
content.Add("message",in_list_data.user_name+" - "+in_list_data.user_license_plate_number + " 違規轉彎");
|
|||
|
httpClient.PostAsync("https://notify-api.line.me/api/notify", new FormUrlEncodedContent(content));
|
|||
|
}
|
|||
|
|
|||
|
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>
|
|||
|
// 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)
|
|||
|
{
|
|||
|
// 比對名單,若與vip125名單相符合則回傳ok
|
|||
|
var in_vip125_data = await _context.el125_car_table.FindAsync(violation_car_table.license_plate_number);
|
|||
|
if (in_vip125_data != null)
|
|||
|
{
|
|||
|
return Ok();
|
|||
|
}
|
|||
|
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;
|
|||
|
|
|||
|
// 若與進入車輛 則違規圖片批配
|
|||
|
var in_car_data = await (from c in _context.yuntech_in_car_table
|
|||
|
where c.license_plate_number == license_plate_number
|
|||
|
where c.out_time == null
|
|||
|
orderby c.in_time descending
|
|||
|
select c).FirstOrDefaultAsync();
|
|||
|
if (in_car_data != null)
|
|||
|
{
|
|||
|
// 处理找到记录的情况
|
|||
|
_context.yuntech_in_car_table.Remove(in_car_data);
|
|||
|
await _context.SaveChangesAsync();
|
|||
|
in_car_data.out_time = DateTime.Now;
|
|||
|
in_car_data.out_car_img = violation_car_table.car_end_img;
|
|||
|
_context.yuntech_in_car_table.Add(in_car_data);
|
|||
|
await _context.SaveChangesAsync();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
//取得現在時間
|
|||
|
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 || time.Hour < 6)
|
|||
|
{
|
|||
|
// 如果时间晚于或等于17:00,或者早于6:00,则同时更新车牌号码和车辆入场图片
|
|||
|
data.car_start_img = in_car_img;
|
|||
|
_context.violation_car_table.Add(data);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// 如果时间早于或等于17:00,则只更新车牌号码
|
|||
|
_context.violation_car_table.Add(data);
|
|||
|
}
|
|||
|
|
|||
|
await _context.SaveChangesAsync();
|
|||
|
|
|||
|
// 比對名單,若與名單相符合則上傳至其他API
|
|||
|
var in_list_data_1 = await _context.yuntech_parking_user_list.FindAsync(license_plate_number);
|
|||
|
if (in_list_data_1 != null)
|
|||
|
{
|
|||
|
//傳送line
|
|||
|
HttpClient httpClient = new HttpClient();
|
|||
|
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
|
|||
|
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "ykmUs2ZECQ9LDiNR4waT8w6NLXjWuYE9P8safG5xTZ6");
|
|||
|
var content = new Dictionary<string, string>();
|
|||
|
content.Add("message", in_list_data_1.user_name + " - " + in_list_data_1.user_license_plate_number + " 違規轉彎");
|
|||
|
httpClient.PostAsync("https://notify-api.line.me/api/notify", new FormUrlEncodedContent(content));
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
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();
|
|||
|
|
|||
|
// 比對名單,若與名單相符合則上傳至其他API
|
|||
|
var in_list_data_1 = await _context.yuntech_parking_user_list.FindAsync(license_plate_number);
|
|||
|
if (in_list_data_1 != null)
|
|||
|
{
|
|||
|
//傳送line
|
|||
|
HttpClient httpClient = new HttpClient();
|
|||
|
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
|
|||
|
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "ykmUs2ZECQ9LDiNR4waT8w6NLXjWuYE9P8safG5xTZ6");
|
|||
|
var content = new Dictionary<string, string>();
|
|||
|
content.Add("message", in_list_data_1.user_name + " - " + in_list_data_1.user_license_plate_number + " 違規轉彎");
|
|||
|
httpClient.PostAsync("https://notify-api.line.me/api/notify", new FormUrlEncodedContent(content));
|
|||
|
}
|
|||
|
|
|||
|
return Ok();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
_context.violation_car_table.Add(violation_car_table);
|
|||
|
await _context.SaveChangesAsync();
|
|||
|
|
|||
|
// 車位數量+1
|
|||
|
var yuntech_parking = await _context.yuntech_parking.FirstOrDefaultAsync();
|
|||
|
if (yuntech_parking != null)
|
|||
|
{
|
|||
|
string now_num_str = yuntech_parking.now_num;
|
|||
|
int now_num_int;
|
|||
|
Int32.TryParse(now_num_str, out now_num_int);
|
|||
|
now_num_int = now_num_int + 1;
|
|||
|
now_num_str = now_num_int.ToString();
|
|||
|
yuntech_parking.now_num = now_num_str;
|
|||
|
// 保存更改
|
|||
|
await _context.SaveChangesAsync();
|
|||
|
}
|
|||
|
|
|||
|
// 比對名單,若與名單相符合則上傳至其他API
|
|||
|
var in_list_data = await _context.yuntech_parking_user_list.FindAsync(license_plate_number);
|
|||
|
if (in_list_data != null)
|
|||
|
{
|
|||
|
//傳送line
|
|||
|
HttpClient httpClient = new HttpClient();
|
|||
|
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
|
|||
|
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "ykmUs2ZECQ9LDiNR4waT8w6NLXjWuYE9P8safG5xTZ6");
|
|||
|
var content = new Dictionary<string, string>();
|
|||
|
content.Add("message", in_list_data.user_name + " - " + in_list_data.user_license_plate_number + " 違規轉彎");
|
|||
|
httpClient.PostAsync("https://notify-api.line.me/api/notify", new FormUrlEncodedContent(content));
|
|||
|
}
|
|||
|
return Ok();
|
|||
|
}
|
|||
|
#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();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|