private List<CommandAnswerError> ValidateAutocompleteEmptyPlaces(AutocompleteEmptyPlacesCommand command)
        {
            var errors = new List<CommandAnswerError>();

            if (command.CountValue < 0)
            {
                errors.Add(new CommandAnswerError
                               {
                                   FieldName = "Количетсво",
                                   Title = "Количество человек не должно быть меньше нуля"
                               });
            }

            if (command.DaysOfWeek.All(b => b == false))
            {
                errors.Add(new CommandAnswerError
                {
                    FieldName = "Дни",
                    Title = "Выберите хотя бы 1 день"
                });
            }

            if (command.NextDays < 1)
            {
                errors.Add(new CommandAnswerError
                {
                    FieldName = "Количество дней",
                    Title = "Выберите хотя бы 1 день"
                });
            }

            if (command.NextDays > 31)
            {
                errors.Add(new CommandAnswerError
                {
                    FieldName = "Количество дней",
                    Title = "Нельзя регистрировать более чем за месяц"
                });
            }
            return errors;
        }
        public AutocompleteEmptyPlacesCommandAnswer AutocompleteEmptyPlaces(AutocompleteEmptyPlacesCommand command)
        {
            var user = this._tokenManager.GetUserByToken(command.Token.Value);
            var hospital = this._hospitalManager.GetHospitalByUser(user);
            var section =
                this._hospitalSectionProfileRepository.GetModels()
                    .FirstOrDefault(model => model.Id == command.HospitalSectionProfileId);

            var errors = this.ValidateAutocompleteEmptyPlaces(command);
            if (errors.Any())
            {
                var sexes =
                    Enum.GetValues(typeof(Sex))
                        .Cast<Sex>()
                        .Select(sex => new KeyValuePair<int, string>((int)sex, sex.ToCorrectString()))
                        .ToList();

                var hospitalSectionProfiles =
                    this._hospitalSectionProfileRepository.GetModels()
                        .Where(model => model.HospitalId == hospital.Id)
                        .ToList();

                var hospitalSectionProfilePairs =
                    hospitalSectionProfiles.Select(model => new KeyValuePair<int, string>(model.Id, model.Name))
                        .ToList();

                var hasGenderFactor = hospitalSectionProfiles.FirstOrDefault().HasGenderFactor;

                return new AutocompleteEmptyPlacesCommandAnswer
                           {
                               Token = command.Token.Value,
                               Errors = errors,
                               HospitalSectionProfiles = hospitalSectionProfilePairs,
                               HasGenderFactor = hasGenderFactor,
                               Sexes = sexes,
                               DaysOfWeek = command.DaysOfWeek
                           };
            }

            var placeStatistics = _emptyPlaceByTypeStatisticRepository.GetModels();

            var forNextDays = command.NextDays;
            var startDay = DateTime.Now.Date;
            var endDay = startDay.AddDays(forNextDays);

            var correctPlaceStatistics = ((IDbSet<EmptyPlaceByTypeStatisticStorageModel>)placeStatistics)
                .Include(model => model.EmptyPlaceStatistic)
                .Where(model => model.Sex == (Sex?)command.SexId)
                .Where(model => model.EmptyPlaceStatistic.Date >= startDay && model.EmptyPlaceStatistic.Date <= endDay)
                .Where(model => model.EmptyPlaceStatistic.HospitalSectionProfileId == command.HospitalSectionProfileId)
                .ToList();

            var otherGenderPlaceStatistics = ((IDbSet<EmptyPlaceByTypeStatisticStorageModel>)placeStatistics)
                .Include(model => model.EmptyPlaceStatistic)
                .Where(model => model.Sex != (Sex?)command.SexId)
                .Where(model => model.EmptyPlaceStatistic.Date >= startDay && model.EmptyPlaceStatistic.Date <= endDay)
                .Where(model => model.EmptyPlaceStatistic.HospitalSectionProfileId == command.HospitalSectionProfileId)
                .ToList();

            for (var day = 0; day < forNextDays; day++)
            {
                var nextDate = startDay.AddDays(day);

                if (!command.DaysOfWeek[(int) nextDate.Date.DayOfWeek])
                {
                    continue;
                }

                if (!correctPlaceStatistics.Select(model => model.EmptyPlaceStatistic.Date).Contains(nextDate))
                {
                    var otherGender = otherGenderPlaceStatistics.FirstOrDefault(model => model.EmptyPlaceStatistic.Date == nextDate);

                    if (otherGender != null)
                    {
                        var emptyPlaceStatisticModelId = otherGender.EmptyPlaceStatisticId;
                        var recordToAdd = new EmptyPlaceByTypeStatisticStorageModel
                        {
                            Sex = (Sex?) command.SexId,
                            Count = command.CountValue,
                            EmptyPlaceStatisticId = emptyPlaceStatisticModelId
                        };
                        this._emptyPlaceByTypeStatisticRepository.Add(recordToAdd);
                        continue;
                    }

                    var newStatistic = new EmptyPlaceStatisticStorageModel
                    {
                        Date = nextDate,
                        HospitalSectionProfileId = command.HospitalSectionProfileId,
                        CreateTime = DateTime.Now,
                        EmptyPlaceByTypeStatistics = new[]
                        {
                            new EmptyPlaceByTypeStatisticStorageModel
                            {
                                Sex = (Sex?) command.SexId,
                                Count = command.CountValue
                            }
                        }
                    };
                    this._emptyPlaceStatisticRepository.Add(newStatistic);
                }
            }

            this._emptyPlaceStatisticRepository.SaveChanges();

            var messageText = $"Автозаполнение свободных дат для отделения *{section.Name}* было успешно выполнено";

            return new AutocompleteEmptyPlacesCommandAnswer
                       {
                           Token = command.Token.Value,
                           HasDialogMessage = true,
                           DialogMessage = messageText
                       };
        }
        public ActionResult AutocompleteEmptyPlaces(AutocompleteEmptyPlacesCommand command)
        {
            var answer = _hospitalRegistrationsService.AutocompleteEmptyPlaces(command);

            if (answer.Errors.Any())
            {
                return this.View(
                    "ShowAutocompletePage",
                    new ShowAutocompletePageCommandAnswer
                        {
                            HospitalSectionProfileId = command.HospitalSectionProfileId,
                            SexId = command.SexId,
                            Token = command.Token.Value,
                            CountValue = command.CountValue,
                            Errors = answer.Errors,
                            HasGenderFactor = answer.HasGenderFactor,
                            HospitalSectionProfiles = answer.HospitalSectionProfiles,
                            Sexes = answer.Sexes,
                            DaysOfWeek = answer.DaysOfWeek
                        });
            }

            return RedirectToAction("ShowAutocompletePage", "ChangeHospitalRegistrationPage", new
            {
                Token = command.Token,
                HospitalSectionProfileId = command.HospitalSectionProfileId,
                SexId = command.SexId,
                answer.DialogMessage,
                answer.HasDialogMessage
            });
        }