public ActionResult ApplyChangesHospitalRegistration(GetChangeHospitalRegistrationCommand command)
        {
            var answer = _hospitalRegistrationsService.ApplyChangesHospitalRegistration(command);
            if (answer.Errors.Any())
            {
                return View("ChangeHospitalRegistration", new ChangeHospitalRegistrationForSelectedSectionCommandAnswer
                {
                    Date = command.Date,
                    HospitalProfileId = command.HospitalProfileId,
                    Token = command.Token.Value,
                    StatisticItems = command.FreeHospitalSectionsForRegistration,
                    Errors = answer.Errors
                });
            }

            return RedirectToAction("Step2", "ChangeHospitalRegistrationPage", new
            {
                Token = answer.Token,
                Date = DateTime.ParseExact(command.Date.Split(' ').First(), "MM/dd/yyyy", CultureInfo.InvariantCulture),
                answer.DialogMessage,
                answer.HasDialogMessage
            });
        }
        private List<CommandAnswerError> ValidateApplyChangesHospitalRegistrationCommand(
            GetChangeHospitalRegistrationCommand command)
        {
            List<CommandAnswerError> list = new List<CommandAnswerError>();
            foreach (HospitalRegistrationCountStatisticItem element in command.FreeHospitalSectionsForRegistration)
            {
                if (element.FreePlacesCount < 0)
                    list.Add(new CommandAnswerError
                    {
                        FieldName = "Число свободных мест",
                        Title = "Число свободных мест не может быть отрицательным"
                    });

            }
            return list;
        }
        public GetChangeHospitalRegistrationCommandAnswer ApplyChangesHospitalRegistration(
            GetChangeHospitalRegistrationCommand command)
        {
            DateTime date = DateTime.ParseExact(command.Date.Split(' ').First(), "MM/dd/yyyy", CultureInfo.InvariantCulture);

            var errors = this.ValidateApplyChangesHospitalRegistrationCommand(command);

            if (errors.Any())
            {
                return new GetChangeHospitalRegistrationCommandAnswer
                {
                    Date = command.Date,
                    Token = command.Token.Value,
                    Errors = errors
                };
            }

            var emptyPlaceStatisticsId =
            _emptyPlaceStatisticRepository.GetModels()
                .FirstOrDefault(model => model.HospitalSectionProfileId == command.HospitalProfileId && model.Date == date)
                .Id;

            var freeHospitalSectionsForRegistrationList = command.FreeHospitalSectionsForRegistration;

            var freeHospitalSectionsForRegistrationInTable =
                ((IDbSet<EmptyPlaceByTypeStatisticStorageModel>) _emptyPlaceByTypeStatisticRepository.GetModels())
                    .Where(model => model.EmptyPlaceStatisticId == emptyPlaceStatisticsId).ToList();

            var result = freeHospitalSectionsForRegistrationList
                .Select(element => freeHospitalSectionsForRegistrationInTable
                    .Where(model => element != null
                        && model.Sex.ToString() == element.Sex.ToString())
                        .Select(model => new EmptyPlaceByTypeStatisticStorageModel
                        {
                            Id = model.Id,
                            Count = element.RegisteredCount + element.FreePlacesCount,
                            Sex = element.Sex,
                            EmptyPlaceStatisticId = emptyPlaceStatisticsId
                        }).FirstOrDefault()).ToList();

            foreach (var emptyPlace in result)
            {
                if (emptyPlace == null)
                {
                    var source = freeHospitalSectionsForRegistrationList.FirstOrDefault(item => result.Where(model => model != null).All(model => model.Sex != item.Sex));
                    var newEmptyPlace = new EmptyPlaceByTypeStatisticStorageModel
                    {
                        Count = source.OpenCount,
                        Sex = source.Sex,
                        EmptyPlaceStatisticId = result.FirstOrDefault(model => model != null).EmptyPlaceStatisticId
                    };
                    _emptyPlaceByTypeStatisticRepository.Add(newEmptyPlace);
                }
                else
                {
                    _emptyPlaceByTypeStatisticRepository.Update(emptyPlace.Id, emptyPlace);
                }
            }
            _emptyPlaceByTypeStatisticRepository.SaveChanges();

            var messageText = $"Количество свободных мест для {command.SectionProfileName} успешно изменено";

            return new GetChangeHospitalRegistrationCommandAnswer
            {
                Token = (Guid)command.Token,
                HasDialogMessage = true,
                DialogMessage = messageText
            };
        }