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