Compare commits

..

2 Commits

Author SHA1 Message Date
82530baf77 更新自動上傳excel檔的後端功能 2024-09-12 14:12:39 +08:00
B10912128
0007090004 交接秉棋_v1 2024-08-14 16:12:28 +08:00
11 changed files with 908 additions and 4 deletions

View File

@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "8.0.6",
"commands": [
"dotnet-ef"
]
}
}
}

View File

@ -0,0 +1,150 @@
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.Services;
using Parking_space_WebAPI.Authorization;
using WebApi_data_value.Models;
namespace WebApi_data_value.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
[ApiExplorerSettings(GroupName = "校園大內網")]
public class El125_car_tableController : ControllerBase
{
private readonly SqlContext _context;
public El125_car_tableController(SqlContext context)
{
_context = context;
}
// GET: api/El125_car_table
[HttpGet]
public async Task<ActionResult<IEnumerable<El125_car_table>>> Getel125_car_table()
{
if (_context.el125_car_table == null)
{
return NotFound();
}
return await _context.el125_car_table.ToListAsync();
}
// GET: api/El125_car_table/5
[HttpGet("{id}")]
public async Task<ActionResult<El125_car_table>> GetEl125_car_table(string id)
{
if (_context.el125_car_table == null)
{
return NotFound();
}
var el125_car_table = await _context.el125_car_table.FindAsync(id);
if (el125_car_table == null)
{
return NotFound();
}
return el125_car_table;
}
// PUT: api/El125_car_table/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutEl125_car_table(string id, El125_car_table el125_car_table)
{
if (id != el125_car_table.license_plate_number)
{
return BadRequest();
}
_context.Entry(el125_car_table).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!El125_car_tableExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/El125_car_table
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<El125_car_table>> PostEl125_car_table(El125_car_table el125_car_table)
{
if (_context.el125_car_table == null)
{
return Problem("Entity set 'SqlContext.el125_car_table' is null.");
}
var license_plate_number = el125_car_table.license_plate_number;
var data = await (from c in _context.el125_car_table
where c.license_plate_number == license_plate_number
select c
).FirstOrDefaultAsync();
if (data == null)
{
_context.el125_car_table.Add(el125_car_table);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (El125_car_tableExists(el125_car_table.license_plate_number))
{
return Conflict();
}
else
{
throw;
}
}
return Ok("ok");
}
return Ok("車牌重複");
}
// DELETE: api/El125_car_table/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteEl125_car_table(string id)
{
if (_context.el125_car_table == null)
{
return NotFound();
}
var el125_car_table = await _context.el125_car_table.FindAsync(id);
if (el125_car_table == null)
{
return NotFound();
}
_context.el125_car_table.Remove(el125_car_table);
await _context.SaveChangesAsync();
return NoContent();
}
private bool El125_car_tableExists(string id)
{
return (_context.el125_car_table?.Any(e => e.license_plate_number == id)).GetValueOrDefault();
}
}
}

View File

@ -262,6 +262,13 @@ namespace Parking_space_WebAPI.Controllers
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)
@ -345,10 +352,35 @@ namespace Parking_space_WebAPI.Controllers
[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秒前的時間
@ -393,6 +425,8 @@ namespace Parking_space_WebAPI.Controllers
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

View File

@ -9,6 +9,7 @@ using Parking_space_WebAPI.Models;
using Parking_space_WebAPI.Services;
using Parking_space_WebAPI.ViewModel;
using Parking_space_WebAPI.Authorization;
using OfficeOpenXml;
namespace Parking_space_WebAPI.Controllers
@ -109,6 +110,7 @@ namespace Parking_space_WebAPI.Controllers
/// <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<Yuntech_in_car_table>> GetYuntech_in_car_table_date(string id, DateTime start_time, DateTime end_time)
{
@ -117,6 +119,7 @@ namespace Parking_space_WebAPI.Controllers
where c.location == id
where c.in_time >= start_time
where c.in_time <= end_time
orderby c.in_time descending // 按進入時間降序排列
select new Yuntech_in_car_table
{
license_plate_number = c.license_plate_number,
@ -306,6 +309,40 @@ namespace Parking_space_WebAPI.Controllers
// 保存更改
await _context.SaveChangesAsync();
}
//判斷月租臨停車輛 +1
// 與名單比對車牌號碼
var in_list_data = await _context.yuntech_parking_user_list
.FirstOrDefaultAsync(user => user.user_license_plate_number == yuntech_in_car_table.license_plate_number);
// 車牌號碼相同,更新月租車位數量
if (in_list_data != null)
{
var monthly_rent_data_1 = await _context.yuntech_monthly_rent_number
.FirstOrDefaultAsync(rent => rent.category == "月租");
if (monthly_rent_data_1 != null)
{
int now_number;
Int32.TryParse(monthly_rent_data_1.number, out now_number);
now_number = now_number + 1;
monthly_rent_data_1.number = now_number.ToString();
// 保存更改
await _context.SaveChangesAsync();
}
}
else {
var monthly_rent_data_2 = await _context.yuntech_monthly_rent_number
.FirstOrDefaultAsync(rent => rent.category == "臨停");
if (monthly_rent_data_2 != null)
{
int now_number;
Int32.TryParse(monthly_rent_data_2.number, out now_number);
now_number = now_number + 1;
monthly_rent_data_2.number = now_number.ToString();
// 保存更改
await _context.SaveChangesAsync();
}
}
}
catch (DbUpdateException)
@ -389,6 +426,40 @@ namespace Parking_space_WebAPI.Controllers
await _context.SaveChangesAsync();
}
//判斷月租臨停車輛 +1
// 與名單比對車牌號碼
var in_list_data = await _context.yuntech_parking_user_list
.FirstOrDefaultAsync(user => user.user_license_plate_number == yuntech_in_car_table.license_plate_number);
// 車牌號碼相同,更新月租車位數量
if (in_list_data != null)
{
var monthly_rent_data_1 = await _context.yuntech_monthly_rent_number
.FirstOrDefaultAsync(rent => rent.category == "月租");
if (monthly_rent_data_1 != null)
{
int now_number;
Int32.TryParse(monthly_rent_data_1.number, out now_number);
now_number = now_number + 1;
monthly_rent_data_1.number = now_number.ToString();
// 保存更改
await _context.SaveChangesAsync();
}
}
else
{
var monthly_rent_data_2 = await _context.yuntech_monthly_rent_number
.FirstOrDefaultAsync(rent => rent.category == "臨停");
if (monthly_rent_data_2 != null)
{
int now_number;
Int32.TryParse(monthly_rent_data_2.number, out now_number);
now_number = now_number + 1;
monthly_rent_data_2.number = now_number.ToString();
// 保存更改
await _context.SaveChangesAsync();
}
}
}
catch (DbUpdateException)
{
@ -456,6 +527,40 @@ namespace Parking_space_WebAPI.Controllers
// 保存更改
await _context.SaveChangesAsync();
}
//判斷月租臨停車輛 -1
// 與名單比對車牌號碼
var in_list_data = await _context.yuntech_parking_user_list
.FirstOrDefaultAsync(user => user.user_license_plate_number == yuntech_out_car_table.license_plate_number);
// 車牌號碼相同,更新月租車位數量
if (in_list_data != null)
{
var monthly_rent_data_1 = await _context.yuntech_monthly_rent_number
.FirstOrDefaultAsync(rent => rent.category == "月租");
if (monthly_rent_data_1 != null)
{
int now_number;
Int32.TryParse(monthly_rent_data_1.number, out now_number);
now_number = now_number - 1;
monthly_rent_data_1.number = now_number.ToString();
// 保存更改
await _context.SaveChangesAsync();
}
}
else
{
var monthly_rent_data_2 = await _context.yuntech_monthly_rent_number
.FirstOrDefaultAsync(rent => rent.category == "臨停");
if (monthly_rent_data_2 != null)
{
int now_number;
Int32.TryParse(monthly_rent_data_2.number, out now_number);
now_number = now_number - 1;
monthly_rent_data_2.number = now_number.ToString();
// 保存更改
await _context.SaveChangesAsync();
}
}
}
else
{
@ -519,6 +624,39 @@ namespace Parking_space_WebAPI.Controllers
// 保存更改
await _context.SaveChangesAsync();
}
//判斷月租臨停車輛 -1
// 與名單比對車牌號碼
var in_list_data = await _context.yuntech_parking_user_list
.FirstOrDefaultAsync(user => user.user_license_plate_number == yuntech_out_car_table.license_plate_number);
// 車牌號碼相同,更新月租車位數量
if (in_list_data != null)
{
var monthly_rent_data_1 = await _context.yuntech_monthly_rent_number
.FirstOrDefaultAsync(rent => rent.category == "月租");
if (monthly_rent_data_1 != null)
{
int now_number;
Int32.TryParse(monthly_rent_data_1.number, out now_number);
now_number = now_number - 1;
monthly_rent_data_1.number = now_number.ToString();
// 保存更改
await _context.SaveChangesAsync();
}
}
else
{
var monthly_rent_data_2 = await _context.yuntech_monthly_rent_number
.FirstOrDefaultAsync(rent => rent.category == "臨停");
if (monthly_rent_data_2 != null)
{
int now_number;
Int32.TryParse(monthly_rent_data_2.number, out now_number);
now_number = now_number - 1;
monthly_rent_data_2.number = now_number.ToString();
// 保存更改
await _context.SaveChangesAsync();
}
}
}
else
{
@ -558,6 +696,172 @@ namespace Parking_space_WebAPI.Controllers
}
#endregion
#region Excel文件
/// <summary>
/// 生成包含時間範圍內車輛資料的 Excel 文件
/// </summary>
/// <param name="id">位置名稱</param>
/// <param name="start_time">起始時間</param>
/// <param name="end_time">結束時間</param>
/// <returns></returns>
[HttpGet("location_name_2_-{id}-start_time-{start_time}-end_time-{end_time}-export_excel")]
public async Task<IActionResult> ExportYuntech_in_car_table_date_to_excel(string id, DateTime start_time, DateTime end_time)
{
var in_car_table = await (from c in _context.yuntech_in_car_table
where c.location == id
where c.in_time >= start_time
where c.in_time <= end_time
orderby c.in_time descending // 按進入時間降序排列
select new Yuntech_in_car_table
{
license_plate_number = c.license_plate_number,
in_time = c.in_time,
out_time = c.out_time,
location = c.location,
}).ToListAsync();
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("YuntechInCarTable");
// 添加標題行
worksheet.Cells["A1"].Value = "進入位置";
worksheet.Cells["B1"].Value = "車牌號碼";
worksheet.Cells["C1"].Value = "進入時間";
worksheet.Cells["D1"].Value = "出去時間";
// 添加資料
int row = 2;
foreach (var item in in_car_table)
{
worksheet.Cells["A" + row].Value = item.location;
worksheet.Cells["B" + row].Value = item.license_plate_number;
worksheet.Cells["C" + row].Value = item.in_time.ToString(); // 可以自行調整日期時間的格式
worksheet.Cells["D" + row].Value = item.out_time?.ToString();
row++;
}
// 將ExcelPackage保存到內存流中
MemoryStream stream = new MemoryStream(package.GetAsByteArray());
// 設置HttpResponseMessage的內容
Response.Headers.Add("Content-Disposition", $"attachment; filename={id}_car_table_{start_time:yyyyMMddHHmmss}_{end_time:yyyyMMddHHmmss}.xlsx");
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
}
#endregion
#region Excel文件
/// <summary>
/// 自動生成包含時間範圍內車輛資料的 Excel 文件
/// </summary>
/// <param name="id">位置名稱</param>
/// <param name="start_time">起始時間</param>
/// <param name="end_time">結束時間</param>
/// <returns></returns>
[HttpGet("auto_export_excel")]
public async Task<IActionResult> ExportYuntech_in_car_table_date_to_excel_auto()
{
var start_time = DateTime.Now.AddHours(-12);
var end_time = DateTime.Now;
var in_car_table = await (from c in _context.yuntech_in_car_table
where c.in_time >= start_time
where c.in_time <= end_time
orderby c.in_time descending // 按進入時間降序排列
select new Yuntech_in_car_table
{
license_plate_number = c.license_plate_number,
in_time = c.in_time,
out_time = c.out_time,
location = c.location,
}).ToListAsync();
// 創建ExcelPackage對象
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("YuntechInCarTable");
// 添加標題行
worksheet.Cells["A1"].Value = "進入位置";
worksheet.Cells["B1"].Value = "車牌號碼";
worksheet.Cells["C1"].Value = "進入時間";
worksheet.Cells["D1"].Value = "出去時間";
// 添加資料
int row = 2;
foreach (var item in in_car_table)
{
worksheet.Cells["A" + row].Value = item.location;
worksheet.Cells["B" + row].Value = item.license_plate_number;
worksheet.Cells["C" + row].Value = item.in_time.ToString(); // 可以自行調整日期時間的格式
worksheet.Cells["D" + row].Value = item.out_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-all_excel")]
public async Task<IActionResult> GenerateExcel()
{
var yuntech_in_car_table = await (from c in _context.yuntech_in_car_table
orderby c.in_time descending, c.out_time descending
select new Yuntech_in_car_table
{
license_plate_number = c.license_plate_number,
in_time = c.in_time,
out_time = c.out_time,
location = c.location,
}).ToListAsync();
// 創建ExcelPackage對象
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("YuntechInCarTable");
// 添加標題行
worksheet.Cells["A1"].Value = "進入位置";
worksheet.Cells["B1"].Value = "車牌號碼";
worksheet.Cells["C1"].Value = "進入時間";
worksheet.Cells["D1"].Value = "出去時間";
// 添加資料
int row = 2;
foreach (var item in yuntech_in_car_table)
{
worksheet.Cells["A" + row].Value = item.location;
worksheet.Cells["B" + row].Value = item.license_plate_number;
worksheet.Cells["C" + row].Value = item.in_time.ToString(); // 可以自行調整日期時間的格式
worksheet.Cells["D" + row].Value = item.out_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 Yuntech_in_car_tableExists(DateTime? id)
{
return (_context.yuntech_in_car_table?.Any(e => e.in_time == id)).GetValueOrDefault();

View File

@ -0,0 +1,223 @@
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.Authorization;
using WebApi_data_value.Models;
namespace Parking_space_WebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
[ApiExplorerSettings(GroupName = "校園大內網")]
public class Yuntech_monthly_rent_numberController : ControllerBase
{
private readonly SqlContext _context;
public Yuntech_monthly_rent_numberController(SqlContext context)
{
_context = context;
}
#region
// GET: api/Yuntech_monthly_rent_number
/// <summary>
/// 取出所有月租臨停資料(URL 區域 顯示數量)
/// </summary>
[HttpGet]
public async Task<ActionResult<IEnumerable<Yuntech_monthly_rent_number>>> GetYuntech_monthly_rent_number()
{
if (_context.yuntech_monthly_rent_number == null)
{
return NotFound();
}
return await _context.yuntech_monthly_rent_number.ToListAsync();
}
#endregion
#region
// GET: api/Yuntech_monthly_rent_number/parking_space_area-{id}
/// <summary>
/// 從類別抓月租臨停資料
/// </summary>
[HttpGet("parking_space_area-{id}")]
public async Task<IEnumerable<Yuntech_monthly_rent_number>> GetYuntech_monthly_rent_number(string id)
{
var yuntech_monthly_rent_number = await (from c in _context.yuntech_monthly_rent_number
where c.category == id
select new Yuntech_monthly_rent_number
{
category = c.category,
number = c.number,
}).ToArrayAsync();
return yuntech_monthly_rent_number;
}
#endregion
#region
/// <summary>
/// 更新月租臨停資料
/// </summary>
// PUT: api/Yuntech_monthly_rent_number/{id}
[HttpPut("{id}")]
public async Task<IActionResult> PutYuntech_monthly_rent_number(string id, Yuntech_monthly_rent_number yuntech_monthly_rent_number)
{
if (id != yuntech_monthly_rent_number.category)
{
return BadRequest();
}
_context.Entry(yuntech_monthly_rent_number).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!Yuntech_monthly_rent_numberExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
#endregion
#region
/// <summary>
/// 新增月租臨停資料(URL 區域 顯示數量)
/// </summary>
// POST: api/Yuntech_monthly_rent_number
[HttpPost]
public async Task<ActionResult<Yuntech_monthly_rent_number>> PostYuntech_monthly_rent_number(Yuntech_monthly_rent_number yuntech_monthly_rent_number)
{
if (_context.yuntech_monthly_rent_number == null)
{
return Problem("Entity set 'SqlContext.yuntech_monthly_rent_number' is null.");
}
_context.yuntech_monthly_rent_number.Add(yuntech_monthly_rent_number);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (Yuntech_monthly_rent_numberExists(yuntech_monthly_rent_number.category))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction("GetYuntech_monthly_rent_number", new { id = yuntech_monthly_rent_number.category }, yuntech_monthly_rent_number);
}
#endregion
#region
/// <summary>
/// 刪除月租臨停資料
/// </summary>
// DELETE: api/Yuntech_monthly_rent_number/{id}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteYuntech_monthly_rent_number(string id)
{
if (_context.yuntech_monthly_rent_number == null)
{
return NotFound();
}
var yuntech_monthly_rent_number = await _context.yuntech_monthly_rent_number.FindAsync(id);
if (yuntech_monthly_rent_number == null)
{
return NotFound();
}
_context.yuntech_monthly_rent_number.Remove(yuntech_monthly_rent_number);
await _context.SaveChangesAsync();
return NoContent();
}
#endregion
#region
/// <summary>
/// 更新車位數量
/// </summary>
[HttpPost("update-parking-number")]
public async Task<IActionResult> UpdateParkingNumber(string license_plate_number, bool isIncrease)
{
int change = isIncrease ? 1 : -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 += change;
now_num_str = now_num_int.ToString();
yuntech_parking.now_num = now_num_str;
// 保存更改
await _context.SaveChangesAsync();
}
// 比對車牌號碼
var in_list_data = await _context.yuntech_parking_user_list
.FirstOrDefaultAsync(user => user.user_license_plate_number == license_plate_number);
if (in_list_data != null)
{
// 車牌號碼相同,更新月租車位數量
var monthly_rent_data = await _context.yuntech_monthly_rent_number
.FirstOrDefaultAsync(rent => rent.category == "月租");
if (monthly_rent_data != null)
{
int now_number;
Int32.TryParse(monthly_rent_data.number, out now_number);
now_number += change;
monthly_rent_data.number = now_number.ToString();
// 保存更改
await _context.SaveChangesAsync();
}
}
else
{
// 車牌號碼不同,更新臨停車位數量
var temp_rent_data = await _context.yuntech_monthly_rent_number
.FirstOrDefaultAsync(rent => rent.category == "臨停");
if (temp_rent_data != null)
{
int now_number;
Int32.TryParse(temp_rent_data.number, out now_number);
now_number += change;
temp_rent_data.number = now_number.ToString();
// 保存更改
await _context.SaveChangesAsync();
}
}
return Ok();
}
#endregion
private bool Yuntech_monthly_rent_numberExists(string id)
{
return (_context.yuntech_monthly_rent_number?.Any(e => e.category == id)).GetValueOrDefault();
}
}
}

View File

@ -8,6 +8,9 @@ using Microsoft.EntityFrameworkCore;
using Parking_space_WebAPI.Services;
using WebApi_data_value.Models;
using Parking_space_WebAPI.Authorization;
using OfficeOpenXml;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using Parking_space_WebAPI.Models;
namespace WebApi_data_value.Controllers
{
@ -151,6 +154,157 @@ namespace WebApi_data_value.Controllers
}
#endregion
#region excel檔
[HttpPost("excel_upload")]
public async Task<IActionResult> UploadExcel([FromForm] IFormFile file)
{
// 检查文件是否上传成功
if (file == null || file.Length == 0)
{
return BadRequest("没有上传文件或文件为空。");
}
try
{
using (var stream = new MemoryStream())
{
await file.CopyToAsync(stream);
// 检查文件是否为有效的Excel文件
try
{
using (var package = new ExcelPackage(stream))
{
if (package.Workbook.Worksheets.Count == 0)
{
return BadRequest("Excel文件不包含任何工作表。");
}
var worksheet = package.Workbook.Worksheets[0];
// 检查工作表是否存在和非空
if (worksheet == null || worksheet.Dimension == null)
{
return BadRequest("工作表为空或未找到。");
}
// 確認EXCEL格式符合規格
string user_name_check = worksheet.Cells[1, 3].Text;
string user_license_plate_number_check = worksheet.Cells[1, 5].Text;
string user_phone_number_check = worksheet.Cells[1, 4].Text;
if (user_name_check != "車主姓名" && user_license_plate_number_check != "車牌號碼" && user_phone_number_check !="聯絡電話")
{
return BadRequest("無效的Excel文件。");
}
// 若符合規格 將資料庫中的月租名單資料全部刪除
try
{
// 刪除所有現有資料
var existingData_all = await _context.yuntech_parking_user_list.ToListAsync();
if (existingData_all.Any())
{
_context.yuntech_parking_user_list.RemoveRange(existingData_all);
await _context.SaveChangesAsync();
}
}
catch
{
//一筆一筆刪除
var existingData = _context.yuntech_parking_user_list.ToList();
if (existingData.Count != 0)
{
for (int i = 0; i < existingData.Count; i++)
{
var old_data = existingData[i];
try
{
_context.yuntech_parking_user_list.Remove(old_data);
await _context.SaveChangesAsync();
}
catch
{
var stop_1 = 0;
}
}
}
}
// 將EXCEL輸入至資料庫中
for (int row = 2; row <= worksheet.Dimension.Rows; row++)
{
string user_name = worksheet.Cells[row, 3].Text;
string user_license_plate_number = worksheet.Cells[row, 5].Text;
string user_phone_number = worksheet.Cells[row, 4].Text;
string user_state_check = "True";
try
{
if (user_license_plate_number != null && user_license_plate_number != "" && user_name != null && user_name!="")
{
var now_data = new Yuntech_parking_user_list();
now_data.user_name = user_name;
now_data.user_license_plate_number = user_license_plate_number;
now_data.user_phone_number = user_phone_number;
now_data.user_state_check = "True";
_context.yuntech_parking_user_list.Add(now_data);
}
}
catch
{
var stop1 = 1;
}
}
await _context.SaveChangesAsync();
}
}
catch (Exception ex)
{
return BadRequest("無效的Excel文件。");
}
}
return Ok();
}
catch (Exception ex)
{
// 记录异常信息
return StatusCode(500, "内部服务器错误。");
}
}
#endregion
#region
/// <summary>
/// 透過車牌號碼搜尋是否為月租名單成員
/// </summary>
/// <param name="id">車牌號碼 ex:ABC4321</param>
/// <returns></returns>
[HttpGet("license_plate_number-{id}")]
public async Task<IEnumerable<Yuntech_parking_user_list>> Getlicense_plate_number(string id)
{
var parking_user_list = await (from c in _context.yuntech_parking_user_list
where c.user_license_plate_number == id
select new Yuntech_parking_user_list
{
user_name = c.user_name,
user_license_plate_number = c.user_license_plate_number,
user_state_check = c.user_state_check,
user_phone_number = c.user_phone_number,
}).ToListAsync();
return parking_user_list;
}
#endregion
#region 使
/// <summary>
/// 刪除校內停車場使用者名單資料

View File

@ -0,0 +1,7 @@
namespace WebApi_data_value.Models
{
public class El125_car_table
{
public string? license_plate_number { get; set; }
}
}

View File

@ -0,0 +1,9 @@
namespace WebApi_data_value.Models
{
public class Yuntech_monthly_rent_number
{
public string? category { get; set; }
public string? number { get; set; }
}
}

View File

@ -6,5 +6,7 @@
public string? user_license_plate_number { get; set; }=null;
public string? user_state_check { get; set; } = null;
public string? user_phone_number { get; set; } = null;
}
}

View File

@ -58,6 +58,11 @@ namespace Parking_space_WebAPI.Services
// 雲科登記車輛名單
public DbSet<Yuntech_parking_user_list> yuntech_parking_user_list { get; set; } = null!;
//雲科月租與臨停
public DbSet<Yuntech_monthly_rent_number> yuntech_monthly_rent_number { get; set; } = null!;
//EL125車輛
public DbSet<El125_car_table> el125_car_table { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder builder)
{
@ -90,7 +95,11 @@ namespace Parking_space_WebAPI.Services
builder.Entity<Yuntech_parking>().HasKey(o => new { o.all_num }); //Primary Key
builder.Entity<Yuntech_monthly_rent_number>().HasKey(o => new { o.category }); //Primary Key
builder.Entity<Yuntech_parking_user_list>().HasKey(o => new { o.user_license_plate_number });//Primary Key
builder.Entity<El125_car_table>().HasKey(o => new { o.license_plate_number });//Primary Key
}

View File

@ -20,19 +20,19 @@
<ItemGroup>
<PackageReference Include="Emgu.CV" Version="4.8.0.5324" />
<PackageReference Include="EPPlus" Version="7.0.10" />
<PackageReference Include="EPPlus" Version="4.5.3.3" />
<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" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.29" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.32" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.16.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.16" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.17" />
<PackageReference Include="MySql.Data" Version="8.0.32" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />