/// <summary> /// 單筆Log /// </summary> public string GetLogString(Dictionary <string, CLUBRecord> ClubDic, VolunteerRecord each, StudentRecord sr) { StringBuilder log = new StringBuilder(); string classname = sr.Class != null ? sr.Class.Name : ""; string SeatNo = sr.SeatNo.HasValue ? sr.SeatNo.Value.ToString() : ""; log.AppendLine(string.Format("班級「{0}」座號「{1}」姓名「{2}」學號「{3}」", classname, SeatNo, sr.Name, sr.StudentNumber)); if (!string.IsNullOrEmpty(each.Content)) { log.AppendLine(string.Format("志願序「{0}」", GetVoluntString(ClubDic, each.Content))); } else { log.AppendLine(string.Format("志願序「{0}」", "「無」")); } return(log.ToString()); }
void BGW_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { dataGridViewX1.Enabled = true; this.Text = "學生選填明細"; if (!e.Cancelled) { if (e.Error == null) { SetColumn(); #region 建立Row //取得該班級的學生基本資料 List <一名學生> list = _VolRow._StudentDic.Values.ToList(); //依座號排序 list.Sort(SortMergeList); foreach (一名學生 each in list) { DataGridViewRow row = new DataGridViewRow(); row.CreateCells(dataGridViewX1); row.Cells[0].Value = each.seat_no.HasValue ? each.seat_no.Value.ToString() : ""; //座號 row.Cells[1].Value = each.student_name; //姓名 row.Cells[2].Value = each.gender; //姓名 if (_VolRow._Volunteer.ContainsKey(each.student_id)) { #region 必須有填志願才會被填入社團資料 //學生基本資料 VolunteerRecord obj = _VolRow._Volunteer[each.student_id]; //ClubDic社團是從選社志願內的資料 //因此它為0時,就表示多數數資料都沒有填志願 if (ClubDic.Count != 0) { //取得單一學生志願序選填狀況 if (!string.IsNullOrEmpty(obj.Content)) { XmlElement Element = XmlHelper.LoadXml(obj.Content); foreach (XmlElement xml in Element.SelectNodes("Club")) { //所選填的必須只有設定之數量 int ClubIndex = 0; int.TryParse(xml.GetAttribute("Index"), out ClubIndex); if (ClubIndex <= _By_V.學生選填志願數 && ClubIndex != 0) { string clubID = xml.GetAttribute("Ref_Club_ID"); //是否包含此社團 if (ClubDic.ContainsKey(clubID)) { CLUBRecord cr = ClubDic[clubID]; //+3是因為由第 3 Column起始 row.Cells[ClubIndex + 2].Value = cr.ClubName; row.Cells[ClubIndex + 2].ReadOnly = true; } } } } } #endregion } if (_VolRow._SCJDic.ContainsKey(each.student_id)) { #region 當此學生有社團參與記錄時 SCJoin scj = _VolRow._SCJDic[each.student_id]; if (_VolRow._ClubDic.ContainsKey(scj.RefClubID)) { CLUBRecord club = _VolRow._ClubDic[scj.RefClubID]; row.Cells[1].Value += "(" + club.ClubName + ")"; if (scj.Lock) { foreach (DataGridViewCell cell in row.Cells) { cell.Style.BackColor = Color.GreenYellow; } } else { foreach (DataGridViewCell cell in row.Cells) { cell.Style.BackColor = Color.Yellow; } } } #endregion } else { StudentIDList.Add(each.student_id); } dataGridViewX1.Rows.Add(row); } #endregion } else { MsgBox.Show("已發生錯誤!!\n" + e.Error.Message); } } else { MsgBox.Show("資料取得動作已取消"); } }
/// <summary> /// 傳入分配 /// </summary> private bool Allocation(學生選社亂數檔 ran, int NumberIndex) { //社團選社資料 VolunteerRecord vr = ran._record; XmlElement xml = XmlHelper.LoadXml(vr.Content); //每一個社團 foreach (XmlElement each in xml.SelectNodes("Club")) { int index = 0; int.TryParse(each.GetAttribute("Index"), out index); //當進行第一輪志願分配時 if (index != NumberIndex) { continue; } string clubID = each.GetAttribute("Ref_Club_ID"); string StudentID = vr.RefStudentID; //2013/4/8號 //需建立一個ClubCount字典 //覆蓋 - 把本期除了鎖定學生之社團記錄清除 //略過 - 本期的社團記錄不予更動 //1.須檢查目前系統是否有要加入的社團(例如該社團被選社後刪除) //2.目前此社團是否已額滿或目前社團人數為可加入狀態 if (!CLUBDic.ContainsKey(clubID)) { continue; } //不存在社團記錄,表示可以新增 if (!SCJLockDic.ContainsKey(StudentID)) { #region 存在社團記錄,表示可以新增 SCJoin scj = new SCJoin(); 一個社團檢查 一社團 = CLUBCheckDic[clubID]; if (!一社團.人數未滿) { if (StudentDic.ContainsKey(StudentID)) { 一名學生 一學生 = StudentDic[StudentID]; sb_Log.AppendLine(string.Format("序號「{0}」班級「{1}」學生「{2}」開始進行第「{3}」志願分配", ran._Index, 一學生.class_name, 一學生.student_name, NumberIndex.ToString())); sb_Log.AppendLine(string.Format("分配失敗原因:社團「{0}」人數已滿", 一社團._ClubObj.ClubName)); sb_Log.AppendLine(""); return(false); } } if (StudentDic.ContainsKey(StudentID)) { 一名學生 一學生 = StudentDic[StudentID]; //序號「{0}」 ran._Index sb_Log.AppendLine(string.Format("序號「{0}」班級「{1}」學生「{2}」開始進行第「{3}」志願分配", ran._Index, 一學生.class_name, 一學生.student_name, NumberIndex.ToString())); //你必須是本社團科別限制之學生 //Count大於0,表示有科別限制 if (一社團.DeptList.Count > 0) { if (!一社團.DeptList.Contains(一學生.dept_name)) { sb_Log.AppendLine(string.Format("分配失敗原因:志願「{0}」受到科別限制「{1}」", 一社團._ClubObj.ClubName, 一學生.dept_name)); sb_Log.AppendLine(""); //本社團選社失敗 continue; } } if (一社團.男女限制 == 一學生.gender || 一社團.男女限制 == GetVolunteerData.男女.限制) { if (一學生.grade_year == "1" || 一學生.grade_year == "7" || 一學生.grade_year == "10") { if (一社團.一年級未滿) { scj.RefClubID = clubID; scj.RefStudentID = StudentID; 一社團._Now_ClubStudentCount++; 一社團._Now_GradeYear1++; sb_Log.AppendLine(string.Format("已入選社團「{0}」", 一社團._ClubObj.ClubName)); sb_Log.AppendLine(""); InsertList1.Add(scj); return(true); } else { sb_Log.AppendLine(string.Format("分配失敗原因:志願「{0}」年級「{1}」人數已滿", 一社團._ClubObj.ClubName, 一學生.grade_year)); sb_Log.AppendLine(""); } } else if (一學生.grade_year == "2" || 一學生.grade_year == "8" || 一學生.grade_year == "11") { if (一社團.二年級未滿) { scj.RefClubID = clubID; scj.RefStudentID = StudentID; 一社團._Now_ClubStudentCount++; 一社團._Now_GradeYear2++; sb_Log.AppendLine(string.Format("已入選社團「{0}」", 一社團._ClubObj.ClubName)); sb_Log.AppendLine(""); InsertList1.Add(scj); return(true); } else { sb_Log.AppendLine(string.Format("分配失敗原因:志願「{0}」年級「{1}」人數已滿", 一社團._ClubObj.ClubName, 一學生.grade_year)); sb_Log.AppendLine(""); } } else if (一學生.grade_year == "3" || 一學生.grade_year == "9" || 一學生.grade_year == "12") { if (一社團.年級未滿) { scj.RefClubID = clubID; scj.RefStudentID = StudentID; 一社團._Now_ClubStudentCount++; 一社團._Now_GradeYear3++; sb_Log.AppendLine(string.Format("已入選社團「{0}」", 一社團._ClubObj.ClubName)); sb_Log.AppendLine(""); InsertList1.Add(scj); return(true); } else { sb_Log.AppendLine(string.Format("分配失敗原因:志願「{0}」年級「{1}」人數已滿", 一社團._ClubObj.ClubName, 一學生.grade_year)); sb_Log.AppendLine(""); } } else { sb_Log.AppendLine(string.Format("分配社團「{0}」失敗,未符合年級設定", 一社團._ClubObj.ClubName)); sb_Log.AppendLine(""); } } else { sb_Log.AppendLine(string.Format("分配社團「{0}」失敗,未符合男女限制「{1}」", 一社團._ClubObj.ClubName, 一社團.男女限制)); sb_Log.AppendLine(""); } } #endregion } else //已存在,要判斷是覆蓋還是略過 { #region 已存在,要判斷是覆蓋還是略過 //已有社團記錄時 - 覆蓋 if (By_V.已有社團記錄時) { #region 覆蓋 SCJoin scj_del = SCJLockDic[StudentID]; if (!scj_del.Lock) { #region 未鎖定 //新增一筆資料 SCJoin scj = new SCJoin(); //因為覆蓋所以 - 列入刪除 if (!DeleteList.Contains(scj_del)) { DeleteList.Add(scj_del); } 一個社團檢查 一社團 = CLUBCheckDic[clubID]; if (!一社團.人數未滿) { if (StudentDic.ContainsKey(StudentID)) { 一名學生 一學生 = StudentDic[StudentID]; sb_Log.AppendLine(string.Format("序號「{0}」班級「{1}」學生「{2}」開始進行第「{3}」志願分配", ran._Index, 一學生.class_name, 一學生.student_name, NumberIndex.ToString())); sb_Log.AppendLine(string.Format("分配失敗原因:社團「{0}」人數已滿", 一社團._ClubObj.ClubName)); sb_Log.AppendLine(""); return(false); } } if (StudentDic.ContainsKey(StudentID)) { 一名學生 一學生 = StudentDic[StudentID]; string clubName = ""; if (CLUBDic.ContainsKey(scj_del.RefClubID)) { clubName = CLUBDic[scj_del.RefClubID].ClubName; } //序號「{0}」 ran._Index sb_Log.AppendLine(string.Format("序號「{0}」班級「{1}」學生「{2}」開始進行第「{3}」志願分配", ran._Index, 一學生.class_name, 一學生.student_name, NumberIndex.ToString())); sb_Log.AppendLine(string.Format("已是社團「{0}」參與學生,因「未被鎖定」將進行重新分配", clubName)); //你必須是本社團科別限制之學生 //Count大於0,表示有科別限制 if (一社團.DeptList.Count > 0) { if (!一社團.DeptList.Contains(一學生.dept_name)) { sb_Log.AppendLine(string.Format("社團「{0}」分配失敗!原因:志願「{1}」受到科別限制「{2}」", 一社團._ClubObj.ClubName, NumberIndex, 一學生.dept_name)); sb_Log.AppendLine(""); //本社團選社失敗 continue; } } if (一社團.男女限制 == 一學生.gender || 一社團.男女限制 == GetVolunteerData.男女.限制) { if (一學生.grade_year == "1" || 一學生.grade_year == "7" || 一學生.grade_year == "10") { if (一社團.一年級未滿) { scj.RefStudentID = StudentID; scj.RefClubID = clubID; 一社團._Now_ClubStudentCount++; 一社團._Now_GradeYear1++; InsertList2.Add(scj); sb_Log.AppendLine(string.Format("已入選社團「{0}」", 一社團._ClubObj.ClubName)); sb_Log.AppendLine(""); return(true); } else { sb_Log.AppendLine(string.Format("分配失敗原因:志願「{0}」年級「{1}」人數已滿", 一社團._ClubObj.ClubName, 一學生.grade_year)); sb_Log.AppendLine(""); } } else if (一學生.grade_year == "2" || 一學生.grade_year == "8" || 一學生.grade_year == "11") { if (一社團.二年級未滿) { scj.RefStudentID = StudentID; scj.RefClubID = clubID; 一社團._Now_ClubStudentCount++; 一社團._Now_GradeYear2++; InsertList2.Add(scj); sb_Log.AppendLine(string.Format("已入選社團「{0}」", 一社團._ClubObj.ClubName)); sb_Log.AppendLine(""); return(true); } else { sb_Log.AppendLine(string.Format("分配失敗原因:志願「{0}」年級「{1}」人數已滿", 一社團._ClubObj.ClubName, 一學生.grade_year)); sb_Log.AppendLine(""); } } else if (一學生.grade_year == "3" || 一學生.grade_year == "9" || 一學生.grade_year == "12") { if (一社團.年級未滿) { scj.RefStudentID = StudentID; scj.RefClubID = clubID; 一社團._Now_ClubStudentCount++; 一社團._Now_GradeYear3++; sb_Log.AppendLine(string.Format("已入選社團「{0}」", 一社團._ClubObj.ClubName)); sb_Log.AppendLine(""); InsertList2.Add(scj); return(true); } else { sb_Log.AppendLine(string.Format("分配失敗原因:志願「{0}」年級「{1}」人數已滿", 一社團._ClubObj.ClubName, 一學生.grade_year)); sb_Log.AppendLine(""); } } else { sb_Log.AppendLine(string.Format("分配社團「{0}」失敗,未符合年級設定", 一社團._ClubObj.ClubName)); sb_Log.AppendLine(""); } } else { sb_Log.AppendLine(string.Format("分配社團「{0}」失敗,未符合男女限制「{1}」", 一社團._ClubObj.ClubName, 一社團.男女限制)); sb_Log.AppendLine(""); } } #endregion } else { //取得社團記錄 SCJoin scj = SCJLockDic[StudentID]; string clubName = ""; if (CLUBDic.ContainsKey(scj.RefClubID)) { clubName = CLUBDic[scj_del.RefClubID].ClubName; } // 2018/03/08 羿均 同步code //2017/9/1 - 修正Bug //這是多餘的程式邏輯 - By Dylan if (StudentDic.ContainsKey(StudentID)) { 一名學生 一學生 = StudentDic[StudentID]; //序號「{0}」 ran._Index sb_Log.AppendLine(string.Format("序號「{0}」班級「{1}」學生「{2}」開始進行第「{3}」志願分配", ran._Index, 一學生.class_name, 一學生.student_name, NumberIndex.ToString())); sb_Log.AppendLine(string.Format("已是社團「{0}」鎖定學生,將不變更社團", clubName)); sb_Log.AppendLine(""); return(true); } #region 鎖定 //一個社團檢查 一社團 = CLUBCheckDic[scj_del.RefClubID]; //if (StudentDic.ContainsKey(StudentID)) //{ // 一名學生 一學生 = StudentDic[StudentID]; // if (一學生.grade_year == "1" || 一學生.grade_year == "7" || 一學生.grade_year == "10") // { // 一社團._Now_ClubStudentCount++; // 一社團._Now_GradeYear1++; // return true; // } // else if (一學生.grade_year == "2" || 一學生.grade_year == "8" || 一學生.grade_year == "11") // { // 一社團._Now_ClubStudentCount++; // 一社團._Now_GradeYear2++; // return true; // } // else if (一學生.grade_year == "3" || 一學生.grade_year == "9" || 一學生.grade_year == "12") // { // 一社團._Now_ClubStudentCount++; // 一社團._Now_GradeYear3++; // return true; // } //} #endregion } #endregion } else { //2017/9/1 - 修正Bug //這是多餘的程式邏輯 - By Dylan #region 略過 //取得社團記錄 SCJoin scj = SCJLockDic[StudentID]; string clubName = ""; if (CLUBDic.ContainsKey(scj.RefClubID)) { clubName = CLUBDic[scj.RefClubID].ClubName; } if (StudentDic.ContainsKey(StudentID)) { 一名學生 一學生 = StudentDic[StudentID]; //序號「{0}」 ran._Index sb_Log.AppendLine(string.Format("目前設定:「略過」已入選學生,序號「{0}」學生「{1}」社團「{2}」將不變更", ran._Index, 一學生.student_name, clubName)); sb_Log.AppendLine(""); return(true); //if (一學生.grade_year == "1" || 一學生.grade_year == "7" || 一學生.grade_year == "10") //{ // 一社團._Now_ClubStudentCount++; // 一社團._Now_GradeYear1++; // return true; //} //else if (一學生.grade_year == "2" || 一學生.grade_year == "8" || 一學生.grade_year == "11") //{ // 一社團._Now_ClubStudentCount++; // 一社團._Now_GradeYear2++; // return true; //} //else if (一學生.grade_year == "3" || 一學生.grade_year == "9" || 一學生.grade_year == "12") //{ // 一社團._Now_ClubStudentCount++; // 一社團._Now_GradeYear3++; // return true; //} } #endregion } #endregion } } //選社失敗 return(false); }
public override string Import(List <Campus.DocumentValidator.IRowStream> Rows) { if (mOption.Action == ImportAction.InsertOrUpdate) { #region 學號:學生系統編號 Dictionary <string, string> StudentDic = new Dictionary <string, string>(); List <string> StudentNumberList = new List <string>(); foreach (IRowStream Row in Rows) { string StudentNumber = Row.GetValue("學號"); if (!StudentNumberList.Contains(StudentNumber)) { StudentNumberList.Add(StudentNumber); } } DataTable dt = tool._Q.Select(string.Format("select id,student_number from student where student_number in ('{0}')", string.Join("','", StudentNumberList))); //學生ID清單 List <string> StudentIDList = new List <string>(); foreach (DataRow row in dt.Rows) { string id = "" + row["id"]; string number = "" + row["student_number"]; if (!StudentDic.ContainsKey(number)) { StudentDic.Add(number, id); } if (!StudentIDList.Contains(id)) { StudentIDList.Add(id); } } //學生Log專用的記錄 List <StudentRecord> StudentList = K12.Data.Student.SelectByIDs(StudentIDList); Dictionary <string, StudentRecord> StudentLogDic = new Dictionary <string, StudentRecord>(); foreach (StudentRecord each in StudentList) { if (!StudentLogDic.ContainsKey(each.ID)) { StudentLogDic.Add(each.ID, each); } } #endregion Dictionary <string, CLUBRecord> ClubDic = new Dictionary <string, CLUBRecord>(); Dictionary <string, CLUBRecord> ClubLogDic = new Dictionary <string, CLUBRecord>(); List <CLUBRecord> CLUBList = tool._A.Select <CLUBRecord>(); foreach (CLUBRecord each in CLUBList) { string CourseKey = each.SchoolYear + "," + each.Semester + "," + each.ClubName; if (!ClubDic.ContainsKey(CourseKey)) { ClubDic.Add(CourseKey, each); } if (!ClubLogDic.ContainsKey(each.UID)) { ClubLogDic.Add(each.UID, each); } } //系統內是否已經有志願序清單 //學年度學期學號 : 志願序 Dictionary <string, VolunteerRecord> VolunteerDic = new Dictionary <string, VolunteerRecord>(); List <VolunteerRecord> vrList = tool._A.Select <VolunteerRecord>(string.Format("ref_student_id in ('{0}')", string.Join("','", StudentDic.Values))); foreach (VolunteerRecord each in vrList) { string kkey = each.SchoolYear + "," + each.Semester + "," + each.RefStudentID; if (!VolunteerDic.ContainsKey(kkey)) { VolunteerDic.Add(kkey, each); } //Log if (!Log_Dic.ContainsKey(kkey)) { LogVolunteer im = new LogVolunteer(); im.lo_Vol = each.CopyExtension(); im.ClubDic = ClubLogDic; Log_Dic.Add(kkey, im); } } List <VolunteerRecord> VolunteerInsertList = new List <VolunteerRecord>(); List <VolunteerRecord> VolunteerUpdateList = new List <VolunteerRecord>(); foreach (IRowStream Row in Rows) { //教師名稱 string StudentNumber = Row.GetValue("學號"); //取得學生ID if (StudentDic.ContainsKey(StudentNumber)) { string SchoolYear = Row.GetValue("學年度"); string Semester = Row.GetValue("學期"); string jkey = SchoolYear + "," + Semester + "," + StudentDic[StudentNumber]; if (!VolunteerDic.ContainsKey(jkey)) { #region 取得志願XML DSXmlHelper dsx = new DSXmlHelper("xml"); for (int x = 1; x <= 學生選填志願數; x++) { //依學年度+學期+社團名稱 找到社團 string CLUBName = Row.GetValue("志願" + x); if (!string.IsNullOrEmpty(CLUBName)) { string CourseKey = SchoolYear + "," + Semester + "," + CLUBName; if (ClubDic.ContainsKey(CourseKey)) { string clubID = ClubDic[CourseKey].UID; dsx.AddElement("Club"); dsx.SetAttribute("Club", "Index", "" + x); dsx.SetAttribute("Club", "Ref_Club_ID", clubID); } } } #endregion //建立Record VolunteerRecord Vol = new VolunteerRecord(); Vol.SchoolYear = int.Parse(SchoolYear); Vol.Semester = int.Parse(Semester); Vol.RefStudentID = StudentDic[StudentNumber]; Vol.Content = dsx.BaseElement.OuterXml; VolunteerInsertList.Add(Vol); } else { #region 取得志願XML DSXmlHelper dsx = new DSXmlHelper("xml"); for (int x = 1; x <= 學生選填志願數; x++) { //依學年度+學期+社團名稱 找到社團 string CLUBName = Row.GetValue("志願" + x); if (!string.IsNullOrEmpty(CLUBName)) { string CourseKey = SchoolYear + "," + Semester + "," + CLUBName; if (ClubDic.ContainsKey(CourseKey)) { string clubID = ClubDic[CourseKey].UID; dsx.AddElement("Club"); dsx.SetAttribute("Club", "Index", "" + x); dsx.SetAttribute("Club", "Ref_Club_ID", clubID); } } } #endregion VolunteerRecord Vol = VolunteerDic[jkey]; Vol.Content = dsx.BaseElement.OuterXml; VolunteerUpdateList.Add(Vol); if (Log_Dic.ContainsKey(jkey)) { Log_Dic[jkey].New_Vol = Vol; } } } } if (VolunteerInsertList.Count > 0) { StringBuilder mstrLog1 = new StringBuilder(); mstrLog1.AppendLine("新增社團志願序:"); foreach (VolunteerRecord each in VolunteerInsertList) { if (StudentLogDic.ContainsKey(each.RefStudentID)) { mstrLog1.AppendLine(Importbot.GetLogString(ClubLogDic, each, StudentLogDic[each.RefStudentID])); } } tool._A.InsertValues(VolunteerInsertList); FISCA.LogAgent.ApplicationLog.Log("社團", "新增匯入志願序", mstrLog1.ToString()); } if (VolunteerUpdateList.Count > 0) { StringBuilder mstrLog2 = new StringBuilder(); mstrLog2.AppendLine("更新社團志願序:"); foreach (VolunteerRecord each in VolunteerUpdateList) { string CourseKey = each.SchoolYear + "," + each.Semester + "," + each.RefStudentID; if (Log_Dic.ContainsKey(CourseKey)) { if (StudentLogDic.ContainsKey(each.RefStudentID)) { string classname = StudentLogDic[each.RefStudentID].Class != null ? StudentLogDic[each.RefStudentID].Class.Name : ""; string SeatNo = StudentLogDic[each.RefStudentID].SeatNo.HasValue ? StudentLogDic[each.RefStudentID].SeatNo.Value.ToString() : ""; mstrLog2.AppendLine(string.Format("班級「{0}」座號「{1}」姓名「{2}」學號「{3}」", classname, SeatNo, StudentLogDic[each.RefStudentID].Name, StudentLogDic[each.RefStudentID].StudentNumber)); } mstrLog2.AppendLine(Importbot.SetLog(Log_Dic[CourseKey])); } } tool._A.UpdateValues(VolunteerUpdateList); FISCA.LogAgent.ApplicationLog.Log("社團", "更新匯入志願序", mstrLog2.ToString()); } } return(""); }
public 學生選社亂數檔(VolunteerRecord record, int Number) { _Number = Number; _record = record; AllocationSucceeds = false; }
public AutoVolunteer() { // List List <string> studentIDList = new List <string>(); // Index clubID Dictionary <int, string> ClubIDDic = new Dictionary <int, string>(); // studentID volunteer Dictionary <string, string> studentVolunteer = new Dictionary <string, string>(); // 取得社團資料清單 AccessHelper access = new AccessHelper(); List <CLUBRecord> allClubList = access.Select <CLUBRecord>("school_year = 106 AND semester = 1"); // 取得學生資料清單 List <StudentRecord> sr = Student.SelectAll(); // volunteer List <VolunteerRecord> vrList = new List <VolunteerRecord>(); Exception exc = null; // BGW BackgroundWorker BGW = new BackgroundWorker() { WorkerReportsProgress = true }; BGW.ProgressChanged += delegate(object sender, ProgressChangedEventArgs e) { MotherForm.SetStatusBarMessage("隨機分配學生社團志願!", e.ProgressPercentage); }; BGW.DoWork += delegate { try { // 取得所有社團 UID int index = 0; foreach (CLUBRecord club in allClubList) { ClubIDDic.Add(index++, club.UID); } BGW.ReportProgress(10); Random random = new Random(); int min = 0; int max = ClubIDDic.Count(); int c = 1; // 紀錄:一般生學生ID foreach (StudentRecord s in sr) { if (s.Status.ToString() == "一般") { if (studentIDList.Contains(s.ID)) { MessageBox.Show("學生ID重複!!!!!!!!!!"); } if (!studentIDList.Contains(s.ID)) { studentIDList.Add(s.ID); } } } List <StudentRecord> studentRecord = Student.SelectByIDs(studentIDList); // 刪除資料: 避免學生有兩筆社團志願紀錄 UpdateHelper qh = new UpdateHelper(); string deleteSQL = "DELETE FROM $k12.volunteer.universal WHERE school_year = 106 AND semester = 1"; qh.Execute(deleteSQL); // 紀錄:新增學生隨機分配社團志願 foreach (StudentRecord s in studentRecord) { BGW.ReportProgress(10 + 90 * c / studentRecord.Count); VolunteerRecord vr = new VolunteerRecord(); string content = ""; for (int i = 1; i <= 5; i++) { content += string.Format("<Club Index=\"{0}\" Ref_Club_ID=\"{1}\"/>", i, ClubIDDic[random.Next(min, max)]); } vr.RefStudentID = s.ID; vr.Content = "<xml>" + content + "</xml>"; vr.SchoolYear = 106; vr.Semester = 1; vrList.Add(vr); c++; } access.SaveAll(vrList); } catch (Exception ex) { exc = ex; } }; BGW.RunWorkerCompleted += delegate { if (exc == null) { MessageBox.Show("隨機分配學生志願成功"); } else { throw new Exception("隨機分配學生社團志願 發生錯誤", exc); } }; BGW.RunWorkerAsync(); }