예제 #1
0
 public async Task <OperationDataResult <Paged <PlanningPnModel> > > Index([FromBody] PlanningsRequestModel requestModel)
 {
     return(await _planningService.Index(requestModel));
 }
        public async Task <OperationDataResult <Paged <PlanningPnModel> > > Index(PlanningsRequestModel pnRequestModel)
        {
            try
            {
                var sdkCore =
                    await _coreService.GetCore();

                await using var sdkDbContext = sdkCore.DbContextHelper.GetDbContext();

                var planningsQuery = _dbContext.Plannings
                                     .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                                     .AsQueryable();

                if (!string.IsNullOrEmpty(pnRequestModel.NameFilter))
                {
                    planningsQuery = planningsQuery.Where(x =>
                                                          x.NameTranslations.Any(y => y.Name.Contains(pnRequestModel.NameFilter)));
                }

                if (!string.IsNullOrEmpty(pnRequestModel.DescriptionFilter))
                {
                    planningsQuery = planningsQuery.Where(x =>
                                                          x.Description.Contains(pnRequestModel.DescriptionFilter));
                }

                var excludeSort = new List <string> {
                    "TranslatedName"
                };
                // sort
                planningsQuery = QueryHelper.AddSortToQuery(planningsQuery, pnRequestModel.Sort,
                                                            pnRequestModel.IsSortDsc, excludeSort);

                // ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator
                foreach (var tagId in pnRequestModel.TagIds)
                {
                    planningsQuery = planningsQuery.Where(x => x.PlanningsTags.Any(y =>
                                                                                   y.PlanningTagId == tagId && y.WorkflowState != Constants.WorkflowStates.Removed));
                }

                // ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator
                foreach (var deviceUserId in pnRequestModel.DeviceUserIds)
                {
                    planningsQuery = planningsQuery.Where(x => x.PlanningSites
                                                          .Where(y => y.WorkflowState != Constants.WorkflowStates.Removed)
                                                          .Select(y => y.SiteId)
                                                          .Contains(deviceUserId));
                }

                // calculate total before pagination
                var total = await planningsQuery.Select(x => x.Id).CountAsync();

                // add select
                var localeString = await _userService.GetCurrentUserLocale();

                if (string.IsNullOrEmpty(localeString))
                {
                    return(new OperationDataResult <Paged <PlanningPnModel> >(false,
                                                                              _itemsPlanningLocalizationService.GetString("LocaleDoesNotExist")));
                }
                var language                = sdkDbContext.Languages.Single(x => x.LanguageCode == localeString);
                var languageIemPlanning     = _dbContext.Languages.Single(x => x.Id == language.Id);
                var planningQueryWithSelect = AddSelectToPlanningQuery(planningsQuery, languageIemPlanning);


                if (pnRequestModel.Sort == "TranslatedName")
                {
                    planningQueryWithSelect = pnRequestModel.IsSortDsc
                        ? planningQueryWithSelect.OrderByDescending(x => x.TranslatedName)
                        : planningQueryWithSelect.OrderBy(x => x.TranslatedName);
                }

                planningQueryWithSelect
                    = planningQueryWithSelect
                      .Skip(pnRequestModel.Offset)
                      .Take(pnRequestModel.PageSize);

                var checkListIds = await planningsQuery.Select(x => x.RelatedEFormId).ToListAsync();

                var checkListWorkflowState = sdkDbContext.CheckLists.Where(x => checkListIds.Contains(x.Id))
                                             .Select(checkList => new KeyValuePair <int, string>(checkList.Id, checkList.WorkflowState))
                                             .ToList();

                // add select and take objects from db
                var sql = planningQueryWithSelect.ToQueryString();
                List <PlanningPnModel> plannings = await planningQueryWithSelect.ToListAsync();

                // get site names

                var assignedSitesFromPlanning = plannings.SelectMany(y => y.AssignedSites).ToList();

                var sites = await sdkDbContext.Sites
                            .AsNoTracking()
                            .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                            .Where(x => assignedSitesFromPlanning.Select(y => y.SiteId).Contains(x.Id))
                            .Select(x => new CommonDictionaryModel
                {
                    Id   = x.Id,
                    Name = x.Name,
                }).ToListAsync();

                foreach (var planning in plannings)
                {
                    foreach (var assignedSite in assignedSitesFromPlanning)
                    {
                        foreach (var site in sites.Where(site => site.Id == assignedSite.SiteId))
                        {
                            assignedSite.Name = site.Name;
                        }
                    }

                    var(_, value) = checkListWorkflowState.SingleOrDefault(x => x.Key == planning.BoundEform.RelatedEFormId);
                    planning.BoundEform.IsEformRemoved = value == Constants.WorkflowStates.Removed;

                    // This is done to update existing Plannings to using EFormSdkFolderId instead of EFormSdkFolderName
                    if ((planning.Folder.EFormSdkFolderId == 0 || planning.Folder.EFormSdkFolderId == null) && planning.Folder.EFormSdkFolderName != null)
                    {
                        var locateFolder = await sdkDbContext.Folders
                                           .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                                           .Where(x => x.Name == planning.Folder.EFormSdkFolderName)
                                           .FirstOrDefaultAsync();

                        if (locateFolder != null)
                        {
                            var thePlanning = await _dbContext.Plannings.SingleAsync(x => x.Id == planning.Id);

                            thePlanning.SdkFolderId = locateFolder.Id;
                            await thePlanning.Update(_dbContext);

                            planning.Folder.EFormSdkFolderId = locateFolder.Id;
                        }
                    }

                    var folder = await sdkDbContext.Folders
                                 .Include(x => x.Parent)
                                 .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                                 .Where(y => y.Id == planning.Folder.EFormSdkFolderId)
                                 .Select(x => new
                    {
                        x.Name,
                        x.Parent,
                        x.Id,
                    })
                                 .FirstOrDefaultAsync();

                    if (folder != null)
                    {
                        planning.Folder.EFormSdkFolderId = folder.Id;
                        if (folder.Parent != null)
                        {
                            planning.Folder.EFormSdkParentFolderName = folder.Parent.Name;
                        }
                        else
                        {
                            planning.Folder.EFormSdkFolderName = null;
                        }
                    }
                }

                var planningsModel = new Paged <PlanningPnModel>
                {
                    Total    = total,
                    Entities = plannings
                };

                return(new OperationDataResult <Paged <PlanningPnModel> >(true, planningsModel));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                return(new OperationDataResult <Paged <PlanningPnModel> >(false,
                                                                          _itemsPlanningLocalizationService.GetString("ErrorObtainingLists")));
            }
        }