Exemplo n.º 1
0
        void bkw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bkw                  = ((BackgroundWorker)sender);
            int                  schoolyear       = (int)((object[])e.Argument)[0];
            AccessHelper         helper           = (AccessHelper)((object[])e.Argument)[1];
            List <StudentRecord> selectedStudents = (List <StudentRecord>)((object[])e.Argument)[2];

            bkw.ReportProgress(1, null);
            AngelDemonComputer computer           = new AngelDemonComputer();
            int packageSize                       = 50;
            int packageCount                      = 0;
            List <StudentRecord>         package  = null;
            List <List <StudentRecord> > packages = new List <List <StudentRecord> >();

            foreach (StudentRecord s in selectedStudents)
            {
                if (packageCount == 0)
                {
                    package = new List <StudentRecord>(packageSize);
                    packages.Add(package);
                    packageCount = packageSize;
                    packageSize += 50;
                    if (packageSize > _MaxPackageSize)
                    {
                        packageSize = _MaxPackageSize;
                    }
                }
                package.Add(s);
                packageCount--;
            }
            double maxStudents = selectedStudents.Count;

            if (maxStudents == 0)
            {
                maxStudents = 1;
            }
            double computedStudents = 0;
            bool   allPass          = true;

            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>();
            XmlDocument doc = new XmlDocument();

            foreach (List <StudentRecord> var in packages)
            {
                computedStudents += var.Count;
                Dictionary <StudentRecord, List <string> > errormessages = computer.FillSchoolYearDemonScore(schoolyear, helper, var);
                helper.StudentHelper.FillSchoolYearEntryScore(false, var);

                #region 每個學生去整理新增跟修改的資料
                foreach (StudentRecord stu in var)
                {
                    if (stu.Fields.ContainsKey("CalcSchoolYearMoralScores"))
                    {
                        Dictionary <string, decimal> entryScore = (Dictionary <string, decimal>)stu.Fields["CalcSchoolYearMoralScores"];
                        //有分項成績
                        if (entryScore.Count > 0)
                        {
                            int?gradeyear = null;
                            #region 判斷年級
                            foreach (SemesterEntryScoreInfo score in stu.SemesterEntryScoreList)
                            {
                                if (score.Entry == "德行" && score.SchoolYear == schoolyear)
                                {
                                    if (gradeyear == null || score.GradeYear > gradeyear)
                                    {
                                        gradeyear = score.GradeYear;
                                    }
                                }
                            }
                            #endregion
                            //年級沒有問題
                            if (gradeyear != null)
                            {
                                string updateid = "";
                                #region 找到ID,將計算分項與現有成績的分向做聯集
                                Dictionary <int, Dictionary <string, string> > scoreID = (Dictionary <int, Dictionary <string, string> >)stu.Fields["SchoolYearEntryScoreID"];
                                //只處理德行分項
                                foreach (SchoolYearEntryScoreInfo sc in stu.SchoolYearEntryScoreList)
                                {
                                    if (sc.Entry == "德行" && sc.SchoolYear == schoolyear)
                                    {
                                        updateid = scoreID[sc.SchoolYear]["行為"];
                                        //如果計算的結果並不包含已存在成績的分項,將該分項及成績加入至計算的結果
                                        if (!entryScore.ContainsKey(sc.Entry))
                                        {
                                            entryScore.Add(sc.Entry, sc.Score);
                                        }
                                    }
                                }
                                #endregion
                                if (updateid != "")
                                {
                                    XmlElement entryScoreInfo = doc.CreateElement("SchoolYearEntryScore");
                                    foreach (string entry in entryScore.Keys)
                                    {
                                        XmlElement entryElement = doc.CreateElement("Entry");
                                        entryElement.SetAttribute("分項", entry);
                                        entryElement.SetAttribute("成績", "" + entryScore[entry]);
                                        entryScoreInfo.AppendChild(entryElement);
                                    }
                                    updateList.Add(new SmartSchool.Feature.Score.EditScore.UpdateInfo(updateid, "" + gradeyear, entryScoreInfo));
                                }
                                else
                                {
                                    XmlElement entryScoreInfo = doc.CreateElement("SchoolYearEntryScore");
                                    foreach (string entry in entryScore.Keys)
                                    {
                                        XmlElement entryElement = doc.CreateElement("Entry");
                                        entryElement.SetAttribute("分項", entry);
                                        entryElement.SetAttribute("成績", "" + entryScore[entry]);
                                        entryScoreInfo.AppendChild(entryElement);
                                    }
                                    insertList.Add(new SmartSchool.Feature.Score.AddScore.InsertInfo(stu.StudentID, "" + schoolyear, "", "" + gradeyear, "行為", entryScoreInfo));
                                }
                            }
                        }
                    }
                }
                #endregion


                if (errormessages.Count > 0)
                {
                    allPass = false;
                }
                if (bkw.CancellationPending)
                {
                    break;
                }
                else
                {
                    bkw.ReportProgress((int)((computedStudents * 100.0) / maxStudents), errormessages);
                }
            }
            if (allPass)
            {
                e.Result = new object[] { insertList, updateList, selectedStudents }
            }
            ;
            else
            {
                e.Result = null;
            }
        }