using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Transactions; using Bowin.Common.Linq; using Bowin.Common.Linq.Entity; using EMIS.DataLogic; using EMIS.DataLogic.EducationManage; using EMIS.Entities; using EMIS.ViewModel; using EMIS.ViewModel.EducationManage; namespace EMIS.CommonLogic.EducationManage { public class TrainingClassServices : BaseServices, ITrainingClassServices { public TrainingClassDAL TrainingClassDAL { get; set; } public IGridResultSet GetTrainingClassViewGrid(ConfiguretView conditionView, Guid? schoolyearID, Guid? examinationBatchID, Guid? examinationTypeID, Guid? examinationProjectID, int? schoolAreaID, int? pageIndex, int? pageSize) { Expression> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); Expression> batchExp = (x => true); Expression> projectExp = (x => true); if (schoolyearID.HasValue) { batchExp = batchExp.And(x => x.SchoolyearID == schoolyearID); } if (examinationBatchID.HasValue) { batchExp = batchExp.And(x => x.ExaminationBatchID == examinationBatchID); } if (examinationTypeID.HasValue) { projectExp = projectExp.And(x => x.ExaminationTypeID == examinationTypeID); } if (examinationProjectID.HasValue) { projectExp = projectExp.And(x => x.ExaminationProjectID == examinationProjectID); } if (schoolAreaID.HasValue) { exp = exp.And(x => x.SchoolAreaID == schoolAreaID); } var query = TrainingClassDAL.GetTrainingClassViewQueryable(exp, batchExp, projectExp); if (!string.IsNullOrEmpty(conditionView.ConditionValue) && !string.IsNullOrEmpty(conditionView.Attribute)) { query = query.DynamicWhere(conditionView.Attribute, conditionView.Condition, conditionView.ConditionValue); } return query.OrderByDescending(x => x.SchoolyearCode).ThenByDescending(x => x.ExaminationBatchCreateTime) .ThenBy(x => x.ExaminationTypeName) .ThenBy(x => x.ExaminationProjectName).ThenBy(x => x.Name) .ToGridResultSet(pageIndex, pageSize); } public List GetTrainingClassExportViewList(ConfiguretView conditionView, Guid? schoolyearID, Guid? examinationBatchID, Guid? examinationTypeID, Guid? examinationProjectID, int? schoolAreaID, IList trainingClassIDList) { Expression> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); Expression> batchExp = (x => true); Expression> projectExp = (x => true); if (schoolyearID.HasValue) { batchExp = batchExp.And(x => x.SchoolyearID == schoolyearID); } if (examinationBatchID.HasValue) { batchExp = batchExp.And(x => x.ExaminationBatchID == examinationBatchID); } if (examinationTypeID.HasValue) { projectExp = projectExp.And(x => x.ExaminationTypeID == examinationTypeID); } if (examinationProjectID.HasValue) { projectExp = projectExp.And(x => x.ExaminationProjectID == examinationProjectID); } if (schoolAreaID.HasValue) { exp = exp.And(x => x.SchoolAreaID == schoolAreaID); } if (trainingClassIDList.Count > 0) { exp = exp.And(x => trainingClassIDList.Contains(x.TrainingClassID)); } var query = TrainingClassDAL.GetTrainingClassExportViewQueryable(exp, batchExp, projectExp); if (!string.IsNullOrEmpty(conditionView.ConditionValue) && !string.IsNullOrEmpty(conditionView.Attribute)) { query = query.DynamicWhere(conditionView.Attribute, conditionView.Condition, conditionView.ConditionValue); } return query.OrderByDescending(x => x.SchoolyearCode).ThenByDescending(x => x.ExaminationBatchCreateTime) .ThenBy(x => x.ExaminationTypeName) .ThenBy(x => x.ExaminationProjectName).ThenBy(x => x.Name) .ToList(); } public void GenerateTrainingClass(Guid examinationBatchID, Guid? examinationProjectID, int? schoolAreaID) { Expression> batchExp = (x => x.ExaminationBatchID == examinationBatchID); Expression> projectExp = (x => true); Expression> gradeExp = (x => true); if (examinationProjectID.HasValue) { projectExp = projectExp.And(x => x.ExaminationProjectID == examinationProjectID); } if (schoolAreaID.HasValue) { gradeExp = gradeExp.And(x => x.SchoolAreaID == schoolAreaID); } var classStudentList = TrainingClassDAL.GetGenerateTrainingClassViewByRegistration(batchExp, projectExp, gradeExp).ToList(); var trainingClassList = (from student in classStudentList group student by new { student.SchoolyearID, student.SchoolyearCode, student.ExaminationBatchProjectID, student.ExaminationBatchID, student.ExaminationBatchName, student.ExaminationProjectID, student.ExaminationProjectName, student.SchoolAreaID, student.SchoolAreaName } into g select new { ExaminationBatchProjectID = g.Key.ExaminationBatchProjectID, SchoolAreaID = g.Key.SchoolAreaID, Name = g.Key.SchoolyearCode + g.Key.ExaminationBatchName + g.Key.ExaminationProjectName + "(" + g.Key.SchoolAreaName + ")培训班" }).ToList(); var allNameList = trainingClassList.Select(x => x.Name).Distinct().ToList(); //检查重复名称 Expression> nameCondition = (x => false); allNameList.ForEach(name => nameCondition = nameCondition.Or(x => x.Name.StartsWith(name))); var dupTrainingClassList = TrainingClassDAL.TrainingClassRepository.GetList(nameCondition).ToList(); var insertList = new List(); var insertStudentList = new List(); trainingClassList.ForEach(x => { var trainingClass = new EM_TrainingClass { TrainingClassID = Guid.NewGuid(), ExaminationBatchProjectID = x.ExaminationBatchProjectID, SchoolAreaID = x.SchoolAreaID, Name = x.Name }; var dupCount = dupTrainingClassList.Where(w => w.Name.StartsWith(x.Name)).Count(); if (dupCount > 0) { trainingClass.Name += "-" + dupCount.ToString(); } this.SetNewStatus(trainingClass); var studentIDList = (from student in classStudentList where student.ExaminationBatchProjectID == trainingClass.ExaminationBatchProjectID && student.SchoolAreaID == trainingClass.SchoolAreaID select new Relation2KeyTable { Key1 = trainingClass.TrainingClassID, Key2 = student.UserID }).ToList(); insertList.Add(trainingClass); insertStudentList.AddRange(studentIDList); }); using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable })) { UnitOfWork.BulkInsert(insertList); UnitOfWork.BulkInsert(insertStudentList); scope.Complete(); } } public IGridResultSet GetTrainingClassStudentViewGrid(ConfiguretView conditionView, Guid trainingClassID, int? schoolAreaID, Guid? collegeID, int? year, int? standardID, Guid? classmajorID, string name, string idNumber, int? pageIndex, int? pageSize) { Expression> exp = (x => x.TrainingClassID == trainingClassID); Expression> facultExp = (x => true); Expression> grademajorExp = (x => true); Expression> classmajorExp = (x => true); Expression> userExp = (x => true); Expression> studentExp = (x => true); if (schoolAreaID.HasValue) { grademajorExp = grademajorExp.And(x => x.SchoolAreaID == schoolAreaID); } if (collegeID.HasValue) { facultExp = facultExp.And(x => x.CollegeID == collegeID); } if (year.HasValue) { grademajorExp = grademajorExp.And(x => x.SchoolyearID == year); } if (standardID.HasValue) { facultExp = facultExp.And(x => x.StandardID == standardID); } if (classmajorID.HasValue) { classmajorExp = classmajorExp.And(x => x.ClassmajorID == classmajorID); } if (!string.IsNullOrEmpty(name)) { userExp = userExp.And(x => x.Name.Contains(name)); } if (!string.IsNullOrEmpty(idNumber)) { studentExp = studentExp.And(x => x.IDNumber.Contains(idNumber)); } var query = TrainingClassDAL.GetTrainingClassStudentViewQueryable(exp, facultExp, grademajorExp, classmajorExp, userExp, studentExp); if (!string.IsNullOrEmpty(conditionView.ConditionValue) && !string.IsNullOrEmpty(conditionView.Attribute)) { query = query.DynamicWhere(conditionView.Attribute, conditionView.Condition, conditionView.ConditionValue); } return query.OrderBy(x => x.ClassmajorName).ThenBy(x => x.UserName).ToGridResultSet(pageIndex, pageSize); } public TrainingClassSplitView GetTrainingClassSplitView(Guid trainingClassID) { Expression> exp = (x => x.TrainingClassID == trainingClassID); var trainingClassView = TrainingClassDAL.GetTrainingClassSplitViewQueryable(exp).FirstOrDefault(); if (trainingClassView != null) { var result = new TrainingClassSplitView { TrainingClassID = trainingClassView.TrainingClassID, Name = trainingClassView.Name, StudentCount = trainingClassView.StudentCount, ClassmajorCount = trainingClassView.ClassmajorCount }; return result; } else { return null; } } public void Split(Guid trainingClassID, string newName, IList userIDList) { var mainTrainingClass = TrainingClassDAL.TrainingClassRepository.GetSingle(x => x.TrainingClassID == trainingClassID, (x => x.CF_Student)); if (mainTrainingClass == null) { throw new Exception("找不到对应的培训班,请检查输入是否正确。"); } var newTrainingClass = new EM_TrainingClass { TrainingClassID = Guid.NewGuid(), ExaminationBatchProjectID = mainTrainingClass.ExaminationBatchProjectID, SchoolAreaID = mainTrainingClass.SchoolAreaID, Name = newName }; this.SetNewStatus(newTrainingClass); newTrainingClass.CF_Student = new HashSet(mainTrainingClass.CF_Student.Where(x => userIDList.Contains(x.UserID)).ToList()); var insertList = new List() { newTrainingClass }; var mainClassStudentList = new HashSet(mainTrainingClass.CF_Student); mainClassStudentList.RemoveWhere(x => userIDList.Contains(x.UserID)); if (mainClassStudentList.Count == 0) { throw new Exception("分班功能不允许清空原班的所有学生。"); } using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable })) { this.UnitOfWork.BulkInsert(insertList); this.UnitOfWork.BulkInsert(insertList, (x => x.CF_Student)); this.UnitOfWork.Delete(mainTrainingClass, (x => x.CF_Student)); mainTrainingClass.CF_Student = mainClassStudentList; this.UnitOfWork.BulkInsert(new List { mainTrainingClass }, (x => x.CF_Student)); scope.Complete(); } } private void CheckMergeClass(List trainingClassList) { if (trainingClassList.Count < 2) { throw new Exception("请选择至少两个培训班进行合班操作。"); } var distinctBatchProjectList = trainingClassList.GroupBy(x => new { x.ExaminationBatchProjectID, x.SchoolAreaID }).Select(x => x.Key).ToList(); if (distinctBatchProjectList.Count > 1) { throw new Exception("合班操作只能针对同一批次,同一项目,同一校区的培训班。"); } } public string GetMergeName(IList trainingClassIDList) { var trainingClassList = this.TrainingClassDAL.TrainingClassRepository.GetList(x => trainingClassIDList.Contains(x.TrainingClassID)).OrderBy(x => x.Name).ToList(); CheckMergeClass(trainingClassList); return trainingClassList.First().Name; } public void Merge(TrainingClassMergeView trainingClassMergeView) { var trainingClassList = this.TrainingClassDAL.TrainingClassRepository.GetList(x => trainingClassMergeView.TrainingClassIDList.Contains(x.TrainingClassID), (x => x.CF_Student)).ToList(); CheckMergeClass(trainingClassList); var trainingClassTemp = trainingClassList.First(); var insertList = new List(); var allStudentList = trainingClassList.SelectMany(x => x.CF_Student).GroupBy(x => x.UserID).Select(x => x.FirstOrDefault()).ToList(); var newTrainingClass = new EM_TrainingClass { TrainingClassID = Guid.NewGuid(), ExaminationBatchProjectID = trainingClassTemp.ExaminationBatchProjectID, SchoolAreaID = trainingClassTemp.SchoolAreaID, Name = trainingClassMergeView.Name, CF_Student = new HashSet(allStudentList) }; this.SetNewStatus(newTrainingClass); insertList.Add(newTrainingClass); using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable })) { this.UnitOfWork.Delete(trainingClassList, (x => x.CF_Student)); this.UnitOfWork.Delete(trainingClassList); this.UnitOfWork.BulkInsert(insertList); this.UnitOfWork.BulkInsert(insertList, (x => x.CF_Student)); scope.Complete(); } } public void AddStudent(Guid trainingClassID, IList userIDList) { var trainingClass = this.TrainingClassDAL.TrainingClassRepository.GetSingle(x => x.TrainingClassID == trainingClassID, (x => x.CF_Student)); var trainingClassUserIDList = trainingClass.CF_Student.Select(x => x.UserID).ToList(); userIDList = userIDList.Where(x => !trainingClassUserIDList.Contains(x)).ToList(); List trainingClassStudentIDList = userIDList.Select(x => new Relation2KeyTable { Key1 = trainingClassID, Key2 = x }).ToList(); using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable })) { this.UnitOfWork.BulkInsert(trainingClassStudentIDList); scope.Complete(); } } public void DelStudent(Guid trainingClassID, IList userIDList) { var trainingClass = this.TrainingClassDAL.TrainingClassRepository.GetSingle(x => x.TrainingClassID == trainingClassID, (x => x.CF_Student)); trainingClass.CF_Student = new HashSet(trainingClass.CF_Student.Where(x => !userIDList.Contains(x.UserID)).ToList()); this.UnitOfWork.Commit(); } public List GetTrainingClassStudentIDList(IList trainingClassIDList) { Expression> exp = (x => trainingClassIDList.Contains(x.TrainingClassID)); var trainingClassList = TrainingClassDAL.TrainingClassRepository.GetList(exp, (x => x.CF_Student)).ToList(); if (trainingClassList.Count == 0) { return new List(); } return trainingClassList.SelectMany(x => x.CF_Student).Select(x => x.UserID).Distinct().ToList(); } public TrainingClassView GetTrainingClassView(Guid? trainingClassID) { Expression> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); Expression> batchExp = (x => true); Expression> projectExp = (x => true); exp = exp.And(x => x.TrainingClassID == trainingClassID); var query = TrainingClassDAL.GetTrainingClassViewQueryable(exp, batchExp, projectExp); return query.FirstOrDefault(); } public void Edit(TrainingClassView trainingClassView) { try { var trainingClass = TrainingClassDAL.TrainingClassRepository.Entities.Where(x => x.TrainingClassID == trainingClassView.TrainingClassID).FirstOrDefault(); trainingClass.Name = trainingClassView.Name; UnitOfWork.Commit(); } catch (Exception) { throw; } } } }