TrainingClassServices.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Transactions;
  7. using Bowin.Common.Linq;
  8. using Bowin.Common.Linq.Entity;
  9. using EMIS.DataLogic;
  10. using EMIS.DataLogic.EducationManage;
  11. using EMIS.Entities;
  12. using EMIS.ViewModel;
  13. using EMIS.ViewModel.EducationManage;
  14. namespace EMIS.CommonLogic.EducationManage
  15. {
  16. public class TrainingClassServices : BaseServices, ITrainingClassServices
  17. {
  18. public TrainingClassDAL TrainingClassDAL { get; set; }
  19. public IGridResultSet<TrainingClassView> GetTrainingClassViewGrid(ConfiguretView conditionView, Guid? schoolyearID, Guid? examinationBatchID,
  20. Guid? examinationTypeID, Guid? examinationProjectID, int? schoolAreaID, int? pageIndex, int? pageSize)
  21. {
  22. Expression<Func<EM_TrainingClass, bool>> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  23. Expression<Func<EX_ExaminationBatch, bool>> batchExp = (x => true);
  24. Expression<Func<EX_ExaminationProject, bool>> projectExp = (x => true);
  25. if (schoolyearID.HasValue)
  26. {
  27. batchExp = batchExp.And(x => x.SchoolyearID == schoolyearID);
  28. }
  29. if (examinationBatchID.HasValue)
  30. {
  31. batchExp = batchExp.And(x => x.ExaminationBatchID == examinationBatchID);
  32. }
  33. if (examinationTypeID.HasValue)
  34. {
  35. projectExp = projectExp.And(x => x.ExaminationTypeID == examinationTypeID);
  36. }
  37. if (examinationProjectID.HasValue)
  38. {
  39. projectExp = projectExp.And(x => x.ExaminationProjectID == examinationProjectID);
  40. }
  41. if (schoolAreaID.HasValue)
  42. {
  43. exp = exp.And(x => x.SchoolAreaID == schoolAreaID);
  44. }
  45. var query = TrainingClassDAL.GetTrainingClassViewQueryable(exp, batchExp, projectExp);
  46. if (!string.IsNullOrEmpty(conditionView.ConditionValue) && !string.IsNullOrEmpty(conditionView.Attribute))
  47. {
  48. query = query.DynamicWhere(conditionView.Attribute, conditionView.Condition, conditionView.ConditionValue);
  49. }
  50. return query.OrderByDescending(x => x.SchoolyearCode).ThenByDescending(x => x.ExaminationBatchCreateTime)
  51. .ThenBy(x => x.ExaminationTypeName)
  52. .ThenBy(x => x.ExaminationProjectName).ThenBy(x => x.Name)
  53. .ToGridResultSet(pageIndex, pageSize);
  54. }
  55. public List<TrainingClassExportView> GetTrainingClassExportViewList(ConfiguretView conditionView, Guid? schoolyearID, Guid? examinationBatchID, Guid? examinationTypeID,
  56. Guid? examinationProjectID, int? schoolAreaID, IList<Guid> trainingClassIDList)
  57. {
  58. Expression<Func<EM_TrainingClass, bool>> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  59. Expression<Func<EX_ExaminationBatch, bool>> batchExp = (x => true);
  60. Expression<Func<EX_ExaminationProject, bool>> projectExp = (x => true);
  61. if (schoolyearID.HasValue)
  62. {
  63. batchExp = batchExp.And(x => x.SchoolyearID == schoolyearID);
  64. }
  65. if (examinationBatchID.HasValue)
  66. {
  67. batchExp = batchExp.And(x => x.ExaminationBatchID == examinationBatchID);
  68. }
  69. if (examinationTypeID.HasValue)
  70. {
  71. projectExp = projectExp.And(x => x.ExaminationTypeID == examinationTypeID);
  72. }
  73. if (examinationProjectID.HasValue)
  74. {
  75. projectExp = projectExp.And(x => x.ExaminationProjectID == examinationProjectID);
  76. }
  77. if (schoolAreaID.HasValue)
  78. {
  79. exp = exp.And(x => x.SchoolAreaID == schoolAreaID);
  80. }
  81. if (trainingClassIDList.Count > 0)
  82. {
  83. exp = exp.And(x => trainingClassIDList.Contains(x.TrainingClassID));
  84. }
  85. var query = TrainingClassDAL.GetTrainingClassExportViewQueryable(exp, batchExp, projectExp);
  86. if (!string.IsNullOrEmpty(conditionView.ConditionValue) && !string.IsNullOrEmpty(conditionView.Attribute))
  87. {
  88. query = query.DynamicWhere(conditionView.Attribute, conditionView.Condition, conditionView.ConditionValue);
  89. }
  90. return query.OrderByDescending(x => x.SchoolyearCode).ThenByDescending(x => x.ExaminationBatchCreateTime)
  91. .ThenBy(x => x.ExaminationTypeName)
  92. .ThenBy(x => x.ExaminationProjectName).ThenBy(x => x.Name)
  93. .ToList();
  94. }
  95. public void GenerateTrainingClass(Guid examinationBatchID, Guid? examinationProjectID, int? schoolAreaID)
  96. {
  97. Expression<Func<EX_ExaminationBatch, bool>> batchExp = (x => x.ExaminationBatchID == examinationBatchID);
  98. Expression<Func<EX_ExaminationProject, bool>> projectExp = (x => true);
  99. Expression<Func<CF_Grademajor, bool>> gradeExp = (x => true);
  100. if (examinationProjectID.HasValue)
  101. {
  102. projectExp = projectExp.And(x => x.ExaminationProjectID == examinationProjectID);
  103. }
  104. if (schoolAreaID.HasValue)
  105. {
  106. gradeExp = gradeExp.And(x => x.SchoolAreaID == schoolAreaID);
  107. }
  108. var classStudentList = TrainingClassDAL.GetGenerateTrainingClassViewByRegistration(batchExp, projectExp, gradeExp).ToList();
  109. var trainingClassList = (from student in classStudentList
  110. group student by new {
  111. student.SchoolyearID,
  112. student.SchoolyearCode,
  113. student.ExaminationBatchProjectID,
  114. student.ExaminationBatchID,
  115. student.ExaminationBatchName,
  116. student.ExaminationProjectID,
  117. student.ExaminationProjectName,
  118. student.SchoolAreaID,
  119. student.SchoolAreaName
  120. } into g
  121. select new
  122. {
  123. ExaminationBatchProjectID = g.Key.ExaminationBatchProjectID,
  124. SchoolAreaID = g.Key.SchoolAreaID,
  125. Name = g.Key.SchoolyearCode + g.Key.ExaminationBatchName + g.Key.ExaminationProjectName + "(" + g.Key.SchoolAreaName + ")培训班"
  126. }).ToList();
  127. var allNameList = trainingClassList.Select(x => x.Name).Distinct().ToList();
  128. //检查重复名称
  129. Expression<Func<EM_TrainingClass, bool>> nameCondition = (x => false);
  130. allNameList.ForEach(name => nameCondition = nameCondition.Or(x => x.Name.StartsWith(name)));
  131. var dupTrainingClassList = TrainingClassDAL.TrainingClassRepository.GetList(nameCondition).ToList();
  132. var insertList = new List<EM_TrainingClass>();
  133. var insertStudentList = new List<Relation2KeyTable>();
  134. trainingClassList.ForEach(x => {
  135. var trainingClass = new EM_TrainingClass
  136. {
  137. TrainingClassID = Guid.NewGuid(),
  138. ExaminationBatchProjectID = x.ExaminationBatchProjectID,
  139. SchoolAreaID = x.SchoolAreaID,
  140. Name = x.Name
  141. };
  142. var dupCount = dupTrainingClassList.Where(w => w.Name.StartsWith(x.Name)).Count();
  143. if (dupCount > 0)
  144. {
  145. trainingClass.Name += "-" + dupCount.ToString();
  146. }
  147. this.SetNewStatus(trainingClass);
  148. var studentIDList = (from student in classStudentList
  149. where student.ExaminationBatchProjectID == trainingClass.ExaminationBatchProjectID
  150. && student.SchoolAreaID == trainingClass.SchoolAreaID
  151. select new Relation2KeyTable { Key1 = trainingClass.TrainingClassID, Key2 = student.UserID }).ToList();
  152. insertList.Add(trainingClass);
  153. insertStudentList.AddRange(studentIDList);
  154. });
  155. using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
  156. {
  157. UnitOfWork.BulkInsert(insertList);
  158. UnitOfWork.BulkInsert<EM_TrainingClass, CF_Student>(insertStudentList);
  159. scope.Complete();
  160. }
  161. }
  162. public IGridResultSet<TrainingClassStudentView> GetTrainingClassStudentViewGrid(ConfiguretView conditionView, Guid trainingClassID,
  163. int? schoolAreaID, Guid? collegeID, int? year, int? standardID, Guid? classmajorID, string name, string idNumber, int? pageIndex, int? pageSize)
  164. {
  165. Expression<Func<EM_TrainingClass, bool>> exp = (x => x.TrainingClassID == trainingClassID);
  166. Expression<Func<CF_Facultymajor, bool>> facultExp = (x => true);
  167. Expression<Func<CF_Grademajor, bool>> grademajorExp = (x => true);
  168. Expression<Func<CF_Classmajor, bool>> classmajorExp = (x => true);
  169. Expression<Func<Sys_User, bool>> userExp = (x => true);
  170. Expression<Func<CF_Student, bool>> studentExp = (x => true);
  171. if (schoolAreaID.HasValue)
  172. {
  173. grademajorExp = grademajorExp.And(x => x.SchoolAreaID == schoolAreaID);
  174. }
  175. if (collegeID.HasValue)
  176. {
  177. facultExp = facultExp.And(x => x.CollegeID == collegeID);
  178. }
  179. if (year.HasValue)
  180. {
  181. grademajorExp = grademajorExp.And(x => x.SchoolyearID == year);
  182. }
  183. if (standardID.HasValue)
  184. {
  185. facultExp = facultExp.And(x => x.StandardID == standardID);
  186. }
  187. if (classmajorID.HasValue)
  188. {
  189. classmajorExp = classmajorExp.And(x => x.ClassmajorID == classmajorID);
  190. }
  191. if (!string.IsNullOrEmpty(name))
  192. {
  193. userExp = userExp.And(x => x.Name.Contains(name));
  194. }
  195. if (!string.IsNullOrEmpty(idNumber))
  196. {
  197. studentExp = studentExp.And(x => x.IDNumber.Contains(idNumber));
  198. }
  199. var query = TrainingClassDAL.GetTrainingClassStudentViewQueryable(exp, facultExp, grademajorExp, classmajorExp, userExp, studentExp);
  200. if (!string.IsNullOrEmpty(conditionView.ConditionValue) && !string.IsNullOrEmpty(conditionView.Attribute))
  201. {
  202. query = query.DynamicWhere(conditionView.Attribute, conditionView.Condition, conditionView.ConditionValue);
  203. }
  204. return query.OrderBy(x => x.ClassmajorName).ThenBy(x => x.UserName).ToGridResultSet(pageIndex, pageSize);
  205. }
  206. public TrainingClassSplitView GetTrainingClassSplitView(Guid trainingClassID)
  207. {
  208. Expression<Func<EM_TrainingClass, bool>> exp = (x => x.TrainingClassID == trainingClassID);
  209. var trainingClassView = TrainingClassDAL.GetTrainingClassSplitViewQueryable(exp).FirstOrDefault();
  210. if (trainingClassView != null)
  211. {
  212. var result = new TrainingClassSplitView
  213. {
  214. TrainingClassID = trainingClassView.TrainingClassID,
  215. Name = trainingClassView.Name,
  216. StudentCount = trainingClassView.StudentCount,
  217. ClassmajorCount = trainingClassView.ClassmajorCount
  218. };
  219. return result;
  220. }
  221. else
  222. {
  223. return null;
  224. }
  225. }
  226. public void Split(Guid trainingClassID, string newName, IList<Guid> userIDList)
  227. {
  228. var mainTrainingClass = TrainingClassDAL.TrainingClassRepository.GetSingle(x => x.TrainingClassID == trainingClassID,
  229. (x => x.CF_Student));
  230. if (mainTrainingClass == null)
  231. {
  232. throw new Exception("找不到对应的培训班,请检查输入是否正确。");
  233. }
  234. var newTrainingClass = new EM_TrainingClass
  235. {
  236. TrainingClassID = Guid.NewGuid(),
  237. ExaminationBatchProjectID = mainTrainingClass.ExaminationBatchProjectID,
  238. SchoolAreaID = mainTrainingClass.SchoolAreaID,
  239. Name = newName
  240. };
  241. this.SetNewStatus(newTrainingClass);
  242. newTrainingClass.CF_Student = new HashSet<CF_Student>(mainTrainingClass.CF_Student.Where(x => userIDList.Contains(x.UserID)).ToList());
  243. var insertList = new List<EM_TrainingClass>() { newTrainingClass };
  244. var mainClassStudentList = new HashSet<CF_Student>(mainTrainingClass.CF_Student);
  245. mainClassStudentList.RemoveWhere(x => userIDList.Contains(x.UserID));
  246. if (mainClassStudentList.Count == 0)
  247. {
  248. throw new Exception("分班功能不允许清空原班的所有学生。");
  249. }
  250. using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
  251. {
  252. this.UnitOfWork.BulkInsert(insertList);
  253. this.UnitOfWork.BulkInsert(insertList, (x => x.CF_Student));
  254. this.UnitOfWork.Delete(mainTrainingClass, (x => x.CF_Student));
  255. mainTrainingClass.CF_Student = mainClassStudentList;
  256. this.UnitOfWork.BulkInsert(new List<EM_TrainingClass> { mainTrainingClass }, (x => x.CF_Student));
  257. scope.Complete();
  258. }
  259. }
  260. private void CheckMergeClass(List<EM_TrainingClass> trainingClassList)
  261. {
  262. if (trainingClassList.Count < 2)
  263. {
  264. throw new Exception("请选择至少两个培训班进行合班操作。");
  265. }
  266. var distinctBatchProjectList = trainingClassList.GroupBy(x => new { x.ExaminationBatchProjectID, x.SchoolAreaID }).Select(x => x.Key).ToList();
  267. if (distinctBatchProjectList.Count > 1)
  268. {
  269. throw new Exception("合班操作只能针对同一批次,同一项目,同一校区的培训班。");
  270. }
  271. }
  272. public string GetMergeName(IList<Guid> trainingClassIDList)
  273. {
  274. var trainingClassList = this.TrainingClassDAL.TrainingClassRepository.GetList(x => trainingClassIDList.Contains(x.TrainingClassID)).OrderBy(x => x.Name).ToList();
  275. CheckMergeClass(trainingClassList);
  276. return trainingClassList.First().Name;
  277. }
  278. public void Merge(TrainingClassMergeView trainingClassMergeView)
  279. {
  280. var trainingClassList = this.TrainingClassDAL.TrainingClassRepository.GetList(x => trainingClassMergeView.TrainingClassIDList.Contains(x.TrainingClassID),
  281. (x => x.CF_Student)).ToList();
  282. CheckMergeClass(trainingClassList);
  283. var trainingClassTemp = trainingClassList.First();
  284. var insertList = new List<EM_TrainingClass>();
  285. var allStudentList = trainingClassList.SelectMany(x => x.CF_Student).GroupBy(x => x.UserID).Select(x => x.FirstOrDefault()).ToList();
  286. var newTrainingClass = new EM_TrainingClass
  287. {
  288. TrainingClassID = Guid.NewGuid(),
  289. ExaminationBatchProjectID = trainingClassTemp.ExaminationBatchProjectID,
  290. SchoolAreaID = trainingClassTemp.SchoolAreaID,
  291. Name = trainingClassMergeView.Name,
  292. CF_Student = new HashSet<CF_Student>(allStudentList)
  293. };
  294. this.SetNewStatus(newTrainingClass);
  295. insertList.Add(newTrainingClass);
  296. using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
  297. {
  298. this.UnitOfWork.Delete(trainingClassList, (x => x.CF_Student));
  299. this.UnitOfWork.Delete(trainingClassList);
  300. this.UnitOfWork.BulkInsert(insertList);
  301. this.UnitOfWork.BulkInsert(insertList, (x => x.CF_Student));
  302. scope.Complete();
  303. }
  304. }
  305. public void AddStudent(Guid trainingClassID, IList<Guid> userIDList)
  306. {
  307. var trainingClass = this.TrainingClassDAL.TrainingClassRepository.GetSingle(x => x.TrainingClassID == trainingClassID,
  308. (x => x.CF_Student));
  309. var trainingClassUserIDList = trainingClass.CF_Student.Select(x => x.UserID).ToList();
  310. userIDList = userIDList.Where(x => !trainingClassUserIDList.Contains(x)).ToList();
  311. List<Relation2KeyTable> trainingClassStudentIDList = userIDList.Select(x => new Relation2KeyTable { Key1 = trainingClassID, Key2 = x }).ToList();
  312. using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
  313. {
  314. this.UnitOfWork.BulkInsert<EM_TrainingClass, CF_Student>(trainingClassStudentIDList);
  315. scope.Complete();
  316. }
  317. }
  318. public void DelStudent(Guid trainingClassID, IList<Guid> userIDList)
  319. {
  320. var trainingClass = this.TrainingClassDAL.TrainingClassRepository.GetSingle(x => x.TrainingClassID == trainingClassID,
  321. (x => x.CF_Student));
  322. trainingClass.CF_Student = new HashSet<CF_Student>(trainingClass.CF_Student.Where(x => !userIDList.Contains(x.UserID)).ToList());
  323. this.UnitOfWork.Commit();
  324. }
  325. public List<Guid> GetTrainingClassStudentIDList(IList<Guid> trainingClassIDList)
  326. {
  327. Expression<Func<EM_TrainingClass, bool>> exp = (x => trainingClassIDList.Contains(x.TrainingClassID));
  328. var trainingClassList = TrainingClassDAL.TrainingClassRepository.GetList(exp, (x => x.CF_Student)).ToList();
  329. if (trainingClassList.Count == 0)
  330. {
  331. return new List<Guid>();
  332. }
  333. return trainingClassList.SelectMany(x => x.CF_Student).Select(x => x.UserID).Distinct().ToList();
  334. }
  335. public TrainingClassView GetTrainingClassView(Guid? trainingClassID)
  336. {
  337. Expression<Func<EM_TrainingClass, bool>> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  338. Expression<Func<EX_ExaminationBatch, bool>> batchExp = (x => true);
  339. Expression<Func<EX_ExaminationProject, bool>> projectExp = (x => true);
  340. exp = exp.And(x => x.TrainingClassID == trainingClassID);
  341. var query = TrainingClassDAL.GetTrainingClassViewQueryable(exp, batchExp, projectExp);
  342. return query.FirstOrDefault();
  343. }
  344. public void Edit(TrainingClassView trainingClassView)
  345. {
  346. try
  347. {
  348. var trainingClass = TrainingClassDAL.TrainingClassRepository.Entities.Where(x => x.TrainingClassID == trainingClassView.TrainingClassID).FirstOrDefault();
  349. trainingClass.Name = trainingClassView.Name;
  350. UnitOfWork.Commit();
  351. }
  352. catch (Exception)
  353. {
  354. throw;
  355. }
  356. }
  357. }
  358. }