private List <GdcoTicket.Model.GdcoTicket> CreateGdcoTickets(List <AllEgsOutputModel> results, string mode)
        {
            var gdcoTickets = new List <GdcoTicket.Model.GdcoTicket>();
            HashSet <String> failedDescriptions = new HashSet <String>();

            if (results == null || !results.Any())
            {
                return(gdcoTickets);
            }

            // Check for failed tests, create tickets
            foreach (var result in results)
            {
                var errorTypeToExchangeResults = new Dictionary <string, List <AllEngineeringGroupsResult> >();
                var exchangeResults            = result.AllEgsOutput.Where(eo => eo.TestStatus.Equals("Failed") && !string.IsNullOrEmpty(eo.QcName));
                var mdmId = string.IsNullOrEmpty(result.MdmId) ? result.ProjectId : result.MdmId;

                KustoAccess ka = new KustoAccess();

                // compare the current ticket with CIS Kusto feed, if different, cut the ticket
                var kustoErrMsg = "";

                if (exchangeResults != null && exchangeResults.Any())
                {
                    kustoErrMsg = ka.GetErrorEsgByMdmid(mdmId);
                }

                foreach (var exchangeResult in exchangeResults)
                {
                    if (!errorTypeToExchangeResults.ContainsKey(exchangeResult.QcName))
                    {
                        errorTypeToExchangeResults[exchangeResult.QcName] = new List <AllEngineeringGroupsResult> {
                            exchangeResult
                        };
                    }
                    else
                    {
                        var exchangeResultList = errorTypeToExchangeResults[exchangeResult.QcName];
                        exchangeResultList.Add(exchangeResult);
                    }
                }

                if (!errorTypeToExchangeResults.Any())
                {
                    continue;
                }

                foreach (var key in errorTypeToExchangeResults.Keys)
                {
                    var    exchangeErrorList = errorTypeToExchangeResults[key];
                    var    projectId         = string.IsNullOrEmpty(result.ProjectId) ? result.MdmId : result.ProjectId;
                    var    currentWorkOrder  = exchangeErrorList.First().WorkOrderName;
                    var    errorCode         = key;
                    string errorTitle        = null;
                    var    errorDescription  = GetExoResultError(exchangeErrorList);
                    var    dcCode            = result.DataCenterCode;
                    var    requestOwner      = "oadpm";
                    string severity          = null;
                    string parentTicketId    = null;

                    // KustoErrMsg is empty means we couldn't found ticket in Kusto feed, we should email
                    if (String.IsNullOrEmpty(kustoErrMsg))
                    {
                        // send email if there is something diff between us and their Kusto feed
                        EmailBody.AppendLine("<tr><td>" + projectId + "</td><td>" + errorTitle + "</td><td>" + errorDescription + "</td></tr>");
                    }

                    if (result.GroupType.IndexOf("PreRack", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        continue;
                    }

                    var gdcoTicket = new GdcoTicketHandler().CreateTicket(projectId, currentWorkOrder,
                                                                          errorCode, errorTitle, errorDescription, dcCode, requestOwner, severity, parentTicketId, mode, "msassetsku");
                    gdcoTickets.Add(gdcoTicket);

                    try
                    {
                        if (mode.Equals("P"))
                        {
                            new GdcoTicketHandler().AssignParent(projectId, gdcoTicket.Id);
                        }
                        else if (mode.Equals("M"))
                        {
                            var cisProject = new CisAccess().GetCisProjectsFromCache().CisProjects.Where(c => c.MdmId.Equals(projectId)).FirstOrDefault();
                            if (cisProject != null && cisProject.UpdateAssetDataTicketId != null)
                            {
                                new GdcoTicketHandler().AssignParent(Convert.ToInt64(cisProject.UpdateAssetDataTicketId), gdcoTicket.Id);
                            }
                        }
                        else if (mode.Equals("D"))
                        {
                            var       dsDataSet       = new DSDataSetAccess().GetInProgressExchangeDeploymentsToRTEG();
                            DSDataSet updateAssetTask = null;
                            DSDataSet oaTask          = null;
                            if (dsDataSet.ContainsKey(projectId))
                            {
                                updateAssetTask = dsDataSet[projectId].Where(d => (d.GDCOFaultCode.Equals("124107") || d.GDCOFaultCode.Equals("124246") || d.GDCOFaultCode.Equals("124054"))).FirstOrDefault();
                                oaTask          = dsDataSet[projectId].Where(d => (d.GDCOFaultCode.Equals("124110"))).FirstOrDefault();
                            }
                            if (updateAssetTask != null && updateAssetTask.TicketState.Equals("Created") && updateAssetTask.TicketId != null)
                            {
                                new GdcoTicketHandler().AssignParent(Convert.ToInt64(updateAssetTask.TicketId), gdcoTicket.Id);
                            }
                            else if (oaTask != null && oaTask.TicketId != null)
                            {
                                new GdcoTicketHandler().AssignParent(Convert.ToInt64(oaTask.TicketId), gdcoTicket.Id);
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                    errorDescription = errorDescription.Length > 1000 ? errorDescription.Substring(0, 1000) : errorDescription;
                    failedDescriptions.Add(errorDescription);
                }
            }

            // Check for passed cases that have failed earlier and ticket is not updated. Update the tickets.
            var pids       = new List <string>();
            var failedPids = new List <string>();

            foreach (var result in results)
            {
                if (result.GroupType.IndexOf("PreRack", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    continue;
                }
                var failedExchangeResults = result.AllEgsOutput.Where(eo => eo.TestStatus.Equals("Failed") && !string.IsNullOrEmpty(eo.QcName));
                var pid = string.IsNullOrEmpty(result.ProjectId) ? result.MdmId : result.ProjectId;
                if (!failedExchangeResults.Any())
                {
                    pids.Add(pid);
                }
                else
                {
                    failedPids.Add(pid);
                }
            }
            if (pids.Any())
            {
                foreach (var pid in pids)
                {
                    gdcoTickets.AddRange(new GdcoTicketHandler().UpdateTicket(pid, "msassetsku"));
                }
            }
            if (failedPids.Any())
            {
                foreach (var pid in failedPids)
                {
                    gdcoTickets.AddRange(new GdcoTicketHandler().UpdateFailedTicket(pid, "msassetsku", failedDescriptions));
                }
            }
            return(gdcoTickets);
        }
        private List <GdcoTicket.Model.GdcoTicket> CreateGdcoTickets(List <AllEgsOutputModel> results, string mode)
        {
            var gdcoTickets = new List <GdcoTicket.Model.GdcoTicket>();
            HashSet <String> failedDescriptions = new HashSet <String>();
            KustoAccess      ka = new KustoAccess();

            if (results == null || !results.Any())
            {
                return(gdcoTickets);
            }

            // Check for failed tests, create tickets
            foreach (var result in results)
            {
                var errorTypeToAllEgsResults = new Dictionary <string, List <AllEngineeringGroupsResult> >();
                var allEgsResults            = result.AllEgsOutput.Where(eo => eo.TestStatus.Equals("Failed") && !string.IsNullOrEmpty(eo.QcName));
                var mdmId = string.IsNullOrEmpty(result.ProjectId) ? result.MdmId : result.ProjectId;
                // compare the current ticket with CIS Kusto feed, if different, cut the ticket
                var kustoErrMsg = "";

                if (allEgsResults != null && allEgsResults.Any())
                {
                    kustoErrMsg = ka.GetErrorEsgByMdmid(mdmId);
                }

                foreach (var allEgResult in allEgsResults)
                {
                    if (!errorTypeToAllEgsResults.ContainsKey(allEgResult.QcName))
                    {
                        errorTypeToAllEgsResults[allEgResult.QcName] = new List <AllEngineeringGroupsResult> {
                            allEgResult
                        };
                    }
                    else
                    {
                        var exchangeResultList = errorTypeToAllEgsResults[allEgResult.QcName];
                        exchangeResultList.Add(allEgResult);
                    }
                }

                if (!errorTypeToAllEgsResults.Any())
                {
                    continue;
                }

                foreach (var key in errorTypeToAllEgsResults.Keys)
                {
                    var    allEgsErrorList  = errorTypeToAllEgsResults[key];
                    var    projectId        = string.IsNullOrEmpty(result.ProjectId) ? result.MdmId : result.ProjectId;
                    var    currentWorkOrder = allEgsErrorList.First().WorkOrderName.Trim();
                    var    errorCode        = key;
                    string errorTitle       = null;
                    var    errorDescription = GetAllEgsResultError(allEgsErrorList);
                    var    dcCode           = result.DataCenterCode.Trim();
                    var    requestOwner     = "oadpm";
                    string severity         = null;
                    string parentTicketId   = null;

                    var gdcoTicket = new GdcoTicket.Model.GdcoTicket();
                    try
                    {
                        if (String.IsNullOrEmpty(kustoErrMsg))
                        {
                            // send email if there is something diff between us and their Kusto feed
                            EmailBody.AppendLine("<tr><td>" + projectId + "</td><td>" + errorTitle + "</td><td>" + errorDescription + "</td></tr>");
                        }


                        if (result.GroupType.IndexOf("PreRack", StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            continue;
                        }

                        // Leave cutting ticket part here and will move into above if later
                        if (ErrorCodeMapping.LiveErrorChanges.Contains(errorCode))
                        {
                            var internalErrorCode = mode.Equals("P") ? ErrorCodeMapping.InternalGfsdErrorToInternalFc[key] : ErrorCodeMapping.InternalCisErrorToInternalFc[key];
                            errorTitle       = ErrorCodeMapping.InternalFcToGdcoTicketTitle.Keys.Contains(internalErrorCode) ? ErrorCodeMapping.InternalFcToGdcoTicketTitle[internalErrorCode] : "";
                            errorDescription = GetAllEgsNewResultError(allEgsErrorList, internalErrorCode);
                            gdcoTicket       = new GdcoTicketHandler().CreateErrorTicket(projectId, currentWorkOrder,
                                                                                         internalErrorCode, errorTitle, errorDescription, dcCode, requestOwner, severity, parentTicketId, mode, "allegs_msassetsku");
                        }
                        else
                        {
                            gdcoTicket = new GdcoTicketHandler().CreateTicket(projectId, currentWorkOrder,
                                                                              errorCode, errorTitle, errorDescription, dcCode, requestOwner, severity, parentTicketId, mode, "allegs_msassetsku");
                        }
                    }
                    catch (Exception ex)
                    {
                        SendEmail.SendExoSkuMsAssetReportEmail(Constants.serviceAccountAlias, Constants.automationTeam, "Exception in CreateTicket(), Ticket Creation failed for ", "ProjectId: " + projectId + " Exception in CreateTicket(): " + ex);
                        continue;
                    }
                    errorDescription = errorDescription.Length > 1000 ? errorDescription.Substring(0, 1000) : errorDescription;
                    failedDescriptions.Add(errorDescription);
                    gdcoTickets.Add(gdcoTicket);
                    try
                    {
                        if (mode.Equals("P"))
                        {
                            new GdcoTicketHandler().AssignParent(projectId, gdcoTicket.Id);
                        }
                        else if (mode.Equals("M"))
                        {
                            var cisProject = cisProjectModel.CisProjects.Where(c => c.MdmId.Equals(projectId)).FirstOrDefault();
                            if (cisProject != null && cisProject.UpdateAssetDataTicketId != null)
                            {
                                new GdcoTicketHandler().AssignParent(Convert.ToInt64(cisProject.UpdateAssetDataTicketId), gdcoTicket.Id);
                            }
                        }
                        else if (mode.Equals("D"))
                        {
                            var       dsDataSet       = new DSDataSetAccess().GetInProgressAllEGsDeploymentsToRTEG();
                            DSDataSet updateAssetTask = null;
                            DSDataSet oaTask          = null;
                            if (dsDataSet.ContainsKey(projectId))
                            {
                                updateAssetTask = dsDataSet[projectId].Where(d => (d.GDCOFaultCode.Equals("124107") || d.GDCOFaultCode.Equals("124246") || d.GDCOFaultCode.Equals("124054"))).FirstOrDefault();
                                oaTask          = dsDataSet[projectId].Where(d => (d.GDCOFaultCode.Equals("124110"))).FirstOrDefault();
                            }
                            if (updateAssetTask != null && updateAssetTask.TicketState.Equals("Created") && updateAssetTask.TicketId != null)
                            {
                                new GdcoTicketHandler().AssignParent(Convert.ToInt64(updateAssetTask.TicketId), gdcoTicket.Id);
                            }
                            else if (oaTask != null && oaTask.TicketId != null)
                            {
                                new GdcoTicketHandler().AssignParent(Convert.ToInt64(oaTask.TicketId), gdcoTicket.Id);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        SendEmail.SendExoSkuMsAssetReportEmail(Constants.serviceAccountAlias, Constants.automationTeam, "Exception in CreateGdcoTickets()", "ProjectId: " + projectId + " Exception in CreateGdcoTickets(): " + ex);
                        continue;
                    }
                }
            }

            // Check for passed cases that have failed earlier and ticket is not updated. Update the tickets.
            var pids       = new List <string>();
            var failedPids = new List <string>();

            foreach (var result in results)
            {
                if (result.GroupType.IndexOf("PreRack", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    continue;
                }
                var failedAllEgsResults = result.AllEgsOutput.Where(eo => eo.TestStatus.Equals("Failed") && !string.IsNullOrEmpty(eo.QcName));
                var pid = string.IsNullOrEmpty(result.ProjectId) ? result.MdmId : result.ProjectId;

                if (!failedAllEgsResults.Any())
                {
                    pids.Add(pid);
                }
                else
                {
                    failedPids.Add(pid);
                }
            }
            if (pids.Any())
            {
                foreach (var pid in pids)
                {
                    gdcoTickets.AddRange(new GdcoTicketHandler().UpdateTicket(pid, "allegs_msassetsku"));
                }
            }
            if (failedPids.Any())
            {
                foreach (var pid in failedPids)
                {
                    gdcoTickets.AddRange(new GdcoTicketHandler().UpdateFailedTicket(pid, "allegs_msassetsku", failedDescriptions));
                }
            }
            return(gdcoTickets);
        }