public async Task<IEnumerable<SchedulePracticeEntity>> GetShedulePracticeAsync(int userId) { var cadet = await _context.Cadets.GetAsync(userId); if (cadet != null) { var group = cadet.Group; var sched = cadet.SchedulePractices; if (sched.Count() > 0) { var shedule = new List<SchedulePracticeEntity>(); foreach (var d in sched.Select(d => d.DayOfWeek).ToList().Distinct()) { SchedulePracticeEntity entity = new SchedulePracticeEntity(); entity.DayOfWeek = d; entity.Dates = new List<DateTime>(); DateTime startDate = group.PracticeStartDate; DateTime endDate = group.PracticeEndDate; for (DateTime i = startDate; i <= endDate; i = i.AddDays(1)) { if (DayOfWeekRus(i) == d) { ((List<DateTime>)entity.Dates).Add(i); } } entity.Practices = new List<LessonPracticeEntity>(); foreach (var lesson in sched.Where(l => l.DayOfWeek == d)) { var instr = await _context.Instructors.GetAsync(lesson.InstructorId); var less = new LessonPracticeEntity { Id = lesson.Id, StartTime = lesson.StartTime, EndTime = lesson.EndTime, Instructor = instr.Initials, Cadet = cadet.Initials, }; ((List<LessonPracticeEntity>)entity.Practices).Add(less); } shedule.Add(entity); } return shedule; } } return null; }
public async Task<string> GetSchedulePracticeReportAsync(int month, int year) { int maxDaysInMonth = DateTime.DaysInMonth(year, month); DateTime date = new DateTime(year, month, maxDaysInMonth); var groups = await _context.Groups.FindAsync(g => g.PracticeStartDate <= date); List<Cadet> cadets = new List<Cadet>(); foreach (var g in groups) { foreach (var u in g.Cadets) { cadets.Add(u); } } List<SchedulePractice> schedulePractice = new List<SchedulePractice>(); foreach (var c in cadets) { foreach (var s in c.SchedulePractices) { schedulePractice.Add(s); } } //таким велосипедом получили все занятия которые проводятся в нужном месяце string path = string.Format("{0}Reports/{1}{2}{3}.pdf", _serverPath, "РасписаниеПрактическихЗанятий", month, year); if (File.Exists(path)) { File.Delete(path); } SchedulePracticeReport report = new SchedulePracticeReport(path) { Month = month, Year = year, }; List<SchedulePracticeEntity> lessons = new List<SchedulePracticeEntity>(); foreach (var less in schedulePractice.Select(s => s.DayOfWeek).Distinct()) { SchedulePracticeEntity entity = new SchedulePracticeEntity(); entity.DayOfWeek = less; entity.Dates = DaysOfMonth(month, year).Where(d => DayOfWeekRus(d) == less); List<LessonPracticeEntity> list = new List<LessonPracticeEntity>(); foreach (var item in schedulePractice.Where(s => s.DayOfWeek == less)) { LessonPracticeEntity l = new LessonPracticeEntity { StartTime = item.StartTime, EndTime = item.EndTime, Cadet = item.Cadet.Initials, Instructor = item.Instructor.Initials, }; list.Add(l); } entity.Practices = list; lessons.Add(entity); } report.Practices = lessons; return report.GetReport(); }