Пример #1
0
        public async Task <ActionResult> GetFilteredSimpleLogs([FromBody] DateFiltration dateFiltration)
        {
            var role = User.Claims.FirstOrDefault(x => x.Type == "Role").Value;
            var userIdThatWantToRecieveLogs = User.Claims.FirstOrDefault(x => x.Type == "UserID").Value;

            var userThatWantToRecieveLogs = await DB.Users.FirstOrDefaultAsync(x => x.Id == userIdThatWantToRecieveLogs);

            List <string> userIds = new List <string>();

            #region Level one
            if (role == "Level one")
            {
                var currentTime = DateTime.Now;
                if (dateFiltration.TimeFrom == null &&
                    dateFiltration.TimeTill == null &&
                    dateFiltration.LastHour == false &&
                    dateFiltration.LastDay == false &&
                    dateFiltration.LastWeek == false &&
                    dateFiltration.LastMonth == false)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddHours(-12) && x.Date <= currentTime && x.WarehouseId == dateFiltration.WarehouseId)
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.LastHour)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddHours(-1) && x.Date <= currentTime && x.WarehouseId == dateFiltration.WarehouseId)
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.LastDay)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddDays(-1) && x.Date <= currentTime && x.WarehouseId == dateFiltration.WarehouseId)
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.LastWeek)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddDays(-7) && x.Date <= currentTime && x.WarehouseId == dateFiltration.WarehouseId)
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.LastMonth)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddMonths(-1) && x.Date <= currentTime && x.WarehouseId == dateFiltration.WarehouseId)
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.TimeTill != null || dateFiltration.TimeFrom != null)
                {
                    if (dateFiltration.TimeFrom != null && dateFiltration.TimeTill == null)
                    {
                        var from = dateFiltration.TimeFrom.Value.AddDays(+1).AddHours(-21);
                        return(Ok(await DB.SimpleLogTableDB
                                  .Where(x => x.Date >= from && x.Date <= from.AddDays(+1) && x.WarehouseId == dateFiltration.WarehouseId)
                                  .OrderByDescending(x => x.Date)
                                  .ToListAsync()));
                    }
                    else
                    {
                        var till = dateFiltration.TimeTill.Value.AddDays(+1).AddHours(+3);
                        var from = dateFiltration.TimeFrom.Value.AddDays(+1).AddHours(-21);
                        return(Ok(await DB.SimpleLogTableDB.Where(x => x.Date >= from && x.Date <= till && x.WarehouseId == dateFiltration.WarehouseId).OrderByDescending(x => x.Date).ToListAsync()));
                    }
                }
            }
            #endregion Level one
            #region Level two
            if (role == "Level two")
            {
                // Getting all users that serves to this employee
                userIds = await DB.Users
                          .Where(x => x.ReportsTo == userThatWantToRecieveLogs.Id || x.Id == userThatWantToRecieveLogs.Id)
                          .Select(x => x.Id)
                          .Distinct().
                          ToListAsync()
                ;

                // Getting all level four users that serves to level three
                var employeesOfEmployees = await DB.Users.Where(x => userIds.Any(y => y == x.ReportsTo)).Select(x => x.Id).ToListAsync();

                employeesOfEmployees.ForEach(x => userIds.Add(x));
                var userList = await DB.Users.Where(x => userIds.Any(y => y == x.Id)).Select(x => x.FullName).ToListAsync();

                var username    = User.Claims.FirstOrDefault(x => x.Type == "FullName").Value;
                var currentTime = DateTime.Now;
                if (dateFiltration.TimeFrom == null &&
                    dateFiltration.TimeTill == null &&
                    dateFiltration.LastHour == false &&
                    dateFiltration.LastWeek == false &&
                    dateFiltration.LastMonth == false)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddHours(-12) && x.Date <= currentTime && x.WarehouseId == dateFiltration.WarehouseId && userList.Any(y => y == x.UserName))
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.LastHour)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddHours(-1) && x.Date <= currentTime && x.WarehouseId == dateFiltration.WarehouseId && userList.Any(y => y == x.UserName))
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.LastWeek)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddDays(-7) && x.Date <= currentTime && x.WarehouseId == dateFiltration.WarehouseId && userList.Any(y => y == x.UserName))
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.LastMonth)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddMonths(-1) && x.Date <= currentTime && x.WarehouseId == dateFiltration.WarehouseId && userList.Any(y => y == x.UserName))
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.TimeTill != null || dateFiltration.TimeFrom != null)
                {
                    if (dateFiltration.TimeFrom != null && dateFiltration.TimeTill == null)
                    {
                        var from = dateFiltration.TimeFrom.Value.AddDays(+1).AddHours(-21);
                        return(Ok(await DB.SimpleLogTableDB
                                  .Where(x => x.Date >= from && x.Date <= from.AddDays(+1) && x.WarehouseId == dateFiltration.WarehouseId && userList.Any(y => y == x.UserName))
                                  .OrderByDescending(x => x.Date)
                                  .ToListAsync()));
                    }
                    else
                    {
                        var till = dateFiltration.TimeTill.Value.AddDays(+1).AddHours(+3);
                        var from = dateFiltration.TimeFrom.Value.AddDays(+1).AddHours(-21);
                        return(Ok(await DB.SimpleLogTableDB
                                  .Where(x => x.Date >= from && x.Date <= till && x.WarehouseId == dateFiltration.WarehouseId && userList.Any(y => y == x.UserName))
                                  .OrderByDescending(x => x.Date)
                                  .ToListAsync()));
                    }
                }
            }
            #endregion Level two
            #region Level three
            if (role == "Level three")
            {
                // Getting all users that serves to this employee
                userIds = await DB.Users
                          .Where(x => x.ReportsTo == userThatWantToRecieveLogs.Id || x.Id == userThatWantToRecieveLogs.Id)
                          .Select(x => x.Id)
                          .Distinct().
                          ToListAsync()
                ;

                var userList = await DB.Users.Where(x => userIds.Any(y => y == x.Id)).Select(x => x.FullName).ToListAsync();

                var username    = User.Claims.FirstOrDefault(x => x.Type == "FullName").Value;
                var currentTime = DateTime.Now;
                if (dateFiltration.TimeFrom == null &&
                    dateFiltration.TimeTill == null &&
                    dateFiltration.LastHour == false &&
                    dateFiltration.LastWeek == false &&
                    dateFiltration.LastMonth == false)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddHours(-12) && x.Date <= currentTime && x.WarehouseId == dateFiltration.WarehouseId && userList.Any(y => y == x.UserName))
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.LastHour)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddHours(-1) && x.Date <= currentTime && x.WarehouseId == dateFiltration.WarehouseId && userList.Any(y => y == x.UserName))
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.LastWeek)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddDays(-7) && x.Date <= currentTime && x.WarehouseId == dateFiltration.WarehouseId && userList.Any(y => y == x.UserName))
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.LastMonth)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddMonths(-1) && x.Date <= currentTime && x.WarehouseId == dateFiltration.WarehouseId && userList.Any(y => y == x.UserName))
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.TimeTill != null || dateFiltration.TimeFrom != null)
                {
                    if (dateFiltration.TimeFrom != null && dateFiltration.TimeTill == null)
                    {
                        var from = dateFiltration.TimeFrom.Value.AddDays(+1).AddHours(-21);
                        return(Ok(await DB.SimpleLogTableDB
                                  .Where(x => x.Date >= from && x.Date <= from.AddDays(+1) && x.WarehouseId == dateFiltration.WarehouseId && userList.Any(y => y == x.UserName))
                                  .OrderByDescending(x => x.Date)
                                  .ToListAsync()));
                    }
                    else
                    {
                        var till = dateFiltration.TimeTill.Value.AddDays(+1).AddHours(+3);
                        var from = dateFiltration.TimeFrom.Value.AddDays(+1).AddHours(-21);
                        return(Ok(await DB.SimpleLogTableDB
                                  .Where(x => x.Date >= from && x.Date <= till && x.WarehouseId == dateFiltration.WarehouseId && userList.Any(y => y == x.UserName))
                                  .OrderByDescending(x => x.Date)
                                  .ToListAsync()));
                    }
                }
            }
            #endregion Level three
            #region Level four
            if (role == "Level four")
            {
                var username    = User.Claims.FirstOrDefault(x => x.Type == "FullName").Value;
                var currentTime = DateTime.Now;
                if (dateFiltration.TimeFrom == null &&
                    dateFiltration.TimeTill == null &&
                    dateFiltration.LastHour == false &&
                    dateFiltration.LastWeek == false &&
                    dateFiltration.LastMonth == false)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddDays(-1) && x.Date <= currentTime && x.UserName == username && x.WarehouseId == dateFiltration.WarehouseId)
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.LastHour)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddHours(-1) && x.Date <= currentTime && x.UserName == username && x.WarehouseId == dateFiltration.WarehouseId)
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.LastWeek)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddDays(-7) && x.Date <= currentTime && x.UserName == username && x.WarehouseId == dateFiltration.WarehouseId)
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.LastMonth)
                {
                    return(Ok(await DB.SimpleLogTableDB
                              .Where(x => x.Date >= currentTime.AddMonths(-1) && x.Date <= currentTime && x.UserName == username && x.WarehouseId == dateFiltration.WarehouseId)
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                if (dateFiltration.TimeTill != null || dateFiltration.TimeFrom != null)
                {
                    if (dateFiltration.TimeFrom != null && dateFiltration.TimeTill == null)
                    {
                        var from = dateFiltration.TimeFrom.Value.AddDays(+1).AddHours(-21);
                        return(Ok(await DB.SimpleLogTableDB
                                  .Where(x => x.Date >= from && x.Date <= from.AddDays(+1) && x.UserName == username && x.WarehouseId == dateFiltration.WarehouseId)
                                  .OrderByDescending(x => x.Date)
                                  .ToListAsync()));
                    }
                    else
                    {
                        var till = dateFiltration.TimeTill.Value.AddDays(+1).AddHours(+3);
                        var from = dateFiltration.TimeFrom.Value.AddDays(+1).AddHours(-21);
                        return(Ok(await DB.SimpleLogTableDB.Where(x => x.Date >= from && x.Date <= till && x.UserName == username && x.WarehouseId == dateFiltration.WarehouseId).OrderByDescending(x => x.Date).ToListAsync()));
                    }
                }
            }
            #endregion Level four
            return(Ok());
        }
Пример #2
0
        public async Task <ActionResult <List <SimpleLogTableController> > > GetAdminLogs([FromBody] DateFiltration dateFiltration)
        {
            var currentTime = DateTime.Now;

            if (dateFiltration.TimeFrom == null &&
                dateFiltration.TimeTill == null &&
                dateFiltration.LastHour == false &&
                dateFiltration.LastDay == false &&
                dateFiltration.LastWeek == false &&
                dateFiltration.LastMonth == false)
            {
                return(Ok(await DB.AdminLogTable
                          .Where(x => x.Date >= currentTime.AddMonths(-1) && x.Date <= currentTime)
                          .OrderByDescending(x => x.Date)
                          .ToListAsync()));
            }
            if (dateFiltration.LastHour)
            {
                return(Ok(await DB.AdminLogTable
                          .Where(x => x.Date >= currentTime.AddHours(-1) && x.Date <= currentTime)
                          .OrderByDescending(x => x.Date)
                          .ToListAsync()));
            }
            if (dateFiltration.LastDay)
            {
                return(Ok(await DB.AdminLogTable
                          .Where(x => x.Date >= currentTime.AddDays(-1) && x.Date <= currentTime)
                          .OrderByDescending(x => x.Date)
                          .ToListAsync()));
            }
            if (dateFiltration.LastWeek)
            {
                var a = await DB.AdminLogTable
                        .Where(x => x.Date >= currentTime.AddDays(-7) && x.Date <= currentTime)
                        .OrderByDescending(x => x.Date)
                        .ToListAsync();

                return(Ok(await DB.AdminLogTable
                          .Where(x => x.Date >= currentTime.AddDays(-7) && x.Date <= currentTime)
                          .OrderByDescending(x => x.Date)
                          .ToListAsync()));
            }
            if (dateFiltration.LastMonth)
            {
                return(Ok(await DB.AdminLogTable
                          .Where(x => x.Date >= currentTime.AddMonths(-1) && x.Date <= currentTime)
                          .OrderByDescending(x => x.Date)
                          .ToListAsync()));
            }
            if (dateFiltration.TimeTill != null || dateFiltration.TimeFrom != null)
            {
                if (dateFiltration.TimeFrom != null && dateFiltration.TimeTill == null)
                {
                    var from = dateFiltration.TimeFrom.Value.AddDays(+1).AddHours(-21);
                    return(Ok(await DB.AdminLogTable
                              .Where(x => x.Date >= from && x.Date <= from.AddDays(+1))
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
                else
                {
                    var till = dateFiltration.TimeTill.Value.AddDays(+1).AddHours(+3);
                    var from = dateFiltration.TimeFrom.Value.AddDays(+1).AddHours(-21);
                    return(Ok(await DB.AdminLogTable.Where(x => x.Date >= from && x.Date <= till)
                              .OrderByDescending(x => x.Date)
                              .ToListAsync()));
                }
            }
            return(Ok());
        }
Пример #3
0
        /// <summary>
        /// Runs whole filtration process and creates grouped list to display.
        /// </summary>
        /// <param name="avaiableSurveys">List of surveys currently saved in IsolatedStorage.</param>
        public void Display(ObservableCollection <SurveyBasicInfo> avaiableSurveys)
        {
            ObservableCollection <GroupedOC <ResultBasicInfo> > filteredResults = new ObservableCollection <GroupedOC <ResultBasicInfo> >();

            if (App.AppDictionary.ContainsKey("FilteredResults"))
            {
                App.AppDictionary["FilteredResults"] = filteredResults;
            }
            else
            {
                App.AppDictionary.Add("FilteredResults", filteredResults);
            }
            Busy.IsEnabled = true;
            Thread searchingThread = new Thread(() =>
            {
                ObservableCollection <SurveyBasicInfo> searchList;
                if (SurveyFiltration.IsEnabled)
                {
                    if (SurveyFiltration.SelectedSurveys.Contains(SurveyFiltration.AllSurveysItem))
                    {
                        searchList = avaiableSurveys;
                    }
                    else
                    {
                        searchList = SurveyFiltration.SelectedSurveys;
                    }
                }
                else
                {
                    searchList = avaiableSurveys;
                }
                foreach (var survey in searchList)
                {
                    XDocument document = LoadListOfResults(survey.SurveyId);
                    if (document != null)
                    {
                        GroupedOC <ResultBasicInfo> singleSurveyResults = new GroupedOC <ResultBasicInfo>(survey.Name, survey.SurveyId, survey.IsFavorite.ToString());
                        XElement root = document.Element("results");
                        var results   = from result in root.Elements("result")
                                        where DateFiltration.IsMatching(result.Element("time").Value) &&
                                        LocationFiltration.IsMatching(result.Element("latitude") != null ? result.Element("latitude").Value : null,
                                                                      result.Element("longitude") != null ? result.Element("longitude").Value : null)
                                        select new ResultBasicInfo()
                        {
                            Id                = result.Attribute("id").Value,
                            Title             = result.Attribute("title").Value,
                            IsResultSent      = Convert.ToBoolean(result.Attribute("isSent").Value),
                            IsResultCompleted = Convert.ToBoolean(result.Attribute("isCompleted").Value),
                            Latitude          = result.Element("latitude") != null ? result.Element("latitude").Value : null,
                            Longitude         = result.Element("longitude") != null ? result.Element("longitude").Value : null,
                            Time              = result.Element("time").Value,
                            ParentId          = result.Element("parentId").Value
                        };

                        foreach (var basicInfo in results)
                        {
                            singleSurveyResults.Add(basicInfo);
                        }
                        if (singleSurveyResults.Count > 0)
                        {
                            filteredResults.Add(singleSurveyResults);
                        }
                    }
                }
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    EventHandler handler = SearchingCompletedEventHandler;
                    if (handler != null)
                    {
                        handler(this, EventArgs.Empty);
                    }
                });
            });

            searchingThread.Start();
        }