Ejemplo n.º 1
0
        /// <summary>
        /// Save Heatmap Item scores and Risk Score
        /// </summary>
        /// <param name="clients"></param>
        private void SaveClientHeatMapScores(List <Client> clients)
        {
            DateTime today       = DateTime.Now.Date;
            bool     isDataExist = _m3PactContext.ClientHeatMapRisk.Where(x => x.M3dailyDate == today).Any();

            if (!isDataExist)
            {
                List <ClientHeatMapItemScore> heatMapItemsScoresToSave    = new List <ClientHeatMapItemScore>();
                List <ClientHeatMapRisk>      heatMapRiskToSave           = new List <ClientHeatMapRisk>();
                List <HeatMapItem>            metricKPIsInHeatMap         = GetM3MetricKPIsInHeatMap();
                IPendingChecklistRepository   _pendingChecklistRepository = new PendingChecklistRepository(_m3PactContext, _Configuration);
                if (metricKPIsInHeatMap != null && metricKPIsInHeatMap.Count > 0 && metricKPIsInHeatMap.Count == 5)
                {
                    var heatMapItemKpis = (from metricData in _m3PactContext.M3metricClientKpiDaily
                                           join metricHeatMapData in metricKPIsInHeatMap on metricData.KpiId equals metricHeatMapData.Kpiid
                                           orderby metricData.ClientId ascending
                                           select new
                    {
                        metricData,
                        metricHeatMapData
                    }).Where(x => x.metricData.InsertedDate == today).ToList();

                    foreach (Client client in clients)
                    {
                        int?m3DailyScore          = 0;
                        var clientHeatMapItemKpis = heatMapItemKpis.Where(x => x.metricData.ClientId == client.ClientId);
                        if (clientHeatMapItemKpis.Any())
                        {
                            foreach (var item in clientHeatMapItemKpis)
                            {
                                ClientHeatMapItemScore heatMapItemScore = new ClientHeatMapItemScore();
                                heatMapItemScore.Score           = item.metricData.IsDeviated ? DomainConstants.HeatMapScore : 0;
                                heatMapItemScore.ClientId        = item.metricData.ClientId;
                                heatMapItemScore.HeatMapItemId   = item.metricHeatMapData.HeatMapItemId;
                                heatMapItemScore.HeatMapItemDate = today;
                                heatMapItemScore.RecordStatus    = DomainConstants.RecordStatusActive;
                                heatMapItemScore.CreatedDate     = DateTime.Now;
                                heatMapItemScore.CreatedBy       = DomainConstants.Admin;
                                heatMapItemScore.ModifiedDate    = DateTime.Now;
                                heatMapItemScore.ModifiedBy      = DomainConstants.Admin;
                                heatMapItemsScoresToSave.Add(heatMapItemScore);

                                m3DailyScore += heatMapItemScore.Score;
                            }

                            ClientHeatMapRisk existingClientHeatMapRisk = new ClientHeatMapRisk();
                            ClientHeatMapRisk newClientHeatMapRisk      = new ClientHeatMapRisk();
                            existingClientHeatMapRisk = _m3PactContext.ClientHeatMapRisk.Where(c => c.ClientId == client.ClientId).OrderByDescending(c => c.ClientHeatMapRiskId)?.FirstOrDefault();
                            int?risk = 0;
                            if (existingClientHeatMapRisk != null)
                            {
                                newClientHeatMapRisk.ChecklistWeeklyDate  = existingClientHeatMapRisk.ChecklistWeeklyDate;
                                newClientHeatMapRisk.ChecklistMonthlyDate = existingClientHeatMapRisk.ChecklistMonthlyDate;
                                int existingWeeklyScore  = 0;
                                int existingMonthlyScore = 0;

                                if (existingClientHeatMapRisk.ChecklistWeeklyDate != null)
                                {
                                    existingWeeklyScore = _pendingChecklistRepository.GetScore(existingClientHeatMapRisk.ClientHeatMapRiskId, DomainConstants.WEEK, "save").Value;
                                }
                                if (existingClientHeatMapRisk.ChecklistMonthlyDate != null)
                                {
                                    existingMonthlyScore = _pendingChecklistRepository.GetScore(existingClientHeatMapRisk.ClientHeatMapRiskId, DomainConstants.MONTH, "Save").Value;
                                }
                                risk += existingWeeklyScore + existingMonthlyScore + m3DailyScore;
                            }
                            else
                            {
                                risk = m3DailyScore;
                            }
                            newClientHeatMapRisk.RiskScore     = risk;
                            newClientHeatMapRisk.M3dailyDate   = today;
                            newClientHeatMapRisk.ClientId      = client.ClientId;
                            newClientHeatMapRisk.CreatedBy     = DomainConstants.Admin;
                            newClientHeatMapRisk.ModifiedBy    = DomainConstants.Admin;
                            newClientHeatMapRisk.CreatedDate   = DateTime.Now;
                            newClientHeatMapRisk.ModifiedDate  = DateTime.Now;
                            newClientHeatMapRisk.EffectiveTime = DateTime.Now;
                            newClientHeatMapRisk.RecordStatus  = DomainConstants.RecordStatusActive;
                            heatMapRiskToSave.Add(newClientHeatMapRisk);
                        }
                    }
                    if (heatMapItemsScoresToSave != null && heatMapItemsScoresToSave.Count > 0)
                    {
                        _m3PactContext.ClientHeatMapItemScore.AddRange(heatMapItemsScoresToSave);
                        if (heatMapRiskToSave != null && heatMapRiskToSave.Count > 0)
                        {
                            _m3PactContext.ClientHeatMapRisk.AddRange(heatMapRiskToSave);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// To calculate riskscores for each client on changing heatmap items
        /// </summary>
        private void RecalculateRiskScores()
        {
            try
            {
                UserContext   userContext = UserHelper.getUserContext();
                List <Client> clientIds   = _m3PactContext.Client?.Where(c => c.IsActive == DomainConstants.RecordStatusActive).ToList();
                if (clientIds?.Count > 0)
                {
                    int weeklyChecklistTypeId  = _m3PactContext.CheckListType.Where(c => c.CheckListTypeCode == DomainConstants.WEEK && c.RecordStatus == DomainConstants.RecordStatusActive).FirstOrDefault().CheckListTypeId;
                    int monthlyChecklistTypeId = _m3PactContext.CheckListType.Where(c => c.CheckListTypeCode == DomainConstants.MONTH && c.RecordStatus == DomainConstants.RecordStatusActive).FirstOrDefault().CheckListTypeId;
                    int metricTypeId           = _m3PactContext.CheckListType.Where(c => c.CheckListTypeCode == DomainConstants.M3 && c.RecordStatus == DomainConstants.RecordStatusActive).FirstOrDefault().CheckListTypeId;

                    List <HeatMapItem> weeklyHeatMapItems  = _pendingChecklistRepository.GetHeatMapWithType(weeklyChecklistTypeId);
                    List <HeatMapItem> monthlyHeatMapItems = _pendingChecklistRepository.GetHeatMapWithType(monthlyChecklistTypeId);
                    List <HeatMapItem> metricHeatMapItems  = _pendingChecklistRepository.GetHeatMapWithType(metricTypeId);

                    foreach (Client client in clientIds)
                    {
                        ClientHeatMapRisk existingRiskScore = _m3PactContext.ClientHeatMapRisk.Where(c => c.ClientId == client.ClientId).OrderByDescending(c => c.ClientHeatMapRiskId)?.FirstOrDefault();

                        if (existingRiskScore != null)
                        {
                            //To Calculate Metric scores and insert into item table and update Riskscore table
                            if (existingRiskScore.M3dailyDate != null)
                            {
                                List <ClientHeatMapItemScore> metricHeatmapScores = _pendingChecklistRepository.GetHeatMapScoresToUpdate(existingRiskScore.M3dailyDate.Value, client.ClientId, metricTypeId);
                                List <HeatMapItem>            metricItemsToAdd    = metricHeatMapItems.Where(c => !metricHeatmapScores.Select(d => d.HeatMapItemId).Contains(c.HeatMapItemId))?.ToList();
                                if (metricItemsToAdd?.Count > 0)
                                {
                                    int existingMetricscore = _pendingChecklistRepository.GetScore(existingRiskScore.ClientHeatMapRiskId, DomainConstants.M3).Value;

                                    var heatMapItemKpisResponse = (from metricData in _m3PactContext.M3metricClientKpiDaily
                                                                   join metricHeatMapData in metricItemsToAdd on metricData.KpiId equals metricHeatMapData.Kpiid
                                                                   select new
                                    {
                                        metricData,
                                        metricHeatMapData
                                    }).Where(x => x.metricData.InsertedDate == existingRiskScore.M3dailyDate && x.metricData.ClientId == client.ClientId).ToList();

                                    List <ClientHeatMapItemScore> metricHeatMapItemsToSave = new List <ClientHeatMapItemScore>();
                                    if (heatMapItemKpisResponse.Any())
                                    {
                                        foreach (var item in heatMapItemKpisResponse)
                                        {
                                            ClientHeatMapItemScore heatMapItemScore = new ClientHeatMapItemScore();
                                            heatMapItemScore.Score           = item.metricData.IsDeviated ? DomainConstants.HeatMapScore : 0;
                                            heatMapItemScore.ClientId        = item.metricData.ClientId;
                                            heatMapItemScore.HeatMapItemId   = item.metricHeatMapData.HeatMapItemId;
                                            heatMapItemScore.HeatMapItemDate = existingRiskScore.M3dailyDate.Value;
                                            heatMapItemScore.RecordStatus    = DomainConstants.RecordStatusActive;
                                            heatMapItemScore.CreatedDate     = DateTime.Now;
                                            heatMapItemScore.CreatedBy       = DomainConstants.Admin;
                                            heatMapItemScore.ModifiedDate    = DateTime.Now;
                                            heatMapItemScore.ModifiedBy      = DomainConstants.Admin;
                                            metricHeatMapItemsToSave.Add(heatMapItemScore);
                                        }
                                        _m3PactContext.ClientHeatMapItemScore.AddRange(metricHeatMapItemsToSave);

                                        metricHeatmapScores = metricHeatmapScores.Where(c => c.HeatMapItem.StartTime <= DateTime.Now && c.HeatMapItem.EndTime >= DateTime.Now)?.ToList();
                                        metricHeatmapScores.AddRange(metricHeatMapItemsToSave);
                                        existingRiskScore.RiskScore = (existingRiskScore.RiskScore - existingMetricscore) + metricHeatmapScores.Sum(c => c.Score);
                                    }
                                }
                            }


                            //To Calculate Weekly changed scores and insert nto item table and update Riskscore table
                            if (existingRiskScore.ChecklistWeeklyDate != null)
                            {
                                List <ClientHeatMapItemScore> heatmapScores = _pendingChecklistRepository.GetHeatMapScoresToUpdate(existingRiskScore.ChecklistWeeklyDate.Value, client.ClientId, weeklyChecklistTypeId);
                                int existingWeeklyscore = _pendingChecklistRepository.GetScore(existingRiskScore.ClientHeatMapRiskId, DomainConstants.WEEK).Value;
                                if (weeklyHeatMapItems?.Count > 0)
                                {
                                    List <HeatMapItem> itemToAdd = weeklyHeatMapItems.Where(c => !heatmapScores.Select(d => d.HeatMapItemId).Contains(c.HeatMapItemId))?.ToList();
                                    if (itemToAdd?.Count > 0)
                                    {
                                        List <ClientChecklistResponseBusinessModel> submittedResponse = _pendingChecklistRepository.GetWeeklyPendingChecklistQuestions(client.ClientCode, existingRiskScore.ChecklistWeeklyDate.Value, DomainConstants.WEEK);

                                        List <ClientHeatMapItemScore> heatMapItemsToSave = _pendingChecklistRepository.MapHeatMapItemScores(client.ClientId, existingRiskScore.ChecklistWeeklyDate.Value, submittedResponse, itemToAdd);
                                        _m3PactContext.ClientHeatMapItemScore.AddRange(heatMapItemsToSave);

                                        heatmapScores = heatmapScores.Where(c => c.HeatMapItem.StartTime <= DateTime.Now && c.HeatMapItem.EndTime >= DateTime.Now)?.ToList();
                                        heatmapScores.AddRange(heatMapItemsToSave);
                                        existingRiskScore.RiskScore = (existingRiskScore.RiskScore - existingWeeklyscore) + heatmapScores.Sum(c => c.Score);
                                    }
                                    else
                                    {
                                        heatmapScores = heatmapScores.Where(c => c.HeatMapItem.StartTime <= DateTime.Now && c.HeatMapItem.EndTime >= DateTime.Now)?.ToList();
                                        existingRiskScore.RiskScore = (existingRiskScore.RiskScore - existingWeeklyscore) + heatmapScores.Sum(c => c.Score);
                                    }
                                }
                                else
                                {
                                    existingRiskScore.ChecklistWeeklyDate = null;
                                    heatmapScores = heatmapScores.Where(c => c.HeatMapItem.StartTime <= DateTime.Now && c.HeatMapItem.EndTime >= DateTime.Now)?.ToList();
                                    existingRiskScore.RiskScore = (existingRiskScore.RiskScore - existingWeeklyscore) + heatmapScores.Sum(c => c.Score);
                                }
                            }
                            else if (weeklyHeatMapItems?.Count > 0)
                            {
                                DateTime?checklistSubmittedDate = _pendingChecklistRepository.GetLastSubmittedChecklist(client.ClientId, weeklyChecklistTypeId);

                                if (checklistSubmittedDate != null && checklistSubmittedDate != default(DateTime))
                                {
                                    List <ClientChecklistResponseBusinessModel> submittedResponse = _pendingChecklistRepository.GetWeeklyPendingChecklistQuestions(client.ClientCode, checklistSubmittedDate.Value, DomainConstants.WEEK);

                                    List <ClientHeatMapItemScore> heatMapItemsToSave = _pendingChecklistRepository.MapHeatMapItemScores(client.ClientId, checklistSubmittedDate.Value, submittedResponse, weeklyHeatMapItems);

                                    if (heatMapItemsToSave?.Count > 0)
                                    {
                                        _m3PactContext.ClientHeatMapItemScore.AddRange(heatMapItemsToSave);
                                        existingRiskScore.RiskScore           = (existingRiskScore.RiskScore) + heatMapItemsToSave?.Sum(c => c.Score);
                                        existingRiskScore.ChecklistWeeklyDate = checklistSubmittedDate;
                                    }
                                }
                            }


                            // //To Calculate Weekly changed scores and insert into item table and update Riskscore table
                            if (existingRiskScore.ChecklistMonthlyDate != null)
                            {
                                List <ClientHeatMapItemScore> heatmapScores = _pendingChecklistRepository.GetHeatMapScoresToUpdate(existingRiskScore.ChecklistMonthlyDate.Value, client.ClientId, monthlyChecklistTypeId);
                                int existingMonthlyscore = _pendingChecklistRepository.GetScore(existingRiskScore.ClientHeatMapRiskId, DomainConstants.MONTH).Value;
                                if (monthlyHeatMapItems?.Count > 0)
                                {
                                    List <HeatMapItem> itemsToAdd = monthlyHeatMapItems.Where(c => !heatmapScores.Select(d => d.HeatMapItemId).Contains(c.HeatMapItemId))?.ToList();
                                    if (itemsToAdd?.Count > 0)
                                    {
                                        List <ClientChecklistResponseBusinessModel> submittedResponse = _pendingChecklistRepository.GetWeeklyPendingChecklistQuestions(client.ClientCode, existingRiskScore.ChecklistMonthlyDate.Value, DomainConstants.MONTH);

                                        List <ClientHeatMapItemScore> heatMapItemsToSave = _pendingChecklistRepository.MapHeatMapItemScores(client.ClientId, existingRiskScore.ChecklistMonthlyDate.Value, submittedResponse, itemsToAdd);
                                        _m3PactContext.ClientHeatMapItemScore.AddRange(heatMapItemsToSave);

                                        heatmapScores = heatmapScores.Where(c => c.HeatMapItem.StartTime <= DateTime.Now && c.HeatMapItem.EndTime >= DateTime.Now)?.ToList();
                                        heatmapScores.AddRange(heatMapItemsToSave);
                                        existingRiskScore.RiskScore = (existingRiskScore.RiskScore - existingMonthlyscore) + heatmapScores.Sum(c => c.Score);
                                    }
                                    else
                                    {
                                        heatmapScores = heatmapScores.Where(c => c.HeatMapItem.StartTime <= DateTime.Now && c.HeatMapItem.EndTime >= DateTime.Now)?.ToList();
                                        existingRiskScore.RiskScore = (existingRiskScore.RiskScore - existingMonthlyscore) + heatmapScores.Sum(c => c.Score);
                                    }
                                }
                                else
                                {
                                    existingRiskScore.ChecklistMonthlyDate = null;
                                    heatmapScores = heatmapScores.Where(c => c.HeatMapItem.StartTime <= DateTime.Now && c.HeatMapItem.EndTime >= DateTime.Now)?.ToList();//check
                                    existingRiskScore.RiskScore = (existingRiskScore.RiskScore - existingMonthlyscore) + heatmapScores.Sum(c => c.Score);
                                }
                            }
                            else if (monthlyHeatMapItems?.Count > 0)
                            {
                                DateTime?checklistSubmittedDate = _pendingChecklistRepository.GetLastSubmittedChecklist(client.ClientId, monthlyChecklistTypeId);

                                if (checklistSubmittedDate != null && checklistSubmittedDate != default(DateTime))
                                {
                                    List <ClientChecklistResponseBusinessModel> submittedResponse = _pendingChecklistRepository.GetWeeklyPendingChecklistQuestions(client.ClientCode, checklistSubmittedDate.Value, DomainConstants.MONTH);

                                    List <ClientHeatMapItemScore> heatMapItemsToSave = _pendingChecklistRepository.MapHeatMapItemScores(client.ClientId, checklistSubmittedDate.Value, submittedResponse, monthlyHeatMapItems);

                                    if (heatMapItemsToSave?.Count > 0)
                                    {
                                        _m3PactContext.ClientHeatMapItemScore.AddRange(heatMapItemsToSave);
                                        existingRiskScore.RiskScore            = (existingRiskScore.RiskScore) + heatMapItemsToSave?.Sum(c => c.Score);
                                        existingRiskScore.ChecklistMonthlyDate = checklistSubmittedDate;
                                    }
                                }
                            }

                            existingRiskScore.ModifiedBy    = userContext.UserId;
                            existingRiskScore.ModifiedDate  = DateTime.Now;
                            existingRiskScore.EffectiveTime = DateTime.Now;

                            _m3PactContext.ClientHeatMapRisk.Update(existingRiskScore);
                        }
                    }
                    _m3PactContext.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }