123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- 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<TrainingClassView> GetTrainingClassViewGrid(ConfiguretView conditionView, Guid? schoolyearID, Guid? examinationBatchID,
- Guid? examinationTypeID, Guid? examinationProjectID, int? schoolAreaID, int? pageIndex, int? pageSize)
- {
- Expression<Func<EM_TrainingClass, bool>> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
- Expression<Func<EX_ExaminationBatch, bool>> batchExp = (x => true);
- Expression<Func<EX_ExaminationProject, bool>> 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<TrainingClassExportView> GetTrainingClassExportViewList(ConfiguretView conditionView, Guid? schoolyearID, Guid? examinationBatchID, Guid? examinationTypeID,
- Guid? examinationProjectID, int? schoolAreaID, IList<Guid> trainingClassIDList)
- {
- Expression<Func<EM_TrainingClass, bool>> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
- Expression<Func<EX_ExaminationBatch, bool>> batchExp = (x => true);
- Expression<Func<EX_ExaminationProject, bool>> 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<Func<EX_ExaminationBatch, bool>> batchExp = (x => x.ExaminationBatchID == examinationBatchID);
- Expression<Func<EX_ExaminationProject, bool>> projectExp = (x => true);
- Expression<Func<CF_Grademajor, bool>> 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<Func<EM_TrainingClass, bool>> 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<EM_TrainingClass>();
- var insertStudentList = new List<Relation2KeyTable>();
- 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<EM_TrainingClass, CF_Student>(insertStudentList);
- scope.Complete();
- }
- }
- public IGridResultSet<TrainingClassStudentView> GetTrainingClassStudentViewGrid(ConfiguretView conditionView, Guid trainingClassID,
- int? schoolAreaID, Guid? collegeID, int? year, int? standardID, Guid? classmajorID, string name, string idNumber, int? pageIndex, int? pageSize)
- {
- Expression<Func<EM_TrainingClass, bool>> exp = (x => x.TrainingClassID == trainingClassID);
- Expression<Func<CF_Facultymajor, bool>> facultExp = (x => true);
- Expression<Func<CF_Grademajor, bool>> grademajorExp = (x => true);
- Expression<Func<CF_Classmajor, bool>> classmajorExp = (x => true);
- Expression<Func<Sys_User, bool>> userExp = (x => true);
- Expression<Func<CF_Student, bool>> 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<Func<EM_TrainingClass, bool>> 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<Guid> 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<CF_Student>(mainTrainingClass.CF_Student.Where(x => userIDList.Contains(x.UserID)).ToList());
- var insertList = new List<EM_TrainingClass>() { newTrainingClass };
- var mainClassStudentList = new HashSet<CF_Student>(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<EM_TrainingClass> { mainTrainingClass }, (x => x.CF_Student));
- scope.Complete();
- }
- }
- private void CheckMergeClass(List<EM_TrainingClass> 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<Guid> 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<EM_TrainingClass>();
- 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<CF_Student>(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<Guid> 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<Relation2KeyTable> 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<EM_TrainingClass, CF_Student>(trainingClassStudentIDList);
- scope.Complete();
- }
- }
- public void DelStudent(Guid trainingClassID, IList<Guid> userIDList)
- {
- var trainingClass = this.TrainingClassDAL.TrainingClassRepository.GetSingle(x => x.TrainingClassID == trainingClassID,
- (x => x.CF_Student));
- trainingClass.CF_Student = new HashSet<CF_Student>(trainingClass.CF_Student.Where(x => !userIDList.Contains(x.UserID)).ToList());
- this.UnitOfWork.Commit();
- }
- public List<Guid> GetTrainingClassStudentIDList(IList<Guid> trainingClassIDList)
- {
- Expression<Func<EM_TrainingClass, bool>> exp = (x => trainingClassIDList.Contains(x.TrainingClassID));
- var trainingClassList = TrainingClassDAL.TrainingClassRepository.GetList(exp, (x => x.CF_Student)).ToList();
- if (trainingClassList.Count == 0)
- {
- return new List<Guid>();
- }
- return trainingClassList.SelectMany(x => x.CF_Student).Select(x => x.UserID).Distinct().ToList();
- }
- public TrainingClassView GetTrainingClassView(Guid? trainingClassID)
- {
- Expression<Func<EM_TrainingClass, bool>> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
- Expression<Func<EX_ExaminationBatch, bool>> batchExp = (x => true);
- Expression<Func<EX_ExaminationProject, bool>> 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;
- }
- }
- }
- }
|