/// <summary> /// 建構式,傳入多筆行事曆及代課教師 /// </summary> /// <param name="Calendars"></param> /// <param name="Teacher"></param> public frmAbsenceConfirm(List<CalendarRecord> Calendars) { InitializeComponent(); if (K12.Data.Utility.Utility.IsNullOrEmpty(Calendars)) { MessageBox.Show("沒有選取的行事曆!"); this.Close(); return; } mCalendars = Calendars; ReplaceCalendarList mRepalceCalendarList = new ReplaceCalendarList(); mReplacePairs = new List<ReplaceCalendarList>(); mRepalceCalendarList.DateTimes = mCalendars.Select(x => x.StartDateTime).ToList(); mRepalceCalendarList.Calendars = mCalendars; mReplacePairs.Add(mRepalceCalendarList); }
/// <summary> /// 重新整理UI /// </summary> private void RefreshUI() { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (sender, e) => { List<SchoolYearSemesterDate> SchoolYearSemesterDates = mAccessHelper.Select<SchoolYearSemesterDate>(); SchoolYearSemesterDate SelectedSchoolYearSemesterDate = null; Tuple<DateTime, DateTime> StartEndDate = new Tuple<DateTime, DateTime>(new DateTime(), new DateTime()); //取得目前學年度學期 foreach (SchoolYearSemesterDate vSchoolYearSemesterDate in SchoolYearSemesterDates) { StartEndDate = vSchoolYearSemesterDate.GetStartEndDate(); if (mCalendars[0].StartDateTime >= StartEndDate.Item1 && mCalendars[0].EndDateTime <= StartEndDate.Item2) { SelectedSchoolYearSemesterDate = vSchoolYearSemesterDate; break; } } if (SelectedSchoolYearSemesterDate != null) { //取得所有要代課的日期 List<DateTime> NextDateTimes = mCalendars.Select(x=>x.StartDateTime).ToList(); NextDateTimes = GetNextDateTimes(NextDateTimes); //測試下個日期區間是否在學年度學期內 while (IsInDateRange(NextDateTimes,StartEndDate.Item1,StartEndDate.Item2)) { ReplaceCalendarList Pair = new ReplaceCalendarList(); Pair.DateTimes = NextDateTimes; mReplacePairs.Add(Pair); NextDateTimes = GetNextDateTimes(NextDateTimes); } } if (mReplacePairs.Count >1) { List<DateTime> SelectedDates = new List<DateTime>(); for (int i = 1; i < mReplacePairs.Count; i++) { mReplacePairs[i].DateTimes.ForEach (x => { if (!SelectedDates.Contains(x)) SelectedDates.Add(x); } ); } List<CalendarRecord> records = Calendar.Instance.FindByTeacherNameAndDates( mCalendars[0].TeacherName, SelectedDates); List<ReplaceCalendarList> removeList = new List<ReplaceCalendarList>(); for (int i = 1; i < mReplacePairs.Count; i++) { ReplaceCalendarList CurrentPair = mReplacePairs[i]; CurrentPair.Calendars = new List<CalendarRecord>(); for(int j=0;j<CurrentPair.DateTimes.Count;j++) { CalendarRecord mCalendar = mCalendars[j]; CalendarRecord vCalendar = records.Find(x => x.StartDateTime.ToShortDateString().Equals(CurrentPair.DateTimes[j].ToShortDateString()) && x.Weekday.Equals(mCalendar.Weekday) && x.Period.Equals(mCalendar.Period) && x.TeacherName.Equals(mCalendar.TeacherName) && x.ClassName.Equals(mCalendar.ClassName) && x.ClassroomName.Equals(mCalendar.ClassroomName) && x.FullSubject.Equals(mCalendar.FullSubject)); if (vCalendar != null) if (string.IsNullOrWhiteSpace(vCalendar.ReplaceID) && string.IsNullOrWhiteSpace(vCalendar.ExchangeID)) CurrentPair.Calendars.Add(vCalendar); } if (K12.Data.Utility.Utility.IsNullOrEmpty(CurrentPair.Calendars)) removeList.Add(CurrentPair); } removeList.ForEach(x => mReplacePairs.Remove(x)); } }; worker.RunWorkerCompleted += (sender, e) => { grdReplace.Rows.Clear(); foreach (CalendarRecord vCalendar in mCalendars) { int RowIndex = grdReplace.Rows.Add(); DataGridViewRow row = grdReplace.Rows[RowIndex]; row.Tag = vCalendar; row.SetValues(vCalendar.TeacherName, vCalendar.Date, vCalendar.DisplayWeekday, vCalendar.Period, vCalendar.ClassName + " " + vCalendar.FullSubject); } List<string> AbsenceList = Utility.GetAbsenceList(); AbsenceList.ForEach(x => cmbAbsenceName.Items.Add(x)); }; worker.RunWorkerAsync(); }