protected void OnButtonAppointForwardersClicked(object sender, EventArgs e) { var toAdd = new List <AtWorkForwarder>(); foreach (var forwarder in ForwardersAtDay.Where(f => DriversAtDay.All(d => d.WithForwarder != f))) { var defaulDriver = DriversAtDay.FirstOrDefault(d => d.WithForwarder == null && d.Employee.DefaultForwarder?.Id == forwarder.Employee.Id); if (defaulDriver != null) { defaulDriver.WithForwarder = forwarder; } else { toAdd.Add(forwarder); } } if (toAdd.Count == 0) { return; } var orders = LogisticAreaRepository.OrdersCountByArea(uow, DialogAtDate, 12); var districtsBottles = orders.GroupBy(x => x.DistrictId).ToDictionary(x => x.Key, x => x.Sum(o => o.WaterCount)); foreach (var forwarder in toAdd) { var driversToAdd = DriversAtDay.Where(x => x.WithForwarder == null && x.Car != null && x.Car.TypeOfUse != CarTypeOfUse.Largus).ToList(); if (driversToAdd.Count == 0) { logger.Warn("Не осталось водителей для добавленя экспедиторов."); break; } Func <int, int> ManOnDistrict = (int districtId) => driversAtDay.Where(dr => dr.Car != null && dr.Car.TypeOfUse != CarTypeOfUse.Largus && dr.Districts.Any(dd2 => dd2.District.Id == districtId)) .Sum(dr => dr.WithForwarder == null ? 1 : 2); var driver = driversToAdd.OrderByDescending(x => districtsBottles.Where(db => x.Employee.Districts.Any(dd => dd.District.Id == db.Key)) .Max(db => (double)db.Value / ManOnDistrict(db.Key))).First(); var testSum = driversToAdd.ToDictionary(x => x, x => districtsBottles.Where(db => x.Employee.Districts.Any(dd => dd.District.Id == db.Key)) .Max(db => (double)db.Value / ManOnDistrict(db.Key))); driver.WithForwarder = forwarder; } MessageDialogWorks.RunInfoDialog("Готово."); }
void SelectDrivers_ObjectSelected(object sender, OrmReferenceObjectSectedEventArgs e) { var addDrivers = e.GetEntities <Employee>().ToList(); logger.Info("Получаем авто для водителей..."); MainClass.progressBarWin.ProgressStart(2); var onlyNew = addDrivers.Where(x => driversAtDay.All(y => y.Employee.Id != x.Id)).ToList(); var allCars = CarRepository.GetCarsbyDrivers(uow, onlyNew.Select(x => x.Id).ToArray()); MainClass.progressBarWin.ProgressAdd(); foreach (var driver in addDrivers) { if (driversAtDay.Any(x => x.Employee.Id == driver.Id)) { logger.Warn("Водитель {0} уже добавлен. Пропускаем...", driver.ShortName); continue; } var atwork = new AtWorkDriver(driver, DialogAtDate, allCars.FirstOrDefault(x => x.Driver.Id == driver.Id) ); if (driver.DefaultForwarder != null) { var forwarder = ForwardersAtDay.FirstOrDefault(x => x.Employee.Id == driver.DefaultForwarder.Id); if (forwarder == null) { if (MessageDialogWorks.RunQuestionDialog($"Водитель {driver.ShortName} обычно ездить с экспедитором {driver.DefaultForwarder.ShortName}. Он отсутствует в списке экспедиторов. Добавить его в список?")) { forwarder = new AtWorkForwarder(driver.DefaultForwarder, DialogAtDate); observableForwardersAtDay.Add(forwarder); } } if (forwarder != null && DriversAtDay.All(x => x.WithForwarder != forwarder)) { atwork.WithForwarder = forwarder; } } driversAtDay.Add(atwork); } MainClass.progressBarWin.ProgressAdd(); DriversAtDay = driversAtDay.OrderBy(x => x.Employee.ShortName).ToList(); logger.Info("Ок"); MainClass.progressBarWin.ProgressClose(); }