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