private static DomainScore GetJHDomainScore(string strDomain, SemesterDomainScore scDomain)
        {
            DomainScore domain = scDomain.RawScore;

            if (domain == null)
            {
                domain        = new DomainScore();
                domain.Domain = strDomain;
            }

            return(domain);
        }
        public void Calculate()
        {
            foreach (StudentScore student in Students)
            {
                //成績計算規則。
                ScoreCalculator calcRule = student.CalculationRule;

                //沒有成績計算規則的不計算。
                if (calcRule == null)
                {
                    continue;
                }

                //領域總分。
                Dictionary <string, decimal> domainTotal = new Dictionary <string, decimal>();
                //學期數計數。
                Dictionary <string, decimal> semsCount = new Dictionary <string, decimal>();
                //計算六學期的總分。
                foreach (SemesterData sems in student.SHistory.GetGradeYearSemester())
                {
                    SemesterData sd = new SemesterData(0, sems.SchoolYear, sems.Semester);

                    //學生不包含該學期的成績,直接跳過。
                    if (!student.SemestersScore.Contains(sd))
                    {
                        continue;
                    }

                    //將該學期的成績資料取出。
                    SCSemsScore scscore = student.SemestersScore[sd];

                    //加總各領域成績。
                    foreach (string strDomain in scscore.Domain)
                    {
                        SCSemsDomainScore objDomain = scscore.Domain[strDomain];

                        if (!objDomain.Value.HasValue)
                        {
                            continue;                            //沒有成績不計算。
                        }
                        SumDomainScore(domainTotal, semsCount, strDomain, objDomain.Value.Value);
                    }
                    //學習領域。
                    if (scscore.LearnDomainScore.HasValue)
                    {
                        SumDomainScore(domainTotal, semsCount, LearnDomain, scscore.LearnDomainScore.Value);
                    }
                    //課程學習。
                    if (scscore.CourseLearnScore.HasValue)
                    {
                        SumDomainScore(domainTotal, semsCount, CourseLearning, scscore.CourseLearnScore.Value);
                    }
                }

                //計算總分的算數平均。
                //2017/5/9 穎驊修正 ,因應 高雄 [08-05][03] 畢業資格判斷成績及格標準調整 項目,
                // 領域 分數超過60分 ,以 四捨五入取到小數第二位 , 低於60分 採用 無條件進位至整數 (EX : 59.01 =60)
                // (只有高雄版有如此機制,新竹版照舊不管分數高低都是四捨五入)
                foreach (string strDomain in domainTotal.Keys)
                {
                    //學期數是「0」的不計算。
                    if (semsCount[strDomain] <= 0)
                    {
                        continue;
                    }

                    decimal score = 0;

                    if (JHEvaluation.ScoreCalculation.Program.Mode == JHEvaluation.ScoreCalculation.ModuleMode.HsinChu)
                    {
                        score = calcRule.ParseGraduateScore(domainTotal[strDomain] / semsCount[strDomain]);
                    }

                    if (JHEvaluation.ScoreCalculation.Program.Mode == JHEvaluation.ScoreCalculation.ModuleMode.KaoHsiung)
                    {
                        if ((domainTotal[strDomain] / semsCount[strDomain]) >= 60)
                        {
                            score = calcRule.ParseGraduateScore(domainTotal[strDomain] / semsCount[strDomain]);
                        }
                        else
                        {
                            score = Math.Ceiling(domainTotal[strDomain] / semsCount[strDomain]);
                        }
                    }



                    if (strDomain == LearnDomain)
                    {
                        student.GraduateScore.LearnDomainScore = score;
                    }
                    else if (strDomain == CourseLearning)
                    {
                        student.GraduateScore.CourseLearnScore = score;
                    }
                    else
                    {
                        if (!student.GraduateScore.Contains(strDomain))
                        {
                            student.GraduateScore.Add(strDomain, new GraduateScore());
                        }

                        student.GraduateScore[strDomain].Value = score;
                    }
                }
            }
        }
        public void Calculate()
        {
            foreach (StudentScore student in Students)
            {
                //成績計算規則。
                ScoreCalculator calcRule = student.CalculationRule;

                //沒有成績計算規則的不計算。
                if (calcRule == null)
                {
                    continue;
                }

                //領域總分。
                Dictionary <string, decimal> domainTotal = new Dictionary <string, decimal>();
                //學期數計數。
                Dictionary <string, decimal> semsCount = new Dictionary <string, decimal>();
                //計算六學期的總分。
                foreach (SemesterData sems in student.SHistory.GetGradeYearSemester())
                {
                    SemesterData sd = new SemesterData(0, sems.SchoolYear, sems.Semester);

                    //學生不包含該學期的成績,直接跳過。
                    if (!student.SemestersScore.Contains(sd))
                    {
                        continue;
                    }

                    //將該學期的成績資料取出。
                    SCSemsScore scscore = student.SemestersScore[sd];

                    //加總各領域成績。
                    foreach (string strDomain in scscore.Domain)
                    {
                        SCSemsDomainScore objDomain = scscore.Domain[strDomain];

                        if (!objDomain.Value.HasValue)
                        {
                            continue;                            //沒有成績不計算。
                        }
                        SumDomainScore(domainTotal, semsCount, strDomain, objDomain.Value.Value);
                    }
                    //學習領域。
                    if (scscore.LearnDomainScore.HasValue)
                    {
                        SumDomainScore(domainTotal, semsCount, LearnDomain, scscore.LearnDomainScore.Value);
                    }
                    //課程學習。
                    if (scscore.CourseLearnScore.HasValue)
                    {
                        SumDomainScore(domainTotal, semsCount, CourseLearning, scscore.CourseLearnScore.Value);
                    }
                }

                //計算總分的算數平均。
                foreach (string strDomain in domainTotal.Keys)
                {
                    //學期數是「0」的不計算。
                    if (semsCount[strDomain] <= 0)
                    {
                        continue;
                    }

                    decimal score = calcRule.ParseGraduateScore(domainTotal[strDomain] / semsCount[strDomain]);

                    if (strDomain == LearnDomain)
                    {
                        student.GraduateScore.LearnDomainScore = score;
                    }
                    else if (strDomain == CourseLearning)
                    {
                        student.GraduateScore.CourseLearnScore = score;
                    }
                    else
                    {
                        if (!student.GraduateScore.Contains(strDomain))
                        {
                            student.GraduateScore.Add(strDomain, new GraduateScore());
                        }

                        student.GraduateScore[strDomain].Value = score;
                    }
                }
            }
        }
        public void Save()
        {
            List <JHSemesterScoreRecord> addSemsScore      = new List <JHSemesterScoreRecord>();
            List <JHSemesterScoreRecord> updateSemsScore   = new List <JHSemesterScoreRecord>();
            SubjectScoreLogFormater      subjLogFormater   = new SubjectScoreLogFormater();
            DomainScoreLogFormater       domainLogFormater = new DomainScoreLogFormater();

            foreach (StudentScore student in Students)
            {
                #region 決定要新增還是更新。
                JHSemesterScoreRecord JHScore = GetJHSemesterScore(student.Id, student.SemestersScore[SemesterData.Empty]);
                SCSemsScore           SCScore = student.SemestersScore[SemesterData.Empty];

                if (string.IsNullOrEmpty(JHScore.ID))
                {
                    addSemsScore.Add(JHScore);
                }
                else
                {
                    updateSemsScore.Add(JHScore);
                }
                #endregion

                #region 產生科目資料。
                JHScore.Subjects.Clear();
                foreach (string strSubject in SCScore.Subject)
                {
                    SemesterSubjectScore objSCSubject = SCScore.Subject[strSubject];
                    SubjectScore         objJHSubject = GetJHSubjectScore(strSubject, objSCSubject);
                    LogData subjLog = new LogData(strSubject);
                    subjLog.Formater = subjLogFormater;

                    decimal?score = objSCSubject.Value.HasValue ? (decimal?)(double)objSCSubject.Value : null;

                    //記錄 Log
                    subjLog.Add(new LogData("成績", objJHSubject.Score + "", score.ToString()));
                    subjLog.Add(new LogData("權重", objJHSubject.Credit + "", objSCSubject.Weight + ""));
                    subjLog.Add(new LogData("節數", objJHSubject.Period + "", objSCSubject.Period + ""));
                    if (Program.Mode == ModuleMode.KaoHsiung)
                    {
                        subjLog.Add(new LogData("努力程度", objJHSubject.Effort + "", objSCSubject.Effort + ""));
                    }
                    subjLog.Add(new LogData("文字評量", objJHSubject.Text + "", objSCSubject.Text));
                    subjLog.Add(new LogData("領域", objJHSubject.Domain + "", objSCSubject.Domain));
                    SCScore.Subject.Log.Add(subjLog);

                    objJHSubject.Score  = score;
                    objJHSubject.Credit = objSCSubject.Weight;
                    objJHSubject.Period = objSCSubject.Period;
                    objJHSubject.Effort = objSCSubject.Effort;
                    objJHSubject.Text   = objSCSubject.Text;
                    objJHSubject.Domain = objSCSubject.Domain;

                    JHScore.Subjects.Add(strSubject, objJHSubject);
                }

                //排序科目名稱。
                Dictionary <string, SubjectScore> orderSubject = new Dictionary <string, SubjectScore>(JHScore.Subjects);
                JHScore.Subjects.Clear();
                foreach (string subjName in Util.SortSubjectDomain(orderSubject.Keys))
                {
                    JHScore.Subjects.Add(subjName, orderSubject[subjName]);
                }
                #endregion

                #region 產生領域資料。
                JHScore.Domains.Clear();
                foreach (string strDomain in SCScore.Domain)
                {
                    //彈性課程不記錄領域領域。
                    if (Util.IsVariableDomain(strDomain))
                    {
                        continue;
                    }

                    SemesterDomainScore objSCDomain = SCScore.Domain[strDomain];
                    DomainScore         objJHDomain = GetJHDomainScore(strDomain, objSCDomain);
                    LogData             domainLog   = new LogData(strDomain);
                    domainLog.Formater = subjLogFormater;

                    decimal?score = objSCDomain.Value.HasValue ? (decimal?)(double)objSCDomain.Value : null;

                    //記錄 Log
                    domainLog.Add(new LogData("成績", objJHDomain.Score + "", score + ""));
                    domainLog.Add(new LogData("權重", objJHDomain.Credit + "", objSCDomain.Weight + ""));
                    domainLog.Add(new LogData("節數", objJHDomain.Period + "", objSCDomain.Period + ""));
                    if (Program.Mode == ModuleMode.KaoHsiung)
                    {
                        domainLog.Add(new LogData("努力程度", objJHDomain.Effort + "", objSCDomain.Effort + ""));
                    }
                    domainLog.Add(new LogData("文字評量", objJHDomain.Text + "", objSCDomain.Text));
                    SCScore.Domain.Log.Add(domainLog);

                    objJHDomain.Score  = score;
                    objJHDomain.Credit = objSCDomain.Weight;
                    objJHDomain.Period = objSCDomain.Period;
                    objJHDomain.Effort = objSCDomain.Effort;
                    objJHDomain.Text   = objSCDomain.Text;

                    JHScore.Domains.Add(strDomain, objJHDomain);
                }

                //記錄 Log
                SCScore.LearningLog.Formater    = domainLogFormater;
                SCScore.LearningLog.OriginValue = JHScore.CourseLearnScore + "";
                SCScore.LearningLog.NewValue    = SCScore.LearnDomainScore + "";
                SCScore.CourseLog.Formater      = domainLogFormater;
                SCScore.CourseLog.OriginValue   = JHScore.CourseLearnScore + "";
                SCScore.CourseLog.NewValue      = SCScore.CourseLearnScore + "";

                JHScore.LearnDomainScore = SCScore.LearnDomainScore;
                JHScore.CourseLearnScore = SCScore.CourseLearnScore;

                //排序領域名稱。
                Dictionary <string, DomainScore> orderDomain = new Dictionary <string, DomainScore>(JHScore.Domains);
                JHScore.Domains.Clear();
                foreach (string domainName in Util.SortSubjectDomain(orderDomain.Keys))
                {
                    JHScore.Domains.Add(domainName, orderDomain[domainName]);
                }
                #endregion
            }

            #region 新增科目成績
            FunctionSpliter <JHSemesterScoreRecord, JHSemesterScoreRecord> addSpliter =
                new FunctionSpliter <JHSemesterScoreRecord, JHSemesterScoreRecord>(500, 5);
            addSpliter.Function = delegate(List <JHSemesterScoreRecord> part)
            {
                // 加入檢查當科目與領域成績筆數0不新增
                List <JHSemesterScoreRecord> insertPart = new List <JHSemesterScoreRecord> ();

                foreach (JHSemesterScoreRecord rec in part)
                {
                    // 沒有任何領域或科目成績
                    if (rec.Domains.Count == 0 && rec.Subjects.Count == 0)
                    {
                        continue;
                    }

                    insertPart.Add(rec);
                }

                if (insertPart.Count > 0)
                {
                    JHSemesterScore.Insert(insertPart);
                }

                return(new List <JHSemesterScoreRecord>());
            };
            addSpliter.ProgressChange = delegate(int progress)
            {
                Reporter.Feedback("新增科目成績...", Util.CalculatePercentage(addSemsScore.Count, progress));
            };
            addSpliter.Execute(addSemsScore);
            #endregion

            #region 更新科目成績
            FunctionSpliter <JHSemesterScoreRecord, JHSemesterScoreRecord> updateSpliter =
                new FunctionSpliter <JHSemesterScoreRecord, JHSemesterScoreRecord>(500, 5);
            updateSpliter.Function = delegate(List <JHSemesterScoreRecord> part)
            {
                JHSemesterScore.Update(part);
                return(new List <JHSemesterScoreRecord>());
            };
            updateSpliter.ProgressChange = delegate(int progress)
            {
                Reporter.Feedback("更新科目成績...", Util.CalculatePercentage(updateSemsScore.Count, progress));
            };
            updateSpliter.Execute(updateSemsScore);
            #endregion
        }