public static void Save(string path, Schedule[] schedules, ClassRepository classRepository, LessonRepository lessonRepository) { IWorkbook workbook = null; var directoryInfo = new DirectoryInfo(path); if (!directoryInfo.Parent.Exists) { Directory.CreateDirectory(directoryInfo.Parent.FullName); } var fs = File.OpenWrite(path); if (path.IndexOf(".xlsx") > 0) // 2007版本 { workbook = new XSSFWorkbook(); } else if (path.IndexOf(".xls") > 0) // 2003版本 { workbook = new HSSFWorkbook(); } var lessons = lessonRepository.Get(); var classes = classRepository.Get(); if (schedules.Length > 0) { var classSchedules = new Dictionary <Class, List <Schedule> >(); foreach (var schedule in schedules) { var @class = classes.Where(_class => _class.ID == schedule.ClassID).First(); if (!classSchedules.ContainsKey(@class)) { var _schedules = new List <Schedule>() { schedule }; classSchedules.Add(@class, _schedules); } else { classSchedules[@class].Add(schedule); } } var classSchedulesList = classSchedules.ToList(); classSchedulesList.Sort((x, y) => { return(x.Key.ID.CompareTo(y.Key.ID)); }); foreach (var keyValue in classSchedulesList) { var _class = keyValue.Key; var _schedules = keyValue.Value; ISheet sheet = workbook.CreateSheet(_class.Name); int rowIndex = 0; int colIndex = 0; IRow row = null; row = sheet.CreateRow(rowIndex++); row.CreateCell(colIndex++).SetCellValue("一"); row.CreateCell(colIndex++).SetCellValue("二"); row.CreateCell(colIndex++).SetCellValue("三"); row.CreateCell(colIndex++).SetCellValue("四"); row.CreateCell(colIndex++).SetCellValue("五"); rowIndex = 0; colIndex = 0; foreach (var schedule in _schedules) { var lesson = lessons.Where(_lesson => _lesson.ID == schedule.LessonID).First(); rowIndex = lesson.No; colIndex = lesson.WeekDay - 1; var course = _class.Courses.Where(_course => _course.CourseType == schedule.CourseType).First(); row = sheet.GetRow(rowIndex); if (row == null) { row = sheet.CreateRow(rowIndex); } row.CreateCell(colIndex).SetCellValue(course.Name); } } } workbook.Write(fs); fs.Close(); }
public Schedule[] Do(ClassRepository classRepository, ScheduleRepository scheduleRepository, LessonRepository lessonRepository) { Schedule[] result = null; var lessons = new List <Lesson>(lessonRepository.Get()); var classes = classRepository.Get(); var classesCount = classes.Count(); List <Schedule> schedules = new List <Schedule>(); List <Class> isScheduleClasses = new List <Class>(); int days = 0; int period = 0; foreach (var lesson in lessons) { days = Math.Max(lesson.WeekDay, days); period = Math.Max(lesson.No, period); } while (true) { foreach (var @class in classes) { if (isScheduleClasses.Contains(@class)) { continue; } @class.ResetCourse(); bool flag1 = false; List <Schedule> _schedules = new List <Schedule>(); var keyValuePairs = @class.GetLessonCourses(lessons, days, period); foreach (var keyValuePair in keyValuePairs) { var lesson = keyValuePair.Key; var course = keyValuePair.Value; var teacherID = course.TeacherID; var lessonID = lesson.ID; if (!scheduleRepository.IsTeacherAvaliable(teacherID, lessonID)) { flag1 = true; break; } else { if (teacherID > 0) { var temp = new List <Schedule>(); temp.AddRange(schedules); temp.AddRange(_schedules); var _schedulesResult = temp.Where(schedule => schedule.TeacherID > 0 && schedule.TeacherID == teacherID && schedule.LessonID == lessonID); if (_schedulesResult.Count() > 0) { flag1 = true; break; } } flag1 = false; course.ScheduleLesson(); _schedules.Add(new Schedule(ScheduleType.Schedule, @class.ID, lessonID, teacherID, course.CourseType)); continue; } } if (flag1) { continue; } else { isScheduleClasses.Add(@class); schedules.AddRange(_schedules); } } if (isScheduleClasses.Count == classesCount) { // 筛选结果 foreach (var lesson in lessons) { var id = lesson.ID; var _schedules = schedules.Where(schedule => schedule.LessonID == id && schedule.TeacherID > 0); var distinct = _schedules.Distinct(m => m.TeacherID); if (distinct.Count() < _schedules.Count()) { isScheduleClasses.Clear(); schedules.Clear(); } } if (schedules.Count > 0) { // 保存结果 result = new Schedule[schedules.Count]; schedules.CopyTo(result); break; } else { continue; } } } return(result); }
public Lesson LessonGet(int id) { return lessonRepository.Get(id); }
// GET: api/Lesson public async Task <IEnumerable <Lesson> > Get() { LessonRepository repository = new LessonRepository(); return(await repository.Get()); }