using System; using System.Collections.Generic; using System.Linq; using System.Text; using EMIS.DataLogic.ScoreManage; using EMIS.ViewModel.ScoreManage; using EMIS.Entities; using System.Linq.Expressions; using EMIS.ViewModel; using Bowin.Common.Linq; using Bowin.Common.Linq.Entity; using Bowin.Common.DataTime; using Bowin.Common.Utility; using System.Text.RegularExpressions; using EMIS.DataLogic.Repositories; using EMIS.DataLogic.ExaminationApply; using EMIS.DataLogic.Common.CalendarManage; using EMIS.ViewModel.CacheManage; namespace EMIS.CommonLogic.ScoreManage { public class LevelScoreServices : BaseServices, ILevelScoreServices { public LevelScoreDAL levelScoreDAL { get; set; } public StudentRepository studentRepository { get; set; } public ExaminationSubjectDAL examinationSubjectDAL { get; set; } public SchoolYearDAL schoolYearDAL { get; set; } public LevelSettingDAL levelSettingDAL { get; set; } //学生版过级成绩 public Bowin.Common.Linq.Entity.IGridResultSet GetStudentLevelScoreGrid(ViewModel.ConfiguretView configuretView, Guid? schoolYearID, Guid? ExaminationSubjectID,Guid userId, int pageIndex, int pageSize) { Expression> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); exp=(x=>x.UserID==userId); var query = levelScoreDAL.GetLevelScoreViewQueryable(exp); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim()); //query = query.GroupBy(x => new { x.LevelScoreID, x.SchoolyearCode, x.ExaminationSubjectName,x.ClassmajorName }) // .Select(g => new // { // LevelScoreID = g.Key.LevelScoreID, // SchoolyearCode = g.Key.SchoolyearCode, // ExaminationSubjectName = g.Key.ExaminationSubjectName, // ClassmajorName = g.Key.ClassmajorName // }); var queryLevelName = levelSettingDAL.GetLevelSettingViewQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList(); var result = query.OrderByDescending(x => x.SchoolyearCode).ThenBy(x => x.CollegeName).ThenBy(x => x.ClassmajorName).ThenBy(x => x.LoginID).ToGridResultSet(pageIndex, pageSize); result.rows.ForEach(x => x.LevelName = queryLevelName.Where(t => t.MaxScore >= x.TotalScore && t.MinScore <= x.TotalScore && t.ExaminationSubjectName == x.ExaminationSubjectName).OrderByDescending(o => o.MaxScore).ThenByDescending(o => o.MinScore).Select(w => w.LevelName).FirstOrDefault()); //return (query).OrderByDescending(x => x.SchoolyearCode).ThenByDescending(x => x.ExaminationSubjectName).ThenByDescending(x => x.ClassmajorName).ToGridResultSet(pageIndex, pageSize); return result; } //教师版过级成绩 public Bowin.Common.Linq.Entity.IGridResultSet GetLevelScoreGrid(ViewModel.ConfiguretView configuretView, Guid? schoolYearID, Guid? collegeID, int? Years, int? standardID, Guid? ClassmajorID, Guid? ExaminationSubjectID, int? learningformID, int? education, string LearnSystem, Guid? levelSettingID, int pageIndex, int pageSize) { Expression> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); if (learningformID.HasValue) exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.LearningformID == learningformID); if (!string.IsNullOrEmpty(LearnSystem) && LearnSystem != "-1") { var LearnSystems = Convert.ToDecimal(LearnSystem); exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.LearnSystem == LearnSystems); } if (education.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.EducationID == education.Value); } if (schoolYearID.HasValue) { exp = exp.And(x => x.SchoolyearID == schoolYearID); } if (Years.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.GradeID == Years); } if (collegeID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CF_College.CollegeID == collegeID); } if (standardID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID == standardID); } if (ClassmajorID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.ClassmajorID == ClassmajorID); } LevelSettingView levelSetting = null; if (levelSettingID.HasValue) { levelSetting = levelSettingDAL.GetLevelSettingViewQueryable(x => x.LevelSettingID == levelSettingID).FirstOrDefault(); } var query = levelScoreDAL.GetLevelScoreViewQueryable(exp); if (ExaminationSubjectID.HasValue) { query = query.Where(x => x.ExaminationSubjectID == ExaminationSubjectID); if (levelSetting != null) { query = query.Where(x => x.TotalScore >= levelSetting.MinScore && x.TotalScore <= levelSetting.MaxScore); } } if (!string.IsNullOrEmpty(configuretView.ConditionValue)) query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim()); //query = query.GroupBy(x => new { x.LevelScoreID, x.SchoolyearCode, x.ExaminationSubjectName,x.ClassmajorName }) // .Select(g => new // { // LevelScoreID = g.Key.LevelScoreID, // SchoolyearCode = g.Key.SchoolyearCode, // ExaminationSubjectName = g.Key.ExaminationSubjectName, // ClassmajorName = g.Key.ClassmajorName // }); var queryLevelName = levelSettingDAL.GetLevelSettingViewQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList(); var result = GetQueryByDataRangeByCollege(query).OrderByDescending(x => x.SchoolyearCode).ThenBy(x => x.CollegeName).ThenBy(x => x.ClassmajorName).ThenBy(x => x.LoginID).ToGridResultSet(pageIndex, pageSize); result.rows.ForEach(x => x.LevelName = queryLevelName.Where(t => t.MaxScore >= x.TotalScore && t.MinScore <= x.TotalScore && t.ExaminationSubjectName == x.ExaminationSubjectName).OrderByDescending(o => o.MaxScore).ThenByDescending(o => o.MinScore).Select(w => w.LevelName).FirstOrDefault()); //return (query).OrderByDescending(x => x.SchoolyearCode).ThenByDescending(x => x.ExaminationSubjectName).ThenByDescending(x => x.ClassmajorName).ToGridResultSet(pageIndex, pageSize); return result; } public List GetLevelScoreList(ViewModel.ConfiguretView configuretView, Guid? schoolYearID, Guid? collegeID, int? Years, int? standardID, Guid? ClassmajorID, Guid? ExaminationSubjectID,int? learningformID,int? education,string LearnSystem) { Expression> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); if (learningformID.HasValue) exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.LearningformID == learningformID); if (!string.IsNullOrEmpty(LearnSystem) && LearnSystem != "-1") { var LearnSystems = Convert.ToDecimal(LearnSystem); exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.LearnSystem == LearnSystems); } if (education.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.EducationID == education.Value); } var query = levelScoreDAL.GetLevelScoreViewQueryable(exp); if (schoolYearID.HasValue) query = query.Where(x => x.SchoolyearID == schoolYearID); if (Years.HasValue) query = query.Where(x => x.Years == Years); if (collegeID.HasValue) query = query.Where(x => x.CollegeID == collegeID); if (standardID.HasValue) query = query.Where(x => x.StandardID == standardID); if (ClassmajorID.HasValue) query = query.Where(x => x.ClassmajorID == ClassmajorID); if (ExaminationSubjectID.HasValue) query = query.Where(x => x.ExaminationSubjectID == ExaminationSubjectID); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim()); var queryLevelName = levelSettingDAL.GetLevelSettingViewQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList(); var result = this.GetQueryByDataRangeByCollege(query).OrderByDescending(x => x.SchoolyearCode).ThenBy(x => x.CollegeName).ThenBy(x => x.ClassmajorName).ThenBy(x => x.LoginID).ToList(); result.ForEach(x => x.LevelName = queryLevelName.Where(t => t.MaxScore >= x.TotalScore && t.MinScore <= x.TotalScore && t.ExaminationSubjectName == x.ExaminationSubjectName).OrderByDescending(o => o.MaxScore).ThenByDescending(o => o.MinScore).Select(w => w.LevelName).FirstOrDefault()); return result; //return (query).OrderByDescending(x => x.SchoolyearCode).ThenByDescending(x => x.SchoolyearCode).ThenByDescending(x => x.ExaminationSubjectName).ThenByDescending(x => x.ClassmajorName).ToList(); } public LevelScoreView GetLevelScoreView(Guid? LevelScoreID) { var query = levelScoreDAL.GetLevelScoreViewQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).Where(x => x.LevelScoreID == LevelScoreID).FirstOrDefault(); return query; } public ER_LevelScore GetLevelScoreEntity(Guid? LevelScoreID) { return levelScoreDAL.levelScoreRepository.GetSingle(x => x.LevelScoreID == LevelScoreID); } public void Save(LevelScoreView LevelScoreView) { try { //同一学期同一学号同一科目重复。 ER_LevelScore levelScoreRepeat = levelScoreDAL.levelScoreRepository.Entities.Where(x => x.SchoolyearID == LevelScoreView.SchoolyearID && x.UserID == LevelScoreView.UserID&& x.ExaminationSubjectID == LevelScoreView.ExaminationSubjectID).FirstOrDefault(); ER_LevelScore LevelScoreEntity = null; if (LevelScoreView.LevelScoreID == null || LevelScoreView.LevelScoreID == Guid.Empty) { if (levelScoreRepeat!=null) throw new Exception("已存在该学生成绩,请核查。"); LevelScoreEntity = new ER_LevelScore(); LevelScoreEntity.LevelScoreID = Guid.NewGuid(); SetNewStatus(LevelScoreEntity); UnitOfWork.Add(LevelScoreEntity); } else { if (levelScoreRepeat != null && levelScoreRepeat.LevelScoreID != LevelScoreView.LevelScoreID) throw new Exception("已存在该学生成绩,请核查。"); LevelScoreEntity = GetLevelScoreEntity(LevelScoreView.LevelScoreID); if (LevelScoreEntity == null) throw new Exception("未找到相对应的等级成绩!"); SetModifyStatus(LevelScoreEntity); } LevelScoreEntity.ScoreNo = LevelScoreView.ScoreNo; LevelScoreEntity.ExaminationSubjectID = LevelScoreView.ExaminationSubjectID; LevelScoreEntity.SchoolyearID = LevelScoreView.SchoolyearID; LevelScoreEntity.ExaminationDate = LevelScoreView.ExaminationDate; //LevelScoreEntity.ValidDate = LevelScoreView.ValidDate; LevelScoreEntity.UserID = LevelScoreView.UserID; LevelScoreEntity.TotalScore = LevelScoreView.TotalScore; LevelScoreEntity.Remark = LevelScoreView.Remark; UnitOfWork.Commit(); } catch (Exception) { throw; } } public void Delete(IList LevelScoreID) { if (LevelScoreID.Count > 0) { UnitOfWork.Delete(x => LevelScoreID.Contains(x.LevelScoreID)); } } public void LevelScoreImport(Dictionary cellheader, out int OkCount, out List errdataList, out int ErrCount, string sourcePhysicalPath) { StringBuilder errorMsg = new StringBuilder(); // 错误信息 List errList = new List(); //错误数据行 DataTimeHelper dth = new DataTimeHelper(); #region 1.1解析文件,存放到一个List集合里 // 1.1解析文件,存放到一个List集合里 cellheader.Remove("ErrorMessage");//去除异常列、导入操作不需要 List enlist = NpoiExcelHelper.ExcelToEntityList(cellheader, sourcePhysicalPath, out errorMsg, out errList); cellheader.Add("ErrorMessage", "错误信息");//还原字典项 #endregion #region 1.2 将Excel数据写入数据库中 #region 1.2.1 对List集合进行有效性校验 if (enlist.Count() <= 0) { throw new Exception("请填写Excel模板信息数据。"); } #region 1.2.1.1数据逻辑验证 var loginIDList = enlist.Select(x => x.LoginID).Distinct().ToList(); var schoolyearCodeList = enlist.Select(x => x.SchoolyearCode).Distinct().ToList(); var subjectNameList = enlist.Select(x => x.ExaminationSubjectName).Distinct().ToList(); var studentList = studentRepository.GetList(x => loginIDList.Contains(x.Sys_User.LoginID), (x => x.Sys_User)).ToList(); var schoolyearList = schoolYearDAL.schoolyearRepository.GetList(x => schoolyearCodeList.Contains(x.Code)).ToList(); var subjectList = examinationSubjectDAL.ExaminationSubjectRepository.GetList(x => subjectNameList.Contains(x.Name)).ToList(); var levelScoreKeyList = (from ls in enlist.GroupBy(x => new { x.SchoolyearCode, x.LoginID, x.ExaminationSubjectName }) join u in studentList on ls.Key.LoginID equals u.Sys_User.LoginID join sy in schoolyearList on ls.Key.SchoolyearCode equals sy.Code join s in subjectList on ls.Key.ExaminationSubjectName equals s.Name select new { sy.SchoolyearID, u.UserID, s.ExaminationSubjectID }).ToList(); var dbLevelScoreList = levelScoreDAL.levelScoreRepository.Entities.SelectByKeys(levelScoreKeyList); for (int i = 0; i < enlist.Count; i++) { LevelScoreView en = enlist[i]; string errorMsgStr = "第" + (i + 1) + "行数据检测异常:"; bool isHaveNoInputValue = false; // 是否含有未输入项 #region 检测必填项是否必填 if (string.IsNullOrEmpty(en.LoginID)) { errorMsgStr += "学号不能为空;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } if (string.IsNullOrEmpty(en.ExaminationSubjectName)) { errorMsgStr += "考试科目不能为空;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } if (string.IsNullOrEmpty(en.SchoolyearCode)) { errorMsgStr += "学年学期不能为空;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } if (en.TotalScore==null) { errorMsgStr += "总成绩不能为空;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } #endregion #region 验证数据格式 //总成绩 Regex reg = new Regex(@"^[0-9]+([.]{1}[0-9]+){0,1}$"); if (!string.IsNullOrEmpty(en.TotalScore.ToString())) { if (!reg.IsMatch(en.TotalScore + "")) { errorMsgStr += "总成绩格式不正确;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } } //考试日期 //reg = new Regex(@"^[-/:0-9]+$"); DateTime result; if (!string.IsNullOrEmpty(en.ExaminationDateStr)) { if (!DateTime.TryParse(en.ExaminationDateStr, out result)) { errorMsgStr += "考试日期格式不正确;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } } //有效日期 //reg = new Regex(@"^[-/:0-9]+$"); if (!string.IsNullOrEmpty(en.ValidDateStr)) { if (!DateTime.TryParse(en.ValidDateStr, out result)) { errorMsgStr += "有效日期格式不正确;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } } #endregion #region 信息是否存在 if (!string.IsNullOrEmpty(en.LoginID)) { CF_Student student = studentList.Where(x => x.Sys_User.LoginID == en.LoginID).FirstOrDefault(); if (student == null) { errorMsgStr += "该学号的学生信息不存在;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } else en.UserID = student.UserID; } if (!string.IsNullOrEmpty(en.SchoolyearCode)) { EMIS.Entities.CF_Schoolyear Schoolyear = schoolyearList.Where(x => x.Code == en.SchoolyearCode).FirstOrDefault(); if (Schoolyear == null) { errorMsgStr += "该学年学期信息不存在;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } else en.SchoolyearID = Schoolyear.SchoolyearID; } if ( !string.IsNullOrEmpty(en.ExaminationSubjectName)) { EX_ExaminationSubject examinationSubject = subjectList.Where(x => x.Name == en.ExaminationSubjectName).FirstOrDefault(); if (examinationSubject == null) { errorMsgStr += "该考试科目信息不存在;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } else en.ExaminationSubjectID = examinationSubject.ExaminationSubjectID; } //验证同一学期同一学号同一科目重复 if (en.ExaminationSubjectID != null && en.SchoolyearID != null && en.UserID != null) { //导入的信息中的重复 var enlistRepeat = enlist.Where(x => x.SchoolyearID == en.SchoolyearID && x.UserID == en.UserID && x.ExaminationSubjectID == en.ExaminationSubjectID); if (enlistRepeat != null && enlistRepeat.Count() > 1) { errorMsgStr += "导入的信息中该学生成绩有重复,已导入其中一条;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } //导入信息和数据库的重复 ER_LevelScore levelScoreRepeat = dbLevelScoreList.Where(x => x.SchoolyearID == en.SchoolyearID && x.UserID == en.UserID && x.ExaminationSubjectID == en.ExaminationSubjectID).FirstOrDefault(); if (levelScoreRepeat != null) { errorMsgStr += "已存在该学生成绩;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } } #endregion 唯一值 //if (!string.IsNullOrEmpty(en.UserID.ToString()) && !string.IsNullOrEmpty(en.ExaminationSubjectID.ToString())) //{ // ER_LevelScore levelScoreEnetey = levelScoreDAL.levelScoreRepository.Entities.Where(x => x.UserID == en.UserID && x.ExaminationSubjectID == en.ExaminationSubjectID).FirstOrDefault(); // if (levelScoreEnetey != null) // { // errorMsgStr += "对应的学号和考试科目已经存在;"; // en.ErrorMessage = errorMsgStr; // isHaveNoInputValue = true; // } //} if (isHaveNoInputValue) // 若必填项有值未填 { en.IsExcelVaildateOK = false; en.ErrorMessage = errorMsgStr; errList.Add(en); errorMsg.AppendLine(errorMsgStr); } } #endregion // TODO:其他检测 #region 1.2.1.3 循环写入验证成功的数据 List levelScoreList = new List(); for (int i = 0; i < enlist.Count; i++) { LevelScoreView enA = enlist[i]; if (enA.IsExcelVaildateOK == false) // 上面验证不通过,不进行此步验证 { continue; } var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; ER_LevelScore levelScore = new ER_LevelScore(); levelScore.LevelScoreID = Guid.NewGuid(); levelScore.UserID = enA.UserID; levelScore.ScoreNo = enA.ScoreNo; levelScore.ExaminationSubjectID = enA.ExaminationSubjectID; levelScore.TotalScore = enA.TotalScore; levelScore.Remark = enA.Remark; //if (!string.IsNullOrEmpty(enA.ExaminationDateStr)) //levelScore.ExaminationDate = Convert.ToDateTime(dth.ToDateTimeValue(enA.ExaminationDateStr)); if (!string.IsNullOrEmpty(enA.ExaminationDateStr)) { if (dth.ToDateTimeValue(enA.ExaminationDateStr) != string.Empty) levelScore.ExaminationDate = Convert.ToDateTime(dth.ToDateTimeValue(enA.ExaminationDateStr)); else levelScore.ExaminationDate = Convert.ToDateTime(enA.ExaminationDateStr); } //levelScore.ExaminationDate = enA.ExaminationDateStr; //levelScore.ExaminationDate = Convert.ToDateTime(enA.ExaminationDateStr); if (!string.IsNullOrEmpty(enA.ValidDateStr)) { //if (dth.ToDateTimeValue(enA.ValidDateStr) != string.Empty) // levelScore.ValidDate = Convert.ToDateTime(dth.ToDateTimeValue(enA.ValidDateStr)); //else // levelScore.ValidDate = Convert.ToDateTime(enA.ValidDateStr); } levelScore.SchoolyearID = enA.SchoolyearID; SetNewStatus(levelScore); levelScoreList.Add(levelScore); } #endregion UnitOfWork.BulkInsert(levelScoreList);//统一写入 #endregion #endregion #region 1.3 返回各项数据值 OkCount = enlist.Distinct().Count() - errList.Distinct().Count();//共条数减去失败条数 errdataList = errList.Distinct().ToList(); ErrCount = errList.Distinct().Count(); #endregion } } }