Ejemplo n.º 1
0
 /// <summary>
 /// To get the Weeklist Questions both monthly and weekly
 /// </summary>
 /// <param name="clientCode"></param>
 /// <param name="pendingChecklistDate"></param>
 /// <param name="checklistType"></param>
 /// <returns></returns>
 public List <ClientChecklistResponseViewModel> GetWeeklyPendingChecklistQuestions(string clientCode, DateTime pendingChecklistDate, string checklistType)
 {
     try
     {
         List <ClientChecklistResponseViewModel>     clientChecklistResponseViewModels = new List <ClientChecklistResponseViewModel>();
         List <ClientChecklistResponseBusinessModel> clientChecklistResponseBusiness   = _pendingChecklistRepository.GetWeeklyPendingChecklistQuestions(clientCode, pendingChecklistDate, checklistType);
         if (clientChecklistResponseBusiness != null && clientChecklistResponseBusiness.Count > 0)
         {
             foreach (ClientChecklistResponseBusinessModel crb in clientChecklistResponseBusiness)
             {
                 ClientChecklistResponseViewModel cvm = new ClientChecklistResponseViewModel();
                 cvm.ActualFreeForm          = crb.ActualFreeForm;
                 cvm.QuestionCode            = crb.QuestionCode;
                 cvm.ActualResponse          = crb.ActualResponse;
                 cvm.CheckListAttributeMapID = crb.CheckListAttributeMapID;
                 cvm.ChecklistName           = crb.ChecklistName;
                 cvm.ClientCheckListMapID    = crb.ClientCheckListMapID;
                 cvm.ExpectedRespone         = crb.ExpectedRespone;
                 cvm.IsKPI           = crb.IsKPI;
                 cvm.Questionid      = crb.Questionid;
                 cvm.QuestionText    = crb.QuestionText;
                 cvm.RequireFreeform = crb.RequireFreeform;
                 clientChecklistResponseViewModels.Add(cvm);
             }
         }
         return(clientChecklistResponseViewModels);
     }
     catch (Exception ex)
     {
         _logger.Log(ex, LogLevel.Error, ex.Message);
         return(null);
     }
 }
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;
            }
        }