public async Task <OperationDataResult <Paged <TrashInspectionModel> > > Index(TrashInspectionRequestModel pnRequestModel)
        {
            try
            {
                var trashInspectionSettings = _options.Value;

                var trashInspectionsQuery = _dbContext.TrashInspections
                                            .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                                            .AsNoTracking()
                                            .AsQueryable();

                if (!string.IsNullOrEmpty(pnRequestModel.NameFilter))
                {
                    if (int.TryParse(pnRequestModel.NameFilter, out var i))
                    {
                        trashInspectionsQuery = trashInspectionsQuery.Where(x =>
                                                                            x.Id == i);
                    }
                    else
                    {
                        if (DateTime.TryParse(pnRequestModel.NameFilter, out var dateTime))
                        {
                            trashInspectionsQuery = trashInspectionsQuery.Where(x =>
                                                                                x.Date.Year == dateTime.Year &&
                                                                                x.Date.Month == dateTime.Month &&
                                                                                x.Date.Day == dateTime.Day);
                        }
                        else
                        {
                            trashInspectionsQuery = trashInspectionsQuery.Where(x =>
                                                                                x.Comment.Contains(pnRequestModel.NameFilter) ||
                                                                                x.WeighingNumber.Contains(pnRequestModel.NameFilter));
                        }
                    }
                }

                trashInspectionsQuery = QueryHelper.AddSortToQuery(trashInspectionsQuery, pnRequestModel.Sort, pnRequestModel.IsSortDsc);

                var total = await trashInspectionsQuery.Select(x => x.Id).CountAsync();

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

                var timeZoneInfo = await _userService.GetCurrentUserTimeZoneInfo();

                var trashInspections = await AddSelectToQuery(trashInspectionsQuery, timeZoneInfo, trashInspectionSettings.Token)
                                       .ToListAsync();

                var core = await _coreHelper.GetCore();

                var eFormIds = new List <KeyValuePair <int, int> >(); // <FractionId, eFormId>

                foreach (var trashInspectionModel in trashInspections)
                {
                    var fractionEFormId = await _dbContext.Fractions
                                          .Where(y => y.Id == trashInspectionModel.FractionId)
                                          .Select(x => x.eFormId)
                                          .FirstOrDefaultAsync();

                    if (trashInspectionModel.Status == 100)
                    {
                        var result = await _dbContext.TrashInspectionCases
                                     .Where(x => x.TrashInspectionId == trashInspectionModel.Id)
                                     .Where(x => x.Status == 100)
                                     .ToListAsync();

                        if (result.Any())
                        {
                            trashInspectionModel.SdkCaseId = int.Parse(result.First().SdkCaseId);

                            trashInspectionModel.SdkCaseId =
                                (int)core.CaseLookupMUId(trashInspectionModel.SdkCaseId).Result.CaseId;
                        }

                        if (eFormIds.Any(x => x.Key == trashInspectionModel.FractionId))
                        {
                            trashInspectionModel.SdkeFormId = eFormIds.First(x => x.Key == trashInspectionModel.FractionId).Value;
                        }
                        else
                        {
                            try
                            {
                                var locale = await _userService.GetCurrentUserLocale();

                                var language = core.DbContextHelper.GetDbContext().
                                               Languages
                                               .Single(x => x.LanguageCode == locale);
                                var eForm = await core.TemplateItemRead(fractionEFormId, language);

                                trashInspectionModel.SdkeFormId = eForm.Id;
                                var kvp =
                                    new KeyValuePair <int, int>(fractionEFormId, eForm.Id);
                                eFormIds.Add(kvp);
                            }
                            catch
                            {
                                // KeyValuePair<int, int> kvp = new KeyValuePair<int, int>(fraction.eFormId, "");
                                // eFormIds.Add(kvp);
                            }
                        }
                    }
                }

                var trashInspectionsModel = new Paged <TrashInspectionModel>
                {
                    Total    = total,
                    Entities = trashInspections
                };


                return(new OperationDataResult <Paged <TrashInspectionModel> >(true, trashInspectionsModel));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                _logger.LogError(e.Message);
                return(new OperationDataResult <Paged <TrashInspectionModel> >(false,
                                                                               _trashInspectionLocalizationService.GetString("ErrorObtainingTrashInspections")));
            }
        }
Exemplo n.º 2
0
 public async Task <OperationDataResult <Paged <TrashInspectionModel> > > Index([FromBody] TrashInspectionRequestModel requestModel)
 {
     return(await _trashInspectionService.Index(requestModel));
 }