SOCDetailGroupServices.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using EMIS.DataLogic.DQPSystem;
  6. using Bowin.Common.Linq.Entity;
  7. using EMIS.ViewModel.DQPSystem;
  8. using EMIS.ViewModel;
  9. using System.Linq.Expressions;
  10. using EMIS.Entities;
  11. using Bowin.Common.Linq;
  12. using EMIS.ViewModel.Students;
  13. namespace EMIS.CommonLogic.DQPSystem
  14. {
  15. public class SOCDetailGroupServices : BaseServices, ISOCDetailGroupServices
  16. {
  17. public SOCDAL SOCDAL { get; set; }
  18. public SOCDetailGroupDAL SOCDetailGroupDAL { get; set; }
  19. public IGridResultSet<SOCDetailView> GetSOCDetailViewGrid(ConfiguretView configuretView, Guid? schoolyearID, Guid? collegeID, int? schoolyearNumID, int? standardID, Guid? coursematerialID, int pageIndex, int pageSize)
  20. {
  21. Expression<Func<DQP_SOCDetail, bool>> exp = x => true;
  22. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  23. exp = exp.And(x => x.DQP_SOC.DQP_SOCStaff.Any(y => y.UserID == curUser.UserID));
  24. exp = exp.And(x => x.IsGroup == true);
  25. if (schoolyearID.HasValue)
  26. {
  27. exp = exp.And(x => x.DQP_SOC.SchoolyearID == schoolyearID);
  28. }
  29. if (collegeID.HasValue)
  30. {
  31. //exp = exp.And(x => x.DQP_SOC == collegeID);
  32. }
  33. if (coursematerialID.HasValue)
  34. {
  35. exp = exp.And(x => x.DQP_SOC.CoursematerialID == coursematerialID);
  36. }
  37. if (!curUser.IsStudent)
  38. {
  39. exp = exp.And(x => x.CreateUserID == curUser.UserID);
  40. }
  41. var query = SOCDAL.GetSOCDetailViewQueryable(exp);
  42. //查询条件
  43. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  44. {
  45. query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim());
  46. }
  47. var result = query
  48. .OrderBy(x => x.SOCID)
  49. .ThenBy(x => x.Name.Length).ThenBy(x => x.Name)
  50. .ToGridResultSet<SOCDetailView>(pageIndex, pageSize);
  51. return result;
  52. }
  53. public IGridResultSet<SOCDetailGroupView> GetSOCDetailGroupViewGrid(ConfiguretView configuretView, Guid? detailID, int pageIndex, int pageSize)
  54. {
  55. Expression<Func<DQP_SOCDetailGroup, bool>> exp = x => true;
  56. if (detailID.HasValue)
  57. {
  58. exp = exp.And(x => x.SOCDetailID == detailID);
  59. }
  60. var query = SOCDetailGroupDAL.GetSOCDetailGroupViewQueryable(exp);
  61. return query.OrderBy(x => x.No).ToGridResultSet<SOCDetailGroupView>(pageIndex, pageSize);
  62. }
  63. public List<SOCDetailGroupView> GetSOCDetailGroupViewList(ConfiguretView configuretView, Guid? detailID)
  64. {
  65. Expression<Func<DQP_SOCDetailGroup, bool>> exp = x => true;
  66. if (detailID.HasValue)
  67. {
  68. exp = exp.And(x => x.SOCDetailID == detailID);
  69. }
  70. var query = SOCDetailGroupDAL.GetSOCDetailGroupViewQueryable(exp);
  71. return query.OrderBy(x => x.No).ToList();
  72. }
  73. public GroupSOCDetailView GetGroupSOCDetailViewGrid(Guid? detailID)
  74. {
  75. Expression<Func<DQP_SOCDetail, bool>> exp = x => true;
  76. if (detailID.HasValue)
  77. {
  78. exp = exp.And(x => x.SOCDetailID == detailID);
  79. }
  80. var query = SOCDetailGroupDAL.GetGroupSOCDetailViewQueryable(exp);
  81. return query.FirstOrDefault();
  82. }
  83. public void GroupSOCDetailSave(GroupSOCDetailView groupSOCDetailView)
  84. {
  85. try
  86. {
  87. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  88. Expression<Func<DQP_SOC, bool>> exp = x => true;
  89. if (groupSOCDetailView != null)
  90. {
  91. List<DQP_SOCDetailGroup> SOCDetailGroupList = new List<DQP_SOCDetailGroup>();
  92. exp = exp.And(x => x.DQP_SOCDetail.Any(y => y.SOCDetailID == groupSOCDetailView.SOCDetailID));
  93. var dbSOCDetailGroupList = SOCDetailGroupDAL.SOCDetailGroupRepository.GetList(x => x.SOCDetailID == groupSOCDetailView.SOCDetailID).ToList() ;
  94. //var query = SOCDetailGroupDAL.SOCRepository.GetList(exp, (x => x.DQP_SOCStaff.FirstOrDefault(y => y.UserID == curUser.UserID).CF_Student), (x => x.DQP_SOCStaff.FirstOrDefault(y => y.UserID == curUser.UserID).CF_Student.Select(w => w.Sys_User))).ToList();
  95. var stuIDList = SOCDetailGroupDAL.GetStudentListForGroup(groupSOCDetailView.SOCDetailID).Select(x => x.UserID).ToList();
  96. var stuList = SOCDetailGroupDAL.studentRepository.GetList(x => stuIDList.Contains(x.UserID), x => x.Sys_User).ToList();
  97. var stuOrderLoginID = stuList.OrderBy(x => x.ClassmajorID).ThenBy(x => x.Sys_User.LoginID).ToList();
  98. var stuCount = stuList.Count();
  99. if (!groupSOCDetailView.IsManual)
  100. {
  101. //不是手动分组的,删掉原来的再重新分组
  102. //UnitOfWork.Delete<DQP_SOCDetailGroup>(x => x.SOCDetailID == groupSOCDetailView.SOCDetailID);
  103. var socDetailGroupList = SOCDetailGroupDAL.SOCDetailGroupRepository.GetList(x => x.SOCDetailID == groupSOCDetailView.SOCDetailID, x => x.CF_Student);
  104. //删除学生
  105. foreach (var socdetailGroup in socDetailGroupList)
  106. {
  107. socdetailGroup.CF_Student = new HashSet<CF_Student>();
  108. }
  109. UnitOfWork.Remove<DQP_SOCDetailGroup>(x => x.SOCDetailID == groupSOCDetailView.SOCDetailID);
  110. UnitOfWork.Commit();
  111. if (groupSOCDetailView.GroupByGroup)
  112. {
  113. var groupnum = groupSOCDetailView.GroupNum;
  114. var other = stuCount % groupnum;
  115. var stunum = stuCount / groupnum;
  116. for (var i = 0; i < groupnum; i++)
  117. {
  118. DQP_SOCDetailGroup SOCDetailGroup = new DQP_SOCDetailGroup();
  119. SOCDetailGroup.SOCDetailGroupID = Guid.NewGuid();
  120. SOCDetailGroup.SOCDetailID = groupSOCDetailView.SOCDetailID;
  121. SOCDetailGroup.No = (i + 1);
  122. this.SetNewStatus(SOCDetailGroup);
  123. if (groupSOCDetailView.IsStudentNo)
  124. {
  125. if (other != 0 && (i + 1) == groupnum)
  126. {
  127. SOCDetailGroup.CF_Student = new HashSet<CF_Student>(stuOrderLoginID.Take((stunum + other) ?? 0).ToList());
  128. }
  129. else
  130. {
  131. SOCDetailGroup.CF_Student = new HashSet<CF_Student>(stuOrderLoginID.Take(stunum ?? 0).ToList());
  132. stuOrderLoginID = stuOrderLoginID.Skip(stunum ?? 0).ToList();
  133. }
  134. }
  135. else if (groupSOCDetailView.IsRandom)
  136. {
  137. if (other != 0 && (i + 1) == groupnum)
  138. {
  139. SOCDetailGroup.CF_Student = new HashSet<CF_Student>(stuList.Take((stunum + other) ?? 0).ToList());
  140. }
  141. else
  142. {
  143. SOCDetailGroup.CF_Student = new HashSet<CF_Student>(stuList.Take(stunum ?? 0).ToList());
  144. stuList = stuList.Skip(stunum ?? 0).ToList();
  145. }
  146. }
  147. SOCDetailGroupList.Add(SOCDetailGroup);
  148. }
  149. }
  150. else if (groupSOCDetailView.GroupByStudent)
  151. {
  152. var stuNum = groupSOCDetailView.StudentNum;
  153. var other = stuCount % stuNum;
  154. var groupNum = stuCount / stuNum;
  155. for (var i = 0; i < groupNum; i++)
  156. {
  157. DQP_SOCDetailGroup SOCDetailGroup = new DQP_SOCDetailGroup();
  158. SOCDetailGroup.SOCDetailGroupID = Guid.NewGuid();
  159. SOCDetailGroup.SOCDetailID = groupSOCDetailView.SOCDetailID;
  160. SOCDetailGroup.No = (i + 1);
  161. this.SetNewStatus(SOCDetailGroup);
  162. if (groupSOCDetailView.IsStudentNo)
  163. {
  164. if (other != 0 && (i + 1) == groupNum)
  165. {
  166. SOCDetailGroup.CF_Student = new HashSet<CF_Student>(stuOrderLoginID.Take((stuNum + other) ?? 0).ToList());
  167. }
  168. else
  169. {
  170. SOCDetailGroup.CF_Student = new HashSet<CF_Student>(stuOrderLoginID.Take(stuNum ?? 0).ToList());
  171. stuOrderLoginID = stuOrderLoginID.Skip(stuNum ?? 0).ToList();
  172. }
  173. }
  174. else if (groupSOCDetailView.IsRandom)
  175. {
  176. if (other != 0 && (i + 1) == groupNum)
  177. {
  178. SOCDetailGroup.CF_Student = new HashSet<CF_Student>(stuList.Take((stuNum + other) ?? 0).ToList());
  179. }
  180. else
  181. {
  182. SOCDetailGroup.CF_Student = new HashSet<CF_Student>(stuList.Take(stuNum ?? 0).ToList());
  183. stuList = stuList.Skip(stuNum ?? 0).ToList();
  184. }
  185. }
  186. SOCDetailGroupList.Add(SOCDetailGroup);
  187. }
  188. }
  189. }
  190. else if (groupSOCDetailView.IsManual)
  191. {
  192. var dbnum = dbSOCDetailGroupList.Max(x => x.No) == null ? 0 : dbSOCDetailGroupList.Max(x => x.No);
  193. var GroupNo = dbnum + 1;
  194. var groupNum = groupSOCDetailView.GroupNum;
  195. for (var i = 0; i < groupNum; i++)
  196. {
  197. DQP_SOCDetailGroup SOCDetailGroup = new DQP_SOCDetailGroup();
  198. SOCDetailGroup.SOCDetailGroupID = Guid.NewGuid();
  199. SOCDetailGroup.SOCDetailID = groupSOCDetailView.SOCDetailID;
  200. SOCDetailGroup.No = GroupNo;
  201. this.SetNewStatus(SOCDetailGroup);
  202. SOCDetailGroupList.Add(SOCDetailGroup);
  203. GroupNo++;
  204. }
  205. }
  206. UnitOfWork.BulkInsert(SOCDetailGroupList);
  207. UnitOfWork.BulkInsert(SOCDetailGroupList, x => x.CF_Student);
  208. }
  209. }
  210. catch (Exception)
  211. {
  212. throw;
  213. }
  214. }
  215. public SOCDetailGroupView GetSOCDetailGroupView(Guid? SOCDetailGroupID)
  216. {
  217. var socDetailGroupView = SOCDetailGroupDAL.GetSOCDetailGroupViewQueryable(x => x.SOCDetailGroupID == SOCDetailGroupID).FirstOrDefault();
  218. return socDetailGroupView;
  219. }
  220. public IGridResultSet<BaseStudentView> GetSOCDetailGroupStudent(Guid? SOCDetailGrpuoID)
  221. {
  222. return SOCDetailGroupDAL.GetSOCDetailGroupViewStudentList(SOCDetailGrpuoID).OrderBy(x => x.LoginID).ToGridResultSet<BaseStudentView>();
  223. }
  224. public void SOCDetailGroupStudentAdd(Guid? SOCDetailGroupID, List<BaseStudentView> listBaseStudentView)
  225. {
  226. if (SOCDetailGroupID.HasValue)
  227. {
  228. if (listBaseStudentView.Count > 0)
  229. {
  230. var SOCDetailGroup = SOCDetailGroupDAL.SOCDetailGroupRepository.GetSingle(x => x.SOCDetailGroupID == SOCDetailGroupID, x => x.CF_Student);
  231. var StuIDList = listBaseStudentView.Select(x => x.UserID).ToList();
  232. var studentList = SOCDetailGroupDAL.studentRepository.GetList(x => StuIDList.Contains(x.UserID)).ToList();
  233. if (SOCDetailGroup != null)
  234. {
  235. SOCDetailGroup.CF_Student = new HashSet<CF_Student>();
  236. foreach (var student in studentList)
  237. {
  238. SOCDetailGroup.CF_Student.Add(student);
  239. }
  240. }
  241. }
  242. else {
  243. var SOCDetailGroup = SOCDetailGroupDAL.SOCDetailGroupRepository.GetSingle(x => x.SOCDetailGroupID == SOCDetailGroupID, x => x.CF_Student);
  244. SOCDetailGroup.CF_Student = new HashSet<CF_Student>();
  245. }
  246. UnitOfWork.Commit();
  247. }
  248. }
  249. public IGridResultSet<BaseStudentView> GetNotGroupStudent(ConfiguretView configuretView, Guid? SOCDetailGrpuoID)
  250. {
  251. var query = SOCDetailGroupDAL.GetNotGroupStudentList(SOCDetailGrpuoID);
  252. //查询条件
  253. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  254. {
  255. query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim());
  256. }
  257. return query.OrderBy(x => x.LoginID).ToGridResultSet<BaseStudentView>();
  258. }
  259. public void SOCDetailGroupDelete(List<Guid?> idList)
  260. {
  261. try
  262. {
  263. if (idList.Count > 0)
  264. {
  265. var socDetailGroupList = SOCDetailGroupDAL.SOCDetailGroupRepository.GetList(x => idList.Contains(x.SOCDetailGroupID), x => x.CF_Student);
  266. //删除学生
  267. foreach (var socdetailGroup in socDetailGroupList)
  268. {
  269. socdetailGroup.CF_Student = new HashSet<CF_Student>();
  270. }
  271. UnitOfWork.Remove<DQP_SOCDetailGroup>(x => idList.Contains(x.SOCDetailGroupID));
  272. UnitOfWork.Commit();
  273. }
  274. }
  275. catch (Exception ex)
  276. {
  277. throw new Exception(ex.Message);
  278. }
  279. }
  280. public List<Guid?> GetUserIDByGroupID(List<Guid> groupIDList)
  281. {
  282. var groupList = SOCDetailGroupDAL.SOCDetailGroupRepository.GetList(x => groupIDList.Contains(x.SOCDetailGroupID), (x => x.CF_Student));
  283. List<Guid?> userIDList = new List<Guid?>();
  284. foreach (var group in groupList)
  285. {
  286. foreach (var stu in group.CF_Student)
  287. {
  288. userIDList.Add(stu.UserID);
  289. }
  290. }
  291. return userIDList.Distinct().ToList();
  292. }
  293. }
  294. }