public List <EnterpriseModel> GetSearchBlacklistedEnterprisesOnDevice(int deviceId, float?hotelLatitude, float?hotelLongitude, float radiusInMeters, int?categoryId, string text) { List <EnterpriseModel> enterprises = null; var hotelPoint = GeographyUtils.GetPointFromLatitudeAndLongitude(hotelLatitude, hotelLongitude); if (hotelPoint != null) { var hotel = RootRepository.HotelRepository.GetHotelFromDevice(deviceId); RootRepository.SecurityRepository.AssertDevicePermissions(deviceId); if (text != null) { text = text.Trim(); } var query = (from e in ProxylessContext.Enterprises .Include(e => e.EnterpriseLocations) .Include(e => e.EnterpriseCategoryMaps.Select(ecm => ecm.Image)) .Include(e => e.EnterpriseCategoryMaps.Select(ecm => ecm.CategoryMap.Image)) .Include( e => e.EnterpriseCategoryMaps.Select( ecm => ecm.HotelBestOfEnterpriseMaps.Select(hboem => hboem.HotelBestOfEnterpriseLocationMaps))) .Where( e => e.IsActive && (e.BlackListEnterpriseMaps.Any(blem => blem.IsActive && blem.FKDevice == deviceId) || e.EnterpriseLocations.Any(el => el.BlackListEnterpriseLocationMaps.Any(blemlm => blemlm.IsActive && blemlm.FKHotel == hotel.PKID)))) select new { BlackListEnterpriseMaps = e.BlackListEnterpriseMaps.Where(blem => blem.IsActive), BlackListEnterpriseLocationMaps = e.EnterpriseLocations.SelectMany(el => el.BlackListEnterpriseLocationMaps.Where(blelm => blelm.IsActive)), CMImage = e.EnterpriseCategoryMaps .Where( ecm => ecm.CategoryMap != null && ecm.CategoryMap.IsActive && ecm.CategoryMap.Image != null && ecm.CategoryMap.Image.IsActive) .Select(ecm => ecm.CategoryMap.Image) .FirstOrDefault(i => i != null && i.IsActive), ECMImage = e.EnterpriseCategoryMaps .Where(ecm => ecm.CategoryMap != null && ecm.CategoryMap.Image != null) .Select(ecm => ecm.Image) .FirstOrDefault(i => i != null && i.IsActive), HCMImage = e.EnterpriseCategoryMaps .Where(ecm => ecm.CategoryMap != null) .SelectMany( ecm => ecm.CategoryMap.HotelCategoryMaps.Where(hcm => hcm != null && hcm.FKDevice == deviceId && hcm.Image != null) .Select(hcm => hcm.Image)).FirstOrDefault(), HotelCategoryMaps = e.EnterpriseCategoryMaps.SelectMany( ecm => ecm.CategoryMap.HotelCategoryMaps.Where(hcm => hcm.IsActive && hcm.FKDevice == deviceId)), Enterprise = new EnterpriseModel { Blacklisted = e.BlackListEnterpriseMaps.Any(blem => blem.IsActive && blem.FKDevice == deviceId), Recommended = e.EnterpriseCategoryMaps.Any(ecm => ecm.HotelBestOfEnterpriseMaps.Any(hboem => hboem.FKDevice == deviceId)), Name = e.Name, PKID = e.PKID, EnterpriseLocations = e.EnterpriseLocations .Where( el => el.IsActive && el.Coordinates.Distance(hotelPoint) < radiusInMeters && (el.Enterprise.BlackListEnterpriseMaps.Any(blem => blem.IsActive) || el.BlackListEnterpriseLocationMaps.Any(blelm => blelm.IsActive))) .Select(el => new EnterpriseLocationModel { Blacklisted = el.BlackListEnterpriseLocationMaps.Any(blelm => blelm.FKHotel == hotel.PKID), Recommended = el.HotelBestOfEnterpriseLocationMaps.Any(hboelm => hboelm.HotelBestOfEnterpriseMap.FKDevice == deviceId), DistanceInMeters = el.Coordinates.Distance(hotelPoint), Phone = el.Phone, PKID = el.PKID, Location = new LocationModel { Address1 = el.Address1, Address2 = el.Address2, City = el.City, ISOCountryCode = el.Country, ISOStateCode = el.State, Latitude = el.Latitude, Longitude = el.Longitude, PostalCode = el.PostalCode, } }) }, }); //var query = ( from e in ProxylessContext.Enterprises.Where( e2 => e2.IsActive ) // let el = e.EnterpriseLocations.Where( el2 => el2.IsActive ) // let ecm = e.EnterpriseCategoryMaps.Where( ecm2 => ecm2.IsActive ) // let fecm = ecm.FirstOrDefault() // let cm = ecm.Where( ecm2 => ecm2.CategoryMap.IsActive ).Select( ecm2 => ecm2.CategoryMap ) // let fcm = cm.FirstOrDefault() // let hcm = cm.SelectMany( cm2 => cm2.HotelCategoryMaps.Where( hcm2 => hcm2.IsActive && hcm2.FKDevice == deviceId ) ) // let fhcm = hcm.OrderBy( hcm2 => hcm2.Ordinal ).FirstOrDefault() // let hboem = ecm.SelectMany( ecm2 => ecm2.HotelBestOfEnterpriseMaps.Where( hboem2 => hboem2.IsActive && hboem2.FKDevice == deviceId ).OrderBy( hboem2 => hboem2.Ordinal ) ) // let hboelm = hboem.SelectMany( hboem2 => hboem2.HotelBestOfEnterpriseLocationMaps ) // let blem = e.BlackListEnterpriseMaps.Where( blem2 => blem2.IsActive && blem2.FKDevice == deviceId ) // let blelm = el.SelectMany( el2 => el2.BlackListEnterpriseLocationMaps.Where( blelm2 => blelm2.IsActive && blelm2.Hotel.Devices.Any( d => d.PKID == deviceId ) ) ) // where el.Any( el2 => ( !blelm.Any() && el2.Coordinates.Distance( hotelPoint ) < radiusInMeters ) // || blelm.Select( blelm2 => blelm2.FKEnterpriseLocation ).Contains( el2.PKID ) ) // && hcm.Any() // && ( blem.Any() || blelm.Any() ) // select new // { // BlackListEnterpriseMaps = blem, // BlackListEnterpriseLocationMaps = blelm, // CMImage = fcm.Image, // ECMImage = fecm.Image, // Enterprise = new EnterpriseModel // { // Blacklisted = blem.Any(), // EnterpriseLocations = ( from el2 in el // where ( !blelm.Any() && el2.Coordinates.Distance( hotelPoint ) < radiusInMeters ) // || blelm.Select( blelm2 => blelm2.FKEnterpriseLocation ).Contains( el2.PKID ) // select new EnterpriseLocationModel // { // Blacklisted = blelm.Select( blelm2 => blelm2.FKEnterpriseLocation ).Contains( el2.PKID ), // DistanceInMiles = ProxylessContext.CalculateDistance( el2.Latitude, el2.Longitude, hotelGeocode.Latitude.Value, hotelGeocode.Longitude.Value ), // Location = new LocationModel // { // Address1 = el2.Address1, // Address2 = el2.Address2, // City = el2.City, // ISOCountryCode = el2.Country, // Geocode = new GeocodeModel // { // Latitude = el2.Latitude, // Longitude = el2.Longitude // }, // PostalCode = el2.PostalCode, // ISOStateCode = el2.State, // }, // Phone = el2.Phone, // PKID = el2.PKID, // Recommended = hboelm.Any( hboelm2 => hboelm2.FKEnterpriseLocation == el2.PKID ) // } ), // Name = e.Name, // PKID = e.PKID, // Recommended = hboem.Any() && !hboelm.Any() // }, // HCMImage = fhcm.Image, // HotelCategoryMaps = hcm // } ); if (categoryId.HasValue) { query = (from q in query where q.HotelCategoryMaps.Any(hcm => hcm.CategoryMap.FKChildCategory == categoryId.Value) select q); } if (!string.IsNullOrWhiteSpace(text)) { query = (from q in query let gebft = ProxylessContext.GetEnterprisesByFreeText(text).Select(gebft => gebft.Key) where gebft.Contains(q.Enterprise.PKID) select q); } var list = query.Take(20).ToList(); list.ForEach(e => { if (e.Enterprise != null) { if (e.CMImage != null) { e.Enterprise.ListItemImage = new ImageModel(e.CMImage); } if (e.HCMImage != null) { e.Enterprise.ListItemImage = new ImageModel(e.HCMImage); } if (e.ECMImage != null) { e.Enterprise.ListItemImage = new ImageModel(e.ECMImage); } if (enterprises == null) { enterprises = new List <EnterpriseModel>(); } enterprises.Add(e.Enterprise); } }); } return(enterprises); }
public List <EnterpriseModel> GetSearchEnterprises(int deviceId, float?hotelLatitude, float?hotelLongitude, float radiusInMeters, int?categoryId, string text, EnterpriseSearchSortTypes sortType) { List <EnterpriseModel> enterprises = null; var device = RootRepository.DeviceRepository.GetDevice(deviceId); var hotelPoint = GeographyUtils.GetPointFromLatitudeAndLongitude(hotelLatitude, hotelLongitude); if (hotelPoint != null) { RootRepository.SecurityRepository.AssertDevicePermissions(deviceId); if (text != null) { text = text.Trim(); } var query = (from e in ProxylessContext.Enterprises .Include(x => x.EnterpriseLocations.Select(el => el.BlackListEnterpriseLocationMaps)) .Include(x => x.EnterpriseLocations.Select(el => el.HotelBestOfEnterpriseLocationMaps)) .Include(x => x.EnterpriseCategoryMaps.Select(ecm => ecm.Image)) .Include(x => x.EnterpriseCategoryMaps.Select(ecm => ecm.CategoryMap.Image)) .Include(x => x.EnterpriseCategoryMaps.Select(ecm => ecm.CategoryMap.HotelCategoryMaps.Select(hcm => hcm.Image))) .Include( x => x.EnterpriseCategoryMaps.Select( ecm => ecm.HotelBestOfEnterpriseMaps.Select(hboem => hboem.HotelBestOfEnterpriseLocationMaps))) .Include(x => x.BlackListEnterpriseMaps) where e.IsActive && e.EnterpriseLocations.Any(x => x.IsActive && x.Latitude != null && x.Longitude != null) select new { CMImage = e.EnterpriseCategoryMaps.FirstOrDefault().CategoryMap.Image, ECMImage = e.EnterpriseCategoryMaps.FirstOrDefault().Image, Enterprise = new EnterpriseModel { Blacklisted = e.BlackListEnterpriseMaps.Any(x => x.FKDevice == deviceId), EnterpriseLocations = e.EnterpriseLocations.Where(x => x.IsActive && x.Latitude != null && x.Longitude != null).Select(el => new EnterpriseLocationModel { BingId = el.BingId, Blacklisted = el.BlackListEnterpriseLocationMaps.Any(x => x.FKHotel == device.FKHotel), DistanceInMeters = el.Coordinates.Distance(hotelPoint), Location = new LocationModel { Address1 = el.Address1, Address2 = el.Address2, City = el.City, ISOCountryCode = el.Country, Latitude = el.Latitude, Longitude = el.Longitude, PostalCode = el.PostalCode, ISOStateCode = el.State }, Phone = el.Phone, PKID = el.PKID, Recommended = el.HotelBestOfEnterpriseLocationMaps.Any(x => x.HotelBestOfEnterpriseMap.FKDevice == deviceId) }), Name = e.Name, PKID = e.PKID, Recommended = e.EnterpriseCategoryMaps.Any(x => x.HotelBestOfEnterpriseMaps.Any(hboem => hboem.FKDevice == deviceId)) }, e.EnterpriseCategoryMaps, HCMImage = e.EnterpriseCategoryMaps.FirstOrDefault() .CategoryMap.HotelCategoryMaps.FirstOrDefault(x => x.FKDevice == deviceId) .Image, }); //var query = ( from e in ProxylessContext.Enterprises.Where( e2 => e2.IsActive ) // let el = e.EnterpriseLocations.Where( el => el.IsActive && el.Coordinates.Distance( hotelPoint ) < radiusInMeters ) // let ecm = e.EnterpriseCategoryMaps.Where( ecm2 => ecm2.IsActive ) // let fecm = ecm.FirstOrDefault() // let cm = ecm.Where( ecm2 => ecm2.CategoryMap.IsActive ).Select( ecm2 => ecm2.CategoryMap ) // let fcm = cm.FirstOrDefault() // let hcm = cm.SelectMany( cm2 => cm2.HotelCategoryMaps.Where( hcm2 => hcm2.IsActive && hcm2.FKDevice == deviceId ) ) // let fhcm = hcm.OrderBy( hcm2 => hcm2.Ordinal ).FirstOrDefault() // let hboem = ecm.SelectMany( ecm2 => ecm2.HotelBestOfEnterpriseMaps.Where( hboem2 => hboem2.IsActive && hboem2.FKDevice == deviceId ).OrderBy( hboem2 => hboem2.Ordinal ) ) // let hboelm = hboem.SelectMany( hboem2 => hboem2.HotelBestOfEnterpriseLocationMaps ) // let blem = e.BlackListEnterpriseMaps.Where( blem2 => blem2.IsActive && blem2.FKDevice == deviceId ) // let blelm = el.SelectMany( el2 => el2.BlackListEnterpriseLocationMaps.Where( blelm2 => blelm2.IsActive == true && blelm2.Hotel.Devices.Any( d => d.PKID == deviceId ) ) ) // where el.Any() // select new // { // CMImage = fcm.Image, // ECMImage = fecm.Image, // Enterprise = new EnterpriseModel() // { // Blacklisted = blem.Any(), // EnterpriseLocations = ( from el2 in el // select new EnterpriseLocationModel() // { // BingId = el2.BingId, // Blacklisted = blelm.Select( blelm2 => blelm2.FKEnterpriseLocation ).Contains( el2.PKID ), // DistanceInMiles = ProxylessContext.CalculateDistance( el2.Latitude, el2.Longitude, hotelGeocode.Latitude.Value, hotelGeocode.Longitude.Value ), // Location = new LocationModel() // { // Address1 = el2.Address1, // Address2 = el2.Address2, // City = el2.City, // Country = new CountryModel // { // ISOCountryCode = el2.Country // }, // Geocode = new GeocodeModel() // { // Latitude = el2.Latitude, // Longitude = el2.Longitude // }, // PostalCode = el2.PostalCode, // State = new StateModel // { // ISOStateCode = el2.State, // } // }, // Phone = el2.Phone, // PKID = el2.PKID, // Recommended = hboelm.Any( hboelm2 => hboelm2.FKEnterpriseLocation == el2.PKID ) // } ), // Name = e.Name, // PKID = e.PKID, // Recommended = hboem.Any() && !hboelm.Any() // }, // EnterpriseCategoryMaps = ecm, // HCMImage = fhcm.Image, // } ); if (categoryId.HasValue) { query = (from q in query where q.EnterpriseCategoryMaps.Any(ecm2 => ecm2.CategoryMap.FKChildCategory == categoryId.Value) select q); } if (!string.IsNullOrWhiteSpace(text)) { var ft = ProxylessContext.GetEnterprisesByFreeText(text).Select(gebft => gebft.Key); query = query.Where(x => ft.Contains(x.Enterprise.PKID)); } switch (sortType) { case EnterpriseSearchSortTypes.ALAPHABETICAL: query = (from q in query orderby q.Enterprise.Name select q); break; case EnterpriseSearchSortTypes.NEAREST: query = (from q in query orderby q.Enterprise.EnterpriseLocations.Min(el => el.DistanceInMeters), q.Enterprise.Name select q); break; } var list = Rp.ExecuteAction(() => query.Take(20)).ToList(); if (list != null) { list.ForEach(e => { if (e.Enterprise != null) { if (e.CMImage != null) { e.Enterprise.ListItemImage = new ImageModel(e.CMImage); } if (e.HCMImage != null) { e.Enterprise.ListItemImage = new ImageModel(e.HCMImage); } if (e.ECMImage != null) { e.Enterprise.ListItemImage = new ImageModel(e.ECMImage); } if (enterprises == null) { enterprises = new List <EnterpriseModel>(); } enterprises.Add(e.Enterprise); } }); } } return(enterprises); }