public bool UpdatePatrolLocation(List <PatrolLastLocationDTO> patrolLocations) { var allPatrols = operationalDataContext.Patrols.ToList(); var list = new List <PatrolLastLocation>(); var index = 1; foreach (var item in patrolLocations) { var patrol = new PatrolLastLocation { PatrolLatLocationId = index++, Altitude = 1, IsNoticed = false, Latitude = item.Latitude, LocationDate = item.LocationDate, Longitude = item.Longitude, PatrolCode = item.PatrolCode, PatrolId = item.PatrolId, Speed = item.Speed, StatusId = item.StatusId.HasValue ? item.StatusId.Value : 1, StatusName = item.StatusName != null ? item.StatusName : "Available", }; var pointString = string.Format( "POINT({0} {1})", item.Longitude.ToString(), item.Latitude.ToString()); DbGeography dbGeography = DbGeography.FromText(pointString); patrol.GeoLocation = dbGeography; var patrolItem = allPatrols.FirstOrDefault(x => x.PatrolPlateNo == item.PatrolPlateNo); if (patrolItem != null) { if (patrolItem.IsInTFM.HasValue && !patrolItem.IsInTFM.Value) { patrolItem.IsInTFM = true; patrolItem.PatrolOriginalId = item.PatrolOriginalId; } patrol.PatrolId = patrolItem.PatrolId; //index++; list.Add(patrol); } } // 25.04.2016 Commented to solve the production issue //operationalDataContext.PatrolLastLocations.RemoveRange(operationalDataContext.PatrolLastLocations.ToList().Where(x => patrolLocations.Any(y => y.PatrolId == x.PatrolId))); operationalDataContext.PatrolLastLocations.RemoveRange(operationalDataContext.PatrolLastLocations.ToList().Where(x => patrolLocations.Any(y => y.PatrolCode == x.PatrolCode))); operationalDataContext.PatrolLastLocations.AddRange(list); return(operationalDataContext.SaveChanges() > 0); }
public bool UpdateOfficerLocation(string officerCode, double lat, double lon) { var pointString = string.Format("POINT({0} {1})", lon.ToString(), lat.ToString()); DbGeography dbGeography = DbGeography.FromText(pointString); var officer = operationalDataContext.Patrols.Where(x => x.PatrolCode == officerCode).FirstOrDefault(); long maxIndex = 0; if (operationalDataContext.PatrolLastLocations != null) { maxIndex = operationalDataContext.PatrolLastLocations.Select(y => y.PatrolLatLocationId).Max(); } if (officer == null) { officer = new Patrol { PatrolCode = officerCode, PatrolPlateNo = "officer", DateCreated = DateTime.Now, PatrolOriginalId = Guid.NewGuid(), StatusId = 1, StatusName = "Available", IsDeleted = false, IsPatrol = false }; operationalDataContext.Patrols.Add(officer); if (!(operationalDataContext.SaveChanges() > 0)) { return(false); } } var officerLocation = operationalDataContext.PatrolLastLocations.Where(p => p.PatrolCode == officerCode).FirstOrDefault(); if (officerLocation == null) { officerLocation = new PatrolLastLocation { PatrolLatLocationId = ++maxIndex, PatrolId = officer.PatrolId, PatrolCode = officer.PatrolCode, Altitude = 1, Latitude = lat, Longitude = lon, IsNoticed = false, GeoLocation = dbGeography, LocationDate = DateTime.Now, Speed = 0, StatusId = 1, StatusName = "Available", IsPatrol = false }; operationalDataContext.PatrolLastLocations.Add(officerLocation); } else { officerLocation.Latitude = lat; officerLocation.Longitude = lon; officerLocation.GeoLocation = dbGeography; officerLocation.LocationDate = DateTime.Now; officerLocation.IsNoticed = false; } if (!(operationalDataContext.SaveChanges() > 0)) { return(false); } return(true); }