public ActionResult ApplyChangesNewHospitalRegistration(GetChangeNewHospitalRegistrationCommand command)
        {
            var answer = _hospitalRegistrationsService.ApplyChangesNewHospitalRegistration(command);

            if (answer.Errors.Any())
            {
                return View("ChangeHospitalRegistrationForNewSection", new ChangeHospitalRegistrationForNewSectionCommandAnswer
                {
                    Date = command.Date,
                    Token = command.Token.Value,
                    FreeHospitalSectionsForRegistration = answer.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> ValidateApplyChangesNewHospitalRegistrationCommand(
            GetChangeNewHospitalRegistrationCommand command)
        {
            List<CommandAnswerError> list = new List<CommandAnswerError>();
            foreach (HospitalRegistrationCountStatisticItem element in command.CountFreePlaces)
            {
                if (element.OpenCount < 0)
                    list.Add(new CommandAnswerError
                    {
                        FieldName = "Число свободных мест (Пол: " + element.Sex?.ToCorrectString() + ")",
                        Title = "Число свободных мест не может быть отрицательным"
                    });

            }
            return list;
        }
        public GetChangeNewHospitalRegistrationCommandAnswer ApplyChangesNewHospitalRegistration(
            GetChangeNewHospitalRegistrationCommand command)
        {
            var date = DateTime.ParseExact(command.Date.Split(' ').First(), "MM/dd/yyyy", CultureInfo.InvariantCulture);

            var errors = this.ValidateApplyChangesNewHospitalRegistrationCommand(command);

            if (errors.Any())
            {
                return new GetChangeNewHospitalRegistrationCommandAnswer
                {
                    Date = command.Date,
                    Token = command.Token.Value,
                    Errors = errors,
                    FreeHospitalSectionsForRegistration = GetFreeSectionsList(command.Date, command.Token.Value)
                };
            }
            var hospitalSectionProfileName =
                _hospitalSectionProfileRepository.GetModels().FirstOrDefault(model => model.Id == command.HospitalProfileId).Name;

            var recordExists =
                _emptyPlaceStatisticRepository.GetModels()
                    .Any(model => model.Date == date && model.HospitalSectionProfileId == command.HospitalProfileId);

            if (!recordExists)
            {
                var newHospitalSectionProfileId = new EmptyPlaceStatisticStorageModel
                {
                    Date = date,
                    CreateTime = DateTime.Now,
                    HospitalSectionProfileId = command.HospitalProfileId,
                    EmptyPlaceByTypeStatistics = command.CountFreePlaces
                        .Select(pair => new EmptyPlaceByTypeStatisticStorageModel
                        {
                            Sex = pair.Sex,
                            Count = pair.OpenCount
                        }
                        ).ToList()
                };

                _emptyPlaceStatisticRepository.Add(newHospitalSectionProfileId);
                _emptyPlaceStatisticRepository.SaveChanges();
            }

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

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