Beispiel #1
0
        private void MoveDealerRealtion(int oldDealerId, int newDealerId)
        {
            using (var context = new DMToolContext())
            {
                try
                {
                    context.Database.ExecuteSqlCommand(string.Format(Command, oldDealerId, newDealerId));

                    var oldconProject = context.ConnProjects.FirstOrDefault(c => c.Id == oldDealerId);

                    var oldcampReg = context.CampaignRegistrations.Where(c => c.DealerId == oldDealerId).ToList();

                    if (oldconProject != null)
                    {
                        context.ConnProjects.Remove(oldconProject);
                        context.SaveChanges();

                        context.ConnProjects.Add(new ConnProject
                        {
                            Id              = newDealerId,
                            CompleteWheels  = oldconProject.CompleteWheels,
                            ServiceContract = oldconProject.ServiceContract,
                            ServiceOnline   = oldconProject.ServiceOnline,
                            ServicePrice    = oldconProject.ServicePrice
                        });
                    }
                    else
                    {
                        context.ConnProjects.Add(new ConnProject
                        {
                            Id = newDealerId
                        });
                    }

                    if (oldcampReg != null)
                    {
                        context.CampaignRegistrations.RemoveRange(oldcampReg);
                        context.SaveChanges();

                        foreach (var item in oldcampReg)
                        {
                            context.CampaignRegistrations.Add(new CampaignRegistration
                            {
                                DealerId   = newDealerId,
                                CampaignId = item.CampaignId,
                                Status     = item.Status
                            });
                        }
                    }

                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    _logger.ErrorFormat("SyncDealer error in MoveDealerRealtion - {0}", e.ToString());
                }
            }
        }
Beispiel #2
0
        private async void CreateNavigationProperties(int dealerId)
        {
            using (var context = new DMToolContext())
            {
                try
                {
                    foreach (var type in TypesWithDealer)
                    {
                        if (type.GetProperty("Year") == null)
                        {
                            var creat = Activator.CreateInstance(type);
                            creat.GetType().GetProperty("DealerId").SetValue(creat, dealerId);

                            context.Set(type).Add(creat);
                        }
                        else
                        {
                            var entities = context.Set(type);
                            var enList   = await entities.SqlQuery("SELECT * FROM " + type.Name + "s").ToListAsync();

                            var years = enList.Select(e => e.GetType().GetProperty("Year").GetValue(e)).Distinct();

                            foreach (var year in years)
                            {
                                var creat = Activator.CreateInstance(type);

                                creat.GetType().GetProperty("DealerId").SetValue(creat, dealerId);
                                creat.GetType().GetProperty("Year").SetValue(creat, year);

                                context.Set(type).Add(creat);
                            }
                        }
                    }

                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    _logger.ErrorFormat("SyncDealer error create navigation property - {0}", e.ToString());
                }
            }
        }
Beispiel #3
0
        public void SyncShowRooms()
        {
            SyncCities();

            var dealersFromApi = Send <IEnumerable <ShowRoomDto> >(
                HttpMethod.Get,
                _showroomUrl);

            int nextDealerId;

            foreach (var apiDealer in dealersFromApi)
            {
                try
                {
                    var dbDealer = _dealerRepository.FirstOrDefault(x => x.Gssn == apiDealer.Gssn);

                    if (dbDealer == null)
                    {
                        var busy = _dealerRepository.FirstOrDefault(apiDealer.Id);

                        //If Id is occupied by other dealers, then we move it with all references
                        if (busy != null && busy.Id == apiDealer.Id)
                        {
                            nextDealerId = GetNewId();
                            MoveDealer(busy, nextDealerId);
                        }

                        CreateDealer(apiDealer);

                        //Add dealer records to all entities
                        CreateNavigationProperties(apiDealer.Id);
                    }
                    else
                    {
                        // If Id does not match
                        if (dbDealer.Id != apiDealer.Id)
                        {
                            var busy = _dealerRepository.FirstOrDefault(apiDealer.Id);
                            // If this id has other dealer
                            if (busy != null)
                            {
                                //Move dealer with this id
                                nextDealerId = GetNewId();
                                MoveDealer(busy, nextDealerId);
                            }

                            CreateDealer(apiDealer);

                            var newDealer = _dealerRepository.FirstOrDefault(apiDealer.Id);

                            //Move ref with oldId on newId
                            MoveDealerRealtion(dbDealer.Id, apiDealer.Id);

                            //Del dealer with old id
                            var delDealers = _dealerRepository.GetAll().Where(d => d.Id == dbDealer.Id).ToList();

                            foreach (var delDealer in delDealers)
                            {
                                _dealerRepository.Delete(delDealer);
                            }

                            _dealerRepository.SaveChanges();
                        }
                        else
                        {
                            if (dbDealer.Name != apiDealer.Name || dbDealer.Id != apiDealer.Id ||
                                dbDealer.Name != apiDealer.Name || dbDealer.CityId != apiDealer.CityId ||
                                dbDealer.Address != apiDealer.Address || dbDealer.CoFiCo != apiDealer.Cofico ||
                                dbDealer.Fax != apiDealer.Fax || dbDealer.Gssn != apiDealer.Gssn ||
                                Math.Abs(dbDealer.Latitude - apiDealer.Latitude) > 0.01 ||
                                Math.Abs(dbDealer.Longitude - apiDealer.Longitude) > 0.01 ||
                                dbDealer.Phone != apiDealer.Phone || dbDealer.Url != apiDealer.Site)
                            {
                                dbDealer.Name      = apiDealer.Name;
                                dbDealer.Id        = apiDealer.Id;
                                dbDealer.Name      = apiDealer.Name;
                                dbDealer.CityId    = apiDealer.CityId;
                                dbDealer.Address   = apiDealer.Address;
                                dbDealer.CoFiCo    = apiDealer.Cofico ?? "0";
                                dbDealer.Fax       = apiDealer.Fax;
                                dbDealer.Gssn      = apiDealer.Gssn;
                                dbDealer.Latitude  = apiDealer.Latitude;
                                dbDealer.Longitude = apiDealer.Longitude;
                                dbDealer.Phone     = apiDealer.Phone;
                                dbDealer.Url       = apiDealer.Site;

                                _dealerRepository.SaveChanges();
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    _logger.ErrorFormat("SyncDealer error({0}) - {1}", apiDealer.Id, e);
                }
            }

            try
            {
                var dealerFromApiIds = dealersFromApi.Select(x => x.Id).ToList();
                using (var context = new DMToolContext())
                {
                    var dealerToDelete = context.Dealers.Where(x => !x.IsArchive && !dealerFromApiIds.Contains(x.Id)).ToList();
                    if (dealerToDelete.Any())
                    {
                        dealerToDelete.ForEach(delaer =>
                        {
                            delaer.IsArchive = true;
                        });
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorFormat("SyncDealer error delete - {0}", ex.ToString());
            }
        }