Ejemplo n.º 1
0
        public static DistanceConfigModel GetData()
        {
            var model = new DistanceConfigModel();
            XDocument doc = XDocument.Load(XmlPath);

            var status = doc.Root.Element("status");
            if (status != null)
            {
                model.Status = Int32.Parse(status.Value);
            }

            var from = doc.Root.Element("from");
            if (from != null)
            {
                model.From = Int32.Parse(from.Value);
            }

            var to = doc.Root.Element("to");
            if (to != null)
            {
                model.To = Int32.Parse(to.Value);
            }

            return model;
        }
Ejemplo n.º 2
0
        public static void SetData(DistanceConfigModel model)
        {
            XDocument doc = XDocument.Load(XmlPath);

            var status = doc.Root.Element("status");
            if (status != null)
            {
                status.Value = model.Status + "";
            }

            var from = doc.Root.Element("from");
            if (from != null)
            {
                from.Value = model.From + "";
            }

            var to = doc.Root.Element("to");
            if (to != null)
            {
                to.Value = model.To + "";
            }

            doc.Save(XmlPath);
        }
Ejemplo n.º 3
0
        public static void CalculateDistance()
        {
            var model = DistanceConfigHelper.GetData();
            if (model.Status == (int)DistanceStatus.Finish)
            {
                return;
            }
            int i = model.From;
            int j = model.To;
            var math = new MathHelper();

            using (var context = new SmartBuyEntities())
            {
                var markets = context.Markets
                    .Where(x => x.IsActive && x.Latitude != null && x.Longitude != null)
                    .ToList();
                while (true)
                {
                    var lat1 = markets[i].Latitude.Value;
                    var lng1 = markets[i].Longitude.Value;
                    var lat2 = markets[j].Latitude.Value;
                    var lng2 = markets[j].Longitude.Value;
                    var distance = math.TravelDistance(lat1, lng1, lat2, lng2);

                    // OK
                    if (distance != -1)
                    {
                        var m1 = markets[i];
                        var m2 = markets[j];
                        var tmp = context.MarketDistances
                            .FirstOrDefault(x => x.FromMarket == m1.Id && x.ToMarket == m2.Id);
                        if (tmp != null)
                        {
                            tmp.Distance = distance;
                        }
                        else
                        {
                            var mDistance = new MarketDistance
                                                {
                                                    FromMarket = markets[i].Id,
                                                    ToMarket = markets[j].Id,
                                                    Distance = distance
                                                };
                            context.MarketDistances.Add(mDistance);
                        }

                        j++;
                        if (j == markets.Count)
                        {
                            i++;

                            // Finish
                            if (i == markets.Count - 1)
                            {
                                DistanceConfigHelper.Finish();
                                context.SaveChanges();
                                return;
                            }
                            j = i + 1;
                        }
                    }
                    else
                    {
                        // Something's wrong, out of quota
                        var savePoint = new DistanceConfigModel
                                            {
                                                From = i,
                                                To = j,
                                                Status = (int) DistanceStatus.Going
                                            };

                        // Save
                        DistanceConfigHelper.SetData(savePoint);
                        context.SaveChanges();
                        return;
                    }
                }
            }
        }