Second upload
This commit is contained in:
parent
a0102de0b0
commit
ae9fad7e14
|
@ -19,11 +19,15 @@ builder.Services.AddCors();
|
|||
builder.Services.AddControllers();
|
||||
|
||||
//執行每日存取剩餘車位
|
||||
builder.Services.AddHostedService<ParkingService>();// 更新月租車位
|
||||
builder.Services.AddScoped<ExcelGenerationService>(); // ExcelGenerationService 下載Excel
|
||||
builder.Services.AddHostedService<DailyExcelGenerationService>(); // 每天設定ExcelGenerationService 存取剩餘車位
|
||||
builder.Services.AddHostedService<ParkingLogCleanupService>(); //執行定期清除3個月前剩餘車位
|
||||
builder.Services.AddHostedService<ParkingUpdateService>(); //執行每一分鐘更新剩餘車位
|
||||
|
||||
builder.Logging.SetMinimumLevel(LogLevel.Information); // 設置日誌
|
||||
|
||||
|
||||
// 連線PostgreSQL資料庫
|
||||
var connectionString = "Server=140.125.20.183;UserID=postgres;password=EL404el404;Database=postgres;port=5432;Search Path=public;CommandTimeout=1800";
|
||||
//var connectionString = "Server=127.0.0.1;UserID=postgres;password=EL404el404;Database=postgres;port=5432;Search Path=public;CommandTimeout=1800";
|
||||
|
|
|
@ -6,8 +6,106 @@ using System.Globalization;
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Parking_space_WebAPI.Services;
|
||||
using WebApi_data_value.Models;
|
||||
// 執行計算剩餘車位並不為負數存入資料庫ParkingLogs
|
||||
using WebApi_data_value.Models;
|
||||
using Microsoft.Extensions.Logging; // 確保引用
|
||||
using System.Linq;
|
||||
|
||||
#region 從進入車輛獲取最新的月租名單
|
||||
public class ParkingService : BackgroundService
|
||||
{
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly ILogger<ParkingService> _logger;
|
||||
|
||||
public ParkingService(IServiceScopeFactory scopeFactory, ILogger<ParkingService> logger)
|
||||
{
|
||||
_scopeFactory = scopeFactory;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await PerformParkingCheckAsync(stoppingToken);
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(50), stoppingToken); //每50秒執行
|
||||
await PerformParkingCheckAsync(stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PerformParkingCheckAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
using (var scope = _scopeFactory.CreateScope())
|
||||
{
|
||||
var _context = scope.ServiceProvider.GetRequiredService<SqlContext>();
|
||||
|
||||
var now = DateTime.Now;
|
||||
var startTime = now.AddHours(-12); // 獲取最近 12 小時進入的車輛
|
||||
var inCars = await _context.yuntech_in_car_table
|
||||
.Where(car => car.in_time >= startTime)
|
||||
.ToListAsync(stoppingToken);
|
||||
|
||||
// 獲取月租名單
|
||||
var monthlyUsers = await _context.yuntech_parking_user_list.ToListAsync(stoppingToken);
|
||||
|
||||
var monthlycar = 0;
|
||||
var temporarycar = 0;
|
||||
|
||||
foreach (var car in inCars)
|
||||
{
|
||||
var matchedUsers = monthlyUsers
|
||||
.Where(user => user.user_license_plate_number == car.license_plate_number)
|
||||
.ToList();
|
||||
|
||||
if (matchedUsers.Count > 0)
|
||||
{
|
||||
|
||||
if (matchedUsers.Count % 2 == 1)
|
||||
{
|
||||
monthlycar++;
|
||||
}
|
||||
else
|
||||
{
|
||||
monthlycar--;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
temporarycar++;
|
||||
}
|
||||
}
|
||||
_logger.LogInformation($"月租車位數量: {monthlycar}");
|
||||
_logger.LogInformation($"臨停車位數量: {temporarycar}");
|
||||
|
||||
|
||||
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);
|
||||
|
||||
if (monthlyRentData != null)
|
||||
{
|
||||
monthlyRentData.number = monthlycar.ToString();
|
||||
}
|
||||
var temporaryRentData = await context.yuntech_monthly_rent_number
|
||||
.FirstOrDefaultAsync(rent => rent.category == "臨停", stoppingToken);
|
||||
|
||||
if (temporaryRentData != null)
|
||||
{
|
||||
temporaryRentData.number = temporarycar.ToString();
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync(stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region 執行計算剩餘車位並不為負數存入資料庫ParkingLogs
|
||||
|
||||
public class ParkingUpdateService : BackgroundService
|
||||
{
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
|
@ -30,10 +128,10 @@ public class ParkingUpdateService : BackgroundService
|
|||
var yuntech_parking = await _context.yuntech_parking.FirstOrDefaultAsync();
|
||||
if (yuntech_parking != null)
|
||||
{
|
||||
|
||||
|
||||
if (Int32.TryParse(yuntech_parking.all_num, out int totalParkingSpaces))
|
||||
{
|
||||
|
||||
|
||||
var monthly_rent_data = await _context.yuntech_monthly_rent_number
|
||||
.FirstOrDefaultAsync(rent => rent.category == "月租");
|
||||
int monthlyRentNumber = 0;
|
||||
|
@ -42,7 +140,7 @@ public class ParkingUpdateService : BackgroundService
|
|||
Int32.TryParse(monthly_rent_data.number, out monthlyRentNumber);
|
||||
}
|
||||
|
||||
|
||||
|
||||
var temporary_rent_data = await _context.yuntech_monthly_rent_number
|
||||
.FirstOrDefaultAsync(rent => rent.category == "臨停");
|
||||
int temporaryRentNumber = 0;
|
||||
|
@ -51,17 +149,17 @@ public class ParkingUpdateService : BackgroundService
|
|||
Int32.TryParse(temporary_rent_data.number, out temporaryRentNumber);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int totalOccupiedSpaces = monthlyRentNumber + Math.Abs(temporaryRentNumber); //確保絕對值
|
||||
int remainingSpaces = totalParkingSpaces - totalOccupiedSpaces;
|
||||
|
||||
// 確保剩餘車位數不為負數
|
||||
yuntech_parking.now_num = Math.Max(remainingSpaces, 0).ToString();
|
||||
|
||||
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
|
||||
|
||||
var log = new ParkingLog
|
||||
{
|
||||
Timestamp = DateTime.Now,
|
||||
|
@ -90,3 +188,4 @@ public class ParkingUpdateService : BackgroundService
|
|||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
Loading…
Reference in New Issue
Block a user