Example #1
0
        /// <inheritdoc />
        public async Task <LazyLoadedResult <DeviceModel> > GetAll(FilteringModel filter, LazyLoadParameters lazyLoadFilters, Sorting sorting, string token)
        {
            var resultLazy = new LazyLoadedResult <DeviceModel>();
            var deviceList = new List <DeviceModel>();
            var companyId  = await _validationHelper.GetCompanyIdByPermission(token, new [] { Const.Permissions.Device.AdminRead });

            List <TenantFullModel> tenants = new List <TenantFullModel>();

            if (companyId != null)
            {
                filter.CompanyIds = new List <int> {
                    companyId.Value
                };
                var tenant = await _httpService.GetTenantSingleByCompanyId(companyId.Value, token);

                tenants.Add(tenant);
            }
            else
            {
                if (filter.TenantIds.Any())
                {
                    var tenantIdsString = filter.TenantIds.Select(x => x.ToString()).ToList();
                    tenants = await _httpService.GetTenantsByIds(token, GetQuery("TenantIds", tenantIdsString));

                    var companyIds = tenants.SelectMany(x => x.Companies).Select(x => x.Id).ToList();
                    filter.CompanyIds = filter.CompanyIds != null ? companyIds.Count > 0 ? filter.CompanyIds.Intersect(companyIds).ToList() :
                                        null : companyIds;
                }
                else
                {
                    tenants = await _httpService.GetTenantsByIds(token);
                }
            }
            var devices = await _deviceRepository.GetWithoutIncludeAsync(GetFilterExpression(filter));

            var devicesHistories = await _deviceHistoryRepository.GetAsync();

            if (!devices.Any())
            {
                return(resultLazy);
            }

            var compIds = (from device in devices select device.CompanyId.ToString()).ToList();

            var branchIds = (from device in devices select device.BranchId.ToString()).ToList();

            var deviceAndLastHistoryQuery = (from device in devices
                                             from history in devicesHistories.Where(x => x.DeviceId == device.Id).OrderByDescending(x => x.CreatedOn).Take(1)
                                             select new
            {
                Device = device,
                LastHistory = history
            });

            var usersIds = deviceAndLastHistoryQuery.Where(x => x.LastHistory.LoggedInUserId.HasValue).Select(x => x.LastHistory.LoggedInUserId.ToString().ToLower()).Distinct().ToList();

            //var companies = await _httpService.GetCompaniesByIds(GetQuery("CompanyIds", compIds), token);
            var companyTenant = tenants.SelectMany(tenant => tenant.Companies, (tenant, company) => new
            {
                Company = company,
                Tenant  = tenant
            });
            var branches = await _httpService.GetBranchesByIds(GetQuery("BranchIds", branchIds), token);

            var users = await _httpService.GetUsersByIdsAsync(token, GetQuery("UsersId", usersIds));

            var fdeviceList = (from deviceInfo in deviceAndLastHistoryQuery
                               join company in companyTenant on deviceInfo.Device.CompanyId equals company.Company.Id into _c
                               from company in _c.DefaultIfEmpty()
                               join branch in branches on deviceInfo.Device.BranchId equals branch.Id into _b
                               from branch in _b.DefaultIfEmpty()
                               join user in users on deviceInfo.LastHistory.LoggedInUserId equals user.Id into _u
                               from user in _u.DefaultIfEmpty()
                               select new DeviceModel
            {
                Id = deviceInfo.Device.Id,
                Udid = deviceInfo.Device.Udid,
                CreatedOn = deviceInfo.Device.CreatedOn,
                HexnodeUdid = deviceInfo.Device.HexnodeUdid,
                DeviceName = deviceInfo.Device.DeviceName,
                IsPending = deviceInfo.Device.IsPending,
                Latitude = deviceInfo.Device.Latitude,
                Longitude = deviceInfo.Device.Longitude,
                Radius = deviceInfo.Device.Radius,
                IsDeleted = deviceInfo.Device.IsDeleted,
                IsOnline = deviceInfo.LastHistory.IsOnline,
                IsInLocation = deviceInfo.LastHistory.IsInLocation,
                StatusSince = deviceInfo.LastHistory.CreatedOn,
                Branch = branch,
                Company = company.Company,
                Tenant = company.Tenant.Adapt <TenantModel>(),
                LoggedInAs = user,
                Phone = deviceInfo.Device.Phone,
                CurrentDeviceLocationLatitude = deviceInfo.LastHistory.CurrentDeviceLocationLatitude,
                CurrentDeviceLocationLongitude = deviceInfo.LastHistory.CurrentDeviceLocationLongitude,
            });

            resultLazy = GetSorted(fdeviceList, sorting).GetPart(lazyLoadFilters);

            return(resultLazy);
        }