public void AddSubject(SchoolYearSubjectScoreInfo info) { if (info.SchoolYear == ((int)this._config.SchoolYear)) { bool found = false; foreach (ScoreData var in this._subjects.Values) { if (var.Name == info.Subject) { var.SetInfo(info.Subject, info.Score); string sign = ""; decimal rS1 = -1, rS2 = -1; // 判斷是否來自學年補考或重修 if (decimal.TryParse(info.Detail.GetAttribute("補考成績"), out rS1)) { if (info.Score == rS1) { sign = this._config.YearResitSign; } } if (decimal.TryParse(info.Detail.GetAttribute("重修成績"), out rS2)) { if (info.Score == rS2) { sign += this._config.YearRepeatSign; } } // 學年成績標示 var.SetYearScoreSign(sign); found = true; } } if (!found) { if (!this._subjects.ContainsKey(info.Subject)) { this._subjects.Add(info.Subject, new ScoreData()); } this._subjects[info.Subject].SetInfo(info.Subject, info.Score); } } }
protected override void OnDataImport(DataImportEventArgs args) { Dictionary <string, List <RowData> > id_Rows = new Dictionary <string, List <RowData> >(); #region 分包裝 foreach (RowData data in args.Items) { if (!id_Rows.ContainsKey(data.ID)) { id_Rows.Add(data.ID, new List <RowData>()); } id_Rows[data.ID].Add(data); } #endregion List <SmartSchool.Feature.Score.AddScore.InsertInfo> insertList = new List <SmartSchool.Feature.Score.AddScore.InsertInfo>(); List <SmartSchool.Feature.Score.EditScore.UpdateInfo> updateList = new List <SmartSchool.Feature.Score.EditScore.UpdateInfo>(); //交叉比對各學生資料 #region 交叉比對各學生資料 foreach (string id in id_Rows.Keys) { XmlDocument doc = new XmlDocument(); StudentRecord studentRec = _StudentCollection[id]; //該學生的學期學年成績 Dictionary <int, Dictionary <string, SchoolYearSubjectScoreInfo> > schoolYearScoreDictionary = new Dictionary <int, Dictionary <string, SchoolYearSubjectScoreInfo> >(); Dictionary <int, string> schoolYearScoreID = new Dictionary <int, string>(); Dictionary <int, string> scoreID = (Dictionary <int, string>)studentRec.Fields["SchoolYearSubjectScoreID"]; #region 整理現有的成績資料 foreach (SchoolYearSubjectScoreInfo var in studentRec.SchoolYearSubjectScoreList) { string key = var.Subject; if (!schoolYearScoreDictionary.ContainsKey(var.SchoolYear)) { schoolYearScoreDictionary.Add(var.SchoolYear, new Dictionary <string, SchoolYearSubjectScoreInfo>()); } if (!schoolYearScoreDictionary[var.SchoolYear].ContainsKey(key)) { schoolYearScoreDictionary[var.SchoolYear].Add(key, var); } //填入學年科目成績編號 if (!schoolYearScoreID.ContainsKey(var.SchoolYear)) { schoolYearScoreID.Add(var.SchoolYear, scoreID[var.SchoolYear]); } } #endregion //要匯入的學期學年成績 Dictionary <int, Dictionary <string, RowData> > schoolYearImportScoreDictionary = new Dictionary <int, Dictionary <string, RowData> >(); #region 整理要匯入的資料 foreach (RowData row in id_Rows[id]) { int t; string subject = row["科目"]; string schoolYear = row["學年度"]; int sy = int.Parse(schoolYear); string key = subject; if (!schoolYearImportScoreDictionary.ContainsKey(sy)) { schoolYearImportScoreDictionary.Add(sy, new Dictionary <string, RowData>()); } if (!schoolYearImportScoreDictionary[sy].ContainsKey(key)) { schoolYearImportScoreDictionary[sy].Add(key, row); } } #endregion //學期年級重整 Dictionary <int, int> schoolYearGradeYear = new Dictionary <int, int>(); //要變更成績的學期 List <int> updatedSchoolYear = new List <int>(); //在變更學期中新增加的成績資料 Dictionary <int, List <RowData> > updatedNewSchoolYearScore = new Dictionary <int, List <RowData> >(); //要增加成績的學期 Dictionary <int, List <RowData> > insertNewSchoolYearScore = new Dictionary <int, List <RowData> >(); //開始處理ImportScore #region 開始處理ImportScore foreach (int sy in schoolYearImportScoreDictionary.Keys) { foreach (string key in schoolYearImportScoreDictionary[sy].Keys) { RowData data = schoolYearImportScoreDictionary[sy][key]; //如果是本來沒有這筆學期的成績就加到insertNewSemesterScore if (!schoolYearScoreDictionary.ContainsKey(sy)) { if (!insertNewSchoolYearScore.ContainsKey(sy)) { insertNewSchoolYearScore.Add(sy, new List <RowData>()); } insertNewSchoolYearScore[sy].Add(data); //加入學期年級 int gy = int.Parse(data["成績年級"]); if (!schoolYearGradeYear.ContainsKey(sy)) { schoolYearGradeYear.Add(sy, gy); } else { schoolYearGradeYear[sy] = gy; } } else { bool hasChanged = false; //修改已存在的資料 if (schoolYearScoreDictionary[sy].ContainsKey(key)) { SchoolYearSubjectScoreInfo score = schoolYearScoreDictionary[sy][key]; #region 填入此學期的年級資料 if (!schoolYearGradeYear.ContainsKey(sy)) { schoolYearGradeYear.Add(sy, score.GradeYear); } #endregion #region 直接修改已存在的成績資料的Detail foreach (string field in args.ImportFields) { string value = data[field]; switch (field) { default: break; case "結算成績": case "補考成績": case "重修成績": if (score.Detail.GetAttribute(field) != value) { score.Detail.SetAttribute(field, value); hasChanged = true; } break; case "成績年級": int gy = int.Parse(data["成績年級"]); if (score.GradeYear != gy) { schoolYearGradeYear[sy] = gy; hasChanged = true; } break; } } decimal topScore = decimal.MinValue, tryParseScore; foreach (var item in new string[] { "結算成績", "補考成績", "重修成績" }) { if (decimal.TryParse(score.Detail.GetAttribute(item), out tryParseScore) && tryParseScore > topScore) { topScore = tryParseScore; } } score.Detail.SetAttribute("學年成績", "" + topScore); #endregion } else//加入新成績至已存在的學期 { //加入學期年級 int gy = int.Parse(data["成績年級"]); if (!schoolYearGradeYear.ContainsKey(sy)) { schoolYearGradeYear.Add(sy, gy); } else { schoolYearGradeYear[sy] = gy; } //加入新成績至已存在的學期 if (!updatedNewSchoolYearScore.ContainsKey(sy)) { updatedNewSchoolYearScore.Add(sy, new List <RowData>()); } updatedNewSchoolYearScore[sy].Add(data); hasChanged = true; } //真的有變更 if (hasChanged) { #region 登錄有變更的學期 if (!updatedSchoolYear.Contains(sy)) { updatedSchoolYear.Add(sy); } #endregion } } } } #endregion //處理已登錄要更新的學期成績 #region 處理已登錄要更新的學期成績 foreach (int sy in updatedSchoolYear) { string scoreid = schoolYearScoreID[sy]; //從學年抓ID string gradeyear = "" + schoolYearGradeYear[sy]; //抓年級 XmlElement subjectScoreInfo = doc.CreateElement("SchoolYearSubjectScore"); #region 產生該學期學年成績的XML foreach (SchoolYearSubjectScoreInfo scoreInfo in schoolYearScoreDictionary[sy].Values) { subjectScoreInfo.AppendChild(doc.ImportNode(scoreInfo.Detail, true)); } if (updatedNewSchoolYearScore.ContainsKey(sy)) { foreach (RowData row in updatedNewSchoolYearScore[sy]) { XmlElement newScore = doc.CreateElement("Subject"); #region 建立newScore foreach (string field in new string[] { "科目", "結算成績", "補考成績", "重修成績" }) { if (args.ImportFields.Contains(field)) { string value = row[field]; switch (field) { default: break; case "科目": newScore.SetAttribute("科目", value); break; case "結算成績": case "補考成績": case "重修成績": newScore.SetAttribute(field, value); break; } } } decimal topScore = decimal.MinValue, tryParseScore; foreach (var item in new string[] { "結算成績", "補考成績", "重修成績" }) { if (decimal.TryParse(newScore.GetAttribute(item), out tryParseScore) && tryParseScore > topScore) { topScore = tryParseScore; } } newScore.SetAttribute("學年成績", "" + topScore); #endregion subjectScoreInfo.AppendChild(newScore); } } #endregion updateList.Add(new SmartSchool.Feature.Score.EditScore.UpdateInfo(scoreid, gradeyear, subjectScoreInfo)); } #endregion //處理新增成績學期 #region 處理新增成績學期 foreach (int sy in insertNewSchoolYearScore.Keys) { XmlElement subjectScoreInfo = doc.CreateElement("SchoolYearSubjectScore"); string gradeyear = "" + schoolYearGradeYear[sy];//抓年級 foreach (RowData row in insertNewSchoolYearScore[sy]) { XmlElement newScore = doc.CreateElement("Subject"); #region 建立newScore foreach (string field in new string[] { "科目", "結算成績", "補考成績", "重修成績" }) { if (args.ImportFields.Contains(field)) { string value = row[field]; switch (field) { default: break; case "科目": newScore.SetAttribute("科目", value); break; case "結算成績": case "補考成績": case "重修成績": newScore.SetAttribute(field, value); break; } } } decimal topScore = decimal.MinValue, tryParseScore; foreach (var item in new string[] { "結算成績", "補考成績", "重修成績" }) { if (decimal.TryParse(newScore.GetAttribute(item), out tryParseScore) && tryParseScore > topScore) { topScore = tryParseScore; } } newScore.SetAttribute("學年成績", "" + topScore); #endregion subjectScoreInfo.AppendChild(newScore); } insertList.Add(new SmartSchool.Feature.Score.AddScore.InsertInfo(studentRec.StudentID, "" + sy, "", gradeyear, "", subjectScoreInfo)); } #endregion } #endregion if (updateList.Count > 0) { #region 分批次兩路上傳 List <List <SmartSchool.Feature.Score.EditScore.UpdateInfo> > updatePackages = new List <List <SmartSchool.Feature.Score.EditScore.UpdateInfo> >(); List <List <SmartSchool.Feature.Score.EditScore.UpdateInfo> > updatePackages2 = new List <List <SmartSchool.Feature.Score.EditScore.UpdateInfo> >(); { List <SmartSchool.Feature.Score.EditScore.UpdateInfo> package = null; int count = 0; foreach (SmartSchool.Feature.Score.EditScore.UpdateInfo var in updateList) { if (count == 0) { package = new List <SmartSchool.Feature.Score.EditScore.UpdateInfo>(30); count = 30; if ((updatePackages.Count & 1) == 0) { updatePackages.Add(package); } else { updatePackages2.Add(package); } } package.Add(var); count--; } } Thread threadUpdateSchoolYearSubjectScore = new Thread(new ParameterizedThreadStart(updateSchoolYearSubjectScore)); threadUpdateSchoolYearSubjectScore.IsBackground = true; threadUpdateSchoolYearSubjectScore.Start(updatePackages); Thread threadUpdateSchoolYearSubjectScore2 = new Thread(new ParameterizedThreadStart(updateSchoolYearSubjectScore)); threadUpdateSchoolYearSubjectScore2.IsBackground = true; threadUpdateSchoolYearSubjectScore2.Start(updatePackages2); threadUpdateSchoolYearSubjectScore.Join(); threadUpdateSchoolYearSubjectScore2.Join(); #endregion } if (insertList.Count > 0) { #region 分批次兩路上傳 List <List <SmartSchool.Feature.Score.AddScore.InsertInfo> > insertPackages = new List <List <SmartSchool.Feature.Score.AddScore.InsertInfo> >(); List <List <SmartSchool.Feature.Score.AddScore.InsertInfo> > insertPackages2 = new List <List <SmartSchool.Feature.Score.AddScore.InsertInfo> >(); { List <SmartSchool.Feature.Score.AddScore.InsertInfo> package = null; int count = 0; foreach (SmartSchool.Feature.Score.AddScore.InsertInfo var in insertList) { if (count == 0) { package = new List <SmartSchool.Feature.Score.AddScore.InsertInfo>(30); count = 30; if ((insertPackages.Count & 1) == 0) { insertPackages.Add(package); } else { insertPackages2.Add(package); } } package.Add(var); count--; } } Thread threadInsertSchoolYearSubjectScore = new Thread(new ParameterizedThreadStart(insertSchoolYearSubjectScore)); threadInsertSchoolYearSubjectScore.IsBackground = true; threadInsertSchoolYearSubjectScore.Start(insertPackages); Thread threadInsertSchoolYearSubjectScore2 = new Thread(new ParameterizedThreadStart(insertSchoolYearSubjectScore)); threadInsertSchoolYearSubjectScore2.IsBackground = true; threadInsertSchoolYearSubjectScore2.Start(insertPackages2); threadInsertSchoolYearSubjectScore.Join(); threadInsertSchoolYearSubjectScore2.Join(); #endregion } }