Second upload

This commit is contained in:
qi_0527 2024-10-25 23:54:13 +08:00
parent 3d5daadbbe
commit fb98b9afae

View File

@ -27,7 +27,7 @@ public class ParkingService : BackgroundService
await PerformParkingCheckAsync(stoppingToken);
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(TimeSpan.FromSeconds(50), stoppingToken); //每50秒執行
await Task.Delay(TimeSpan.FromSeconds(50), stoppingToken); // 每50秒執行
await PerformParkingCheckAsync(stoppingToken);
}
}
@ -39,7 +39,7 @@ public class ParkingService : BackgroundService
var _context = scope.ServiceProvider.GetRequiredService<SqlContext>();
var now = DateTime.Now;
var startTime = now.AddHours(-15); // 獲取最近 15 小時進入的車輛
var startTime = now.AddDays(-2); // 獲取最近兩天內進入的車輛
var inCars = await _context.yuntech_in_car_table
.Where(car => car.in_time >= startTime)
.ToListAsync(stoppingToken);
@ -47,55 +47,83 @@ public class ParkingService : BackgroundService
// 獲取月租名單
var monthlyUsers = await _context.yuntech_parking_user_list.ToListAsync(stoppingToken);
var monthlycar = 0;
var temporarycar = 0;
int monthlycar = 0;
int temporarycar = 0;
foreach (var car in inCars)
{
var matchedUsers = monthlyUsers
.Where(user => user.user_license_plate_number == car.license_plate_number)
.ToList();
bool isMonthlyUser = monthlyUsers.Any(user => user.user_license_plate_number == car.license_plate_number);
if (matchedUsers.Count > 0)
if (isMonthlyUser)
{
if (matchedUsers.Count % 2 == 1)
int monthlyNum = 0;
if (car.in_time.HasValue && !car.out_time.HasValue)
{
monthlycar++;
if ((now - car.in_time.Value).TotalHours > 15)
{
monthlyNum = 0;
}
else
{
int countWithin15Hours = inCars.Count(c => c.license_plate_number == car.license_plate_number
&& c.in_time.HasValue
&& (now - c.in_time.Value).TotalHours <= 15);
monthlyNum = countWithin15Hours % 2 == 1 ? 1 : 0;
}
}
else
else if (car.out_time.HasValue)
{
monthlycar--;
monthlyNum = 0;
}
monthlycar += monthlyNum;
}
else
else // 臨停車輛
{
if (temporarycar % 2 == 1)
int temporaryNum = 0;
if (car.in_time.HasValue && !car.out_time.HasValue)
{
temporarycar++;
if ((now - car.in_time.Value).TotalHours > 15)
{
temporaryNum = 0;
}
else
{
int countWithin15Hours = inCars.Count(c => c.license_plate_number == car.license_plate_number
&& c.in_time.HasValue
&& (now - c.in_time.Value).TotalHours <= 15);
temporaryNum = countWithin15Hours % 2 == 1 ? 1 : 0;
}
}
else
else if (car.out_time.HasValue)
{
temporarycar--;
temporaryNum = 0;
}
temporarycar += temporaryNum;
}
}
//絕對值
// 絕對值
monthlycar = Math.Abs(monthlycar);
temporarycar = Math.Abs(temporarycar);
temporarycar = Math.Abs(temporarycar);
_logger.LogInformation($"月租車位數量: {monthlycar}");
_logger.LogInformation($"臨停車位數量: {temporarycar}");
await UpdateMonthly(_context, monthlycar, temporarycar, stoppingToken); //更新月租跟臨停
await UpdateMonthly(_context, monthlycar, temporarycar, stoppingToken);
}
}
private async Task UpdateMonthly(SqlContext context, int monthlycar, int temporarycar, CancellationToken stoppingToken)
{
var monthlyRentData = await context.yuntech_monthly_rent_number
.FirstOrDefaultAsync(rent => rent.category == "月租", stoppingToken);
@ -103,6 +131,7 @@ public class ParkingService : BackgroundService
{
monthlyRentData.number = monthlycar.ToString();
}
var temporaryRentData = await context.yuntech_monthly_rent_number
.FirstOrDefaultAsync(rent => rent.category == "臨停", stoppingToken);
@ -115,6 +144,8 @@ public class ParkingService : BackgroundService
}
}
#endregion
#region ParkingLogs