From 00070900043bb5e29a2d9bce767a8da372369520 Mon Sep 17 00:00:00 2001 From: B10912128 Date: Wed, 14 Aug 2024 16:12:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=A4=E6=8E=A5=E7=A7=89=E6=A3=8B=5Fv1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WebApi_data_value/.config/dotnet-tools.json | 12 + .../Controllers/El125_car_tableController.cs | 150 +++++++++++ .../Violation_car_tableController.cs | 36 ++- .../Yuntech_in_car_tableController.cs | 248 ++++++++++++++++++ .../Yuntech_monthly_rent_numberController.cs | 223 ++++++++++++++++ .../Yuntech_parking_user_listController.cs | 127 +++++++++ WebApi_data_value/Models/El125_car_table.cs | 7 + .../Models/Yuntech_monthly_rent_number.cs | 9 + WebApi_data_value/Services/SqlContext.cs | 9 + WebApi_data_value/WebApi_data_value.csproj | 6 +- 10 files changed, 823 insertions(+), 4 deletions(-) create mode 100644 WebApi_data_value/.config/dotnet-tools.json create mode 100644 WebApi_data_value/Controllers/El125_car_tableController.cs create mode 100644 WebApi_data_value/Controllers/Yuntech_monthly_rent_numberController.cs create mode 100644 WebApi_data_value/Models/El125_car_table.cs create mode 100644 WebApi_data_value/Models/Yuntech_monthly_rent_number.cs diff --git a/WebApi_data_value/.config/dotnet-tools.json b/WebApi_data_value/.config/dotnet-tools.json new file mode 100644 index 0000000..b99844e --- /dev/null +++ b/WebApi_data_value/.config/dotnet-tools.json @@ -0,0 +1,12 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-ef": { + "version": "8.0.6", + "commands": [ + "dotnet-ef" + ] + } + } +} \ No newline at end of file diff --git a/WebApi_data_value/Controllers/El125_car_tableController.cs b/WebApi_data_value/Controllers/El125_car_tableController.cs new file mode 100644 index 0000000..74260c1 --- /dev/null +++ b/WebApi_data_value/Controllers/El125_car_tableController.cs @@ -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>> 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> 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 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> 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 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(); + } + } +} diff --git a/WebApi_data_value/Controllers/Violation_car_tableController.cs b/WebApi_data_value/Controllers/Violation_car_tableController.cs index 584edd2..ea98c7e 100644 --- a/WebApi_data_value/Controllers/Violation_car_tableController.cs +++ b/WebApi_data_value/Controllers/Violation_car_tableController.cs @@ -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,11 +352,36 @@ namespace Parking_space_WebAPI.Controllers [HttpPost("post_10s")] public async Task> 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); @@ -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 diff --git a/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs b/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs index 070c0fc..5838489 100644 --- a/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs +++ b/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs @@ -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 /// 起始時間 /// 結束時間 /// + [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) { @@ -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,116 @@ namespace Parking_space_WebAPI.Controllers } #endregion + #region 生成時間段Excel文件 + /// + /// 生成包含時間範圍內車輛資料的 Excel 文件 + /// + /// 位置名稱 + /// 起始時間 + /// 結束時間 + /// + + [HttpGet("location_name_2_-{id}-start_time-{start_time}-end_time-{end_time}-export_excel")] + public async Task 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 + /// + /// 生成全部進入車輛EXCEL + /// + /// + [HttpGet("export-all_excel")] + public async Task 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(); diff --git a/WebApi_data_value/Controllers/Yuntech_monthly_rent_numberController.cs b/WebApi_data_value/Controllers/Yuntech_monthly_rent_numberController.cs new file mode 100644 index 0000000..a00913c --- /dev/null +++ b/WebApi_data_value/Controllers/Yuntech_monthly_rent_numberController.cs @@ -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 + /// + /// 取出所有月租臨停資料(URL 區域 顯示數量) + /// + [HttpGet] + public async Task>> 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} + /// + /// 從類別抓月租臨停資料 + /// + [HttpGet("parking_space_area-{id}")] + public async Task> 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 更新月租臨停資料 + /// + /// 更新月租臨停資料 + /// + // PUT: api/Yuntech_monthly_rent_number/{id} + [HttpPut("{id}")] + public async Task 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 新增月租臨停資料 + /// + /// 新增月租臨停資料(URL 區域 顯示數量) + /// + // POST: api/Yuntech_monthly_rent_number + [HttpPost] + public async Task> 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 刪除月租臨停資料 + /// + /// 刪除月租臨停資料 + /// + // DELETE: api/Yuntech_monthly_rent_number/{id} + [HttpDelete("{id}")] + public async Task 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 更新車位數量 + /// + /// 更新車位數量 + /// + [HttpPost("update-parking-number")] + public async Task 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(); + } + } +} diff --git a/WebApi_data_value/Controllers/Yuntech_parking_user_listController.cs b/WebApi_data_value/Controllers/Yuntech_parking_user_listController.cs index 39f9e5e..930e638 100644 --- a/WebApi_data_value/Controllers/Yuntech_parking_user_listController.cs +++ b/WebApi_data_value/Controllers/Yuntech_parking_user_listController.cs @@ -8,6 +8,8 @@ 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; namespace WebApi_data_value.Controllers { @@ -151,6 +153,131 @@ namespace WebApi_data_value.Controllers } #endregion + #region 上傳excel檔 + [HttpPost("excel_upload")] + public async Task 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; + if (user_name_check != "車主姓名" && user_license_plate_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_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_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 刪除校內停車場使用者名單資料 /// /// 刪除校內停車場使用者名單資料 diff --git a/WebApi_data_value/Models/El125_car_table.cs b/WebApi_data_value/Models/El125_car_table.cs new file mode 100644 index 0000000..c0b9a15 --- /dev/null +++ b/WebApi_data_value/Models/El125_car_table.cs @@ -0,0 +1,7 @@ +namespace WebApi_data_value.Models +{ + public class El125_car_table + { + public string? license_plate_number { get; set; } + } +} diff --git a/WebApi_data_value/Models/Yuntech_monthly_rent_number.cs b/WebApi_data_value/Models/Yuntech_monthly_rent_number.cs new file mode 100644 index 0000000..53ba700 --- /dev/null +++ b/WebApi_data_value/Models/Yuntech_monthly_rent_number.cs @@ -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; } + + } +} diff --git a/WebApi_data_value/Services/SqlContext.cs b/WebApi_data_value/Services/SqlContext.cs index 48b815c..bf71624 100644 --- a/WebApi_data_value/Services/SqlContext.cs +++ b/WebApi_data_value/Services/SqlContext.cs @@ -58,6 +58,11 @@ namespace Parking_space_WebAPI.Services // 雲科登記車輛名單 public DbSet yuntech_parking_user_list { get; set; } = null!; + //雲科月租與臨停 + public DbSet yuntech_monthly_rent_number { get; set; } = null!; + + //EL125車輛 + public DbSet el125_car_table { get; set; } = null!; protected override void OnModelCreating(ModelBuilder builder) { @@ -90,7 +95,11 @@ namespace Parking_space_WebAPI.Services builder.Entity().HasKey(o => new { o.all_num }); //Primary Key + builder.Entity().HasKey(o => new { o.category }); //Primary Key + builder.Entity().HasKey(o => new { o.user_license_plate_number });//Primary Key + + builder.Entity().HasKey(o => new { o.license_plate_number });//Primary Key } diff --git a/WebApi_data_value/WebApi_data_value.csproj b/WebApi_data_value/WebApi_data_value.csproj index d0beb31..ce43b17 100644 --- a/WebApi_data_value/WebApi_data_value.csproj +++ b/WebApi_data_value/WebApi_data_value.csproj @@ -20,19 +20,19 @@ - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive - +