Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public IEnumerable<IncidentItem> GetIncidentCardListOptimised(int accountId, Employee employee, bool onlyOpenStatus = false, bool includeArchived = false)
- {
- ConcurrentBag<IncidentItem> resultBag = new ConcurrentBag<IncidentItem>();
- var ownerEmps = GetOwnersByObjectType(Enums.ObjectTypes.IncidentItem);
- //var ownerEmps = imsContext.ItemOwners.Include(o => o.Employee).Where(a => a.AccountID == accountId && a.GlobalObjectType == (int)Enums.ObjectTypes.IncidentItem)
- // .Select(s => new { s.Employee.PhotoFilename, s.Employee.Username, s.Employee.EmployeeID, s.Employee.FirstName, s.Employee.Surname, s.GlobalObjectID, s.GlobalObjectType, s.ItemOwnerID })
- // .AsParallel().ToList();
- var imsDepartaments = GetItemDepartmentsByObjectType(ObjectTypes.IncidentItem);
- //var imsDepartaments = imsContext.ItemDepartments.Include(il => il.Department).Where(a => a.AccountID == accountId && a.GlobalObjectType == (int)Enums.ObjectTypes.IncidentItem)
- // .Select(s => new { s.Department.Title, s.Department.DepartmentID, s.Department.Ref, s.GlobalObjectID, s.GlobalObjectType, s.ItemDepartmentID, s.Department.DepartmentCategoryCSV }).AsParallel().ToList();
- var imsZones = GetItemZoneByObjectType(ObjectTypes.IncidentItem);
- //var imsZones = imsContext.ItemZones.Include(il => il.Zone).Where(a => a.AccountID == accountId && a.GlobalObjectType == (int)Enums.ObjectTypes.IncidentItem)
- // .Select(r => new { r.Zone.ZoneID, r.Zone.Title, r.Zone.NorthEastLatitude, r.Zone.NorthEastLongitude, r.Zone.SouthWestLatitude, r.Zone.SouthWestLongitude, r.Zone.Primary, r.AccountID, r.GlobalObjectID, r.GlobalObjectType }).AsParallel().ToList();
- var imsCatgories = GetIncidentItemCategory();
- //var imsCatgories = imsContext.IncidentItemCategories.Include(c => c.IncidentCategory)
- // .Select(i => new { i.IncidentItemCategoryID, i.IncidentCategory.IncidentCategoryID, i.IncidentItem.IncidentItemID, i.IncidentCategory.Title }).AsParallel().ToList();
- var itemSubscribers = GetItemSubscribersByObjectType(ObjectTypes.IncidentItem, accountId);
- // var itemSubscribers = imsContext.ItemSubscribers.Include(c => c.Employee).Where(s => s.AccountID == accountId && s.GlobalObjectType == (int)Enums.ObjectTypes.IncidentItem).AsParallel().ToList();
- var imsDocuments = GetItemDocumentByObjectType(ObjectTypes.IncidentItem, accountId);
- //var imsDocuments = imsContext.ItemDocuments.Include(d => d.Document).Where(d => d.AccountID == accountId && d.GlobalObjectType == (int)Enums.ObjectTypes.IncidentItem)
- // .Select(r => new { r.GlobalObjectID, r.GlobalObjectType, r.Document.DocumentID, r.Document.DocumentCategoryID, r.Document.Filename, r.Document.AccountID, r.Document.Created, r.Document.CreatedBy, r.Document.Description, r.Document.Title, r.Document.UploadType, r.Document.PrivacyStatus }).AsParallel().ToList();
- var imsComments = GetIncidentComments(accountId);
- //var imsComments = imsContext.IncidentHistories.Where(a => a.AccountID == accountId && a.UpdateType == (int)Enums.UpdateTypes.Incident_Comment_Added)
- // .Select(r => new IncidentHistory { IncidentItemID = r.IncidentItemID, NewValue = r.NewValue }).AsParallel().ToList();
- var imsHistories = GetIncidentHistory(accountId);
- //var imsHistories = this.imsContext.IncidentHistories.Where(a => a.AccountID == accountId && a.UpdateType != (int)Enums.UpdateTypes.Incident_Comment_Added)
- // .Select(h => new IncidentHistory { IncidentItemID = h.IncidentItemID, Created = h.Created }).AsParallel().ToList();
- var imsTags = GetItemTagsByObjectType(ObjectTypes.IncidentItem, accountId);
- //imsContext.ItemTags.Include(a => a.Tag).Where(s => s.GlobalObjectType == (int)Enums.ObjectTypes.IncidentItem && incIds.Contains(s.GlobalObjectID)).Select(r => new { r.GlobalObjectID, r.Tag.TagID, r.Tag.Title, r.ItemTagID }).ToList();
- List<IncidentItem> imsIncItems = new List<IncidentItem>();
- IEnumerable<IncidentItem> incidentItemsAll;
- //incidentItemsAll = imsContext.IncidentItems.Where(a => a.AccountID == accountId && !includeArchived ? a.Archived == false : true);
- incidentItemsAll = GetAll().Where(a => a.AccountID == accountId).ToList();
- if (!includeArchived)
- incidentItemsAll = incidentItemsAll.Where(a => a.Archived == false);
- // Filter for employee
- if (employee != null)
- {
- incidentItemsAll = FilterForEmployee(incidentItemsAll, employee);
- }
- if (onlyOpenStatus)
- {
- incidentItemsAll = incidentItemsAll.Where(a => a.Status == (int)Enums.IncidentStatuses.Open);
- }
- incidentItemsAll = incidentItemsAll.Select(r => new IncidentItem
- {
- IncidentItemID = r.IncidentItemID,
- Title = r.Title,
- Severity = r.Severity,
- IncidentItemTypeID = r.IncidentItemTypeID,
- Created = r.Created,
- Status = r.Status,
- Longitude = r.Longitude,
- Latitude = r.Latitude,
- LocationDetails = r.LocationDetails,
- CreatedBy = r.CreatedBy,
- HeadlineStatus = r.HeadlineStatus,
- HeadlineStatusUpdated = r.HeadlineStatusUpdated,
- PrivacyStatus = r.PrivacyStatus,
- RefCode = r.RefCode,
- Description = r.Description,
- Updated = r.Updated,
- Archived = r.Archived
- });
- imsIncItems = incidentItemsAll.OrderByDescending(a => a.Created).AsParallel().ToList();
- var incIds = imsIncItems.Select(a => a.IncidentItemID).ToList();
- Parallel.ForEach(imsIncItems, (iItem) =>
- {
- iItem.CreatedBy = incidentItemsAll.FirstOrDefault(i => i.IncidentItemID == iItem.IncidentItemID).CreatedBy;
- var history = imsHistories.Where(h => h.IncidentItemID == iItem.IncidentItemID).OrderByDescending(h => h.Created).FirstOrDefault();
- if (iItem.Updated == default(DateTime) && history != null)
- {
- iItem.Updated = history.Created;
- }
- iItem.ItemOwners = ownerEmps.Where(a => a.GlobalObjectID == iItem.IncidentItemID && a.Employee != null)
- .Select(a => new Domain.Entities.ItemOwner
- {
- GlobalObjectID = a.GlobalObjectID,
- GlobalObjectType = a.GlobalObjectType,
- AccountID = accountId,
- ItemOwnerID = a.ItemOwnerID,
- Employee = new Employee
- {
- EmployeeID = a.EmployeeID,
- FirstName = a.Employee?.FirstName,
- Surname = a.Employee?.Surname,
- PhotoFileName = a.Employee?.PhotoFileName,
- Username = a.Employee?.Username
- }
- }).ToList();
- iItem.ItemZones = imsZones.Where(l => l.GlobalObjectID == iItem.IncidentItemID && l.Zone != null)
- .Select(r => new ItemZone
- {
- AccountID = accountId,
- GlobalObjectID = r.GlobalObjectID,
- GlobalObjectType = r.GlobalObjectType,
- Zone = new Domain.Entities.Zone
- {
- ZoneID = r.ZoneID,
- AccountID = accountId,
- Title = r.Zone.Title,
- NorthEastLatitude = r.Zone.NorthEastLatitude,
- NorthEastLongitude = r.Zone.NorthEastLongitude,
- SouthWestLatitude = r.Zone.SouthWestLatitude,
- SouthWestLongitude = r.Zone.SouthWestLongitude,
- Primary = r.Primary
- }
- }).ToList();
- iItem.ItemDocuments = imsDocuments.Where(d => d.GlobalObjectID == iItem.IncidentItemID && d.Document != null)
- .Select(d => new ItemDocument
- {
- AccountID = accountId,
- GlobalObjectID = d.GlobalObjectID,
- GlobalObjectType = d.GlobalObjectType,
- Document = new Document
- {
- DocumentID = d.DocumentID,
- AccountID = accountId,
- Title = d.Document.Title,
- Description = d.Document.Description,
- Filename = d.Document.Filename,
- PrivacyStatus = d.Document.PrivacyStatus,
- UploadType = d.Document.UploadType,
- }
- }).ToList();
- iItem.ItemSubscribers = itemSubscribers.Where(d => d.GlobalObjectID == iItem.IncidentItemID).ToList();
- iItem.CommentsCount = imsComments.Where(a => a.IncidentItemID == iItem.IncidentItemID).Count();
- iItem.ItemDepartments = imsDepartaments.Where(d => d.GlobalObjectID == iItem.IncidentItemID && d.Department != null)
- .Select(r => new ItemDepartment
- {
- AccountID = accountId,
- GlobalObjectID = r.GlobalObjectID,
- GlobalObjectType = r.GlobalObjectType,
- ItemDepartmentID = r.ItemDepartmentID,
- Department = new Department
- {
- DepartmentID = r.DepartmentID,
- Title = r.Department.Title,
- Ref = r.Department.Ref,
- DepartmentCategoryCSV = r.Department.DepartmentCategoryCSV
- }
- }).ToList();
- iItem.IncidentItemCategories = imsCatgories.Where(i => i.IncidentItemId == iItem.IncidentItemID && i.IncidentCategory != null)
- .Select(r => new IncidentItemCategory
- {
- IncidentItemCategoryID = r.IncidentItemCategoryID,
- IncidentCategory = new IncidentCategory
- {
- IncidentCategoryID = r.IncidentCategory.IncidentCategoryID,
- Title = r.IncidentCategory.Title
- },
- IncidentCategoryId = r.IncidentCategoryId,
- IncidentItemId = r.IncidentItemId
- }).ToList();
- if (iItem.ItemTags == null)
- {
- iItem.ItemTags = new List<ItemTag>();
- }
- if (iItem.ItemComments == null)
- {
- iItem.ItemComments = new List<Comment>();
- }
- var tags = imsTags.Where(a => a.GlobalObjectID == iItem.IncidentItemID).ToList();
- if (tags.Count > 0)
- {
- if (iItem.Tags == null)
- {
- iItem.Tags = new List<Tag>();
- }
- tags.ForEach(tgs =>
- {
- if (tgs.Tag != null)
- {
- iItem.ItemTags.Add(
- new ItemTag
- {
- GlobalObjectID = iItem.IncidentItemID,
- ItemTagID = tgs.ItemTagID,
- Tag = new Tag
- {
- GlobalObjectID = iItem.IncidentItemID,
- TagID = tgs.TagID,
- Title = tgs.Tag.Title
- }
- });
- iItem.Tags.Add(new Tag
- {
- TagID = tgs.TagID,
- Title = tgs.Tag.Title
- });
- }
- });
- }
- resultBag.Add(iItem);
- });
- return resultBag.ToList();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement