// GET api/CurrentLocation/5 public TbLocation GetTbLocation(int id) { TbLocation tblocation = db.TbLocations.Find(id); if (tblocation == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } return(tblocation); }
/// <summary> /// 获取最短距离的分店 /// </summary> /// <param name="hibernateOper"></param> /// <param name="loc">地址</param> /// <param name="maxDist">最长距离</param> /// <returns></returns> public static Department GetMinDistDept(IHibernateOper hibernateOper, TbLocation loc, double maxDist) { Department retVal = null; Exec(hibernateOper, delegate(ISession s) { //查找地址码 retVal = GetMinDistDept(s, loc, maxDist); } ); return(retVal); }
// POST api/CurrentLocation public HttpResponseMessage PostTbLocation(TbLocation tblocation) { if (ModelState.IsValid) { db.TbLocations.Add(tblocation); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, tblocation); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = tblocation.locID })); return(response); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } }
// DELETE api/CurrentLocation/5 public HttpResponseMessage DeleteTbLocation(int id) { TbLocation tblocation = db.TbLocations.Find(id); if (tblocation == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } db.TbLocations.Remove(tblocation); try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } return(Request.CreateResponse(HttpStatusCode.OK, tblocation)); }
// PUT api/CurrentLocation/5 public HttpResponseMessage PutTbLocation(int id, TbLocation tblocation) { if (ModelState.IsValid && id == tblocation.locID) { db.Entry(tblocation).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } return(Request.CreateResponse(HttpStatusCode.OK)); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } }
/// <summary> /// 获取最短距离的分店 /// </summary> /// <param name="session"></param> /// <param name="loc">地址</param> /// <param name="maxDist">最长距离</param> /// <returns></returns> public static Department GetMinDistDept(ISession session, TbLocation loc, double maxDist) { Department retVal = null; double minDest = double.MaxValue; string addrCode = loc.AddrCode; string paddrCode = ""; //如果达到街道级别 if (addrCode.Length >= 9) { //获取上级地址码 paddrCode = addrCode.Substring(0, 6); } else { //获取当级地址码 paddrCode = addrCode; } ICriteria criteria = session.CreateCriteria(typeof(Department)); //模糊查找当前代码 ICriterion criterion = Restrictions.Or( Restrictions.Eq("AddrCode", addrCode), Restrictions.Like("AddrCode", paddrCode + "%") ); criteria.Add(criterion); criterion = Restrictions.Eq("Del", false); criteria.Add(criterion); criteria.AddOrder(Order.Desc("AddrCode")); criteria.SetCacheable(true); IList <Department> depts = criteria.List <Department>(); foreach (Department dept in depts) { //如果地址相同则返回该商店 if (dept.Lat == loc.Lat && dept.Lng == loc.Lng) { retVal = dept; break; } else { //计算距离 double dest = LocationUtil.GetDistance(dept.Lat, dept.Lng, loc.Lat, loc.Lng); //取最短距离的分店 if (dest <= maxDist && dest < minDest) { minDest = dest; retVal = dept; } } } return(retVal); }
/// <summary> /// 获取最短距离的分店 /// </summary> /// <param name="session"></param> /// <param name="loc">地址</param> /// <returns></returns> public static Department GetMinDistDept(ISession session, TbLocation loc) { return(GetMinDistDept(session, loc, double.MaxValue)); }
/// <summary> /// 获取最短距离的分店 /// </summary> /// <param name="hibernateOper"></param> /// <param name="loc">地址</param> /// <returns></returns> public static Department GetMinDistDept(IHibernateOper hibernateOper, TbLocation loc) { return(GetMinDistDept(hibernateOper, loc, double.MaxValue)); }