新增city parking的API

This commit is contained in:
威勝 張 2024-05-20 21:52:12 +08:00
parent c42eb41650
commit 8fdacf40eb

View File

@ -325,6 +325,88 @@ namespace Parking_space_WebAPI.Controllers
#endregion #endregion
#region City_parking
/// <summary>
/// City_parking 新增進入資料
/// </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("city_parking_in_car_data")]
public async Task<ActionResult<Yuntech_in_car_table>> PostYuntech_in_car_table_for__city_parking(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.");
}
try
{
//比對1分鐘內有無重複記錄到
DateTime now_time = DateTime.Now;
// 取得 59秒前的時間
DateTime time_59s_ago = now_time.AddSeconds(-59);
var in_car_data = await (from c in _context.yuntech_in_car_table
where c.license_plate_number == yuntech_in_car_table.license_plate_number
where c.out_time == null
orderby c.in_time descending
select c).FirstOrDefaultAsync();
if (in_car_data != null && in_car_data.in_time > time_59s_ago)
{
return Ok();
}
//比對30秒內出去與進入的時間對比
//取得30秒前的時間
DateTime time_30s_ago = now_time.AddSeconds(-30);
var out_car_data = await (from c in _context.yuntech_in_car_table
where c.license_plate_number == yuntech_in_car_table.license_plate_number
orderby c.out_time descending
select c).FirstOrDefaultAsync();
if (out_car_data != null && out_car_data.out_time > time_30s_ago)
{
return Ok(out_car_data);
}
// 新增資料
yuntech_in_car_table.in_time = DateTime.Now;
yuntech_in_car_table.out_time = null;
_context.yuntech_in_car_table.Add(yuntech_in_car_table);
await _context.SaveChangesAsync();
// 車位數量-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 = now_num_int - 1;
now_num_str = now_num_int.ToString();
yuntech_parking.now_num = now_num_str;
// 保存更改
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);
}
#endregion
#region #region
/// <summary> /// <summary>
/// 新增出去雲科的車輛資料 /// 新增出去雲科的車輛資料
@ -388,6 +470,70 @@ namespace Parking_space_WebAPI.Controllers
} }
#endregion #endregion
#region City_parking
/// <summary>
/// City_parking 新增出去資料
/// </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("city_parking_out_car_data")]
public async Task<ActionResult<Yuntech_in_car_table>> PostYuntech_out_car_table_for_city_parking(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();
DateTime now_time = DateTime.Now;
// 取得 30秒前的時間
DateTime time_30s_ago = now_time.AddSeconds(-30);
if (in_car_data != null && in_car_data.in_time < time_30s_ago)
{
// 处理找到记录的情况
_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();
// 車位數量+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 = now_num_int + 1;
now_num_str = now_num_int.ToString();
yuntech_parking.now_num = now_num_str;
// 保存更改
await _context.SaveChangesAsync();
}
}
else
{
// 处理未找到记录的情况
await _context.SaveChangesAsync();
}
return Ok();
}
#endregion
#region #region
/// <summary> /// <summary>
/// 刪除指定資料 /// 刪除指定資料