Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.64 KB | None | 0 0
  1.         public IEnumerable<IncidentItem> GetIncidentCardListOptimised(int accountId, Employee employee, bool onlyOpenStatus = false, bool includeArchived = false)
  2.         {
  3.             ConcurrentBag<IncidentItem> resultBag = new ConcurrentBag<IncidentItem>();
  4.  
  5.             var ownerEmps = GetOwnersByObjectType(Enums.ObjectTypes.IncidentItem);
  6.             //var ownerEmps = imsContext.ItemOwners.Include(o => o.Employee).Where(a => a.AccountID == accountId && a.GlobalObjectType == (int)Enums.ObjectTypes.IncidentItem)
  7.             //                                     .Select(s => new { s.Employee.PhotoFilename, s.Employee.Username, s.Employee.EmployeeID, s.Employee.FirstName, s.Employee.Surname, s.GlobalObjectID, s.GlobalObjectType, s.ItemOwnerID })
  8.             //                                     .AsParallel().ToList();
  9.  
  10.             var imsDepartaments = GetItemDepartmentsByObjectType(ObjectTypes.IncidentItem);
  11.  
  12.             //var imsDepartaments = imsContext.ItemDepartments.Include(il => il.Department).Where(a => a.AccountID == accountId && a.GlobalObjectType == (int)Enums.ObjectTypes.IncidentItem)
  13.             //                                     .Select(s => new { s.Department.Title, s.Department.DepartmentID, s.Department.Ref, s.GlobalObjectID, s.GlobalObjectType, s.ItemDepartmentID, s.Department.DepartmentCategoryCSV }).AsParallel().ToList();
  14.  
  15.             var imsZones = GetItemZoneByObjectType(ObjectTypes.IncidentItem);
  16.             //var imsZones = imsContext.ItemZones.Include(il => il.Zone).Where(a => a.AccountID == accountId && a.GlobalObjectType == (int)Enums.ObjectTypes.IncidentItem)
  17.             //                                        .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();
  18.  
  19.             var imsCatgories = GetIncidentItemCategory();
  20.             //var imsCatgories = imsContext.IncidentItemCategories.Include(c => c.IncidentCategory)
  21.             //                                        .Select(i => new { i.IncidentItemCategoryID, i.IncidentCategory.IncidentCategoryID, i.IncidentItem.IncidentItemID, i.IncidentCategory.Title }).AsParallel().ToList();
  22.  
  23.  
  24.             var itemSubscribers = GetItemSubscribersByObjectType(ObjectTypes.IncidentItem, accountId);
  25.  
  26.             // var itemSubscribers = imsContext.ItemSubscribers.Include(c => c.Employee).Where(s => s.AccountID == accountId && s.GlobalObjectType == (int)Enums.ObjectTypes.IncidentItem).AsParallel().ToList();
  27.  
  28.             var imsDocuments = GetItemDocumentByObjectType(ObjectTypes.IncidentItem, accountId);
  29.             //var imsDocuments = imsContext.ItemDocuments.Include(d => d.Document).Where(d => d.AccountID == accountId && d.GlobalObjectType == (int)Enums.ObjectTypes.IncidentItem)
  30.             //                                       .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();
  31.  
  32.             var imsComments = GetIncidentComments(accountId);
  33.             //var imsComments = imsContext.IncidentHistories.Where(a => a.AccountID == accountId && a.UpdateType == (int)Enums.UpdateTypes.Incident_Comment_Added)
  34.             //                                                .Select(r => new IncidentHistory { IncidentItemID = r.IncidentItemID, NewValue = r.NewValue }).AsParallel().ToList();
  35.  
  36.             var imsHistories = GetIncidentHistory(accountId);
  37.             //var imsHistories = this.imsContext.IncidentHistories.Where(a => a.AccountID == accountId && a.UpdateType != (int)Enums.UpdateTypes.Incident_Comment_Added)
  38.             //                       .Select(h => new IncidentHistory { IncidentItemID = h.IncidentItemID, Created = h.Created }).AsParallel().ToList();
  39.  
  40.  
  41.  
  42.             var imsTags = GetItemTagsByObjectType(ObjectTypes.IncidentItem, accountId);
  43.             //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();
  44.  
  45.  
  46.             List<IncidentItem> imsIncItems = new List<IncidentItem>();
  47.             IEnumerable<IncidentItem> incidentItemsAll;
  48.  
  49.             //incidentItemsAll = imsContext.IncidentItems.Where(a => a.AccountID == accountId && !includeArchived ? a.Archived == false : true);
  50.             incidentItemsAll = GetAll().Where(a => a.AccountID == accountId).ToList();
  51.  
  52.             if (!includeArchived)
  53.                 incidentItemsAll = incidentItemsAll.Where(a => a.Archived == false);
  54.  
  55.             // Filter for employee
  56.             if (employee != null)
  57.             {
  58.                 incidentItemsAll = FilterForEmployee(incidentItemsAll, employee);
  59.             }
  60.  
  61.             if (onlyOpenStatus)
  62.             {
  63.                 incidentItemsAll = incidentItemsAll.Where(a => a.Status == (int)Enums.IncidentStatuses.Open);
  64.             }
  65.  
  66.             incidentItemsAll = incidentItemsAll.Select(r => new IncidentItem
  67.             {
  68.                 IncidentItemID = r.IncidentItemID,
  69.                 Title = r.Title,
  70.                 Severity = r.Severity,
  71.                 IncidentItemTypeID = r.IncidentItemTypeID,
  72.                 Created = r.Created,
  73.                 Status = r.Status,
  74.                 Longitude = r.Longitude,
  75.                 Latitude = r.Latitude,
  76.                 LocationDetails = r.LocationDetails,
  77.                 CreatedBy = r.CreatedBy,
  78.                 HeadlineStatus = r.HeadlineStatus,
  79.                 HeadlineStatusUpdated = r.HeadlineStatusUpdated,
  80.                 PrivacyStatus = r.PrivacyStatus,
  81.                 RefCode = r.RefCode,
  82.                 Description = r.Description,
  83.                 Updated = r.Updated,
  84.                 Archived = r.Archived
  85.             });
  86.  
  87.             imsIncItems = incidentItemsAll.OrderByDescending(a => a.Created).AsParallel().ToList();
  88.             var incIds = imsIncItems.Select(a => a.IncidentItemID).ToList();
  89.  
  90.             Parallel.ForEach(imsIncItems, (iItem) =>
  91.             {
  92.                 iItem.CreatedBy = incidentItemsAll.FirstOrDefault(i => i.IncidentItemID == iItem.IncidentItemID).CreatedBy;
  93.  
  94.                 var history = imsHistories.Where(h => h.IncidentItemID == iItem.IncidentItemID).OrderByDescending(h => h.Created).FirstOrDefault();
  95.  
  96.                 if (iItem.Updated == default(DateTime) && history != null)
  97.                 {
  98.                     iItem.Updated = history.Created;
  99.                 }
  100.  
  101.                 iItem.ItemOwners = ownerEmps.Where(a => a.GlobalObjectID == iItem.IncidentItemID && a.Employee != null)
  102.                 .Select(a => new Domain.Entities.ItemOwner
  103.                 {
  104.                     GlobalObjectID = a.GlobalObjectID,
  105.                     GlobalObjectType = a.GlobalObjectType,
  106.                     AccountID = accountId,
  107.                     ItemOwnerID = a.ItemOwnerID,
  108.                     Employee = new Employee
  109.                     {
  110.                         EmployeeID = a.EmployeeID,
  111.                         FirstName = a.Employee?.FirstName,
  112.                         Surname = a.Employee?.Surname,
  113.                         PhotoFileName = a.Employee?.PhotoFileName,
  114.                         Username = a.Employee?.Username
  115.                     }
  116.  
  117.                 }).ToList();
  118.  
  119.                 iItem.ItemZones = imsZones.Where(l => l.GlobalObjectID == iItem.IncidentItemID && l.Zone != null)
  120.                                                             .Select(r => new ItemZone
  121.                                                             {
  122.                                                                 AccountID = accountId,
  123.                                                                 GlobalObjectID = r.GlobalObjectID,
  124.                                                                 GlobalObjectType = r.GlobalObjectType,
  125.                                                                 Zone = new Domain.Entities.Zone
  126.                                                                 {
  127.                                                                     ZoneID = r.ZoneID,
  128.                                                                     AccountID = accountId,
  129.                                                                     Title = r.Zone.Title,
  130.                                                                     NorthEastLatitude = r.Zone.NorthEastLatitude,
  131.                                                                     NorthEastLongitude = r.Zone.NorthEastLongitude,
  132.                                                                     SouthWestLatitude = r.Zone.SouthWestLatitude,
  133.                                                                     SouthWestLongitude = r.Zone.SouthWestLongitude,
  134.                                                                     Primary = r.Primary
  135.                                                                 }
  136.                                                             }).ToList();
  137.  
  138.                 iItem.ItemDocuments = imsDocuments.Where(d => d.GlobalObjectID == iItem.IncidentItemID && d.Document != null)
  139.                     .Select(d => new ItemDocument
  140.                     {
  141.  
  142.                         AccountID = accountId,
  143.                         GlobalObjectID = d.GlobalObjectID,
  144.                         GlobalObjectType = d.GlobalObjectType,
  145.                         Document = new Document
  146.                         {
  147.                             DocumentID = d.DocumentID,
  148.                             AccountID = accountId,
  149.                             Title = d.Document.Title,
  150.                             Description = d.Document.Description,
  151.                             Filename = d.Document.Filename,
  152.                             PrivacyStatus = d.Document.PrivacyStatus,
  153.                             UploadType = d.Document.UploadType,
  154.                         }
  155.                     }).ToList();
  156.  
  157.                 iItem.ItemSubscribers = itemSubscribers.Where(d => d.GlobalObjectID == iItem.IncidentItemID).ToList();
  158.  
  159.                 iItem.CommentsCount = imsComments.Where(a => a.IncidentItemID == iItem.IncidentItemID).Count();
  160.  
  161.                 iItem.ItemDepartments = imsDepartaments.Where(d => d.GlobalObjectID == iItem.IncidentItemID && d.Department != null)
  162.                                                                 .Select(r => new ItemDepartment
  163.                                                                 {
  164.                                                                     AccountID = accountId,
  165.                                                                     GlobalObjectID = r.GlobalObjectID,
  166.                                                                     GlobalObjectType = r.GlobalObjectType,
  167.                                                                     ItemDepartmentID = r.ItemDepartmentID,
  168.                                                                     Department = new Department
  169.                                                                     {
  170.                                                                         DepartmentID = r.DepartmentID,
  171.                                                                         Title = r.Department.Title,
  172.                                                                         Ref = r.Department.Ref,
  173.                                                                         DepartmentCategoryCSV = r.Department.DepartmentCategoryCSV
  174.                                                                     }
  175.                                                                 }).ToList();
  176.  
  177.                 iItem.IncidentItemCategories = imsCatgories.Where(i => i.IncidentItemId == iItem.IncidentItemID && i.IncidentCategory != null)
  178.                                                                 .Select(r => new IncidentItemCategory
  179.                                                                 {
  180.                                                                     IncidentItemCategoryID = r.IncidentItemCategoryID,
  181.                                                                     IncidentCategory = new IncidentCategory
  182.                                                                     {
  183.                                                                         IncidentCategoryID = r.IncidentCategory.IncidentCategoryID,
  184.                                                                         Title = r.IncidentCategory.Title
  185.                                                                     },
  186.                                                                     IncidentCategoryId = r.IncidentCategoryId,
  187.                                                                     IncidentItemId = r.IncidentItemId
  188.                                                                 }).ToList();
  189.  
  190.                 if (iItem.ItemTags == null)
  191.                 {
  192.                     iItem.ItemTags = new List<ItemTag>();
  193.                 }
  194.  
  195.                 if (iItem.ItemComments == null)
  196.                 {
  197.                     iItem.ItemComments = new List<Comment>();
  198.                 }
  199.  
  200.                 var tags = imsTags.Where(a => a.GlobalObjectID == iItem.IncidentItemID).ToList();
  201.  
  202.                 if (tags.Count > 0)
  203.                 {
  204.                     if (iItem.Tags == null)
  205.                     {
  206.                         iItem.Tags = new List<Tag>();
  207.                     }
  208.  
  209.                     tags.ForEach(tgs =>
  210.                     {
  211.                         if (tgs.Tag != null)
  212.                         {
  213.                             iItem.ItemTags.Add(
  214.                              new ItemTag
  215.                              {
  216.                                  GlobalObjectID = iItem.IncidentItemID,
  217.                                  ItemTagID = tgs.ItemTagID,
  218.                                  Tag = new Tag
  219.                                  {
  220.                                      GlobalObjectID = iItem.IncidentItemID,
  221.                                      TagID = tgs.TagID,
  222.                                      Title = tgs.Tag.Title
  223.                                  }
  224.                              });
  225.                             iItem.Tags.Add(new Tag
  226.                             {
  227.                                 TagID = tgs.TagID,
  228.                                 Title = tgs.Tag.Title
  229.                             });
  230.                         }
  231.  
  232.                     });
  233.                 }
  234.  
  235.                 resultBag.Add(iItem);
  236.             });
  237.  
  238.             return resultBag.ToList();
  239.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement