Пример #1
0
        void _bgworker_DoWork(object sender, DoWorkEventArgs e)
        {
            object[]           obj              = (object[])e.Argument;
            AccessHelper       helper           = (AccessHelper)obj[0];
            List <ClassRecord> selected_classes = (List <ClassRecord>)obj[1];
            ReportOptions      option           = (ReportOptions)obj[2];


            _bgworker.ReportProgress(0);

            StudentCollection student_collection = new StudentCollection();
            //List<StudentRecord> failed = new List<StudentRecord>();
            double student_counter = 1;
            double student_total   = 0;
            List <StudentRecord> selected_students = new List <StudentRecord>();

            foreach (ClassRecord classRec in selected_classes)
            {
                student_total += classRec.Students.Count;
                selected_students.AddRange(classRec.Students);
            }
            #region 資料取得與整理

            int package_size = 50;
            List <StudentRecord> packaged_students = new List <StudentRecord>();
            foreach (StudentRecord record in selected_students)
            {
                packaged_students.Add(record);
                if (packaged_students.Count >= package_size)
                {
                    helper.StudentHelper.FillSemesterSubjectScore(true, packaged_students);
                    helper.StudentHelper.FillSemesterEntryScore(true, packaged_students);
                    packaged_students.Clear();
                }
                _bgworker.ReportProgress((int)(student_counter++ *40.0 / student_total));
            }
            if (packaged_students.Count > 0)
            {
                helper.StudentHelper.FillSemesterSubjectScore(true, packaged_students);
                helper.StudentHelper.FillSemesterEntryScore(true, packaged_students);
                packaged_students.Clear();
            }

            foreach (ClassRecord classRec in selected_classes)
            {
                List <Student> list = new List <Student>();
                foreach (StudentRecord each_stu in classRec.Students)
                {
                    Student student = new Student(each_stu, option.ScoreType, option.PrintSemester, option.PrintEntries);
                    list.Add(student);
                    if (!student_collection.ContainsKey(each_stu.StudentID))
                    {
                        student_collection.Add(each_stu.StudentID, student);
                    }
                }
                #region 科目班排名
                List <string> rankedSubject = new List <string>();
                foreach (Student stu in list)
                {
                    foreach (string subject in stu.SubjectCollection.Keys)
                    {
                        if (rankedSubject.Contains(subject))
                        {
                            continue;
                        }
                        List <decimal> scores = new List <decimal>();
                        Dictionary <decimal, List <SubjectInfo> > rank = new Dictionary <decimal, List <SubjectInfo> >();
                        foreach (Student student in list)
                        {
                            if (student.SubjectCollection.ContainsKey(subject))
                            {
                                decimal score = student.SubjectCollection[subject].GetAverange();
                                scores.Add(score);
                                if (!rank.ContainsKey(score))
                                {
                                    rank.Add(score, new List <SubjectInfo>());
                                }
                                rank[score].Add(student.SubjectCollection[subject]);
                            }
                        }
                        scores.Sort();
                        scores.Reverse();

                        decimal lastscore = decimal.MinValue;
                        int     count     = scores.Count;
                        foreach (decimal score in scores)
                        {
                            if (lastscore != score)
                            {
                                int place = scores.IndexOf(score) + 1;
                                lastscore = score;
                                foreach (SubjectInfo s in rank[lastscore])
                                {
                                    s.Place = place;
                                    s.Radix = count;
                                }
                            }
                        }

                        rankedSubject.Add(subject);
                    }
                }
                #endregion
                #region 分項班排名
                List <string> rankedEntrys = new List <string>();
                foreach (Student stu in list)
                {
                    foreach (string entry in stu.EntryCollection.Keys)
                    {
                        if (rankedEntrys.Contains(entry))
                        {
                            continue;
                        }
                        List <decimal> scores = new List <decimal>();
                        Dictionary <decimal, List <EntryInfo> > rank = new Dictionary <decimal, List <EntryInfo> >();
                        foreach (Student student in list)
                        {
                            if (student.EntryCollection.ContainsKey(entry))
                            {
                                decimal score = student.EntryCollection[entry].GetAverange();
                                scores.Add(score);
                                if (!rank.ContainsKey(score))
                                {
                                    rank.Add(score, new List <EntryInfo>());
                                }
                                rank[score].Add(student.EntryCollection[entry]);
                            }
                        }
                        scores.Sort();
                        scores.Reverse();

                        decimal lastscore = decimal.MinValue;
                        int     count     = scores.Count;
                        //把排名填回每一個成績資料中
                        foreach (decimal score in scores)
                        {
                            if (lastscore != score)
                            {
                                int place = scores.IndexOf(score) + 1;
                                lastscore = score;
                                foreach (EntryInfo s in rank[lastscore])
                                {
                                    s.Place = place;
                                    s.Radix = count;
                                    //處理德行不允許破百
                                    if (s.Name == "德行" && option.FixMoralScore)
                                    {
                                        s.FixToLimit(100);
                                    }
                                }
                            }
                        }

                        rankedEntrys.Add(entry);
                    }
                }
                #endregion
            }
            #endregion
            student_counter = 1;
            #region 產生報表及SnapShop
            MemoryStream template = GetTemplate(option);
            Document     doc      = new Document();
            doc.Sections.Clear();
            List <SmartSchool.Feature.Student.StudentSnapShop> snapShops            = new List <SmartSchool.Feature.Student.StudentSnapShop>();
            MultiThreadWorker <SmartSchool.Feature.Student.StudentSnapShop> mWorker = new MultiThreadWorker <SmartSchool.Feature.Student.StudentSnapShop>();
            mWorker.MaxThreads     = 3;
            mWorker.PackageSize    = 50;
            mWorker.PackageWorker += new EventHandler <PackageWorkEventArgs <SmartSchool.Feature.Student.StudentSnapShop> >(mWorker_PackageWorker);

            foreach (string stuid in student_collection.Keys)
            {
                Student each_stu = student_collection[stuid];
                #region 一個學生產生一個新的Document
                //一個學生產生一個新的Document
                Document each_page = new Document(template, "", LoadFormat.Doc, "");
                #region 建立此學生的成績單
                //合併基本資料
                List <string> merge_keys   = new List <string>();
                List <object> merge_values = new List <object>();
                merge_keys.AddRange(new string[] { "學校名稱", "科別名稱", "班級名稱", "座號", "學號", "姓名", "排名", "各學期成績" });
                merge_values.AddRange(new object[] {
                    SmartSchool.Customization.Data.SystemInformation.SchoolChineseName,
                    each_stu.Department,
                    each_stu.ClassName,
                    each_stu.SeatNo,
                    each_stu.StudentNumber,
                    each_stu.StudentName,
                    option.RatingMethod.ToString(),
                    each_stu
                });
                each_page.MailMerge.MergeField += new Aspose.Words.Reporting.MergeFieldEventHandler(MailMerge_MergeField);
                each_page.MailMerge.Execute(merge_keys.ToArray(), merge_values.ToArray());
                #endregion
                #endregion
                #region 加入新的SnapShop
                SmartSchool.Feature.Student.StudentSnapShop newSnapShop = new SmartSchool.Feature.Student.StudentSnapShop();
                newSnapShop.SnapshopName = "多學期成績單";
                newSnapShop.RefStudentID = stuid;
                newSnapShop.PresentType  = ".Doc";
                MemoryStream stream = new MemoryStream();
                each_page.Save(stream, SaveFormat.Doc);
                newSnapShop.PresentContent = Convert.ToBase64String(stream.ToArray());
                stream.Close();
                snapShops.Add(newSnapShop);
                #endregion
                //每300人次就直接上傳一次snapShops
                #region 每300人次就直接上傳一次
                if (snapShops.Count == 300)
                {
                    mWorker.Run(snapShops);
                    snapShops.Clear();
                }
                #endregion
                //合併成績單至一個檔案
                doc.Sections.Add(doc.ImportNode(each_page.Sections[0], true));
                //回報進度
                _bgworker.ReportProgress((int)(student_counter++ *60.0 / student_total) + 40);
            }
            //將最後一段的SnapShop上傳
            if (snapShops.Count > 0)
            {
                mWorker.Run(snapShops);
                snapShops.Clear();
            }
            #endregion

            e.Result = doc;
        }