From 51f4bafb9fc833f4ff71ec977e2b9ca075647b4b Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 8 Feb 2024 23:44:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E30=E5=A4=A9=E5=85=A7=E7=B4=80?= =?UTF-8?q?=E9=8C=84=E6=AC=A1=E6=95=B8=E6=9F=A5=E8=A9=A2API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Violation_car_tableController.cs | 35 ++++++++++++++++++- .../Yuntech_in_car_tableController.cs | 35 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/WebApi_data_value/Controllers/Violation_car_tableController.cs b/WebApi_data_value/Controllers/Violation_car_tableController.cs index 964f1e4..574327f 100644 --- a/WebApi_data_value/Controllers/Violation_car_tableController.cs +++ b/WebApi_data_value/Controllers/Violation_car_tableController.cs @@ -179,7 +179,7 @@ namespace WebApi_data_value.Controllers } #endregion - #region + #region 尋找違規次數最多 /// /// 尋找違規次數最多 /// @@ -204,6 +204,39 @@ namespace WebApi_data_value.Controllers #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(); + // 使用 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 新增違規車輛 /// /// 新增違規車輛 diff --git a/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs b/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs index 373fa3e..f338dbe 100644 --- a/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs +++ b/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs @@ -155,6 +155,41 @@ namespace WebApi_data_value.Controllers } #endregion + #region 尋找規定時間內每日進入人數 + /// + /// 尋找規定時間內每日進入人數 + /// + /// + [HttpGet("get_day_in_car-{day}")] + public async Task> Getdayvalue(int day) + { + // 取得今日日期 + DateTime today = DateTime.Today; + + // 取得 30 天前的日期 + DateTime DaysAgo = today.AddDays(-day); + var dateValue = new List(); + // 使用 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.yuntech_in_car_table + .Where(c => c.in_time >= date_1.Date) // 只選擇指定日期的資料 + .Where(c => c.in_time <= date_2.Date) // 只選擇指定日期的資料 + .CountAsync(); // 計算該日期的次數 + + dateValue.Add(new { Date = date_1, Occurrences = dailyCount }); + } + + + + + return dateValue; + } + #endregion #region 新增資料