private void SetLog() { sbLog = new StringBuilder(); Dictionary <string, SHSCETakeRecord> delDic = new Dictionary <string, SHSCETakeRecord>(); foreach (SHSCETakeRecord sce in _deleteScoreList) { if (!delDic.ContainsKey(sce.RefStudentID + "_" + sce.RefCourseID)) { delDic.Add(sce.RefStudentID + "_" + sce.RefCourseID, sce); } } foreach (SHSCETakeRecord sce in _addScoreList) { SHStudentRecord student = SHStudent.SelectByID(sce.RefStudentID); SHCourseRecord course = SHCourse.SelectByID(sce.RefCourseID); string exam = (examDict.ContainsKey(sce.RefExamID) ? examDict[sce.RefExamID] : "<未知的試別>"); if (delDic.ContainsKey(sce.RefStudentID + "_" + sce.RefCourseID)) { string classname = student.Class != null ? student.Class.Name : ""; string seatno = student.SeatNo.HasValue ? student.SeatNo.Value.ToString() : ""; sbLog.AppendLine(string.Format("班級「{0}」座號「{1}」姓名「{2}」在試別「{3}」課程「{4}」將成績「{5}」修改為「{6}」。", classname, seatno, student.Name, course.Name, exam, delDic[sce.RefStudentID + "_" + sce.RefCourseID].Score, sce.Score)); } else { string classname = student.Class != null ? student.Class.Name : ""; string seatno = student.SeatNo.HasValue ? student.SeatNo.Value.ToString() : ""; sbLog.AppendLine(string.Format("班級「{0}」座號「{1}」姓名「{2}」在「{3}」課程「{4}」新增成績「{5}」。", classname, seatno, student.Name, course.Name, exam, sce.Score)); } } FISCA.LogAgent.ApplicationLog.Log("讀卡系統", "評量成績", sbLog.ToString()); }
public override void InitializeExport(SmartSchool.API.PlugIn.Export.ExportWizard wizard) { wizard.ExportableFields.AddRange("姓名", "學號", "班級", "座號", "必選修", "校部訂", "學生狀態"); wizard.ExportPackage += delegate(object sender, SmartSchool.API.PlugIn.Export.ExportPackageEventArgs e) { //課程資訊 List <SHCourseRecord> courses = SHCourse.SelectByIDs(e.List); //學生修課資訊 Dictionary <string, List <SHSCAttendRecord> > scattends = new Dictionary <string, List <SHSCAttendRecord> >(); //課程修課學生 Dictionary <string, SHStudentRecord> students = new Dictionary <string, SHStudentRecord>(); #region 取得修課記錄 foreach (SHSCAttendRecord record in SHSCAttend.SelectByStudentIDAndCourseID(new string[] { }, e.List)) { if (!scattends.ContainsKey(record.RefCourseID)) { scattends.Add(record.RefCourseID, new List <SHSCAttendRecord>()); } scattends[record.RefCourseID].Add(record); if (!students.ContainsKey(record.RefStudentID)) { students.Add(record.RefStudentID, null); } } #endregion #region 取得學生資訊 SHStudent.RemoveAll(); foreach (SHStudentRecord record in SHStudent.SelectByIDs(new List <string>(students.Keys))) { if (students.ContainsKey(record.ID)) { students[record.ID] = record; } } #endregion #region 產生 Row Data foreach (SHCourseRecord course in courses) { //Debug if (!scattends.ContainsKey(course.ID)) { continue; } foreach (SHSCAttendRecord record in scattends[course.ID]) { RowData row = new RowData(); row.ID = course.ID; foreach (string field in e.ExportFields) { if (wizard.ExportableFields.Contains(field)) { switch (field) { case "姓名": row.Add(field, students[record.RefStudentID].Name); break; case "學號": row.Add(field, students[record.RefStudentID].StudentNumber); break; case "班級": row.Add(field, (students[record.RefStudentID].Class != null ? students[record.RefStudentID].Class.Name : "")); break; case "座號": row.Add(field, "" + students[record.RefStudentID].SeatNo); break; case "必選修": if (record.Required) { row.Add(field, "" + "必修"); } else { row.Add(field, "" + "選修"); } break; case "校部訂": row.Add(field, "" + record.RequiredBy); break; case "學生狀態": row.Add(field, "" + students[record.RefStudentID].Status.ToString()); break; } } } e.Items.Add(row); } } #endregion ApplicationLog.Log("成績系統.匯入匯出", "匯出課程修課學生", "總共匯出" + e.Items.Count + "筆課程修課學生。"); }; }
void bkwNotPassComputer_DoWork(object sender, DoWorkEventArgs e) { string fileName = (string)((object[])e.Argument)[0]; List <string> courseFieldList = (List <string>)((object[])e.Argument)[1]; List <string> exportFieldList = (List <string>)((object[])e.Argument)[2]; Dictionary <ManualResetEvent, List <RowData> > Filler = (Dictionary <ManualResetEvent, List <RowData> >)((object[])e.Argument)[3]; double totleProgress = 0.0; double packageProgress = 100.0 / Filler.Count; Workbook report = new Workbook(); report.Worksheets[0].Name = _Title; ((BackgroundWorker)sender).ReportProgress(1, _Title + " 資料整理中..."); int RowIndex = 0; int i = 0; foreach (string field in exportFieldList) { if (courseFieldList.Contains(field)) { courseFieldList.Remove(field); } } //填表頭 for (; i < courseFieldList.Count; i++) { report.Worksheets[0].Cells[0, i].PutValue(courseFieldList[i]); } for (int j = 0; j < exportFieldList.Count; j++) { report.Worksheets[0].Cells[0, i + j].PutValue(exportFieldList[j]); } RowIndex = 1; foreach (ManualResetEvent eve in Filler.Keys) { eve.WaitOne(); if (RowIndex <= 65535) { double miniProgress = Filler[eve].Count == 0 ? 1 : packageProgress / Filler[eve].Count; double miniTotle = 0; foreach (RowData row in Filler[eve]) { SHCourseRecord course = null; if (row.ID != "") { course = SHCourse.SelectByID(row.ID); } if (course != null) { if (RowIndex <= 65535) { i = 0; for (; i < courseFieldList.Count; i++) { switch (courseFieldList[i]) { case "課程系統編號": report.Worksheets[0].Cells[RowIndex, i].PutValue(course.ID); break; case "學年度": report.Worksheets[0].Cells[RowIndex, i].PutValue("" + course.SchoolYear); break; case "學期": report.Worksheets[0].Cells[RowIndex, i].PutValue("" + course.Semester); break; case "課程名稱": report.Worksheets[0].Cells[RowIndex, i].PutValue(course.Name); break; default: break; } } for (int j = 0; j < exportFieldList.Count; j++) { report.Worksheets[0].Cells[RowIndex, i + j].PutValue(row.ContainsKey(exportFieldList[j]) ? row[exportFieldList[j]] : ""); } } RowIndex++; } miniTotle += miniProgress; ((BackgroundWorker)sender).ReportProgress((int)(totleProgress + miniTotle), _Title + " 處理中..."); } } totleProgress += packageProgress; ((BackgroundWorker)sender).ReportProgress((int)(totleProgress), _Title + " 處理中..."); } for (int k = 0; k < courseFieldList.Count + exportFieldList.Count; k++) { report.Worksheets[0].AutoFitColumn(k, 0, 150); } report.Worksheets[0].FreezePanes(1, 0, 1, courseFieldList.Count + exportFieldList.Count); e.Result = new object[] { report, fileName, RowIndex > 65535 }; }
private void wizard1_FinishButtonClick(object sender, CancelEventArgs e) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Title = "另存新檔"; saveFileDialog1.FileName = "" + _Title + ".xls"; saveFileDialog1.Filter = "Excel (*.xls)|*.xls|所有檔案 (*.*)|*.*"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { List <string> idlist = new List <string>(); #region 取得選取課程編號 List <SHCourseRecord> selectedCourses = null; if (IDs != null) { selectedCourses = SHCourse.SelectByIDs(IDs); } else { selectedCourses = SHCourse.SelectByIDs(K12.Presentation.NLDPanels.Course.SelectedSource); } foreach (SHCourseRecord course in selectedCourses) { if (!idlist.Contains(course.ID)) { idlist.Add(course.ID); } } #endregion List <string> courseFieldList = new List <string>(); List <string> exportFieldList = new List <string>(_SelectedFields); #region 取得選取欄位 for (int index = 0; index < listViewEx1.Items.Count; index++) { if (listViewEx1.Items[index] != null && listViewEx1.Items[index].Checked) { courseFieldList.Add(listViewEx1.Items[index].Text.Trim()); } } #endregion List <List <string> > splitList = new List <List <string> >(); //把全部課程以_PackageLimint人分一包 #region 把全部課程以_PackageLimint人分一包 int count = 0; List <string> package = new List <string>(); foreach (string id in idlist) { if (count == 0) { count = (splitList.Count + 1) * 50; count = count > _PackageLimint ? _PackageLimint : count; package = new List <string>(_PackageLimint); splitList.Add(package); } package.Add(id); count--; } #endregion //兩條獨立讀取 Dictionary <List <string>, ManualResetEvent> Loader1 = new Dictionary <List <string>, ManualResetEvent>(); Dictionary <List <string>, ManualResetEvent> Loader2 = new Dictionary <List <string>, ManualResetEvent>(); //已讀取資料 Dictionary <ManualResetEvent, List <RowData> > Filler = new Dictionary <ManualResetEvent, List <RowData> >(); int i = 0; foreach (List <string> p in splitList) { ManualResetEvent handleEvent = new ManualResetEvent(false); if ((i & 1) == 0) { Loader1.Add(p, handleEvent); } else { Loader2.Add(p, handleEvent); } Filler.Add(handleEvent, new List <RowData>()); i++; } //在背景執行取得資料 BackgroundWorker bkwDataLoader = new BackgroundWorker(); bkwDataLoader.DoWork += new DoWorkEventHandler(bkwDataLoader_DoWork); bkwDataLoader.RunWorkerAsync(new object[] { Loader1, Filler, exportFieldList }); bkwDataLoader = new BackgroundWorker(); bkwDataLoader.DoWork += new DoWorkEventHandler(bkwDataLoader_DoWork); bkwDataLoader.RunWorkerAsync(new object[] { Loader2, Filler, exportFieldList }); //在背景計算不及格名單 BackgroundWorker bkwNotPassComputer = new BackgroundWorker(); bkwNotPassComputer.WorkerReportsProgress = true; bkwNotPassComputer.DoWork += new DoWorkEventHandler(bkwNotPassComputer_DoWork); bkwNotPassComputer.ProgressChanged += new ProgressChangedEventHandler(bkwNotPassComputer_ProgressChanged); bkwNotPassComputer.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bkwNotPassComputer_RunWorkerCompleted); bkwNotPassComputer.RunWorkerAsync(new object[] { saveFileDialog1.FileName, courseFieldList, exportFieldList, Filler }); this.Close(); } }
public static Dictionary <string, List <StudentSCETakeRec> > GetStudentSCETakeDict(List <string> StudentIDList, string ExamID, int SchoolYear, int Semester) { Dictionary <string, List <StudentSCETakeRec> > value = new Dictionary <string, List <StudentSCETakeRec> >(); if (StudentIDList.Count > 0 && ExamID != "") { SmartSchool.Customization.Data.AccessHelper acc = new SmartSchool.Customization.Data.AccessHelper(); // 取得及格標準 List <SmartSchool.Customization.Data.StudentRecord> StudentRecList = acc.StudentHelper.GetStudents(StudentIDList); Dictionary <string, Dictionary <string, decimal> > passScoreDict = GetStudentApplyLimitDict(StudentRecList); Dictionary <string, string> StudGradYearDict = new Dictionary <string, string>(); foreach (SmartSchool.Customization.Data.StudentRecord rec in StudentRecList) { if (!StudGradYearDict.ContainsKey(rec.StudentID)) { if (rec.RefClass != null) { StudGradYearDict.Add(rec.StudentID, rec.RefClass.GradeYear); } } } // 取得學期對照年為主 List <SHSemesterHistoryRecord> SemsH = SHSemesterHistory.SelectByStudentIDs(StudentIDList); foreach (SHSemesterHistoryRecord rec in SemsH) { foreach (K12.Data.SemesterHistoryItem item in rec.SemesterHistoryItems) { if (item.SchoolYear == SchoolYear && item.Semester == Semester) { if (!StudGradYearDict.ContainsKey(item.RefStudentID)) { StudGradYearDict.Add(item.RefStudentID, item.GradeYear.ToString()); } else { StudGradYearDict[item.RefStudentID] = item.GradeYear.ToString(); } } } } List <SHCourseRecord> CourseRecList = SHCourse.SelectBySchoolYearAndSemester(SchoolYear, Semester); Dictionary <string, SHCourseRecord> CourseRecDict = new Dictionary <string, SHCourseRecord>(); foreach (SHCourseRecord rec in CourseRecList) { if (!CourseRecDict.ContainsKey(rec.ID)) { CourseRecDict.Add(rec.ID, rec); } } QueryHelper qh = new QueryHelper(); string query = "select sc_attend.ref_student_id as sid,course.id as cid,course.course_name,course.subject,sce_take.score as sce_score,course.credit as credit from sc_attend inner join course on sc_attend.ref_course_id=course.id inner join sce_take on sc_attend.id=sce_take.ref_sc_attend_id where sc_attend.ref_student_id in(" + string.Join(",", StudentIDList.ToArray()) + ") and course.school_year=" + SchoolYear + " and course.semester=" + Semester + " and sce_take.ref_exam_id in(" + ExamID + ")"; DataTable dt = qh.Select(query); foreach (DataRow dr in dt.Rows) { string SubjCode = ""; string sid = dr["sid"].ToString(); if (!value.ContainsKey(sid)) { value.Add(sid, new List <StudentSCETakeRec>()); } string cid = dr["cid"].ToString(); StudentSCETakeRec scetRec = new StudentSCETakeRec(); scetRec.StudentID = sid; if (CourseRecDict.ContainsKey(cid)) { string Req = "", notNq = ""; if (CourseRecDict[cid].Required) { Req = "必修"; } else { Req = "選修"; } if (CourseRecDict[cid].NotIncludedInCredit) { notNq = "是"; } else { notNq = "否"; } SubjCode = CourseRecDict[cid].Subject + "_" + CourseRecDict[cid].Credit.Value + "_" + Req + "_" + CourseRecDict[cid].RequiredBy + "_" + CourseRecDict[cid].Entry + "_" + notNq; } scetRec.SubjectCode = SubjCode; string GrStr = ""; if (StudGradYearDict.ContainsKey(sid)) { GrStr = StudGradYearDict[sid] + "_及"; } scetRec.Score = "-1"; decimal dd, passScore = 0; if (decimal.TryParse(dr["sce_score"].ToString(), out dd)) { // 取得及格標準 if (passScoreDict.ContainsKey(sid)) { if (passScoreDict[sid].ContainsKey(GrStr)) { passScore = passScoreDict[sid][GrStr]; } } // 不及格標 * if (dd < passScore) { scetRec.Score = "*" + string.Format("{0:##0.00}", dd); } else { scetRec.Score = string.Format("{0:##0.00}", dd); } scetRec.Status = "1"; } else { // 缺考 scetRec.Status = "4"; } scetRec.SubjectCredit = dr["credit"].ToString(); value[sid].Add(scetRec); } } return(value); }
public override void InitializeImport(SmartSchool.API.PlugIn.Import.ImportWizard wizard) { //學生資訊 Dictionary <string, SHStudentRecord> students = new Dictionary <string, SHStudentRecord>(); //學生修課資訊 studentID -> List:SCAttendRecord Dictionary <string, List <SHSCAttendRecord> > scattends = new Dictionary <string, List <SHSCAttendRecord> >(); //學生修習的課程 courseID -> CourseRecord Dictionary <string, SHCourseRecord> courses = new Dictionary <string, SHCourseRecord>(); //所有課程(依學年度學期分開) schoolYear_semester -> (courseName -> CourseRecord) Dictionary <string, Dictionary <string, SHCourseRecord> > allcourses = new Dictionary <string, Dictionary <string, SHCourseRecord> >(); //studentID_schoolYear_semester -> List:courseName Dictionary <string, List <string> > semesterCourseName = new Dictionary <string, List <string> >(); //準備加入修課的資料 studentID -> (schoolYear_semester_courseName -> RowData) Dictionary <string, Dictionary <string, RowData> > prepareAttends = new Dictionary <string, Dictionary <string, RowData> >(); wizard.PackageLimit = 3000; wizard.ImportableFields.Add("課程系統編號"); wizard.ImportableFields.AddRange("學年度", "學期"); wizard.ImportableFields.Add("課程名稱"); wizard.ImportableFields.AddRange("班級", "座號"); wizard.RequiredFields.AddRange("學年度", "學期"); wizard.RequiredFields.Add("課程名稱"); wizard.ValidateStart += delegate(object sender, SmartSchool.API.PlugIn.Import.ValidateStartEventArgs e) { #region 取得學生資訊 foreach (SHStudentRecord stu in SHStudent.SelectByIDs(e.List)) { if (!students.ContainsKey(stu.ID)) { students.Add(stu.ID, stu); } } #endregion #region 取得修課記錄 MultiThreadWorker <string> loader1 = new MultiThreadWorker <string>(); loader1.MaxThreads = 3; loader1.PackageSize = 250; loader1.PackageWorker += delegate(object sender1, PackageWorkEventArgs <string> e1) { foreach (SHSCAttendRecord record in SHSCAttend.SelectByStudentIDAndCourseID(e1.List, new string[] { })) { if (!scattends.ContainsKey(record.RefStudentID)) { scattends.Add(record.RefStudentID, new List <SHSCAttendRecord>()); } scattends[record.RefStudentID].Add(record); if (!courses.ContainsKey(record.RefCourseID)) { courses.Add(record.RefCourseID, null); } } }; loader1.Run(e.List); #endregion #region 取得課程資訊 MultiThreadWorker <string> loader2 = new MultiThreadWorker <string>(); loader2.MaxThreads = 3; loader2.PackageSize = 250; loader2.PackageWorker += delegate(object sender2, PackageWorkEventArgs <string> e2) { foreach (SHCourseRecord record in SHCourse.SelectByIDs(new List <string>(e2.List))) { if (courses.ContainsKey(record.ID)) { courses[record.ID] = record; } } }; loader2.Run(courses.Keys); foreach (SHCourseRecord course in SHCourse.SelectAll()) { string key = course.SchoolYear + "_" + course.Semester; if (!allcourses.ContainsKey(key)) { allcourses.Add(key, new Dictionary <string, SHCourseRecord>()); } if (!allcourses[key].ContainsKey(course.Name)) { allcourses[key].Add(course.Name, course); } } #endregion }; wizard.ValidateRow += delegate(object sender, SmartSchool.API.PlugIn.Import.ValidateRowEventArgs e) { int i; #region 檢查學生是否存在 SHStudentRecord student = null; if (students.ContainsKey(e.Data.ID)) { student = students[e.Data.ID]; } else { e.ErrorMessage = "壓根就沒有這個學生" + e.Data.ID; return; } #endregion #region 驗證各個欄位格式 bool inputFormatPass = true; foreach (string field in e.SelectFields) { string value = e.Data[field]; switch (field) { default: break; case "學年度": case "學期": if (value == "" || !int.TryParse(value, out i)) { inputFormatPass &= false; e.ErrorFields.Add(field, "必須填入整數"); } break; case "課程名稱": if (value == "") { inputFormatPass &= false; e.ErrorFields.Add(field, "必須填入課程名稱"); } break; case "班級": if (value == "") { } break; case "座號": if (value != "" && !int.TryParse(value, out i)) { inputFormatPass &= false; e.ErrorFields.Add(field, "必須填入空白或整數"); } break; } } #endregion //輸入格式正確才會針對情節做檢驗 #region 驗證各種情節 if (inputFormatPass) { string errorMessage = ""; string sy = e.Data["學年度"]; string se = e.Data["學期"]; string courseName = e.Data["課程名稱"]; string key = e.Data.ID + "_" + sy + "_" + se; string semsKey = sy + "_" + se; //int schoolyear = Framework.Int.ParseInt(sy); //int semester = Framework.Int.ParseInt(se); #region 一個學年度學期不能有重覆的課程名稱 if (!semesterCourseName.ContainsKey(key)) { semesterCourseName.Add(key, new List <string>()); } if (semesterCourseName[key].Contains(courseName)) { errorMessage += (errorMessage == "" ? "" : "\n") + " 同一學年度學期不允許修習多筆相同名稱的課程"; } else { semesterCourseName[key].Add(courseName); } #endregion #region 檢查課程是否存在系統中 bool noCourse = false; if (!allcourses.ContainsKey(semsKey)) { noCourse = true; errorMessage += (errorMessage == "" ? "" : "\n") + " 系統中找不到該課程"; } else if (!allcourses[semsKey].ContainsKey(courseName)) { noCourse = true; errorMessage += (errorMessage == "" ? "" : "\n") + " 系統中找不到該課程"; } else { } #endregion #region 檢查學生是否有修此課程 bool attended = false; if (scattends.ContainsKey(e.Data.ID)) { foreach (SHSCAttendRecord record in scattends[e.Data.ID]) { if (courses[record.RefCourseID].Name == courseName && "" + courses[record.RefCourseID].SchoolYear == sy && "" + courses[record.RefCourseID].Semester == se) { attended = true; } } } else //學生沒修半堂課 { } if (!attended && !noCourse) { if (!e.WarningFields.ContainsKey("無修課記錄")) { e.WarningFields.Add("無修課記錄", "學生在此學期並無修習此課程,將會新增修課記錄"); } if (!prepareAttends.ContainsKey(e.Data.ID)) { prepareAttends.Add(e.Data.ID, new Dictionary <string, RowData>()); } if (!prepareAttends[e.Data.ID].ContainsKey(semsKey + "_" + courseName)) { prepareAttends[e.Data.ID].Add(semsKey + "_" + courseName, e.Data); } } #endregion e.ErrorMessage = errorMessage; } #endregion }; wizard.ImportPackage += delegate(object sender, SmartSchool.API.PlugIn.Import.ImportPackageEventArgs e) { Dictionary <string, List <RowData> > id_Rows = new Dictionary <string, List <RowData> >(); #region 分包裝 foreach (RowData data in e.Items) { if (!id_Rows.ContainsKey(data.ID)) { id_Rows.Add(data.ID, new List <RowData>()); } id_Rows[data.ID].Add(data); } #endregion List <SHSCAttendRecord> insertList = new List <SHSCAttendRecord>(); List <SHSCAttendRecord> updateList = new List <SHSCAttendRecord>(); //交叉比對各學生資料 #region 交叉比對各學生資料 foreach (string id in id_Rows.Keys) { SHStudentRecord studentRec = students[id]; #region 處理要新增的修課記錄 if (prepareAttends.ContainsKey(id)) { foreach (RowData data in prepareAttends[id].Values) { string sy = data["學年度"]; string se = data["學期"]; string semsKey = sy + "_" + se; string courseName = data["課程名稱"]; if (allcourses.ContainsKey(semsKey) && allcourses[semsKey].ContainsKey(courseName)) { SHSCAttendRecord record = new SHSCAttendRecord(); record.RefStudentID = id; record.RefCourseID = allcourses[semsKey][courseName].ID; insertList.Add(record); } } } #endregion } try { if (updateList.Count > 0) { #region 分批次兩路上傳 List <List <SHSCAttendRecord> > updatePackages = new List <List <SHSCAttendRecord> >(); List <List <SHSCAttendRecord> > updatePackages2 = new List <List <SHSCAttendRecord> >(); { List <SHSCAttendRecord> package = null; int count = 0; foreach (SHSCAttendRecord var in updateList) { if (count == 0) { package = new List <SHSCAttendRecord>(30); count = 30; if ((updatePackages.Count & 1) == 0) { updatePackages.Add(package); } else { updatePackages2.Add(package); } } package.Add(var); count--; } } Thread threadUpdateSemesterSubjectScore = new Thread(new ParameterizedThreadStart(Update)); threadUpdateSemesterSubjectScore.IsBackground = true; threadUpdateSemesterSubjectScore.Start(updatePackages); Thread threadUpdateSemesterSubjectScore2 = new Thread(new ParameterizedThreadStart(Update)); threadUpdateSemesterSubjectScore2.IsBackground = true; threadUpdateSemesterSubjectScore2.Start(updatePackages2); threadUpdateSemesterSubjectScore.Join(); threadUpdateSemesterSubjectScore2.Join(); #endregion } } catch (Exception ex) { } if (insertList.Count > 0) { #region 分批次兩路上傳 List <List <SHSCAttendRecord> > insertPackages = new List <List <SHSCAttendRecord> >(); List <List <SHSCAttendRecord> > insertPackages2 = new List <List <SHSCAttendRecord> >(); { List <SHSCAttendRecord> package = null; int count = 0; foreach (SHSCAttendRecord var in insertList) { if (count == 0) { package = new List <SHSCAttendRecord>(30); count = 30; if ((insertPackages.Count & 1) == 0) { insertPackages.Add(package); } else { insertPackages2.Add(package); } } package.Add(var); count--; } } Thread threadInsertSemesterSubjectScore = new Thread(new ParameterizedThreadStart(Insert)); threadInsertSemesterSubjectScore.IsBackground = true; threadInsertSemesterSubjectScore.Start(insertPackages); Thread threadInsertSemesterSubjectScore2 = new Thread(new ParameterizedThreadStart(Insert)); threadInsertSemesterSubjectScore2.IsBackground = true; threadInsertSemesterSubjectScore2.Start(insertPackages2); threadInsertSemesterSubjectScore.Join(); threadInsertSemesterSubjectScore2.Join(); #endregion } ApplicationLog.Log("成績系統.匯入匯出", "匯入課程修課學生", "總共匯入" + (insertList.Count + updateList.Count) + "筆課程修課學生。"); #endregion }; }
public ImportStartupForm() { InitializeComponent(); InitializeSemesters(); //_effortMapper = new EffortMapper(); // 載入預設儲存值 LoadConfigData(); _worker = new BackgroundWorker(); _worker.WorkerReportsProgress = true; _worker.ProgressChanged += delegate(object sender, ProgressChangedEventArgs e) { lblMessage.Text = "" + e.UserState; }; _worker.DoWork += delegate(object sender, DoWorkEventArgs e) { #region Worker DoWork _worker.ReportProgress(0, "訊息:檢查讀卡文字格式…"); #region 檢查文字檔 ValidateTextFiles vtf = new ValidateTextFiles(StudentNumberMax); ValidateTextResult vtResult = vtf.CheckFormat(_files); if (vtResult.Error) { e.Result = vtResult; return; } #endregion //文字檔轉 RawData RawDataCollection rdCollection = new RawDataCollection(); rdCollection.ConvertFromFiles(_files); //RawData 轉 DataRecord DataRecordCollection drCollection = new DataRecordCollection(); drCollection.ConvertFromRawData(rdCollection); _rawDataValidator = new DataValidator <RawData>(); _dataRecordValidator = new DataValidator <DataRecord>(); #region 取得驗證需要的資料 SHCourse.RemoveAll(); _worker.ReportProgress(5, "訊息:取得學生資料…"); List <StudentObj> studentList = GetInSchoolStudents(); List <string> s_ids = new List <string>(); Dictionary <string, List <string> > studentNumberToStudentIDs = new Dictionary <string, List <string> >(); foreach (StudentObj student in studentList) { string sn = student.StudentNumber;// SCValidatorCreator.GetStudentNumberFormat(student.StudentNumber); if (!studentNumberToStudentIDs.ContainsKey(sn)) { studentNumberToStudentIDs.Add(sn, new List <string>()); } studentNumberToStudentIDs[sn].Add(student.StudentID); } foreach (string each in studentNumberToStudentIDs.Keys) { if (studentNumberToStudentIDs[each].Count > 1) { //學號重覆 } } foreach (var dr in drCollection) { if (studentNumberToStudentIDs.ContainsKey(dr.StudentNumber)) { s_ids.AddRange(studentNumberToStudentIDs[dr.StudentNumber]); } } studentList.Clear(); _worker.ReportProgress(10, "訊息:取得課程資料…"); List <SHCourseRecord> courseList = SHCourse.SelectBySchoolYearAndSemester(SchoolYear, Semester); List <SHAEIncludeRecord> aeList = SHAEInclude.SelectAll(); //List<JHSCAttendRecord> scaList = JHSCAttend.SelectAll(); var c_ids = from course in courseList select course.ID; _worker.ReportProgress(15, "訊息:取得修課資料…"); //List<JHSCAttendRecord> scaList2 = JHSCAttend.SelectByStudentIDAndCourseID(s_ids, c_ids.ToList<string>()); List <SHSCAttendRecord> scaList = new List <SHSCAttendRecord>(); FunctionSpliter <string, SHSCAttendRecord> spliter = new FunctionSpliter <string, SHSCAttendRecord>(300, 3); spliter.Function = delegate(List <string> part) { return(SHSCAttend.Select(part, c_ids.ToList <string>(), null, SchoolYear.ToString(), Semester.ToString())); }; scaList = spliter.Execute(s_ids); _worker.ReportProgress(20, "訊息:取得試別資料…"); List <SHExamRecord> examList = SHExam.SelectAll(); #endregion #region 註冊驗證 _worker.ReportProgress(30, "訊息:載入驗證規則…"); _rawDataValidator.Register(new SubjectCodeValidator()); _rawDataValidator.Register(new ClassCodeValidator()); _rawDataValidator.Register(new ExamCodeValidator()); SCValidatorCreator scCreator = new SCValidatorCreator(SHStudent.SelectByIDs(s_ids), courseList, scaList); _dataRecordValidator.Register(scCreator.CreateStudentValidator()); _dataRecordValidator.Register(new ExamValidator(examList)); _dataRecordValidator.Register(scCreator.CreateSCAttendValidator()); _dataRecordValidator.Register(new CourseExamValidator(scCreator.StudentCourseInfo, aeList, examList)); #endregion #region 進行驗證 _worker.ReportProgress(45, "訊息:進行驗證中…"); List <string> msgList = new List <string>(); foreach (RawData rawData in rdCollection) { List <string> msgs = _rawDataValidator.Validate(rawData); msgList.AddRange(msgs); } if (msgList.Count > 0) { e.Result = msgList; return; } foreach (DataRecord dataRecord in drCollection) { List <string> msgs = _dataRecordValidator.Validate(dataRecord); msgList.AddRange(msgs); } if (msgList.Count > 0) { e.Result = msgList; return; } #endregion #region 取得學生的評量成績 _worker.ReportProgress(65, "訊息:取得學生評量成績…"); _deleteScoreList.Clear(); _addScoreList.Clear(); AddScoreDic.Clear(); //var student_ids = from student in scCreator.StudentNumberDictionary.Values select student.ID; //List<string> course_ids = scCreator.AttendCourseIDs; var scaIDs = from sca in scaList select sca.ID; Dictionary <string, SHSCETakeRecord> sceList = new Dictionary <string, SHSCETakeRecord>(); FunctionSpliter <string, SHSCETakeRecord> spliterSCE = new FunctionSpliter <string, SHSCETakeRecord>(300, 3); spliterSCE.Function = delegate(List <string> part) { return(SHSCETake.Select(null, null, null, null, part)); }; foreach (SHSCETakeRecord sce in spliterSCE.Execute(scaIDs.ToList())) { string key = GetCombineKey(sce.RefStudentID, sce.RefCourseID, sce.RefExamID); if (!sceList.ContainsKey(key)) { sceList.Add(key, sce); } } Dictionary <string, SHExamRecord> examTable = new Dictionary <string, SHExamRecord>(); Dictionary <string, SHSCAttendRecord> scaTable = new Dictionary <string, SHSCAttendRecord>(); foreach (SHExamRecord exam in examList) { if (!examTable.ContainsKey(exam.Name)) { examTable.Add(exam.Name, exam); } } foreach (SHSCAttendRecord sca in scaList) { string key = GetCombineKey(sca.RefStudentID, sca.RefCourseID); if (!scaTable.ContainsKey(key)) { scaTable.Add(key, sca); } } _worker.ReportProgress(80, "訊息:成績資料建立…"); foreach (DataRecord dr in drCollection) { SHStudentRecord student = student = scCreator.StudentNumberDictionary[dr.StudentNumber]; SHExamRecord exam = examTable[dr.Exam]; List <SHCourseRecord> courses = new List <SHCourseRecord>(); foreach (SHCourseRecord course in scCreator.StudentCourseInfo.GetCourses(dr.StudentNumber)) { if (dr.Subjects.Contains(course.Subject)) { courses.Add(course); } } foreach (SHCourseRecord course in courses) { string key = GetCombineKey(student.ID, course.ID, exam.ID); if (sceList.ContainsKey(key)) { _deleteScoreList.Add(sceList[key]); } SHSCETakeRecord sh = new SHSCETakeRecord(); sh.RefCourseID = course.ID; sh.RefExamID = exam.ID; sh.RefSCAttendID = scaTable[GetCombineKey(student.ID, course.ID)].ID; sh.RefStudentID = student.ID; //轉型Double再轉回decimal,可去掉小數點後的0 double reScore = (double)dr.Score; decimal Score = decimal.Parse(reScore.ToString()); if (Global.StudentDocRemove) { string qq = Score.ToString(); if (qq.Contains(".")) { string[] kk = qq.Split('.'); sh.Score = decimal.Parse(kk[0]); } else { // sh.Score = decimal.Parse(Score.ToString()); } } else { sh.Score = decimal.Parse(Score.ToString()); } //sceNew.Effort = _effortMapper.GetCodeByScore(dr.Score); //是否有重覆的學生,課程,評量 if (!AddScoreDic.ContainsKey(sh.RefStudentID + "_" + course.ID + "_" + exam.ID)) { _addScoreList.Add(sh); AddScoreDic.Add(sh.RefStudentID + "_" + course.ID + "_" + exam.ID, sh); } } } #endregion _worker.ReportProgress(100, "訊息:背景作業完成…"); e.Result = null; #endregion }; _worker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e) { #region Worker Completed if (e.Error == null && e.Result == null) { if (!_upload.IsBusy) { //如果學生身上已有成績,則提醒使用者 if (_deleteScoreList.Count > 0) { _warn.RunWorkerAsync(); } else { lblMessage.Text = "訊息:成績上傳中…"; FISCA.Presentation.MotherForm.SetStatusBarMessage("成績上傳中…", 0); counter = 0; _upload.RunWorkerAsync(); } } } else { ControlEnable = true; if (e.Error != null) { MsgBox.Show("匯入失敗。" + e.Error.Message); SmartSchool.ErrorReporting.ReportingService.ReportException(e.Error); } else if (e.Result != null && e.Result is ValidateTextResult) { ValidateTextResult result = e.Result as ValidateTextResult; ValidationErrorViewer viewer = new ValidationErrorViewer(); viewer.SetTextFileError(result.LineIndexes, result.ErrorFormatLineIndexes, result.DuplicateLineIndexes); viewer.ShowDialog(); } else if (e.Result != null && e.Result is List <string> ) { ValidationErrorViewer viewer = new ValidationErrorViewer(); viewer.SetErrorLines(e.Result as List <string>); viewer.ShowDialog(); } } #endregion }; _upload = new BackgroundWorker(); _upload.WorkerReportsProgress = true; _upload.ProgressChanged += new ProgressChangedEventHandler(_upload_ProgressChanged); _upload.DoWork += new DoWorkEventHandler(_upload_DoWork); _upload.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_upload_RunWorkerCompleted); _warn = new BackgroundWorker(); _warn.WorkerReportsProgress = true; _warn.DoWork += delegate(object sender, DoWorkEventArgs e) { _warn.ReportProgress(0, "產生警告訊息..."); examDict = new Dictionary <string, string>(); foreach (SHExamRecord exam in SHExam.SelectAll()) { if (!examDict.ContainsKey(exam.ID)) { examDict.Add(exam.ID, exam.Name); } } WarningForm form = new WarningForm(); int count = 0; foreach (SHSCETakeRecord sce in _deleteScoreList) { // 當成績資料是空值跳過 //if (sce.Score.HasValue == false && sce.Effort.HasValue == false && string.IsNullOrEmpty(sce.Text)) //if (sce.Score == null && string.IsNullOrEmpty(sce.Text)) // continue; count++; SHStudentRecord student = SHStudent.SelectByID(sce.RefStudentID); SHCourseRecord course = SHCourse.SelectByID(sce.RefCourseID); string exam = (examDict.ContainsKey(sce.RefExamID) ? examDict[sce.RefExamID] : "<未知的試別>"); string s = ""; if (student.Class != null) { s += student.Class.Name; } if (!string.IsNullOrEmpty("" + student.SeatNo)) { s += " " + student.SeatNo + "號"; } if (!string.IsNullOrEmpty(student.StudentNumber)) { s += " (" + student.StudentNumber + ")"; } s += " " + student.Name; string scoreName = sce.RefStudentID + "_" + sce.RefCourseID + "_" + sce.RefExamID; if (AddScoreDic.ContainsKey(scoreName)) { form.AddMessage(student.ID, s, string.Format("學生在「{0}」課程「{1}」中已有成績「{2}」將修改為「{3}」。", course.Name, exam, sce.Score, AddScoreDic[scoreName].Score)); } else { form.AddMessage(student.ID, s, string.Format("學生在「{0}」課程「{1}」中已有成績「{2}」。", course.Name, exam, sce.Score)); } _warn.ReportProgress((int)(count * 100 / _deleteScoreList.Count), "產生警告訊息..."); } e.Result = form; }; _warn.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e) { WarningForm form = e.Result as WarningForm; if (form.ShowDialog() == DialogResult.OK) { lblMessage.Text = "訊息:成績上傳中…"; FISCA.Presentation.MotherForm.SetStatusBarMessage("成績上傳中…", 0); counter = 0; _upload.RunWorkerAsync(); } else { this.DialogResult = DialogResult.Cancel; } }; _warn.ProgressChanged += delegate(object sender, ProgressChangedEventArgs e) { FISCA.Presentation.MotherForm.SetStatusBarMessage("" + e.UserState, e.ProgressPercentage); }; _files = new List <FileInfo>(); _addScoreList = new List <SHSCETakeRecord>(); _deleteScoreList = new List <SHSCETakeRecord>(); AddScoreDic = new Dictionary <string, SHSCETakeRecord>(); examDict = new Dictionary <string, string>(); }