public HttpResponseMessage GetListDetail(viewModelGetListPlace getListInfo) { //無論公開或私人都會用此功能 所以要找創建清單的userid非登入者的userid List <string> systemTagResult = new List <string>(); List <int> resultplaceid = new List <int>(); List <int> searchallplaceinlist = new List <int>(); List <int> intersectResult = new List <int>(); List <int> tagsList = new List <int>(); List <int> editorIDList = new List <int>(); List <listDetailPlace> resultPlaceInfo = new List <listDetailPlace>(); List <tTag> resultTagInfo = new List <tTag>(); placeListInfo infoItem = new placeListInfo(); int[] tFilterid = getListInfo.filter; int list_createrId = 0; userFactory.userEventRecord(0, 1, db); var dataForm = new { info = infoItem, places = resultPlaceInfo, systemtags = systemTagResult, user_tags = resultTagInfo, editors_id = editorIDList }; var result = new { status = 0, msg = "fail", data = dataForm }; var listModel = db.placeLists.Where(p => p.id == getListInfo.list_id).FirstOrDefault(); //此結果為特定清單中全部的地點id //無論公開或私人都會用此功能 所以要找創建清單的userid非登入者的userid if (listModel != null) { var editInList = db.placeListRelationships.Where(p => p.placelist_id == getListInfo.list_id).Select(q => q.user_id).ToList(); if (editInList != null) { foreach (int i in editInList) { editorIDList.Add(i); } dataForm = new { info = infoItem, places = resultPlaceInfo, systemtags = systemTagResult, user_tags = resultTagInfo, editors_id = editorIDList }; } searchallplaceinlist = db.placeRelationships.Where(p => p.placelist_id == listModel.id).Select(q => q.place_id).ToList(); list_createrId = db.placeLists.Where(p => p.id == getListInfo.list_id).Select(q => q.user_id).FirstOrDefault(); var listCreator = db.users.Where(u => u.id == list_createrId).FirstOrDefault(); infoItem.id = listModel.id; infoItem.creator_id = listModel.user_id; infoItem.name = listModel.name; infoItem.creator_username = listCreator.name; infoItem.description = listModel.description; infoItem.privacy = listModel.privacy; infoItem.createdTime = listModel.created != null?listModel.created.ToString().Substring(0, 10) : ""; infoItem.updatedTime = listModel.updated != null?listModel.updated.ToString().Substring(0, 10) : ""; infoItem.coverImageURL = listModel.cover != null ? listModel.cover : null; dataForm = new { info = infoItem, places = resultPlaceInfo, systemtags = systemTagResult, user_tags = resultTagInfo, editors_id = editorIDList }; result = new { status = 1, msg = "success", data = dataForm }; } //此結果為特定清單中篩選出有標籤的地點id if (tFilterid != null && list_createrId != 0) { intersectResult = searchallplaceinlist; foreach (int i in tFilterid) { //intersectResult = tagFactory.searchTag(list_createrId, ref intersectResult, i, db); intersectResult = tagFactory.searchTag(0, ref intersectResult, i, db); //var searchplacehastag = db.tagRelationships.Where(P => P.tag_id == i).Select(q => q.place_id).ToList(); //intersectResult = intersectResult.Intersect(searchplacehastag).ToList(); //foreach(int p in unionResult) { // resultplaceid.Add(p); //} //if (editorIDList.Count > 0) //{ // foreach (int editorId in editorIDList) // { // var placeInList = new List<int>(); // var editTagPlace = db.tagRelationships.Where(p => p.user_id == editorId && p.tag_id == i).Select(q => q.place_id).ToList(); // if (editTagPlace != null) // { // foreach (int placeId in editTagPlace) // { // placeInList.Add(db.placeRelationships.Where(p => p.place_id == placeId && p.placelist_id == getListInfo.list_id).Select(q => q.place_id).FirstOrDefault()); // } // if (placeInList.Count > 0) // { intersectResult.AddRange(placeInList); // //intersectResult= intersectResult.Intersect(placeInList).ToList(); // } // } // } //} } intersectResult = intersectResult.Distinct().ToList(); if (intersectResult.Count > 0) { foreach (int i in intersectResult) { var placeItem = db.places.Where(p => p.id == i).Select(q => q).FirstOrDefault(); if (placeItem != null) { if (placeItem.type != null) { systemTagResult.Add(placeItem.type); } listDetailPlace placeDetail = new listDetailPlace(); placeDetail.id = placeItem.id; placeDetail.gmap_id = placeItem.gmap_id; placeDetail.name = placeItem.name; placeDetail.phone = placeItem.phone; placeDetail.address = placeItem.address; placeDetail.type = placeItem.type; placeDetail.photo_url = placeItem.photo != null ? placeItem.photo : null; // photo type in db is byte[] resultPlaceInfo.Add(placeDetail); } tagsList.AddRange(db.tagRelationships.Where(p => p.place_id == i && p.user_id == list_createrId).Select(q => q.tag_id).ToList()); if (editorIDList.Count > 0) { foreach (int editorId in editorIDList) { tagsList.AddRange(db.tagRelationships.Where(p => p.place_id == i && p.user_id == editorId).Select(q => q.tag_id).ToList()); } }//var test = from p in db.tagRelations where p.place_id == i group p.tag_id by p.user_id == userlogin ?"userTag":"othersTag" into g select new {g.Key } ; } tagsList = tagsList.Distinct().ToList(); systemTagResult = systemTagResult.Distinct().ToList(); dataForm = new { info = infoItem, places = resultPlaceInfo, systemtags = systemTagResult, user_tags = resultTagInfo, editors_id = editorIDList }; result = new { status = 1, msg = "success", data = dataForm }; } if (tagsList.Count > 0) { foreach (int i in tagsList) { var rtag = db.tags.Where(p => p.id == i && p.type == 2).Select(q => q).FirstOrDefault(); if (rtag != null) { tTag t = new tTag(); t.id = rtag.id; t.name = rtag.name; t.type = rtag.type; resultTagInfo.Add(t); } } } //var place = db.placeLists.Where(p => p.id == getListInfo.list_id).FirstOrDefault(); //var placeSpot = db.placeRelationships.Where(p => p.placelist_id == getListInfo.list_id).Select(p => p.place_id).ToList(); //List<placeListInfo> infoList = new List<placeListInfo>(); //List<tPlaceInfo> relationPlace = new List<tPlaceInfo>(); //List<tagInfo> tagInfoList = new List<tagInfo>(); //List<int> idList = new List<int>(); //List<int> tagList = new List<int>(); //for (int i = 0; i < placeSpot.Count(); i++) //{ // idList.Add(placeSpot[i]); //} //int[] terms = idList.ToArray(); //placeListInfo infoItem = new placeListInfo(); //infoItem.userId = place.user_id; //infoItem.name = place.name; //infoItem.description = place.description; //infoItem.privacy = place.privacy; //infoItem.createdTime = place.created.ToString(); //infoItem.updatedTime = place.updated.ToString(); ////byte[] binaryString = (byte[])place.cover; ////info.cover = Encoding.UTF8.GetString(binaryString); //infoList.Add(infoItem); //foreach (var i in terms) //{ // var tagId = db.tagRelationships.Where(p => p.place_id == i).Select(p => p.tag_id).ToList(); // tPlaceInfo exportPlaceInfo = new tPlaceInfo(); // var placeModel = db.places.FirstOrDefault(p => p.id == i); // for (int j = 0; j < tagId.Count(); j++) // { // tagList.Add(tagId[j]); // } // exportPlaceInfo.name = placeModel.name; // exportPlaceInfo.longitude = placeModel.longitude; // exportPlaceInfo.latitude = placeModel.latitude; // exportPlaceInfo.phone = placeModel.phone; // exportPlaceInfo.address = placeModel.address; // exportPlaceInfo.type = placeModel.type; // exportPlaceInfo.gmap_id = placeModel.gmap_id; // relationPlace.Add(exportPlaceInfo); //} //int[] tagArray = tagList.Distinct().ToArray(); //foreach (int t in tagArray) //{ // var exportTagInfo = new tagInfo(); // var tagInfoModel = db.tags.FirstOrDefault(p => p.id == t); // exportTagInfo.name = tagInfoModel.name; // exportTagInfo.type = tagInfoModel.type; // tagInfoList.Add(exportTagInfo); //} } return(Request.CreateResponse(HttpStatusCode.OK, result)); }
public HttpResponseMessage GetListDetail(viewModelGetListPlace getListInfo) { //無論公開或私人都會用此功能 所以要找創建清單的userid非登入者的userid List <string> systemTagResult = new List <string>(); List <int> resultplaceid = new List <int>(); List <int> searchallplaceinlist = new List <int>(); List <int> intersectResult = new List <int>(); List <int> tagsList = new List <int>(); List <int> editorIDList = new List <int>(); List <int> kingTags = new List <int>(); List <listDetailPlace> resultPlaceInfo = new List <listDetailPlace>(); List <tTag> resultTagInfo = new List <tTag>(); placeListInfo infoItem = new placeListInfo(); int[] tFilterid = getListInfo.filter; int list_createrId = 0; int userlogin = 0; userlogin = userFactory.userIsLoginSession(userlogin); userlogin = userIsLoginCookie(userlogin); userFactory.userEventRecord(0, 1, db); var dataForm = new { info = infoItem, places = resultPlaceInfo, system_tags = systemTagResult, user_tags = resultTagInfo, editors_id = editorIDList }; var result = new { status = 0, msg = "fail", data = dataForm }; var listModel = db.placeLists.Where(p => p.id == getListInfo.list_id).FirstOrDefault(); //此結果為特定清單中全部的地點id //無論公開或私人都會用此功能 所以要找創建清單的userid非登入者的userid if (listModel != null) { var editInList = db.placeListRelationships.Where(p => p.placelist_id == getListInfo.list_id).Select(q => q.user_id).ToList(); if (editInList != null) { foreach (int i in editInList) { editorIDList.Add(i); } dataForm = new { info = infoItem, places = resultPlaceInfo, system_tags = systemTagResult, user_tags = resultTagInfo, editors_id = editorIDList }; } searchallplaceinlist = db.placeRelationships.Where(p => p.placelist_id == listModel.id).Select(q => q.place_id).ToList(); list_createrId = db.placeLists.Where(p => p.id == getListInfo.list_id).Select(q => q.user_id).FirstOrDefault(); var listCreator = db.users.Where(u => u.id == list_createrId).FirstOrDefault(); infoItem.id = listModel.id; infoItem.creator_id = listModel.user_id; infoItem.name = listModel.name; infoItem.creator_username = listCreator.name; infoItem.description = listModel.description; infoItem.privacy = listModel.privacy; infoItem.createdTime = listModel.created != null?listModel.created.ToString().Substring(0, 10) : ""; infoItem.updatedTime = listModel.updated != null?listModel.updated.ToString().Substring(0, 10) : ""; infoItem.coverImageURL = listModel.cover != null ? listModel.cover : null; dataForm = new { info = infoItem, places = resultPlaceInfo, system_tags = systemTagResult, user_tags = resultTagInfo, editors_id = editorIDList }; result = new { status = 1, msg = "success", data = dataForm }; } //此結果為特定清單中篩選出有標籤的地點id if (tFilterid != null && list_createrId != 0) { intersectResult = searchallplaceinlist; foreach (int i in tFilterid) { intersectResult = tagFactory.searchTag(0, ref intersectResult, i, db); } intersectResult = intersectResult.Distinct().ToList(); if (intersectResult.Count > 0) { foreach (int i in intersectResult) { var placeItem = db.places.Where(p => p.id == i).Select(q => q).FirstOrDefault(); if (placeItem != null) { if (placeItem.type != null) { systemTagResult.Add(placeItem.type); } listDetailPlace placeDetail = new listDetailPlace(); if (userlogin == 0) { placeDetail.king_tags = kingTags.ToArray(); } else { var hasKingTag = db.tagRelationships.Where(t => t.user_id == userlogin && t.place_id == i && (t.tag_id == 101 || t.tag_id == 102 || t.tag_id == 103)).Select(t => t.tag_id).ToList(); if (hasKingTag != null) { placeDetail.king_tags = hasKingTag.ToArray(); } } location detailPlaceLocation = new location(); placeDetail.id = placeItem.id; placeDetail.gmap_id = placeItem.gmap_id; placeDetail.name = placeItem.name; placeDetail.phone = placeItem.phone; placeDetail.address = placeItem.address; placeDetail.type = placeItem.type; placeDetail.photo_url = placeItem.photo != null ? placeItem.photo : null; detailPlaceLocation.lon = placeItem.longitude; detailPlaceLocation.lat = placeItem.latitude; placeDetail.location = detailPlaceLocation; resultPlaceInfo.Add(placeDetail); } tagsList.AddRange(db.tagRelationships.Where(p => p.place_id == i && p.user_id == list_createrId).Select(q => q.tag_id).ToList()); if (editorIDList.Count > 0) { foreach (int editorId in editorIDList) { tagsList.AddRange(db.tagRelationships.Where(p => p.place_id == i && p.user_id == editorId).Select(q => q.tag_id).ToList()); } } } tagsList = tagsList.Distinct().ToList(); systemTagResult = systemTagResult.Distinct().ToList(); dataForm = new { info = infoItem, places = resultPlaceInfo, system_tags = systemTagResult, user_tags = resultTagInfo, editors_id = editorIDList }; result = new { status = 1, msg = "success", data = dataForm }; } if (tagsList.Count > 0) { foreach (int i in tagsList) { var rtag = db.tags.Where(p => p.id == i && p.type == 2).Select(q => q).FirstOrDefault(); if (rtag != null) { tTag t = new tTag(); t.id = rtag.id; t.name = rtag.name; t.type = rtag.type; resultTagInfo.Add(t); } } } } return(Request.CreateResponse(HttpStatusCode.OK, result)); }