Second upload

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

View File

@ -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,40 +47,70 @@ 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)
{
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++;
}
else
{
monthlycar--;
}
monthlyNum = 0;
}
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
{
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);
temporarycar = Math.Abs(temporarycar);
@ -88,14 +118,12 @@ public class ParkingService : BackgroundService
_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