2023-02-18 20:12:36 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2023-02-22 16:21:48 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-02-18 20:12:36 +00:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-02-22 16:21:48 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-02-18 20:12:36 +00:00
|
|
|
|
using Pal.Client.Database;
|
|
|
|
|
|
2023-03-30 20:01:43 +00:00
|
|
|
|
namespace Pal.Client.Floors.Tasks;
|
|
|
|
|
|
|
|
|
|
internal sealed class MarkRemoteSeen : DbTask<MarkRemoteSeen>
|
2023-02-18 20:12:36 +00:00
|
|
|
|
{
|
2023-03-30 20:01:43 +00:00
|
|
|
|
private readonly MemoryTerritory _territory;
|
|
|
|
|
private readonly IReadOnlyList<PersistentLocation> _locations;
|
|
|
|
|
private readonly string _accountId;
|
|
|
|
|
|
|
|
|
|
public MarkRemoteSeen(IServiceScopeFactory serviceScopeFactory,
|
|
|
|
|
MemoryTerritory territory,
|
|
|
|
|
IReadOnlyList<PersistentLocation> locations,
|
|
|
|
|
string accountId)
|
|
|
|
|
: base(serviceScopeFactory)
|
2023-02-18 20:12:36 +00:00
|
|
|
|
{
|
2023-03-30 20:01:43 +00:00
|
|
|
|
_territory = territory;
|
|
|
|
|
_locations = locations;
|
|
|
|
|
_accountId = accountId;
|
|
|
|
|
}
|
2023-02-18 20:12:36 +00:00
|
|
|
|
|
2023-03-30 20:01:43 +00:00
|
|
|
|
protected override void Run(PalClientContext dbContext, ILogger<MarkRemoteSeen> logger)
|
|
|
|
|
{
|
|
|
|
|
lock (_territory.LockObj)
|
2023-02-18 20:12:36 +00:00
|
|
|
|
{
|
2023-03-30 20:01:43 +00:00
|
|
|
|
logger.LogInformation("Marking {Count} locations as seen remotely on {Account} in territory {Territory}",
|
|
|
|
|
_locations.Count, _accountId, _territory.TerritoryType);
|
2023-02-18 20:12:36 +00:00
|
|
|
|
|
2023-03-30 20:01:43 +00:00
|
|
|
|
List<int> locationIds = _locations.Select(x => x.LocalId).Where(x => x != null).Cast<int>().ToList();
|
|
|
|
|
List<ClientLocation> locationsToUpdate =
|
|
|
|
|
dbContext.Locations
|
|
|
|
|
.Include(x => x.RemoteEncounters)
|
|
|
|
|
.Where(x => locationIds.Contains(x.LocalId))
|
|
|
|
|
.ToList()
|
|
|
|
|
.Where(x => x.RemoteEncounters.All(encounter => encounter.AccountId != _accountId))
|
|
|
|
|
.ToList();
|
|
|
|
|
foreach (var clientLocation in locationsToUpdate)
|
2023-02-18 20:12:36 +00:00
|
|
|
|
{
|
2023-03-30 20:01:43 +00:00
|
|
|
|
clientLocation.RemoteEncounters.Add(new RemoteEncounter(clientLocation, _accountId));
|
2023-02-18 20:12:36 +00:00
|
|
|
|
}
|
2023-03-30 20:01:43 +00:00
|
|
|
|
|
|
|
|
|
dbContext.SaveChanges();
|
2023-02-18 20:12:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|