新增30天內紀錄次數查詢API

This commit is contained in:
leo 2024-02-08 23:44:13 +08:00
parent 8f881bf0cf
commit 51f4bafb9f
2 changed files with 69 additions and 1 deletions

View File

@ -179,7 +179,7 @@ namespace WebApi_data_value.Controllers
}
#endregion
#region
#region
/// <summary>
/// 尋找違規次數最多
/// </summary>
@ -204,6 +204,39 @@ namespace WebApi_data_value.Controllers
#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>
/// 新增違規車輛

View File

@ -155,6 +155,41 @@ namespace WebApi_data_value.Controllers
}
#endregion
#region
/// <summary>
/// 尋找規定時間內每日進入人數
/// </summary>
/// <returns></returns>
[HttpGet("get_day_in_car-{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.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