/// <summary> /// Realiza a inclusão de um novo PDV /// </summary> public void Create(Model.Api.PdvRaw pdvRaw) { var pdv = ZX.Service.Utils.ConvertFromRaw(pdvRaw); var aux = this.GetByDocument(pdv.Document); if (aux != null) { throw new ApplicationException($"Documento {pdv.Document} já existente. Inclusão não permitida!"); } if (pdv.IdAux == 0) { pdv.IdAux = this.GetMaxId(); } else { // Verificar se id já existe aux = this.GetById(pdv.IdAux); } if (aux != null) { throw new ApplicationException($"Id {pdv.IdAux} já existente. Inclusão não permitida!"); } if (pdv.Address == null || pdv.CoverageArea == null || string.IsNullOrEmpty(pdv.Document) || string.IsNullOrEmpty(pdv.OwnerName) || string.IsNullOrEmpty(pdv.TradingName)) { throw new ApplicationException($"Modelo inválido"); } zdContext.PDVs.InsertOne(pdv); }
public static Model.PDV ConvertFromRaw(Model.Api.PdvRaw pdvRaw) { var pdv = new Model.PDV(); if (pdvRaw.id != null) { pdv.IdAux = int.Parse(pdvRaw.id); } pdv.TradingName = pdvRaw.tradingName; pdv.OwnerName = pdvRaw.ownerName; pdv.Document = pdvRaw.document; pdv.Address = GeoJson.Point(new GeoJson2DGeographicCoordinates(pdvRaw.address.coordinates[0], pdvRaw.address.coordinates[1])); var coverageArea = new List <GeoJson2DGeographicCoordinates>(); foreach (var p in pdvRaw.coverageArea.coordinates) { foreach (var p1 in p) { foreach (var p2 in p1) { coverageArea.Add(GeoJson.Geographic(p2[0], p2[1])); } } } pdv.CoverageArea = GeoJson.Polygon(coverageArea.ToArray()); return(pdv); }
public static Model.Api.PdvRaw ConvertToRaw(Model.PDV pdv) { Model.Api.PdvRaw pdvRaw = new Model.Api.PdvRaw(); pdvRaw.id = pdv.IdAux.ToString(); pdvRaw.tradingName = pdv.TradingName; pdvRaw.ownerName = pdv.OwnerName; pdvRaw.document = pdv.Document; pdvRaw.address.type = "Point"; pdvRaw.address.coordinates = new List <double>() { pdv.Address.Coordinates.Longitude, pdv.Address.Coordinates.Latitude }; pdvRaw.coverageArea.type = "MultiPolygon"; pdvRaw.coverageArea.coordinates = new List <List <List <List <double> > > >(); pdvRaw.coverageArea.coordinates.Add(new List <List <List <double> > >()); pdvRaw.coverageArea.coordinates[0].Add(new List <List <double> >()); foreach (var p in pdv.CoverageArea.Coordinates.Exterior.Positions) { pdvRaw.coverageArea.coordinates[0][0].Add(new List <double>() { p.Longitude, p.Latitude }); } return(pdvRaw); }
/// <summary> /// Faz a busca pelo CNPJ do PDV e retorna se encontrado /// </summary> /// <param name="document">Numero do CNPJ</param> /// <returns></returns> public Model.Api.PdvRaw GetByDocument(string document) { var pdv = zdContext.PDVs.Find(d => d.Document == document.Trim()).SingleOrDefault(); Model.Api.PdvRaw pdvRaw = null; if (pdv != null) { pdvRaw = Utils.ConvertToRaw(pdv); } return(pdvRaw); }
/// <summary> /// Retorna o PDV pelo Id /// </summary> /// <param name="id">Id do PDV</param> /// <returns></returns> public Model.Api.PdvRaw GetById(int id) { var pdv = zdContext.PDVs.Find(d => d.IdAux == id).SingleOrDefault(); Model.Api.PdvRaw pdvRaw = null; if (pdv != null) { pdvRaw = Utils.ConvertToRaw(pdv); } return(pdvRaw); }
/// <summary> /// Dado uma latitude e longitude, retorna pelo PDV mais próximo /// </summary> /// <param name="lat">Latitude</param> /// <param name="lng">Longitude</param> /// <returns></returns> public Model.Api.PdvRaw GetByLatLng(double lat, double lng) { // Define o ponto de referência var p = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(new GeoJson2DGeographicCoordinates(lng, lat)); // Monta o filtro pra retornar os PDVs que atendem o ponto informado var q = Builders <Model.PDV> .Filter.GeoIntersects(_ => _.CoverageArea, p); // Retorna todos os PDVs cujo ponto informado é atendido var pdvs = zdContext.PDVs.Find(q).ToList(); Model.Api.PdvRaw pdvRaw = null; if (pdvs != null && pdvs.Count > 0) { // Ordena por distância do PDV e seleciona o primeiro var pdvMaisPerto = pdvs.OrderBy(_ => _.DistanceFrom(lng, lat)).ToList()[0]; // Transforma pro modelo de apresentação pdvRaw = Utils.ConvertToRaw(pdvMaisPerto); } return(pdvRaw); }
public void Post([FromBody] Model.Api.PdvRaw pdvRaw) { pdvService.Create(pdvRaw); }