1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| public async Task<List<Tool>> GetToolsAsync(GetToolParams criteria) {
IQueryable<Tool> tools = this.dbContext.Tools
.Include(t => t.Type)
.Include(t => t.ToolStatus)
.Include(t => t.AssignedUsers)
.Include(t => t.Tag)
.Include(t => t.Inspections)
.Include(t => t.Groups)
.ThenInclude(g => g.Group)
.Include(t => t.Sites)
.ThenInclude(s => s.Site);
if (criteria.Latitude != null && criteria.Longitude != null && criteria.Distance != null)
{
SqlGeography point = SqlGeography.Point(criteria.Latitude.Value, criteria.Longitude.Value, 4326);
SqlGeography surrounding = point.BufferWithTolerance(criteria.Distance.Value, 0.01, true);
tools = tools.Where(t => t.CurrentPosition != null && (t.CurrentPosition.STIntersects(surrounding) ? true : false));
}
if (criteria.CompanyId.HasValue)
{
tools = tools.Where(t => t.CompanyId == criteria.CompanyId);
}
if (criteria.UserType != null && criteria.UserType.Equals("worker", StringComparison.InvariantCultureIgnoreCase))
{
// TODO refaire le filtre sans faire 2 fois le orderBy
// Utiliser un ForEach?
tools = tools.Where(t => t.ToolStatus.OrderByDescending(ts => ts.ValidationDate).FirstOrDefault().Status != (int)ToolStatus.Broken && t.ToolStatus.OrderByDescending(ts => ts.ValidationDate).FirstOrDefault().Status != (int)ToolStatus.OutOfStock);
}
if (criteria.AssignedUserId.HasValue)
{
tools = tools.Where(t => t.AssignedUsers.OrderByDescending(s => s.ValidationDate).FirstOrDefault().UserId == criteria.AssignedUserId);
}
if (criteria.SiteId.HasValue)
{
tools = tools.Where(
t => t.Sites
.OrderByDescending(ts => ts.ValidationDate)
.Any(ts => ts.IsCurrentAssignedSite && ts.SiteId == criteria.SiteId.Value));
}
if (criteria.HasTag.HasValue && criteria.HasTag.Value)
{
tools = tools.Where(t => t.Tag.Any(ta => ta.Enabled));
}
List<Tool> toolsList = await tools.ToListAsync();
foreach (Tool tool in toolsList)
{
if (tool.Sites != null)
{
tool.AssignedSite = tool.Sites.SingleOrDefault(s => s.SiteLocationType == SiteLocationType.Assigned)?.Site;
tool.CurrentSite = tool.Sites.SingleOrDefault(s => s.IsCurrentAssignedSite)?.Site;
}
if (tool.AssignedUsers != null && tool.AssignedUsers.Count > 0)
{
tool.CurrentAssignedUserId = tool.AssignedUsers.SingleOrDefault(x => x.IsCurrentAssignedUser)?.UserId;
}
}
return toolsList;
} |
Partager