/// <summary> /// 儲存學習歷程項目 /// </summary> /// <param name="SemesterHistoryItemEntityList"></param> public static void SetSemesterHistoryItemEntityList(List <SemesterHistoryItemEntity> SemesterHistoryItemEntityList, List <string> StudntIDList) { Dictionary <string, JHSemesterHistoryRecord> JHSemesterHistoryRecordDic = new Dictionary <string, JHSemesterHistoryRecord>(); foreach (JHSemesterHistoryRecord shr in JHSemesterHistory.SelectByStudentIDs(StudntIDList)) { JHSemesterHistoryRecordDic.Add(shr.RefStudentID, shr); } List <JHSemesterHistoryRecord> UpadteRecList = new List <JHSemesterHistoryRecord>(); foreach (SemesterHistoryItemEntity shie in SemesterHistoryItemEntityList) { K12.Data.SemesterHistoryItem shi = new K12.Data.SemesterHistoryItem(); shi.ClassName = shie.ClassName; shi.GradeYear = shie.GradeYear; shi.SchoolDayCount = shie.SchoolDayCount; shi.SchoolYear = shie.Schoolyear; shi.SeatNo = shie.SeatNo; shi.Semester = shie.Semester; shi.Teacher = shie.TeacherName; if (JHSemesterHistoryRecordDic.ContainsKey(shie.StudentID)) { // 完全沒學習歷程 if (shie.HasSemsterHistoryRec == false && shie.EditMode == SemesterHistoryItemEntity._EditMode.Insert) { JHSemesterHistoryRecordDic[shie.StudentID].SemesterHistoryItems.Add(shi); } // 有學習歷程 if (shie.HasSemsterHistoryRec) { K12.Data.SemesterHistoryItem rmItem = null; foreach (K12.Data.SemesterHistoryItem sh in JHSemesterHistoryRecordDic[shie.StudentID].SemesterHistoryItems) { // 檢查當有同學年度學期移除 if (sh.SchoolYear == shi.SchoolYear && sh.Semester == shi.Semester) { rmItem = sh; } } // 先移除舊的 if (rmItem != null) { JHSemesterHistoryRecordDic[shie.StudentID].SemesterHistoryItems.Remove(rmItem); } if (shie.EditMode == SemesterHistoryItemEntity._EditMode.Insert) { JHSemesterHistoryRecordDic[shie.StudentID].SemesterHistoryItems.Add(shi); } if (shie.EditMode == SemesterHistoryItemEntity._EditMode.Update) { JHSemesterHistoryRecordDic[shie.StudentID].SemesterHistoryItems.Add(shi); } if (shie.EditMode == SemesterHistoryItemEntity._EditMode.Delete) { } } UpadteRecList.Add(JHSemesterHistoryRecordDic[shie.StudentID]); } } JHSemesterHistory.Update(UpadteRecList); }
void wizard_ImportPackage(object sender, SmartSchool.API.PlugIn.Import.ImportPackageEventArgs e) { //根據學生編號、學年度、學期組成主鍵 List <string> keyList = new List <string>(); Dictionary <string, int> schoolYearMapping = new Dictionary <string, int>(); Dictionary <string, int> semesterMapping = new Dictionary <string, int>(); Dictionary <string, string> studentIDMapping = new Dictionary <string, string>(); Dictionary <string, List <RowData> > rowsMapping = new Dictionary <string, List <RowData> >(); //一個學生只會有一筆學期歷程 Dictionary <string, JHSemesterHistoryRecord> studentSemesterHistory = new Dictionary <string, JHSemesterHistoryRecord>(); //掃描每行資料,定出資料的PrimaryKey,並且將PrimaryKey對應到的資料寫成Dictionary foreach (RowData Row in e.Items) { int schoolYear = int.Parse(Row["學年度"]); int semester = int.Parse(Row["學期"]); string studentID = Row.ID; string key = schoolYear + "^_^" + semester + "^_^" + studentID; if (!keyList.Contains(key)) { keyList.Add(key); schoolYearMapping.Add(key, schoolYear); semesterMapping.Add(key, semester); studentIDMapping.Add(key, studentID); rowsMapping.Add(key, new List <RowData>()); } rowsMapping[key].Add(Row); } //一次取得學生學期歷程 List <JHSemesterHistoryRecord> records = JHSemesterHistory.SelectByStudentIDs(studentIDMapping.Values.Distinct()); #region 將學生現有學期歷程做快取 foreach (JHSemesterHistoryRecord var in records) { studentSemesterHistory.Add(var.RefStudentID, var); } #endregion List <JHSemesterHistoryRecord> UpdateRecords = new List <JHSemesterHistoryRecord>(); foreach (string key in keyList) { JHSemesterHistoryRecord record = studentSemesterHistory[studentIDMapping[key]]; List <RowData> Rows = rowsMapping[key]; if (record != null) { for (int i = 0; i < Rows.Count; i++) { string StudentID = Rows[i].ID; string SchoolYear = Rows[i]["學年度"]; string Semester = Rows[i]["學期"]; string GradeYear = Rows[i]["年級"]; string ClassName = Rows[i].ContainsKey("當時班級")?Rows[i]["當時班級"]:string.Empty; string SeatNo = Rows[i].ContainsKey("當時座號")?Rows[i]["當時座號"]:string.Empty; string TeacherName = Rows[i].ContainsKey("當時班導師姓名")?Rows[i]["當時班導師姓名"]:string.Empty; string SchoolDayCount = Rows[i].ContainsKey("上課天數")?Rows[i]["上課天數"]:string.Empty; List <K12.Data.SemesterHistoryItem> Items = record.SemesterHistoryItems.Where(x => x.RefStudentID == StudentID && x.SchoolYear.ToString() == SchoolYear && x.Semester.ToString() == Semester).ToList(); if (Items.Count > 0) { Items[0].GradeYear = K12.Data.Int.Parse(GradeYear); if (Rows[i].ContainsKey("當時班級")) { Items[0].ClassName = ClassName; } if (Rows[i].ContainsKey("當時座號")) { Items[0].SeatNo = K12.Data.Int.ParseAllowNull(SeatNo); } if (Rows[i].ContainsKey("當時班導師姓名")) { Items[0].Teacher = TeacherName; } if (Rows[i].ContainsKey("上課天數")) { Items[0].SchoolDayCount = K12.Data.Int.ParseAllowNull(SchoolDayCount); } } else { K12.Data.SemesterHistoryItem Item = new K12.Data.SemesterHistoryItem(); Item.SchoolYear = K12.Data.Int.Parse(SchoolYear); Item.Semester = K12.Data.Int.Parse(Semester); Item.GradeYear = K12.Data.Int.Parse(GradeYear); Item.ClassName = ClassName; Item.SeatNo = K12.Data.Int.ParseAllowNull(SeatNo); Item.Teacher = TeacherName; Item.SchoolDayCount = K12.Data.Int.ParseAllowNull(SchoolDayCount); record.SemesterHistoryItems.Add(Item); } } UpdateRecords.Add(record); } else { JHSemesterHistoryRecord newrecord = new JHSemesterHistoryRecord(); newrecord.RefStudentID = Rows[0].ID; List <K12.Data.SemesterHistoryItem> Items = new List <K12.Data.SemesterHistoryItem>(); for (int i = 0; i < Rows.Count; i++) { string SchoolYear = Rows[i]["學年度"]; string Semester = Rows[i]["學期"]; string GradeYear = Rows[i].ContainsKey("年級") ? Rows[i]["年級"] : string.Empty; string ClassName = Rows[i].ContainsKey("當時班級") ? Rows[i]["當時班級"] : string.Empty; string SeatNo = Rows[i].ContainsKey("當時座號") ? Rows[i]["當時座號"] : string.Empty; string TeacherName = Rows[i].ContainsKey("當時班導師姓名") ? Rows[i]["當時班導師姓名"] : string.Empty; string SchoolDayCount = Rows[i].ContainsKey("上課天數") ? Rows[i]["上課天數"] : string.Empty; K12.Data.SemesterHistoryItem Item = new K12.Data.SemesterHistoryItem(); Item.SchoolYear = K12.Data.Int.Parse(SchoolYear); Item.Semester = K12.Data.Int.Parse(Semester); Item.GradeYear = K12.Data.Int.Parse(GradeYear); Item.ClassName = ClassName; Item.SeatNo = K12.Data.Int.ParseAllowNull(SeatNo); Item.Teacher = TeacherName; Item.SchoolDayCount = K12.Data.Int.ParseAllowNull(SchoolDayCount); Items.Add(Item); } newrecord.SemesterHistoryItems = Items; UpdateRecords.Add(newrecord); } } if (UpdateRecords.Count > 0) { JHSemesterHistory.Update(UpdateRecords); } }