Ejemplo n.º 1
0
        public void AddTestData()
        {
            var prevTime = time;

            try
            {
                var time = new OffsetSystemTimeService();
                this.time = time;
                //dbContext.ChangeTracker.AutoDetectChangesEnabled = false;

                var    positionId   = dbContext.Position.First().Id;
                var    rand         = new Random(0);
                var    workers      = new List <Employee>();
                var    seniors      = new List <Employee>();
                var    juniors      = new List <Employee>();
                var    advanceYears = new[] { 5, 8, 20, 50, 150, 190 };
                double leaveRate    = 0.15;
                time.AddYears(-advanceYears.Length);

                const int maxRating = 50;

                EmployeeDetail viewModel;
                Employee       employee;
                for (int i = 0; i < 200; i++)
                {
                    viewModel          = GetNewEmployeeViewModel();
                    viewModel.Name     = $"Employee {(i + 1).ToString("0000")}";
                    viewModel.Position = positionId;

                    employee = new Employee();
                    workers.Add(employee);
                    juniors.Add(employee);
                    UpdateEmployeeFromView(employee, viewModel);
                    dbContext.Employee.Add(employee);

                    if (seniors.Count > 0)
                    {
                        employee.Manager = seniors[rand.Next(seniors.Count)];
                    }

                    if (juniors.Count >= Math.Max(3, seniors.Count * 5))
                    {
                        //dbContext.ChangeTracker.DetectChanges();
                        seniors = juniors;
                        juniors = new List <Employee>();
                    }

                    if (advanceYears.Contains(i))
                    {
                        time.AddYears(1);
                        for (int j = workers.Count - 1; j >= 0; j--)
                        {
                            var item = workers[j];
                            if (rand.NextDouble() < leaveRate)
                            {
                                item.Name += " (unemployed)";
                                workers.RemoveAt(j);
                                if (seniors.Remove(item))
                                {
                                    var unmanagedJuniors = juniors.Where(x => x.Manager == item).ToList();
                                    if (seniors.Count == 0 && unmanagedJuniors.Count > 1)
                                    {
                                        var manager = unmanagedJuniors[0];
                                        //dbContext.ChangeTracker.DetectChanges();
                                        seniors.Add(manager);
                                        juniors.Remove(manager);
                                        foreach (var unmanaged in unmanagedJuniors)
                                        {
                                            unmanaged.Manager = manager;
                                        }
                                    }
                                    else
                                    {
                                        foreach (var unmanaged in unmanagedJuniors)
                                        {
                                            unmanaged.Manager = seniors[rand.Next(seniors.Count)];
                                        }
                                    }
                                }
                                juniors.Remove(item);
                                continue;
                            }

                            var itemView = GetEmployeeViewModel(item);
                            if (item.Manager == null)
                            {
                                itemView.Rating = RatingToFloat(maxRating);
                            }
                            else if (itemView.PrevRating1 == 0)
                            {
                                itemView.Rating = RatingToFloat(rand.Next(0, maxRating));
                            }
                            else
                            {
                                itemView.Rating = RatingToFloat(rand.Next(
                                                                    Math.Max(0, RatingToInt(itemView.PrevRating1) - 15),
                                                                    Math.Min(maxRating, RatingToInt(itemView.PrevRating1) + 15)
                                                                    ));
                            }
                            UpdateEmployeeFromView(item, itemView);
                        }
                    }
                }
                dbContext.SaveChanges();
            }
            finally
            {
                time = prevTime;
                dbContext.ChangeTracker.AutoDetectChangesEnabled = true;
            }
        }