diff --git a/EuroTran/EasyBL.WEBAPP/ShowEasy/ServicesService.cs b/EuroTran/EasyBL.WEBAPP/ShowEasy/ServicesService.cs index 8b9e313..5bcfa1a 100644 --- a/EuroTran/EasyBL.WEBAPP/ShowEasy/ServicesService.cs +++ b/EuroTran/EasyBL.WEBAPP/ShowEasy/ServicesService.cs @@ -47,13 +47,44 @@ namespace EasyBL.WEBAPP.SYS var ServiceIDSet = new HashSet(); //去重複值 - - var ServiceRegionSet = FilterByRegion(Lang, RegionID); - var ServiceCountrySet = FilterByCountry(Lang, CountryID); - var ServiceCitySet = FilterByCity(Lang, CityID); - var ServiceSubCategorySet = FilterBySubCategory(Lang, SubCategoryID); + foreach (var service in saService) { + + if (!ServiceIDSet.Contains(service.ServiceID)) { + ServiceIDSet.Add(service.ServiceID); + } + } + + + if (RegionID.Count > 0) { + + ServiceIDSet = FilterByRegion(ServiceIDSet, Lang, RegionID); + + } + + if (CountryID.Count > 0) + { + + ServiceIDSet = FilterByCountry(ServiceIDSet, Lang, CountryID); + + } + + if (CityID.Count > 0) + { + + ServiceIDSet = FilterByCity(ServiceIDSet, Lang, CityID); + + } + + if (SubCategoryID.Count > 0) + { + + ServiceIDSet = FilterBySubCategory(ServiceIDSet, Lang, SubCategoryID); + + } + + saService = FilterByServiceID(ServiceIDSet, saService); srm = new SuccessResponseMessage(null, null); srm.DATA.Add(BLWording.REL, saService); @@ -66,15 +97,13 @@ namespace EasyBL.WEBAPP.SYS return HttpResponseExtension.ToJson(JsonConvert.SerializeObject(srm)); } - //用區域搜尋服務(多選) - public HashSet FilterByRegion(string Lang, List RegionID) + public HashSet FilterByRegion(HashSet ServiceIDSet, string Lang, List RegionID) { //string sMsg = null; var db = SugarBase.GetIntance(); - - //去重複值 - var ServiceIDSet = new HashSet(); + var newServiceFilter = new HashSet(); + var Result = new HashSet(); do { @@ -96,25 +125,35 @@ namespace EasyBL.WEBAPP.SYS foreach (var result in saResult) { - if (!ServiceIDSet.Contains(result.ServiceID)) //ServiceIDSet內若有包含ServiceID欄位 + if (!newServiceFilter.Contains(result.ServiceID)) //ServiceIDSet內若有包含ServiceID欄位 { - ServiceIDSet.Add(result.ServiceID); //則加入該result的ServiceID欄位 + + newServiceFilter.Add(result.ServiceID); //則加入該result的ServiceID欄位 + } } + } } while (false); - return ServiceIDSet; + ServiceIDSet.IntersectWith(newServiceFilter); + + return Result; } //用國家搜尋服務(多選) - public HashSet FilterByCountry(string Lang, List CountryID) + public HashSet FilterByCountry(HashSet ServiceIDSet, string Lang, List CountryID) { + //System.Diagnostics.Debug.WriteLine("first FilterByCountry: " + ServiceIDSet.Count); + var db = SugarBase.GetIntance(); - var ServiceIDSet = new HashSet(); + var newServiceFilter = new HashSet(); + var Result = new HashSet(); + + //System.Diagnostics.Debug.WriteLine(ServiceIDSet); do { @@ -126,10 +165,8 @@ namespace EasyBL.WEBAPP.SYS } - //去重複值 - foreach (var Country in CountryID) - { //參數RegionID為陣列 + { var saResult = db.Queryable() //比對每筆資料的CountryID是否為使用者所選Filter .Where(x => x.LangID == Lang && x.CountryID == Country) @@ -138,25 +175,35 @@ namespace EasyBL.WEBAPP.SYS foreach (var result in saResult) { - if (!ServiceIDSet.Contains(result.ServiceID)) + if (!newServiceFilter.Contains(result.ServiceID)) { - ServiceIDSet.Add(result.ServiceID); + + newServiceFilter.Add(result.ServiceID); + } } + } } while (false); + ServiceIDSet.IntersectWith(newServiceFilter); + + //System.Diagnostics.Debug.WriteLine("last FilterByCountry: " + ServiceIDSet.Count); + return ServiceIDSet; + } //用城市搜尋服務(多選) - public HashSet FilterByCity(string Lang, List CityID) + public HashSet FilterByCity(HashSet ServiceIDSet, string Lang, List CityID) { var db = SugarBase.GetIntance(); - var ServiceIDSet = new HashSet(); + var newServiceFilter = new HashSet(); + var Result = new HashSet(); + System.Diagnostics.Debug.WriteLine("first FilterByCity: " + ServiceIDSet.Count); do { @@ -168,8 +215,6 @@ namespace EasyBL.WEBAPP.SYS } - //去重複值 - foreach (var City in CityID) { //參數RegionID為陣列 @@ -180,27 +225,33 @@ namespace EasyBL.WEBAPP.SYS foreach (var result in saResult) { - if (!ServiceIDSet.Contains(result.ServiceID)) + if (!newServiceFilter.Contains(result.ServiceID)) { - ServiceIDSet.Add(result.ServiceID); + + newServiceFilter.Add(result.ServiceID); + } + + } } } while (false); + ServiceIDSet.IntersectWith(newServiceFilter); + //System.Diagnostics.Debug.WriteLine("last FilterByCity: " + ServiceIDSet.Count); + return ServiceIDSet; } //用子類別搜尋服務(多選) - public HashSet FilterBySubCategory(string Lang, List SubCategoryID) + public HashSet FilterBySubCategory(HashSet ServiceIDSet, string Lang, List SubCategoryID) { var db = SugarBase.GetIntance(); - var ServiceIDSet = new HashSet(); - var ServiceDic = new Dictionary(); + var NewServiceIDSet = new HashSet(); //存進NewServiceIDSet會自動去掉重複值 do { @@ -212,42 +263,73 @@ namespace EasyBL.WEBAPP.SYS } - foreach (var SubCategory in SubCategoryID) - { + var saResult = db.Queryable() //將所有Service列出 + .Where(x => x.LangID == Lang) + .ToList(); //將結果儲存成陣列 - var saResult = db.Queryable() //比對每筆資料的CountryID是否為使用者所選Filter - .Where(x => x.LangID == Lang) - .ToList(); //將結果儲存成陣列 + foreach (var result in saResult) + { - foreach (var result in saResult) + if (!string.IsNullOrEmpty(result.SubCategoryID)) { - System.Diagnostics.Debug.WriteLine("result.SubCategoryID" + ": " + result.SubCategoryID); - - if (!string.IsNullOrEmpty(result.SubCategoryID)) { + var rsSubCategoryIDs = JsonConvert.DeserializeObject>(result.SubCategoryID); //把SubCategoryID JSON string 轉成 List Object - var rsSubCategoryIDs = JsonConvert.DeserializeObject>(result.SubCategoryID); + var list = rsSubCategoryIDs.Select(s => s.id).ToList(); //只取tag為id的資料並存入list - CategoryData data = rsSubCategoryIDs[0]; + if (list.Intersect(SubCategoryID).Any()) { //將list和SubCategoryID做合集 - //System.Diagnostics.Debug.WriteLine("categoryID" + ": " + data.id); + NewServiceIDSet.Add(result.ServiceID); - if (!ServiceIDSet.Contains(result.ServiceID)) - { - ServiceIDSet.Add(result.ServiceID); - } } - - } + } + } while (false); + ServiceIDSet.IntersectWith(NewServiceIDSet); + return ServiceIDSet; } + //FilterByServiceID + public List FilterByServiceID(HashSet ServiceIDSet, List saService) + { + + var saResult = new List(); + + do + { + + //var ServiceIDList = ServiceIDSet.ToList(); + var ServiceDic = new Dictionary(); + + + foreach (var Service in saService) { //把所有的ServiceList轉成Dictionary + + if (!ServiceDic.ContainsKey(Service.ServiceID)) { + ServiceDic.Add(Service.ServiceID, Service); + } + + } + + foreach (var ServiceID in ServiceIDSet) { + + if (ServiceDic.ContainsKey(ServiceID)) { + + saResult.Add(ServiceDic[ServiceID]); //用ServiceID當Key查Dictionary內的值並加入saResult + + } + } + + } while (false); + + return saResult; + } + } } \ No newline at end of file