using System; using System.Collections.Generic; using System.Linq; using System.Text; using EMIS.DataLogic.ScoreManage; using EMIS.DataLogic.Common.Students; using EMIS.ViewModel.ScoreManage; using System.Linq.Expressions; using EMIS.ViewModel; using EMIS.Entities; using Bowin.Common.Linq; using EMIS.ViewModel.CacheManage; using Bowin.Common.Utility; using System.Data; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.HSSF.Util; namespace EMIS.CommonLogic.ScoreManage { public class SpecialityScoreByFinalScoreServices : BaseServices, ISpecialityScoreByFinalScoreServices { public SpecialityScoreSumDAL specialityScoreSumDAL { get; set; } public StudentsDAL StudentsDAL { get; set; } public SpecialityScoreGTView SpecialityScoreSumTable(Guid? collegeID, int? years, int? standardID, int? schoolyearNumID, int? schoolcodeID, Guid? classmajorID, List CourseTypeIDList) { Expression> studentExp = (x => true); Expression> grademajorExp = (x => true); Expression> facultyExp = (x => true); Expression> schoolyearExp = (x => true); Expression> scoreExp = x => true; Expression> missionClassExp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); SpecialityScoreGTView summaryView = new SpecialityScoreGTView(); if (collegeID.HasValue) { facultyExp = facultyExp.And(x => x.CollegeID == collegeID); } if (years.HasValue) { grademajorExp = grademajorExp.And(x => x.GradeID == years); } if (standardID.HasValue) { facultyExp = facultyExp.And(x => x.StandardID == standardID); } if (classmajorID.HasValue) { studentExp = studentExp.And(x => x.ClassmajorID == classmajorID); } if (schoolyearNumID.HasValue) { scoreExp = scoreExp.And(x => x.SchoolyearNumID == schoolyearNumID); } if (schoolcodeID.HasValue) { schoolyearExp = schoolyearExp.And(x => x.SchoolcodeID == schoolcodeID); } if (CourseTypeIDList.Count > 0) { scoreExp = scoreExp.And(x => CourseTypeIDList.Contains((int)x.CourseTypeID)); } var courseQuery = specialityScoreSumDAL.GetCoursematerialViewQueryable(scoreExp, studentExp, grademajorExp, facultyExp, schoolyearExp); courseQuery = courseQuery.OrderBy(x => x.StartTermID).ThenBy(x => x.CourseName); //先获取当前院系所、学年、专业、学年数、学期 var queryCoursematerialPlan = courseQuery.ToList(); var queryFinallyScore = specialityScoreSumDAL.GetFinalScoreQueryable(scoreExp, studentExp, grademajorExp, facultyExp, schoolyearExp).ToList(); var queryCredit= specialityScoreSumDAL.GetCreditQueryable(scoreExp, studentExp, grademajorExp, facultyExp, schoolyearExp).ToList(); var studentList = StudentsDAL.GetBaseStudentViewQueryable(studentExp, grademajorExp, facultyExp).ToList(); summaryView.StudentList = studentList.OrderBy(x => x.LoginID).ToList(); summaryView.SchoolyearNums = new List(); summaryView.SpecialityCreditView = new List(); //开始建立树形结构,顶层是学年数,从大到小排序 List specialityCreditView = new List(); //specialityCreditView.Tital = "学分统计"; if (queryCredit.Count() > 0) { var creditList = (from s in studentList join credit in queryCredit on s.UserID equals credit.UserID into scr from a in scr.DefaultIfEmpty() orderby s.LoginID select new SpecialityCreditView { UserID = s.UserID, RequiredCredit = a == null ? 0 : ((a.RequiredCredit ?? 0) - (a.PractiseCredit ?? 0)), OptionalCourseCredit = a == null ? 0 : (((a.OptionalCourseCredit ?? 0) - (a.SchoolbasedCredit ?? 0) - (a.ElectiveCredit ?? 0) - (a.GeneralEducationCredit ?? 0))), FreeSelectionCourseCredit = a == null ? 0 : (((a.FreeSelectionCourseCredit ?? 0) - (a.ElectivePublicCredit ?? 0))), GeneralEducationCredit = a == null ? 0 : (a.GeneralEducationCredit ?? 0), ElectiveCredit = a == null ? 0 : (a.ElectiveCredit ?? 0), ElectivePublicCredit = a == null ? 0 : (a.ElectivePublicCredit ?? 0), SchoolbasedWithClub = a == null ? 0 : (a.SchoolbasedWithClub ?? 0), SchoolbasedTotalCredit = a == null ? 0 : (a.SchoolbasedNotCredit ?? 0 + a.SchoolbasedCredit ?? 0), SchoolbasedNotCredit = a == null ? 0 : (a.SchoolbasedNotCredit ?? 0), SchoolbasedCredit = a == null ? 0 : (a.SchoolbasedCredit ?? 0), SchoolbasedRealCredit = a == null ? 0 : (a.SchoolbasedRealCredit ?? 0), PractiseWithClub = a == null ? 0 : (a.PractiseWithClub ?? 0), PractiseTotalCredit = a == null ? 0 : (a.PractiseNotCredit ?? 0 + a.PractiseCredit ?? 0), PractiseNotCredit = a == null ? 0 : (a.PractiseNotCredit ?? 0), PractiseCredit = a == null ? 0 : (a.PractiseCredit ?? 0), PractiseRealCredit = a == null ? 0 : (a.PractiseRealCredit ?? 0), ClubWithSchoolbasedCredit = a == null ? 0 : (((a.PractiseWithClub ?? 0) > 4 ? 4 : (a.PractiseWithClub ?? 0)) + ((a.SchoolbasedWithClub ?? 0) > 4 ? 4 : (a.SchoolbasedWithClub ?? 0)) + (a.SchoolbasedNotCredit ?? 0)), TotalCredit = a == null ? 0 : ((a.RequiredCredit ?? 0) + (a.OptionalCourseCredit ?? 0) + (a.FreeSelectionCourseCredit ?? 0) - (a.PractiseCredit ?? 0) - (a.SchoolbasedCredit ?? 0) + (a.PractiseNotCredit ?? 0) + ((a.PractiseWithClub ?? 0) > 4 ? 4 : (a.PractiseWithClub ?? 0)) + (a.SchoolbasedNotCredit ?? 0) + ((a.SchoolbasedWithClub ?? 0) > 4 ? 4 : (a.SchoolbasedWithClub ?? 0))), AvgScore = a == null ? 0 : (Math.Round((decimal)(a.AvgScore ?? 0), 2)), }); specialityCreditView = creditList.ToList(); } //foreach() summaryView.SpecialityCreditView = specialityCreditView; //var schoolyearNums = queryCoursematerialPlan.Select(x => x.SchoolyearNumID).Distinct().OrderBy(x => x).ToList(); var startTermIDs = queryCoursematerialPlan.Select(x => x.StartTermID).Distinct().OrderBy(x => x).ToList(); foreach (var startTermID in startTermIDs) { SpecialityScoreSummaryYearView schoolyearNumView = new SpecialityScoreSummaryYearView(); schoolyearNumView.Title = IdNameExt.GetName(DictionaryItem.CF_Startterm, startTermID); //schoolyearNumView. = schoolyearNum; schoolyearNumView.CoursematerialNum = 0; //第二层,学期,按开课学期排序,不是按学期ID schoolyearNumView.SchoolCodeView = new List(); var schoolcodeList = queryCoursematerialPlan.Where(x => x.StartTermID == startTermID) .GroupBy(x => new { x.StartTermID }) .Select(x => x.Key).OrderBy(x => x.StartTermID).ToList(); foreach (var schoolCode in schoolcodeList) { SpecialityScoreSummarySchoolcodeView schoolCodeView = new SpecialityScoreSummarySchoolcodeView(); schoolCodeView.Title = IdNameExt.GetName(DictionaryItem.CF_Startterm, schoolCode.StartTermID); //schoolCodeView.SchoolCodeID = schoolCode.SchoolcodeID; schoolCodeView.StartTermID = schoolCode.StartTermID; //第三层,课程,按名称排序 schoolCodeView.CoursematerialList = new List(); schoolCodeView.CourseTypeList = new List(); List creditCourseTypeViewList = new List(); var coursematerialList = queryCoursematerialPlan.Where(x => x.StartTermID == schoolCode.StartTermID) .GroupBy(x => new { x.CoursematerialID, x.CourseName, x.Credit, x.CourseTypeID }) .Select(x => x.Key).OrderBy(x => x.CourseName).ToList(); foreach (var coursematerial in coursematerialList) { SpecialityScoreSummaryCoursematerialView coursematerialView = new SpecialityScoreSummaryCoursematerialView(); coursematerialView.Title = coursematerial.CourseName; coursematerialView.CoursematerialID = coursematerial.CoursematerialID; coursematerialView.Credit = coursematerial.Credit; coursematerialView.Tag = specialityScoreSumDAL.GetIsByCourseTypeID((int)coursematerial.CourseTypeID); //该课程所有学生均未录入成绩时,不汇总到专业成绩里 var scoreCount = queryFinallyScore.Where(x => x.StarttermID == schoolCode.StartTermID && x.CoursematerialID == coursematerialView.CoursematerialID).ToList().Count(); if (scoreCount == 0) continue; //最后一层,成绩,按学生学号排序 var scoreList = (from s in studentList join fs in queryFinallyScore.Where(x => x.StarttermID == schoolCode.StartTermID && x.CoursematerialID == coursematerialView.CoursematerialID) on s.UserID equals fs.UserID into dfs from efs in dfs.DefaultIfEmpty() orderby s.LoginID select new SpecialityScoreSummaryScoreView { UserID = s.UserID, Score = (efs == null) ? null : efs.TotalScoreStr, //Credit = efs.ScoreCredit, //Score = (efs == null ) ? (int?)null : Convert.ToInt32(Math.Round((Convert.ToDecimal(efs.TotalScore)), 0)) }).ToList(); coursematerialView.ScoreList = scoreList; schoolCodeView.CoursematerialList.Add(coursematerialView); schoolyearNumView.CoursematerialNum += 1; } //schoolyearNumView.SchoolCodeView.Add(schoolCodeView); //因为某课程所有学生均未录入成绩时,不汇总到专业成绩里,如果某学期和学年数所有课程没有录入成绩时,不显示一二层学期和学年标题 if (schoolCodeView.CoursematerialList.Count > 0) schoolyearNumView.SchoolCodeView.Add(schoolCodeView); } //summaryView.SchoolyearNums.Add(schoolyearNumView); //因为某课程所有学生均未录入成绩时,不汇总到专业成绩里,如果某学期和学年数所有课程没有录入成绩时,不显示一二层学期和学年标题 if (schoolyearNumView.SchoolCodeView.Count > 0) summaryView.SchoolyearNums.Add(schoolyearNumView); } return summaryView; } public List GetCourseTypeID() { return specialityScoreSumDAL.GetCourseTypeQueryble(); } public void ExportSpecialityScoreSumTable(Guid? collegeID, int? years, int? standardID, int? schoolyearNumID, int? schoolcodeID, Guid? classmajorID, List courseTypeIDList, bool isCredit) { string html = ""; NpoiExcelHelper neh = new NpoiExcelHelper(); var specialtyScoreSummaryView = this.SpecialityScoreSumTable(collegeID, years, standardID, schoolyearNumID, schoolcodeID, classmajorID, courseTypeIDList); Func noPassStyleFunc = (x => { var style = (HSSFCellStyle)x.CreateCellStyle(); style.BorderBottom = BorderStyle.THIN; style.BorderLeft = BorderStyle.THIN; style.BorderRight = BorderStyle.THIN; style.BorderTop = BorderStyle.THIN; style.Alignment = HorizontalAlignment.CENTER; style.VerticalAlignment = VerticalAlignment.CENTER; var font = (HSSFFont)x.CreateFont(); font.FontHeightInPoints = 12; font.Boldweight = (short)FontBoldWeight.BOLD; font.Color = HSSFColor.RED.index; style.SetFont(font); return style; }); List styleList = new List(); var dtResult = new DataTable(); if (specialtyScoreSummaryView != null) { var detail = new List(); var firstRow = new List(); var secondRow = new List(); var thirdRow = new List(); var thirdRow1 = new List(); var thirdRow2 = new List(); var forthRow = new List>(); var detailRow = new List(); //生成第一、二、三行表头,学年和学期和课程 //第一行开头 firstRow.Add(""); firstRow.Add("学期"); if (isCredit) { firstRow.Add("学分统计"); } //第二行开头 secondRow.Add(""); //第三行开头 thirdRow.Add(""); thirdRow.Add("次序"); thirdRow.Add("学号"); thirdRow.Add("姓名"); if (isCredit) { thirdRow.Add("总学分"); thirdRow.Add("必修学分"); thirdRow.Add("限选学分"); thirdRow.Add("通识教育"); thirdRow.Add("任选学分"); thirdRow.Add("方向选修"); thirdRow.Add("公共选修"); thirdRow.Add("实践环节"); thirdRow.Add("俱乐部(含校本)"); thirdRow.Add("平均分"); } //第四行(第三行附加) thirdRow1.Add(""); thirdRow1.Add(""); thirdRow1.Add(""); thirdRow1.Add(""); if (isCredit) { thirdRow1.Add(""); thirdRow1.Add(""); thirdRow1.Add(""); thirdRow1.Add(""); thirdRow1.Add(""); thirdRow1.Add(""); thirdRow1.Add(""); thirdRow1.Add(""); thirdRow1.Add(""); thirdRow1.Add(""); } //第五行(第三行附加) thirdRow2.Add(""); thirdRow2.Add(""); thirdRow2.Add(""); thirdRow2.Add(""); if (isCredit) { thirdRow2.Add(""); thirdRow2.Add(""); thirdRow2.Add(""); thirdRow2.Add(""); thirdRow2.Add(""); thirdRow2.Add(""); thirdRow2.Add(""); thirdRow2.Add(""); thirdRow2.Add(""); thirdRow2.Add(""); } for (var i = 0; i < specialtyScoreSummaryView.StudentList.Count; i++) { var student = specialtyScoreSummaryView.StudentList[i]; var credit = specialtyScoreSummaryView.SpecialityCreditView[i]; var studentColumns = new List(); studentColumns.Add(""); studentColumns.Add("" + (i + 1).ToString() + ""); studentColumns.Add("" + student.LoginID + ""); studentColumns.Add("" + student.UserName + ""); if (isCredit) { studentColumns.Add("" + credit.TotalCredit + ""); studentColumns.Add("" + credit.RequiredCredit + ""); studentColumns.Add("" + credit.OptionalCourseCredit + ""); studentColumns.Add("" + credit.GeneralEducationCredit + ""); studentColumns.Add("" + credit.FreeSelectionCourseCredit + ""); studentColumns.Add("" + credit.ElectiveCredit + ""); studentColumns.Add("" + credit.ElectivePublicCredit + ""); studentColumns.Add("" + credit.PractiseNotCredit + ""); studentColumns.Add("" + credit.ClubWithSchoolbasedCredit + ""); studentColumns.Add("" + credit.AvgScore + ""); } forthRow.Add(studentColumns); } //前三行明细 var index = 3; if (isCredit) { index = 13; } for (var i = 0; i < specialtyScoreSummaryView.SchoolyearNums.Count; i++) { var schoolyearNum = specialtyScoreSummaryView.SchoolyearNums[i]; firstRow.Add("" + schoolyearNum.Title + ""); for (var j = 0; j < schoolyearNum.SchoolCodeView.Count; j++) { var schoolcode = schoolyearNum.SchoolCodeView[j]; secondRow.Add("" + schoolcode.Title + ""); for (var k = 0; k < schoolcode.CoursematerialList.Count; k++) { var coursematerial = schoolcode.CoursematerialList[k]; thirdRow.Add("" + coursematerial.Title + ""); thirdRow1.Add("" + coursematerial.Tag + ""); thirdRow2.Add("" + coursematerial.Credit + ""); for (var l = 0; l < coursematerial.ScoreList.Count; l++) { var score = coursematerial.ScoreList[l]; if (score.Score != "" && score.Score != null) { decimal tmpScore; if (decimal.TryParse(score.Score, out tmpScore)) { if (tmpScore < 60) { styleList.Add(new NpoiExcelCellStyle { ColumnIndex = index, RowIndex = l + 5, StyleFunc = noPassStyleFunc }); } } else { styleList.Add(new NpoiExcelCellStyle { ColumnIndex = index, RowIndex = l + 5, StyleFunc = noPassStyleFunc }); } forthRow[l].Add("" + score.Score.ToString() + ""); } else { forthRow[l].Add(""); } } index++; } } } //第一行结尾 firstRow.Add(""); //第二行结尾 secondRow.Add(""); //第三行结尾 thirdRow.Add(""); thirdRow1.Add(""); thirdRow2.Add(""); html += string.Join("", firstRow); html += string.Join("", secondRow); html += string.Join("", thirdRow); html += string.Join("", thirdRow1); html += string.Join("", thirdRow2); for (var i = 0; i < specialtyScoreSummaryView.StudentList.Count; i++) { html += string.Join("", forthRow[i]); html += ""; } } neh.PresetCellStyle(styleList); neh.ExportTable(html, "专业成绩汇总"); } } }