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 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
            };
        }