예제 #1
0
        private static async Task <FilteredTaskInspection> FilterPaginate(ReportRequest input, ILogger logger, bool isFillTask = true, bool isFillInspection = true)
        {
            var sqlRInspections       = new InspectionRepository(logger);
            var sqlRTask              = new TaskRepository(logger);
            var sqlRTaskStatus        = new TaskStatusRepository(logger);
            var sharedTaskInspections = new List <SharedTaskInspection>();

            if (!int.TryParse(input.Paging.Skip, out var skip))
            {
                throw new ValidationException(Error.ParserErrorInt);
            }

            if (!int.TryParse(input.Paging.Limit, out var limit))
            {
                throw new ValidationException(Error.ParserErrorInt);
            }



            List <Inspection> inspections;

            if (isFillInspection)
            {
                inspections = await sqlRInspections.GetAllSortByProperty("DateStart", "DESC");

                foreach (var inspection in inspections)
                {
                    sharedTaskInspections.Add(new SharedTaskInspection {
                        Inspection = inspection
                    });
                }
            }

            List <TrainTask> tasks;

            if (isFillTask)
            {
                tasks = await sqlRTask.GetAllSortByProperty("CreateDate", "DESC");

                foreach (var item in tasks)
                {
                    sharedTaskInspections.Add(new SharedTaskInspection {
                        TrainTask = item
                    });
                }
            }

            var sharedTaskInspectionFiltered = new List <SharedTaskInspection>();

            if (input.Filters.Any())
            {
                foreach (var filter in input.Filters)
                {
                    foreach (var sharedTaskInspection in sharedTaskInspections)
                    {
                        switch (filter.ColumnName)
                        {
                        //Статус
                        case "col1":
                            string filterField = null;
                            if (sharedTaskInspection.Inspection != null)
                            {
                                filterField = GetStringInspectionStatus(sharedTaskInspection.Inspection.Status);
                            }
                            if (sharedTaskInspection.TrainTask != null)
                            {
                                var currentTaskStatus = (await sqlRTaskStatus.ByTaskId(sharedTaskInspection.TrainTask.Id)).Last();
                                filterField = GetStringTaskStatus(currentTaskStatus.Status);
                            }

                            if (StringFilter(filterField, filter))
                            {
                                sharedTaskInspectionFiltered.Add(sharedTaskInspection);
                            }
                            break;

                        default:
                            throw new NotImplementedException(Error.InDevelopment);
                        }
                    }
                }

                sharedTaskInspections = sharedTaskInspectionFiltered;
            }

            if (skip > sharedTaskInspections.Count)
            {
                sharedTaskInspections = new List <SharedTaskInspection>();
            }
            if (skip == sharedTaskInspections.Count)
            {
                limit = 1;
            }
            if (skip + limit > sharedTaskInspections.Count)
            {
                limit = sharedTaskInspections.Count - skip;
            }
            var total = sharedTaskInspections.Count;

            sharedTaskInspections = sharedTaskInspections.GetRange(skip, limit);

            inspections = sharedTaskInspections.Where(x => x.Inspection != null).Select(q => q.Inspection).ToList();
            tasks       = sharedTaskInspections.Where(x => x.TrainTask != null).Select(q => q.TrainTask).ToList();


            return(new FilteredTaskInspection {
                Inspections = inspections, TrainTask = tasks, Total = total
            });
        }