2024-02-04 19:20:00 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-03-05 19:49:58 +08:00
|
|
|
|
using Parking_space_WebAPI.Models;
|
|
|
|
|
using Parking_space_WebAPI.Services;
|
|
|
|
|
using Parking_space_WebAPI.ViewModel;
|
|
|
|
|
using Parking_space_WebAPI.Authorization;
|
2024-02-04 19:20:00 +08:00
|
|
|
|
|
2024-04-24 12:45:39 +08:00
|
|
|
|
|
2024-03-05 19:49:58 +08:00
|
|
|
|
namespace Parking_space_WebAPI.Controllers
|
2024-02-04 19:20:00 +08:00
|
|
|
|
{
|
2024-02-05 22:09:54 +08:00
|
|
|
|
|
2024-02-04 19:20:00 +08:00
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
[ApiController]
|
2024-02-09 16:44:45 +08:00
|
|
|
|
[Authorize]
|
2024-04-24 16:33:49 +08:00
|
|
|
|
[ApiExplorerSettings(GroupName = "校園大內網")]
|
2024-02-04 19:20:00 +08:00
|
|
|
|
public class Yuntech_in_car_tableController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly SqlContext _context;
|
|
|
|
|
|
|
|
|
|
public Yuntech_in_car_tableController(SqlContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
}
|
2024-02-05 22:09:54 +08:00
|
|
|
|
#region 獲取進入雲科的所有資料
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 獲取進入雲科的所有資料
|
|
|
|
|
/// </summary>
|
2024-02-04 19:20:00 +08:00
|
|
|
|
// GET: api/Yuntech_in_car_table
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<Yuntech_in_car_table>>> Getyuntech_in_car_table()
|
|
|
|
|
{
|
|
|
|
|
var in_car_table = await (from c in _context.yuntech_in_car_table
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return in_car_table;
|
|
|
|
|
}
|
2024-02-05 22:09:54 +08:00
|
|
|
|
#endregion
|
2024-02-04 19:20:00 +08:00
|
|
|
|
|
2024-02-19 11:24:08 +08:00
|
|
|
|
|
|
|
|
|
#region 獲取進入雲科的100筆資料
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 獲取進入雲科的100筆資料
|
|
|
|
|
/// </summary>
|
|
|
|
|
// GET: api/Yuntech_in_car_table
|
|
|
|
|
[HttpGet("Amount-{num}")]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<Yuntech_in_car_table>>> Getyuntech_in_car_100_table(int num)
|
|
|
|
|
{
|
|
|
|
|
var in_car_table = await (from c in _context.yuntech_in_car_table
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
).Skip(num).Take(100).ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return in_car_table;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2024-02-04 19:20:00 +08:00
|
|
|
|
#region 獲取進入的單獨地區的所有資料
|
2024-02-05 22:09:54 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 獲取進入的單獨地區的所有資料
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"> 哪個門口</param>
|
|
|
|
|
/// <returns></returns>
|
2024-02-04 19:20:00 +08:00
|
|
|
|
// GET: api/Yuntech_in_car_table/5
|
|
|
|
|
[HttpGet("location-{id}")]
|
|
|
|
|
public async Task<IEnumerable<Yuntech_in_car_table>> GetYuntech_in_car_table(string id)
|
|
|
|
|
{
|
|
|
|
|
var in_car_table = await (from c in _context.yuntech_in_car_table
|
2024-02-21 15:29:01 +08:00
|
|
|
|
where c.location == id
|
|
|
|
|
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,
|
2024-04-24 12:45:39 +08:00
|
|
|
|
}).Skip(0).Take(500).ToListAsync();
|
2024-02-04 19:20:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return in_car_table;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2024-02-05 22:09:54 +08:00
|
|
|
|
#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}")]
|
2024-02-21 15:29:01 +08:00
|
|
|
|
public async Task<IEnumerable<Yuntech_in_car_table>> GetYuntech_in_car_table_date(string id, DateTime start_time, DateTime end_time)
|
2024-02-05 22:09:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return in_car_table;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 透過車牌號碼搜尋所有資料
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 透過車牌號碼搜尋所有資料
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">車牌號碼 ex:ABC4321</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("license_plate_number-{id}")]
|
|
|
|
|
public async Task<IEnumerable<Yuntech_in_car_table>> Getlicense_plate_number(string id)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var in_car_table = await (from c in _context.yuntech_in_car_table
|
|
|
|
|
where c.license_plate_number == id
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return in_car_table;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 獲取進入的單獨地區的單筆資料
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 獲取進入的單獨地區的單筆資料
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">地區</param>
|
|
|
|
|
/// <param name="time">進入時間 ex:2024-02-05T12:21:48.395Z</param>
|
|
|
|
|
/// <returns></returns>
|
2024-02-04 19:20:00 +08:00
|
|
|
|
// GET: api/Yuntech_in_car_table/5
|
2024-02-05 22:09:54 +08:00
|
|
|
|
[HttpGet("location_name-{id}-time-{time}")]
|
2024-02-04 19:20:00 +08:00
|
|
|
|
public async Task<ActionResult<Yuntech_in_car_table>> One_data(string id, DateTime time)
|
|
|
|
|
{
|
|
|
|
|
var in_car_table = await (from c in _context.yuntech_in_car_table
|
|
|
|
|
where c.location == id
|
|
|
|
|
where c.in_time == time
|
2024-02-05 22:09:54 +08:00
|
|
|
|
select new Yuntech_in_car_table
|
|
|
|
|
{
|
2024-02-21 15:29:01 +08:00
|
|
|
|
license_plate_number = c.license_plate_number,
|
2024-02-05 22:09:54 +08:00
|
|
|
|
in_time = c.in_time,
|
|
|
|
|
out_time = c.out_time,
|
|
|
|
|
car_img = c.car_img,
|
|
|
|
|
location = c.location,
|
2024-04-30 16:07:28 +08:00
|
|
|
|
out_car_img = c.out_car_img
|
2024-02-05 22:09:54 +08:00
|
|
|
|
}).FirstAsync();
|
2024-02-04 19:20:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return in_car_table;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2024-02-08 23:44:13 +08:00
|
|
|
|
#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 迴圈逐日計算次數
|
2024-02-21 15:29:01 +08:00
|
|
|
|
for (int i = 1; i <= day; i++)
|
2024-02-08 23:44:13 +08:00
|
|
|
|
{
|
|
|
|
|
DateTime date_1 = DaysAgo;
|
|
|
|
|
date_1 = date_1.AddDays(i);
|
|
|
|
|
DateTime date_2 = DaysAgo;
|
2024-02-21 15:29:01 +08:00
|
|
|
|
date_2 = date_2.AddDays(i + 1);
|
2024-02-08 23:44:13 +08:00
|
|
|
|
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
|
2024-02-04 19:20:00 +08:00
|
|
|
|
|
2024-02-21 15:29:01 +08:00
|
|
|
|
#region 尋找所有進出校園的地點
|
|
|
|
|
///<summary>
|
|
|
|
|
///尋找所有進出校園的地點
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("location_name")]
|
|
|
|
|
public async Task<IEnumerable<object>> Get_in_Yuntech_location_name()
|
|
|
|
|
{
|
|
|
|
|
var location_name_table = await _context.yuntech_in_car_table
|
|
|
|
|
.GroupBy(c=>c.location)
|
|
|
|
|
.Select(group => new
|
|
|
|
|
{
|
|
|
|
|
location_name = group.Key,
|
|
|
|
|
})
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return location_name_table;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2024-02-04 19:20:00 +08:00
|
|
|
|
|
2024-02-05 22:09:54 +08:00
|
|
|
|
#region 新增資料
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 新增進入雲科的車輛資料
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="yuntech_in_car_table"></param>
|
|
|
|
|
/// <returns></returns>
|
2024-02-04 19:20:00 +08:00
|
|
|
|
// POST: api/Yuntech_in_car_table
|
|
|
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<ActionResult<Yuntech_in_car_table>> PostYuntech_in_car_table(Yuntech_in_car_table yuntech_in_car_table)
|
|
|
|
|
{
|
|
|
|
|
if (_context.yuntech_in_car_table == null)
|
|
|
|
|
{
|
|
|
|
|
return Problem("Entity set 'SqlContext.yuntech_in_car_table' is null.");
|
|
|
|
|
}
|
2024-04-30 14:53:22 +08:00
|
|
|
|
|
2024-02-04 19:20:00 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
yuntech_in_car_table.in_time = DateTime.Now;
|
|
|
|
|
yuntech_in_car_table.out_time = null;
|
2024-04-30 14:53:22 +08:00
|
|
|
|
_context.yuntech_in_car_table.Add(yuntech_in_car_table);
|
2024-02-04 19:20:00 +08:00
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (DbUpdateException)
|
|
|
|
|
{
|
|
|
|
|
if (Yuntech_in_car_tableExists(yuntech_in_car_table.in_time))
|
|
|
|
|
{
|
|
|
|
|
return Conflict();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CreatedAtAction("GetYuntech_in_car_table", new { id = yuntech_in_car_table.in_time }, yuntech_in_car_table);
|
|
|
|
|
}
|
2024-02-05 22:09:54 +08:00
|
|
|
|
#endregion
|
2024-02-04 19:20:00 +08:00
|
|
|
|
|
|
|
|
|
|
2024-04-30 14:53:22 +08:00
|
|
|
|
#region 新增出去資料
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 新增出去雲科的車輛資料
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="yuntech_in_car_table"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
// POST: api/Yuntech_in_car_table
|
|
|
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
|
|
|
[HttpPost("out_car")]
|
|
|
|
|
public async Task<ActionResult<Yuntech_in_car_table>> PostYuntech_out_car_table(Yuntech_in_car_table yuntech_out_car_table)
|
|
|
|
|
{
|
|
|
|
|
if (_context.yuntech_in_car_table == null)
|
|
|
|
|
{
|
|
|
|
|
return Problem("Entity set 'SqlContext.yuntech_in_car_table' is null.");
|
|
|
|
|
}
|
|
|
|
|
var license_plate_number = yuntech_out_car_table.license_plate_number;
|
|
|
|
|
var out_car_img = yuntech_out_car_table.out_car_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 = out_car_img;
|
|
|
|
|
_context.yuntech_in_car_table.Add(in_car_data);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// 处理未找到记录的情况
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 刪除資料
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 刪除指定資料
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="license_plate_number">車牌號碼</param>
|
|
|
|
|
/// <param name="time">資料新增時間</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpDelete("license_plate_number-{license_plate_number}-time-{time}")]
|
|
|
|
|
public async Task<IActionResult> Deletein_car_table(string license_plate_number, DateTime time)
|
|
|
|
|
{
|
|
|
|
|
var in_car_table = await (from c in _context.yuntech_in_car_table
|
|
|
|
|
where c.license_plate_number == license_plate_number
|
|
|
|
|
where c.in_time == time
|
|
|
|
|
|
|
|
|
|
select c).FirstAsync();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_context.yuntech_in_car_table.Remove(in_car_table);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2024-02-04 19:20:00 +08:00
|
|
|
|
private bool Yuntech_in_car_tableExists(DateTime? id)
|
|
|
|
|
{
|
|
|
|
|
return (_context.yuntech_in_car_table?.Any(e => e.in_time == id)).GetValueOrDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|