// 取的分數比例 (疑似在此 效率不好,但目前暫時無法再優化,因為此做法是目前ischool  git 上的做法, 如果未來要優化,可能就要直接用SQL)
        private string GetScoreRatio(K12.Data.AssessmentSetupRecord asr)
        {
            XmlElement xmlElement = asr.Extension;

            XmlElement elem = xmlElement.SelectSingleNode("ScorePercentage") as XmlElement;

            string score = elem == null ? string.Empty : elem.InnerText;

            return(score);
        }
Example #2
0
        // 填 EXCEL
        private void FillProgressScoreExcelColunm(Workbook wb)
        {
            // 一個班級  開一個 Worksheet
            foreach (K12.Data.ClassRecord classRecord in _classList)
            {
                #region 填寫表頭
                Worksheet ws = wb.Worksheets[wb.Worksheets.Add()];

                // 複製樣板
                ws.Copy(wb.Worksheets["樣板"]);

                ws.Name = classRecord.Name + "_五育獎";

                // 填表頭 (座號 姓名 兩欄 、兩次評量各自的科目數  最右邊最後比較結果 二欄)
                Range headRange = ws.Cells.CreateRange(0, 0, 2, 2 + selectSubjectCount + 2);

                headRange.SetStyle(wb.Worksheets["樣板"].Cells["A1"].GetStyle());

                // 右邊總成績
                Range resultHeadRange = ws.Cells.CreateRange(2, 2 + selectSubjectCount, 2, 2);

                resultHeadRange.Copy(wb.Worksheets["樣板"].Cells.CreateRange("D3", "E4"));

                resultHeadRange.ColumnWidth = 10;

                #endregion

                // 填科目 、填分數類別
                int subjectPlace = 0;

                foreach (string subject in _selsubjectList)
                {
                    // 科目名稱全名,後面帶權數 EX: 國文(3)
                    string subjectFullName = "";

                    // 這個班級的課程
                    subjectFullName = _courseList.Find(c => c.Class.ID == classRecord.ID && c.Subject == subject) != null ? subject + "(" + _courseList.Find(c => c.Class.ID == classRecord.ID && c.Subject == subject).Credit + ")" : subject + "(?)";

                    // 取得該課程的評分樣版 查詢 定期評量、平時評量的比例
                    K12.Data.AssessmentSetupRecord assessmentsetup = _courseList.Find(c => c.Class.ID == classRecord.ID && c.Subject == subject) != null?_courseList.Find(c => c.Class.ID == classRecord.ID && c.Subject == subject).AssessmentSetup : null;

                    //科目
                    Cell cell_subect = ws.Cells[2, 2 + subjectPlace];

                    cell_subect.Copy(wb.Worksheets["樣板"].Cells["C3"]);

                    cell_subect.Value = subjectFullName;

                    // 填 學期成績 字樣
                    ws.Cells[3, 2 + subjectPlace].Value = "學期成績";

                    ws.Cells[3, 2 + subjectPlace].Copy(wb.Worksheets["樣板"].Cells["C4"]);


                    subjectPlace++;
                }


                // 整理的學生清單 沒有 班級, 代表本班級沒有學生 跳過
                if (!_studentDict.ContainsKey(classRecord.ID))
                {
                    continue;
                }

                int progress     = 100 / (_studentDict[classRecord.ID].Count != 0 ? _studentDict[classRecord.ID].Count : 100);
                int studentCount = 0;
                //填學生資料
                foreach (K12.Data.StudentRecord sr in _studentDict[classRecord.ID])
                {
                    // 座號
                    ws.Cells[4 + studentCount, 0].Value = sr.SeatNo;

                    // 姓名
                    ws.Cells[4 + studentCount, 1].Value = sr.Name;


                    // 填表格樣式(黃)
                    Range dataRange = ws.Cells.CreateRange(4 + studentCount, 0, 1, 2 + selectSubjectCount);

                    dataRange.SetStyle(wb.Worksheets["樣板"].Cells["A5"].GetStyle());

                    // 填表格樣式(右邊 比較結果)
                    Range resultRange = ws.Cells.CreateRange(4 + studentCount, 2 + selectSubjectCount, 1, 2);

                    resultRange.Copy(wb.Worksheets["樣板"].Cells.CreateRange("D5", "E5"));


                    // 填 學期科目成績
                    if (_subjectScoreDict.ContainsKey(sr.ID))
                    {
                        foreach (K12.Data.SubjectScore subjectScore in _subjectScoreDict[sr.ID])
                        {
                            // 科目全名 (EX: 國文(3))
                            string subjectFullName = subjectScore.Subject + "(" + subjectScore.Credit + ")";

                            for (int i = 2; i <= selectSubjectCount + 1; i++)
                            {
                                if ("" + ws.Cells[2, i].Value == subjectFullName)
                                {
                                    // 學期科目成績
                                    ws.Cells[4 + studentCount, i].Value = subjectScore.Score;
                                }
                            }
                        }
                    }

                    // 填總成績 (總成績、排名)
                    if (_awardScoreDict.ContainsKey(sr.ID))
                    {
                        foreach (AwardScore awardScore in _awardScoreDict[sr.ID])
                        {
                            // 學期總成績
                            if (awardScore.ScoreType == "" + ws.Cells[3, 2 + selectSubjectCount].Value)
                            {
                                // 學期總成績
                                ws.Cells[4 + studentCount, 2 + selectSubjectCount].Value = awardScore.Score;

                                // 學期總成績 第一位同學 cell Name
                                string firstCellName = ws.Cells[4, 2 + selectSubjectCount].Name;

                                // 學期總成績 最後一位同學 cell Name
                                string lastCellName = ws.Cells[4 + _studentDict[classRecord.ID].Count - 1, 2 + selectSubjectCount].Name;

                                // 學期總成績 目前此為同學 cell Name
                                string nowCellName = ws.Cells[4 + studentCount, 2 + selectSubjectCount].Name;

                                // 排名(用EXCEL 公式算,同分 同名次 不接續排名)
                                ws.Cells[4 + studentCount, 2 + selectSubjectCount + 1].Formula = "=RANK(" + nowCellName + "," + firstCellName + ":" + lastCellName + ")";
                            }
                        }
                    }

                    studentCount++;

                    _worker.ReportProgress(progress * studentCount, "填寫" + classRecord.Name + "五育獎報表");
                }

                ws.FirstVisibleColumn = 0;// 將打開的介面 調到最左, 要不然就會看到 右邊一片空白。
            }
        }
        // 填 EXCEL
        private void FillProgressScoreExcelColunm(Workbook wb)
        {
            // 一個班級  開一個 Worksheet
            foreach (K12.Data.ClassRecord classRecord in _classList)
            {
                #region 填寫表頭
                Worksheet ws = wb.Worksheets[wb.Worksheets.Add()];

                // 複製樣板
                ws.Copy(wb.Worksheets["樣板"]);

                ws.Name = classRecord.Name + "_進步獎";

                // 填表頭 (座號 姓名 兩欄 、兩次評量各自的科目數 乘四欄 最右邊最後比較結果 六欄)
                Range headRange = ws.Cells.CreateRange(0, 0, 2, 2 + selectSubjectCount * 4 + 6);

                headRange.SetStyle(wb.Worksheets["樣板"].Cells["A1"].GetStyle());

                // 填評量名稱

                // 評量1
                Cell cell_exam1 = ws.Cells[2, 2];

                cell_exam1.Copy(wb.Worksheets["樣板"].Cells["C3"]);

                cell_exam1.Value = _examName1;

                // 要被併的那些格 要另外設定 Styele 要不然會出現原始預設的格子(EX: 沒有框線)
                Range examRange1 = ws.Cells.CreateRange(2, 2, 1, selectSubjectCount * 2);

                examRange1.SetStyle(wb.Worksheets["樣板"].Cells["C3"].GetStyle());


                // 每有選一科 就會占兩格(定期評量、平時評量)
                ws.Cells.Merge(2, 2, 1, selectSubjectCount * 2);


                // 評量2
                Cell cell_exam2 = ws.Cells[2, 2 + selectSubjectCount * 2];

                cell_exam2.Copy(wb.Worksheets["樣板"].Cells["E3"]);

                cell_exam2.Value = _examName2;

                // 要被併的那些格 要另外設定 Styele 要不然會出現原始預設的格子(EX: 沒有框線)
                Range examRange2 = ws.Cells.CreateRange(2, 2 + selectSubjectCount * 2, 1, selectSubjectCount * 2);

                examRange2.SetStyle(wb.Worksheets["樣板"].Cells["E3"].GetStyle());


                // 每有選一科 就會占兩格(定期評量、平時評量)
                ws.Cells.Merge(2, 2 + selectSubjectCount * 2, 1, selectSubjectCount * 2);


                // 右邊總成績
                Range resultHeadRange = ws.Cells.CreateRange(2, 2 + selectSubjectCount * 4, 3, 6);

                resultHeadRange.Copy(wb.Worksheets["樣板"].Cells.CreateRange("G3", "L5"));

                // 依照選擇兩次試別 更改顯示文字
                ws.Cells[4, 2 + selectSubjectCount * 4].Value = _examName1 + "總成績";

                ws.Cells[4, 2 + selectSubjectCount * 4 + 2].Value = _examName2 + "總成績";

                #endregion

                // 填科目 、填分數類別
                int subjectPlace = 0;

                foreach (string subject in _selsubjectList)
                {
                    // 科目名稱全名,後面帶權數 EX: 國文(3)
                    string subjectFullName = "";

                    // 這個班級的課程
                    subjectFullName = _courseList.Find(c => c.Class.ID == classRecord.ID && c.Subject == subject) != null ? subject + "(" + _courseList.Find(c => c.Class.ID == classRecord.ID && c.Subject == subject).Credit + ")" : subject + "(?)";

                    // 取得該課程的評分樣版 查詢 定期評量、平時評量的比例
                    K12.Data.AssessmentSetupRecord assessmentsetup = _courseList.Find(c => c.Class.ID == classRecord.ID && c.Subject == subject) != null?_courseList.Find(c => c.Class.ID == classRecord.ID && c.Subject == subject).AssessmentSetup : null;

                    // 定期的比例
                    int scoreRatio;

                    // 平時的比例 (兩者相加 為100)
                    int assignmentScoreRatio;

                    if (assessmentsetup == null)
                    {
                        scoreRatio = 0;

                        assignmentScoreRatio = 0;
                    }
                    else
                    {
                        scoreRatio = int.Parse(GetScoreRatio(assessmentsetup));

                        assignmentScoreRatio = 100 - scoreRatio;
                    }



                    // 評量1 科目
                    Cell cell_exam_subect1 = ws.Cells[3, 2 + subjectPlace];

                    cell_exam_subect1.Copy(wb.Worksheets["樣板"].Cells["C3"]);

                    cell_exam_subect1.Value = subjectFullName;

                    // 要被併的那一格 要另外設定 Styele 要不然會出現原始預設的格子(EX: 沒有框線)
                    ws.Cells[3, 2 + subjectPlace].SetStyle(wb.Worksheets["樣板"].Cells["C3"].GetStyle());

                    // 科目 占兩格
                    ws.Cells.Merge(3, 2 + subjectPlace, 1, 2);

                    // 評量1 分數類別1 (定期評量)
                    Cell cell_exam1_score1 = ws.Cells[4, 2 + subjectPlace];

                    cell_exam1_score1.Copy(wb.Worksheets["樣板"].Cells["C5"]);

                    cell_exam1_score1.Value = cell_exam1_score1.Value + "(" + scoreRatio + "%)";

                    // 評量1 分數類別2 (平時評量)
                    Cell cell_exam1_score2 = ws.Cells[4, 2 + subjectPlace + 1];

                    cell_exam1_score2.Copy(wb.Worksheets["樣板"].Cells["D5"]);

                    cell_exam1_score2.Value = cell_exam1_score2.Value + "(" + assignmentScoreRatio + "%)";


                    // 評量2 科目
                    Cell cell_exam_subect2 = ws.Cells[3, 2 + subjectPlace + selectSubjectCount * 2];

                    cell_exam_subect2.Copy(wb.Worksheets["樣板"].Cells["E3"]);

                    cell_exam_subect2.Value = subjectFullName;

                    // 要被併的那一格 要另外設定 Styele 要不然會出現原始預設的格子(EX: 沒有框線)
                    ws.Cells[3, 2 + subjectPlace + selectSubjectCount * 2 + 1].SetStyle(wb.Worksheets["樣板"].Cells["E3"].GetStyle());

                    // 科目 占兩格
                    ws.Cells.Merge(3, 2 + subjectPlace + selectSubjectCount * 2, 1, 2);

                    // 評量2 分數類別1 (定期評量)
                    Cell cell_exam2_score1 = ws.Cells[4, 2 + subjectPlace + selectSubjectCount * 2];

                    cell_exam2_score1.Copy(wb.Worksheets["樣板"].Cells["E5"]);

                    cell_exam2_score1.Value = cell_exam2_score1.Value + "(" + scoreRatio + "%)";

                    // 評量2 分數類別2 (平時評量)
                    Cell cell_exam2_score2 = ws.Cells[4, 2 + subjectPlace + selectSubjectCount * 2 + 1];

                    cell_exam2_score2.Copy(wb.Worksheets["樣板"].Cells["F5"]);

                    cell_exam2_score2.Value = cell_exam2_score2.Value + "(" + assignmentScoreRatio + "%)";

                    subjectPlace = subjectPlace + 2;
                }


                // 整理的學生清單 沒有 班級, 代表本班級沒有學生 跳過
                if (!_studentDict.ContainsKey(classRecord.ID))
                {
                    continue;
                }

                int progress     = 100 / (_studentDict[classRecord.ID].Count != 0 ? _studentDict[classRecord.ID].Count : 100);
                int studentCount = 0;
                //填學生資料
                foreach (K12.Data.StudentRecord sr in _studentDict[classRecord.ID])
                {
                    // 座號
                    ws.Cells[5 + studentCount, 0].Value = sr.SeatNo;

                    // 姓名
                    ws.Cells[5 + studentCount, 1].Value = sr.Name;


                    // 填表格樣式(黃)
                    Range dataRange = ws.Cells.CreateRange(5 + studentCount, 0, 1, 2 + selectSubjectCount * 4);

                    dataRange.SetStyle(wb.Worksheets["樣板"].Cells["A6"].GetStyle());

                    // 填表格樣式(右邊 比較結果)
                    Range resultRange = ws.Cells.CreateRange(5 + studentCount, 2 + selectSubjectCount * 4, 1, 6);

                    resultRange.Copy(wb.Worksheets["樣板"].Cells.CreateRange("G6", "L6"));

                    // 填 評量成績
                    if (_sceDict.ContainsKey(sr.ID))
                    {
                        foreach (K12.Data.SCETakeRecord scetRecord in _sceDict[sr.ID])
                        {
                            // 此 成績 屬於 評量成績1
                            if (scetRecord.Exam.Name == _examName1)
                            {
                                // 科目全名 (EX: 國文(3))
                                string subjectFullName = scetRecord.Course.Subject + "(" + scetRecord.Course.Credit + ")";


                                for (int i = 2; i <= selectSubjectCount * 2; i = i + 2)
                                {
                                    if ("" + ws.Cells[3, i].Value == subjectFullName)
                                    {
                                        // 定期評量
                                        ws.Cells[5 + studentCount, i].Value = GetScore(scetRecord);
                                        // 平時評量
                                        ws.Cells[5 + studentCount, i + 1].Value = GetAssignmentScore(scetRecord);
                                    }
                                }
                            }
                            // 此 成績 屬於 評量成績2
                            else if (scetRecord.Exam.Name == _examName2)
                            {
                                // 科目全名 (EX: 國文(3))
                                string subjectFullName = scetRecord.Course.Subject + "(" + scetRecord.Course.Credit + ")";

                                for (int i = 2; i <= selectSubjectCount * 2; i = i + 2)
                                {
                                    if ("" + ws.Cells[3, i + selectSubjectCount * 2].Value == subjectFullName)
                                    {
                                        // 定期評量
                                        ws.Cells[5 + studentCount, i + selectSubjectCount * 2].Value = GetScore(scetRecord);
                                        // 平時評量
                                        ws.Cells[5 + studentCount, i + selectSubjectCount * 2 + 1].Value = GetAssignmentScore(scetRecord);
                                    }
                                }
                            }
                        }
                    }

                    // 填總成績 (總成績、排名)
                    if (_awardScoreDict.ContainsKey(sr.ID))
                    {
                        foreach (AwardScore awardScore in _awardScoreDict[sr.ID])
                        {
                            //ws.Cells[4, 2 + selectSubjectCount * 4].Value = _examName1 + "總成績";

                            //ws.Cells[4, 2 + selectSubjectCount * 4 + 2].Value = _examName2 + "總成績";

                            // 第一次的總成績
                            if (awardScore.ScoreType == "" + ws.Cells[4, 2 + selectSubjectCount * 4].Value)
                            {
                                // 總成績
                                ws.Cells[5 + studentCount, 2 + selectSubjectCount * 4].Value = awardScore.Score;

                                // 第一次總成績 第一位同學 cell Name
                                string firstCellName = ws.Cells[5, 2 + selectSubjectCount * 4].Name;

                                // 第一次總成績 最後一位同學 cell Name
                                string lastCellName = ws.Cells[5 + _studentDict[classRecord.ID].Count - 1, 2 + selectSubjectCount * 4].Name;

                                // 第一次總成績 目前此為同學 cell Name
                                string nowCellName = ws.Cells[5 + studentCount, 2 + selectSubjectCount * 4].Name;

                                // 排名(用EXCEL 公式算,同分 同名次 不接續排名)
                                ws.Cells[5 + studentCount, 2 + selectSubjectCount * 4 + 1].Formula = "=RANK(" + nowCellName + "," + firstCellName + ":" + lastCellName + ")";
                            }

                            // 第二次的總成績
                            if (awardScore.ScoreType == "" + ws.Cells[4, 2 + selectSubjectCount * 4 + 2].Value)
                            {
                                // 總成績
                                ws.Cells[5 + studentCount, 2 + selectSubjectCount * 4 + 2].Value = awardScore.Score;


                                // 第二次總成績 第一位同學 cell Name
                                string firstCellName = ws.Cells[5, 2 + selectSubjectCount * 4 + 2].Name;

                                // 第二次總成績 最後一位同學 cell Name
                                string lastCellName = ws.Cells[5 + _studentDict[classRecord.ID].Count - 1, 2 + selectSubjectCount * 4 + 2].Name;

                                // 第二次總成績 目前此為同學 cell Name
                                string nowCellName = ws.Cells[5 + studentCount, 2 + selectSubjectCount * 4 + 2].Name;

                                // 排名
                                ws.Cells[5 + studentCount, 2 + selectSubjectCount * 4 + +2 + 1].Formula = "=RANK(" + nowCellName + "," + firstCellName + ":" + lastCellName + ")";
                            }


                            // 兩次成績差
                            if (awardScore.ScoreType == "進步分數")
                            {
                                // 進步分數
                                ws.Cells[5 + studentCount, 2 + selectSubjectCount * 4 + 4].Value = awardScore.Score;


                                // 進步分數 第一位同學 cell Name
                                string firstCellName = ws.Cells[5, 2 + selectSubjectCount * 4 + 4].Name;

                                // 進步分數 最後一位同學 cell Name
                                string lastCellName = ws.Cells[5 + _studentDict[classRecord.ID].Count - 1, 2 + selectSubjectCount * 4 + 4].Name;

                                // 進步分數 目前此為同學 cell Name
                                string nowCellName = ws.Cells[5 + studentCount, 2 + selectSubjectCount * 4 + 4].Name;

                                // 排名
                                ws.Cells[5 + studentCount, 2 + selectSubjectCount * 4 + +4 + 1].Formula = "=RANK(" + nowCellName + "," + firstCellName + ":" + lastCellName + ")";
                            }
                        }
                    }



                    studentCount++;

                    _worker.ReportProgress(progress * studentCount, "填寫" + classRecord.Name + "進步獎報表");
                }

                ws.FirstVisibleColumn = 0;// 將打開的介面 調到最左, 要不然就會看到 右邊一片空白。
            }
        }
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            _worker.ReportProgress(0, "開始列印 進步獎報表...");

            _worker.ReportProgress(20, "取得本學期開設的班級課程...");

            // 本學期 班級開設的 課程清單
            _courseList = K12.Data.Course.SelectByClass(int.Parse(_school_year), int.Parse(_semester), _classList);

            // 課程ID List
            List <string> courseIDList = new List <string>();

            foreach (K12.Data.CourseRecord cr in _courseList)
            {
                courseIDList.Add(cr.ID);

                AssessmentSetupDict.Add(cr.ID, cr.AssessmentSetup);
            }



            _worker.ReportProgress(40, "取得本學期評量成績...");

            // 取得學生清單(依班級)
            _studentList = K12.Data.Student.SelectByClasses(_classList);

            // 學生ID List
            List <string> stuIDList = new List <string>();

            //整理學生清單(依照班級ID 分類)
            foreach (K12.Data.StudentRecord sr in _studentList)
            {
                // 若非一般生 不列入排名學生清單
                if (sr.Status != K12.Data.StudentRecord.StudentStatus.一般)
                {
                    continue;
                }

                if (!_studentDict.ContainsKey(sr.Class.ID))
                {
                    _studentDict.Add(sr.Class.ID, new List <K12.Data.StudentRecord>());

                    _studentDict[sr.Class.ID].Add(sr);
                }
                else
                {
                    _studentDict[sr.Class.ID].Add(sr);
                }

                stuIDList.Add(sr.ID);
            }

            // 排序(依照座號)

            foreach (string classID in _studentDict.Keys)
            {
                _studentDict[classID].Sort((x, y) => { return(x.SeatNo.HasValue ? x.SeatNo.Value.CompareTo(y.SeatNo) : -1); });
            }

            // 取得評量成績
            _sceList = K12.Data.SCETake.SelectByStudentAndCourse(stuIDList, courseIDList);

            // 整理評量成績
            foreach (K12.Data.SCETakeRecord scetRecord in _sceList)
            {
                // 只有在本次選擇的科目才納入
                if (!_selsubjectList.Contains(scetRecord.Course.Subject))
                {
                    continue;
                }

                if (!_sceDict.ContainsKey(scetRecord.RefStudentID))
                {
                    _sceDict.Add(scetRecord.RefStudentID, new List <K12.Data.SCETakeRecord>());

                    _sceDict[scetRecord.RefStudentID].Add(scetRecord);
                }
                else
                {
                    _sceDict[scetRecord.RefStudentID].Add(scetRecord);
                }
            }


            _worker.ReportProgress(60, "成績排序中...");


            int progress     = 100 / (_sceDict.Keys.Count != 0 ? _sceDict.Keys.Count:100);
            int studentCount = 0;

            foreach (string stuID in _sceDict.Keys)
            {
                if (!_awardScoreDict.ContainsKey(stuID))
                {
                    _awardScoreDict.Add(stuID, new List <AwardScore>());

                    // 2019/06/12 穎驊筆記, 這邊的程式碼還可以再優化, 待目前城版本測試後 ,再調整
                    foreach (K12.Data.SCETakeRecord scetRecord in _sceDict[stuID])
                    {
                        // 第一次評量成績
                        if (scetRecord.Exam.Name == _examName1)
                        {
                            AwardScore awardScore;

                            awardScore = _awardScoreDict[stuID].Find(awardscore => awardscore.ScoreType == _examName1 + "總成績");

                            // 舊的沒有的話, 第一筆 新增
                            if (awardScore == null)
                            {
                                awardScore = new AwardScore();

                                decimal?credit = scetRecord.Course.Credit;

                                // 效率太慢,另外整理
                                //K12.Data.AssessmentSetupRecord assessmentsetup = scetRecord.Course.AssessmentSetup;

                                K12.Data.AssessmentSetupRecord assessmentsetup = AssessmentSetupDict.ContainsKey(scetRecord.RefCourseID)? AssessmentSetupDict[scetRecord.RefCourseID] :null;

                                if (assessmentsetup == null)
                                {
                                    continue;
                                }

                                // 定期的比例
                                int scoreRatio = int.Parse(GetScoreRatio(assessmentsetup));

                                // 平時的比例 (兩者相加 為100)
                                int assignmentScoreRatio = 100 - scoreRatio;

                                awardScore.ScoreType = _examName1 + "總成績";

                                if (awardScore.Score.HasValue)
                                {
                                    decimal?score;
                                    decimal?assignmentScore;

                                    if (isOriScore)
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * (decimal?)scoreRatio : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * (decimal?)assignmentScoreRatio : null;
                                    }
                                    else
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * scoreRatio * credit : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * assignmentScoreRatio * credit : null;
                                    }

                                    decimal?totalScore = new decimal();

                                    if (score != null)
                                    {
                                        totalScore += score;
                                    }
                                    if (assignmentScore != null)
                                    {
                                        totalScore += assignmentScore;
                                    }

                                    awardScore.Score += totalScore;
                                }
                                else
                                {
                                    decimal?score;
                                    decimal?assignmentScore;

                                    if (isOriScore)
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * (decimal?)scoreRatio : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * (decimal?)assignmentScoreRatio : null;
                                    }
                                    else
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * scoreRatio * credit : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * assignmentScoreRatio * credit : null;
                                    }

                                    decimal?totalScore = new decimal();

                                    if (score != null)
                                    {
                                        totalScore += score;
                                    }
                                    if (assignmentScore != null)
                                    {
                                        totalScore += assignmentScore;
                                    }

                                    awardScore.Score = totalScore;
                                }

                                _awardScoreDict[stuID].Add(awardScore);
                            }
                            // 已經有成績的話 繼續 加總
                            else
                            {
                                decimal?credit = scetRecord.Course.Credit;

                                // 效率太慢,另外整理
                                //K12.Data.AssessmentSetupRecord assessmentsetup = scetRecord.Course.AssessmentSetup;

                                K12.Data.AssessmentSetupRecord assessmentsetup = AssessmentSetupDict.ContainsKey(scetRecord.RefCourseID) ? AssessmentSetupDict[scetRecord.RefCourseID] : null;

                                if (assessmentsetup == null)
                                {
                                    continue;
                                }

                                // 定期的比例
                                int scoreRatio = int.Parse(GetScoreRatio(assessmentsetup));

                                // 平時的比例 (兩者相加 為100)
                                int assignmentScoreRatio = 100 - scoreRatio;

                                if (awardScore.Score.HasValue)
                                {
                                    decimal?score;
                                    decimal?assignmentScore;

                                    if (isOriScore)
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * (decimal?)scoreRatio : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * (decimal?)assignmentScoreRatio : null;
                                    }
                                    else
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * scoreRatio * credit : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * assignmentScoreRatio * credit : null;
                                    }

                                    decimal?totalScore = new decimal();

                                    if (score != null)
                                    {
                                        totalScore += score;
                                    }
                                    if (assignmentScore != null)
                                    {
                                        totalScore += assignmentScore;
                                    }

                                    awardScore.Score += totalScore;
                                }
                                else
                                {
                                    decimal?score;
                                    decimal?assignmentScore;

                                    if (isOriScore)
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * (decimal?)scoreRatio : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * (decimal?)assignmentScoreRatio : null;
                                    }
                                    else
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * scoreRatio * credit : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * assignmentScoreRatio * credit : null;
                                    }

                                    decimal?totalScore = new decimal();

                                    if (score != null)
                                    {
                                        totalScore += score;
                                    }
                                    if (assignmentScore != null)
                                    {
                                        totalScore += assignmentScore;
                                    }

                                    awardScore.Score = totalScore;
                                }
                            }
                        }

                        // 第二次評量成績
                        if (scetRecord.Exam.Name == _examName2)
                        {
                            AwardScore awardScore;

                            awardScore = _awardScoreDict[stuID].Find(awardscore => awardscore.ScoreType == _examName2 + "總成績");

                            // 舊的沒有的話, 第一筆 新增
                            if (awardScore == null)
                            {
                                awardScore = new AwardScore();

                                decimal?credit = scetRecord.Course.Credit;

                                // 效率太慢,另外整理
                                //K12.Data.AssessmentSetupRecord assessmentsetup = scetRecord.Course.AssessmentSetup;

                                K12.Data.AssessmentSetupRecord assessmentsetup = AssessmentSetupDict.ContainsKey(scetRecord.RefCourseID) ? AssessmentSetupDict[scetRecord.RefCourseID] : null;

                                if (assessmentsetup == null)
                                {
                                    continue;
                                }

                                // 定期的比例
                                int scoreRatio = int.Parse(GetScoreRatio(assessmentsetup));

                                // 平時的比例 (兩者相加 為100)
                                int assignmentScoreRatio = 100 - scoreRatio;

                                awardScore.ScoreType = _examName2 + "總成績";

                                if (awardScore.Score.HasValue)
                                {
                                    decimal?score;
                                    decimal?assignmentScore;

                                    if (isOriScore)
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * (decimal?)scoreRatio : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * (decimal?)assignmentScoreRatio : null;
                                    }
                                    else
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * scoreRatio * credit : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * assignmentScoreRatio * credit : null;
                                    }

                                    decimal?totalScore = new decimal();

                                    if (score != null)
                                    {
                                        totalScore += score;
                                    }
                                    if (assignmentScore != null)
                                    {
                                        totalScore += assignmentScore;
                                    }

                                    awardScore.Score += totalScore;
                                }
                                else
                                {
                                    decimal?score;
                                    decimal?assignmentScore;

                                    if (isOriScore)
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * (decimal?)scoreRatio : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * (decimal?)assignmentScoreRatio : null;
                                    }
                                    else
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * scoreRatio * credit : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * assignmentScoreRatio * credit : null;
                                    }

                                    decimal?totalScore = new decimal();

                                    if (score != null)
                                    {
                                        totalScore += score;
                                    }
                                    if (assignmentScore != null)
                                    {
                                        totalScore += assignmentScore;
                                    }

                                    awardScore.Score = totalScore;
                                }

                                _awardScoreDict[stuID].Add(awardScore);
                            }
                            // 已經有成績的話 繼續 加總
                            else
                            {
                                decimal?credit = scetRecord.Course.Credit;

                                // 效率太慢,另外整理
                                //K12.Data.AssessmentSetupRecord assessmentsetup = scetRecord.Course.AssessmentSetup;

                                K12.Data.AssessmentSetupRecord assessmentsetup = AssessmentSetupDict.ContainsKey(scetRecord.RefCourseID) ? AssessmentSetupDict[scetRecord.RefCourseID] : null;

                                if (assessmentsetup == null)
                                {
                                    continue;
                                }

                                // 定期的比例
                                int scoreRatio = int.Parse(GetScoreRatio(assessmentsetup));

                                // 平時的比例 (兩者相加 為100)
                                int assignmentScoreRatio = 100 - scoreRatio;

                                if (awardScore.Score.HasValue)
                                {
                                    decimal?score;
                                    decimal?assignmentScore;

                                    if (isOriScore)
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * (decimal?)scoreRatio : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * (decimal?)assignmentScoreRatio : null;
                                    }
                                    else
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * scoreRatio * credit : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * assignmentScoreRatio * credit : null;
                                    }

                                    decimal?totalScore = new decimal();

                                    if (score != null)
                                    {
                                        totalScore += score;
                                    }
                                    if (assignmentScore != null)
                                    {
                                        totalScore += assignmentScore;
                                    }

                                    awardScore.Score += totalScore;
                                }
                                else
                                {
                                    decimal?score;
                                    decimal?assignmentScore;

                                    if (isOriScore)
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * (decimal?)scoreRatio : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * (decimal?)assignmentScoreRatio : null;
                                    }
                                    else
                                    {
                                        score = GetScore(scetRecord) != "" ? decimal.Parse(GetScore(scetRecord)) * scoreRatio * credit : null;

                                        assignmentScore = GetAssignmentScore(scetRecord) != "" ? decimal.Parse(GetAssignmentScore(scetRecord)) * assignmentScoreRatio * credit : null;
                                    }

                                    decimal?totalScore = new decimal();

                                    if (score != null)
                                    {
                                        totalScore += score;
                                    }
                                    if (assignmentScore != null)
                                    {
                                        totalScore += assignmentScore;
                                    }

                                    awardScore.Score = totalScore;
                                }
                            }
                        }
                    }
                }

                studentCount++;

                _worker.ReportProgress(progress * studentCount, "計算總成績中...");
            }


            // 上面 加權計算完 第一次 、 第二次考試成績後 (除回定期平時的比例100) , 這邊來算 計算進步分數
            foreach (string studentID in _awardScoreDict.Keys)
            {
                decimal?score1 = new decimal();

                decimal?score2 = new decimal();;

                foreach (AwardScore awardScore in _awardScoreDict[studentID])
                {
                    if (awardScore.ScoreType == _examName1 + "總成績")
                    {
                        awardScore.Score = awardScore.Score / 100;

                        score1 = awardScore.Score;
                    }

                    if (awardScore.ScoreType == _examName2 + "總成績")
                    {
                        awardScore.Score = awardScore.Score / 100;

                        score2 = awardScore.Score;
                    }
                }

                // 假如兩次評量都有分數 進步分數為 第二次的總分 減掉 第一次的總分
                if (score1.HasValue && score2.HasValue)
                {
                    AwardScore awardScore = new AwardScore();

                    awardScore.ScoreType = "進步分數";

                    awardScore.Score = score2 - score1;

                    _awardScoreDict[studentID].Add(awardScore);
                }
            }



            _worker.ReportProgress(70, "填寫報表...");

            // 取得 系統預設的樣板
            Workbook wb = new Workbook(new MemoryStream(Properties.Resources.小進步獎樣板));

            #region 填表

            FillProgressScoreExcelColunm(wb);

            #endregion

            //// 把當作樣板的 第一張 移掉
            wb.Worksheets.RemoveAt("樣板");

            wb.Worksheets.RemoveAt("理想產生結果");

            e.Result = wb;

            _worker.ReportProgress(100, "進步獎報表列印完成。");
        }
        internal void Add(HC.JHSCETakeRecord sce)
        {
            if (!_courseDict.ContainsKey(sce.RefCourseID))
            {
                return;                                            //如果評量成績的課程不存在,return。
            }
            JHCourseRecord course = _courseDict[sce.RefCourseID];

            if (!Domains.ContainsKey(course.Domain))
            {
                Domains.Add(course.Domain, new DomainRow(course.Domain));
            }

            DomainRow row = Domains[course.Domain];

            if (_config.DomainSubjectSetup == DomainSubjectSetup.Subject)
            {
                row.Display = false;
            }
            else
            {
                row.Display = true;
            }
            row.AddExamID(sce.RefExamID);

            if (!row.Subjects.ContainsKey(course.Subject))
            {
                row.Subjects.Add(course.Subject, new SubjectRow(course.Subject));
            }
            SubjectRow subjectRow = row.Subjects[course.Subject];

            subjectRow.SetPeriodCredit(course.Period, course.Credit);
            subjectRow.AddScore(sce.RefExamID, sce.Score, sce.AssignmentScore);
            //Report._NoneExamT
            if (course.AssessmentSetup == null)
            {
                Report._NoneExamT = true;
            }
            K12.Data.AssessmentSetupRecord assessmentSetupRecord = course.AssessmentSetup;


            XmlElement extension = assessmentSetupRecord.Extension;


            int examPercentage = 0;

            if (!string.IsNullOrEmpty(extension["ScorePercentage"].InnerText))
            {
                examPercentage = Convert.ToInt32(extension["ScorePercentage"].InnerText.Trim(' ', '\n'));
            }
            int assignmentPercentage = 100 - examPercentage;

            subjectRow.examPercentage       = examPercentage;
            subjectRow.assignmentPercentage = assignmentPercentage;


            //try
            //{
            //    K12.Data.AssessmentSetupRecord assessmentSetupRecord = course.AssessmentSetup;

            //    XmlElement extension = assessmentSetupRecord.Extension;
            //    int examPercentage = 0;
            //    if (!string.IsNullOrEmpty(extension["ScorePercentage"].InnerText))
            //    {
            //        examPercentage = Convert.ToInt32(extension["ScorePercentage"].InnerText.Trim(' ', '\n'));
            //    }
            //    int assignmentPercentage = 100 - examPercentage;

            //    subjectRow.examPercentage = examPercentage;
            //    subjectRow.assignmentPercentage = assignmentPercentage;

            //}
            //catch (Exception ex)
            //{
            //    if (course.AssessmentSetup == null)
            //    {
            //        MsgBox.Show ( _courseDict[sce.RefCourseID].Name + "未設定評分樣板");
            //    } else
            //    {
            //        MsgBox.Show(ex.Message);
            //    }
            //}

            //if (_config.DomainSubjectSetup == DomainSubjectSetup.Subject)
            //    subjectRow.Display = true;
        }