private void btnFinish_Click(object sender, EventArgs e)
        {
            // 取得畫面上上課日期
            List<DateTime> dtList = new List<DateTime>();
            foreach (DataGridViewRow drv in dgDate.Rows)
            {
                DateTime dt = (DateTime)drv.Tag;
                dtList.Add(dt);
            }

            // 刪除課程缺曠
            UDTTransfer.UDTAttendanceDelete(_OldAttendanceList);
            // 刪除課程上課時間
            UDTTransfer.UDTTimeSectionDelete(_OldTimeSectionList);
            // 新增新課程上課時間
            List<UDTTimeSectionDef> InsertTimeSectionList = new List<UDTTimeSectionDef>();

            // 組合新增資料
            foreach (KeyValuePair<int, List<int>> data in _CoursePeriodDict)
            {
                foreach (DateTime dt in dtList)
                {
                    foreach (int period in data.Value)
                    {
                        UDTTimeSectionDef ts = new UDTTimeSectionDef();
                        ts.CourseID = data.Key;
                        ts.Date = dt;
                        ts.Period = period;
                        InsertTimeSectionList.Add(ts);
                    }                
                }            
            }

            if (InsertTimeSectionList.Count > 0)
            {
                UDTTransfer.UDTTimeSectionInsert(InsertTimeSectionList);             
                FISCA.Presentation.Controls.MsgBox.Show("更新成功");
                RetakeEvents.RaiseAssnChanged();
            }
            // 關閉畫面
            this.Close();
        }
        void _bgWorkerCreateData_DoWork(object sender, DoWorkEventArgs e)
        {
            // 清空原有舊資料
            // 課程
            if (_DelCourseIDList.Count > 0)
            {
                List<UDTCourseDef> delCourseList = new List<UDTCourseDef>();
                foreach (UDTCourseDef data in _hasCourseDefList)
                {
                    int coid = int.Parse(data.UID);
                    if (_DelCourseIDList.Contains(coid))
                        delCourseList.Add(data);
                }

                UDTTransfer.UDTCourseDelete(delCourseList);

                // 課程修課
                List<UDTScselectDef> delScselect = new List<UDTScselectDef>();
                foreach (UDTScselectDef data in _hasScselectDefList)
                    if (_DelCourseIDList.Contains(data.CourseID))
                        delScselect.Add(data);

                UDTTransfer.UDTSCSelectDelete(delScselect);
                
                // 課程時間區間
                List<UDTTimeSectionDef> delTimeSectionList = new List<UDTTimeSectionDef>();
                foreach (UDTTimeSectionDef data in _hasTimeSectionDefList)
                    if (_DelCourseIDList.Contains(data.CourseID))
                        delTimeSectionList.Add(data);

                UDTTransfer.UDTTimeSectionDelete(delTimeSectionList);
                
                // 課程缺曠
                List<UDTAttendanceDef> delAttendanceList = new List<UDTAttendanceDef>();
                foreach (UDTAttendanceDef data in _hasAttendanceDefList)
                    if (_DelCourseIDList.Contains(data.CourseID))
                        delAttendanceList.Add(data);

                UDTTransfer.UDTAttendanceDelete(delAttendanceList);
            }


            // 新增課程使用
            List<UDTCourseDef> InsertCourseList = new List<UDTCourseDef>();
            // 取得新增後課程使用
            List<UDTCourseDef> NewInsertedCourseList = new List<UDTCourseDef>();
            // 新增時間區間使用
            List<UDTTimeSectionDef> InsertTimeSectionList = new List<UDTTimeSectionDef>();
            // 新增修課學生
            List<UDTScselectDef> InsertSCSelectList = new List<UDTScselectDef>();

            List<string> courseNameAdd = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J","L","M","N","O","P" }.ToList();
            // 產生課程資料
            foreach (SubjectCourseBase scb in _SubjectCourseBaseList)
            {
                for (int i = 0; i < scb.CreateCount; i++)
                {
                    UDTCourseDef data = new UDTCourseDef();
                    data.SchoolYear = scb.SchoolYear;
                    data.Semester = scb.Semester;
                    data.Month = scb.Month;
                    data.Credit = scb.Credit;
                    data.SubjectType = scb.SubjectType;
                    data.SubjectName = scb.SubjectName;
                    data.SubjectLevel = scb.SubjectLevel;
                    data.DeptName = scb.DeptName;
                    // 課程名稱使用科目名稱加級別
                    if (data.SubjectLevel.HasValue)
                        data.CourseName = scb.SubjectName + QueryData.GetNumber(scb.SubjectLevel.Value.ToString()) +courseNameAdd[i];
                    else
                        data.CourseName = scb.SubjectName + courseNameAdd[i];
                                 
                        InsertCourseList.Add(data);

                    scb.CourseNameList.Add(data.CourseName);
                }
            }

            // 課程名稱與ID對照
            Dictionary<string, int> courseIDDict = new Dictionary<string, int>();

            if (InsertCourseList.Count > 0)
            {
                // 新增課程並取回新增的課程資料
                List<string> CIDList = UDTTransfer.UDTCourseInsert(InsertCourseList);
                NewInsertedCourseList = UDTTransfer.UDTCourseSelectUIDs(CIDList);

                // 課程名稱與ID對照
                foreach (UDTCourseDef data in NewInsertedCourseList)
                {
                    if (!courseIDDict.ContainsKey(data.CourseName))
                        courseIDDict.Add(data.CourseName, int.Parse(data.UID));
                }
                
                // 建立時間區間
                // 取得日期
                List<DateTime> dateList = new List<DateTime>();
                foreach (DataGridViewRow drv in dgDate.Rows)
                    dateList.Add((DateTime)drv.Tag);


                // 處理時間區間
                foreach (SubjectCourseBase scb in _SubjectCourseBaseList)
                {
                    if (scb.PeriodXml == null)
                        continue;

                    List<int> PeriodList = new List<int>();
                    foreach (XElement elm in scb.PeriodXml.Elements("Period"))
                        PeriodList.Add(int.Parse(elm.Value));

                    
                    // 課程名稱
                    foreach (string courseName in scb.CourseNameList)
                    {
                        if (courseIDDict.ContainsKey(courseName))
                        {
                            
                            int cid = courseIDDict[courseName];

                            foreach (DateTime dt in dateList)
                            {
                                foreach (int per in PeriodList)
                                {
                                    UDTTimeSectionDef dataTs = new UDTTimeSectionDef();
                                    dataTs.CourseID = cid;
                                    dataTs.Period = per;
                                    dataTs.Date = dt;
                                    InsertTimeSectionList.Add(dataTs);
                                }
                            }
                        }                    
                    }                        
                }
                // 新增寫入時間區間
                if (InsertTimeSectionList.Count > 0)
                    UDTTransfer.UDTTimeSectionInsert(InsertTimeSectionList);
                
                // 新增修課學生
                foreach (SubjectCourseBase scb in _SubjectCourseBaseList)
                {
                    if (scb.CreateCount < 1)
                        continue;

                    int MaxStudCount = scb.MaxStudentCount;

                    int NewCount = (int)Math.Round(((decimal)scb.StudentIDList.Count / (decimal)scb.CreateCount), 0);
                    if (NewCount > MaxStudCount)
                        MaxStudCount = NewCount;

                    List<int> cousreIDList = new List<int>();
                    foreach (string name in scb.CourseNameList)
                    {
                        if (courseIDDict.ContainsKey(name))
                            cousreIDList.Add(courseIDDict[name]);
                    }
                
                    // 修課學生
                    int courseIdx = 0, seatno = 1;
                    if (cousreIDList.Count > 0)
                    {
                        foreach (SubjectCourseStudentBase stud in scb.StudentIDList)
                        {

                            if (seatno > MaxStudCount)
                            {
                                seatno = 1;
                                courseIdx++;
                            }

                            UDTScselectDef scselect = new UDTScselectDef();
                            scselect.CourseID = cousreIDList[courseIdx];
                            scselect.SeatNo = seatno;
                            scselect.StudentID = stud.StudentID;
                            scselect.Type = stud.Type;
                            InsertSCSelectList.Add(scselect);
                            seatno++;
                        }
                    }
                }
                // 新增修課學生
                if (InsertSCSelectList.Count > 0)
                {
                    UDTTransfer.UDTSCSelectInsert(InsertSCSelectList);
                }
            }
   
        }