using System; using System.Collections.Generic; using System.Linq; using System.Text; using EMIS.DataLogic.DQPSystem; using Bowin.Common.Linq.Entity; using EMIS.ViewModel.DQPSystem; using EMIS.ViewModel; using System.Linq.Expressions; using EMIS.Entities; using Bowin.Common.Linq; using EMIS.ViewModel.Students; namespace EMIS.CommonLogic.DQPSystem { public class SOCDetailGroupServices : BaseServices, ISOCDetailGroupServices { public SOCDAL SOCDAL { get; set; } public SOCDetailGroupDAL SOCDetailGroupDAL { get; set; } public IGridResultSet GetSOCDetailViewGrid(ConfiguretView configuretView, Guid? schoolyearID, Guid? collegeID, int? schoolyearNumID, int? standardID, Guid? coursematerialID, int pageIndex, int pageSize) { Expression> exp = x => true; var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; exp = exp.And(x => x.DQP_SOC.DQP_SOCStaff.Any(y => y.UserID == curUser.UserID)); exp = exp.And(x => x.IsGroup == true); if (schoolyearID.HasValue) { exp = exp.And(x => x.DQP_SOC.SchoolyearID == schoolyearID); } if (collegeID.HasValue) { //exp = exp.And(x => x.DQP_SOC == collegeID); } if (coursematerialID.HasValue) { exp = exp.And(x => x.DQP_SOC.CoursematerialID == coursematerialID); } if (!curUser.IsStudent) { exp = exp.And(x => x.CreateUserID == curUser.UserID); } var query = SOCDAL.GetSOCDetailViewQueryable(exp); //查询条件 if (!string.IsNullOrEmpty(configuretView.ConditionValue)) { query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim()); } var result = query .OrderBy(x => x.SOCID) .ThenBy(x => x.Name.Length).ThenBy(x => x.Name) .ToGridResultSet(pageIndex, pageSize); return result; } public IGridResultSet GetSOCDetailGroupViewGrid(ConfiguretView configuretView, Guid? detailID, int pageIndex, int pageSize) { Expression> exp = x => true; if (detailID.HasValue) { exp = exp.And(x => x.SOCDetailID == detailID); } var query = SOCDetailGroupDAL.GetSOCDetailGroupViewQueryable(exp); return query.OrderBy(x => x.No).ToGridResultSet(pageIndex, pageSize); } public List GetSOCDetailGroupViewList(ConfiguretView configuretView, Guid? detailID) { Expression> exp = x => true; if (detailID.HasValue) { exp = exp.And(x => x.SOCDetailID == detailID); } var query = SOCDetailGroupDAL.GetSOCDetailGroupViewQueryable(exp); return query.OrderBy(x => x.No).ToList(); } public GroupSOCDetailView GetGroupSOCDetailViewGrid(Guid? detailID) { Expression> exp = x => true; if (detailID.HasValue) { exp = exp.And(x => x.SOCDetailID == detailID); } var query = SOCDetailGroupDAL.GetGroupSOCDetailViewQueryable(exp); return query.FirstOrDefault(); } public void GroupSOCDetailSave(GroupSOCDetailView groupSOCDetailView) { try { var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; Expression> exp = x => true; if (groupSOCDetailView != null) { List SOCDetailGroupList = new List(); exp = exp.And(x => x.DQP_SOCDetail.Any(y => y.SOCDetailID == groupSOCDetailView.SOCDetailID)); var dbSOCDetailGroupList = SOCDetailGroupDAL.SOCDetailGroupRepository.GetList(x => x.SOCDetailID == groupSOCDetailView.SOCDetailID).ToList() ; //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(); var stuIDList = SOCDetailGroupDAL.GetStudentListForGroup(groupSOCDetailView.SOCDetailID).Select(x => x.UserID).ToList(); var stuList = SOCDetailGroupDAL.studentRepository.GetList(x => stuIDList.Contains(x.UserID), x => x.Sys_User).ToList(); var stuOrderLoginID = stuList.OrderBy(x => x.ClassmajorID).ThenBy(x => x.Sys_User.LoginID).ToList(); var stuCount = stuList.Count(); if (!groupSOCDetailView.IsManual) { //不是手动分组的,删掉原来的再重新分组 //UnitOfWork.Delete(x => x.SOCDetailID == groupSOCDetailView.SOCDetailID); var socDetailGroupList = SOCDetailGroupDAL.SOCDetailGroupRepository.GetList(x => x.SOCDetailID == groupSOCDetailView.SOCDetailID, x => x.CF_Student); //删除学生 foreach (var socdetailGroup in socDetailGroupList) { socdetailGroup.CF_Student = new HashSet(); } UnitOfWork.Remove(x => x.SOCDetailID == groupSOCDetailView.SOCDetailID); UnitOfWork.Commit(); if (groupSOCDetailView.GroupByGroup) { var groupnum = groupSOCDetailView.GroupNum; var other = stuCount % groupnum; var stunum = stuCount / groupnum; for (var i = 0; i < groupnum; i++) { DQP_SOCDetailGroup SOCDetailGroup = new DQP_SOCDetailGroup(); SOCDetailGroup.SOCDetailGroupID = Guid.NewGuid(); SOCDetailGroup.SOCDetailID = groupSOCDetailView.SOCDetailID; SOCDetailGroup.No = (i + 1); this.SetNewStatus(SOCDetailGroup); if (groupSOCDetailView.IsStudentNo) { if (other != 0 && (i + 1) == groupnum) { SOCDetailGroup.CF_Student = new HashSet(stuOrderLoginID.Take((stunum + other) ?? 0).ToList()); } else { SOCDetailGroup.CF_Student = new HashSet(stuOrderLoginID.Take(stunum ?? 0).ToList()); stuOrderLoginID = stuOrderLoginID.Skip(stunum ?? 0).ToList(); } } else if (groupSOCDetailView.IsRandom) { if (other != 0 && (i + 1) == groupnum) { SOCDetailGroup.CF_Student = new HashSet(stuList.Take((stunum + other) ?? 0).ToList()); } else { SOCDetailGroup.CF_Student = new HashSet(stuList.Take(stunum ?? 0).ToList()); stuList = stuList.Skip(stunum ?? 0).ToList(); } } SOCDetailGroupList.Add(SOCDetailGroup); } } else if (groupSOCDetailView.GroupByStudent) { var stuNum = groupSOCDetailView.StudentNum; var other = stuCount % stuNum; var groupNum = stuCount / stuNum; for (var i = 0; i < groupNum; i++) { DQP_SOCDetailGroup SOCDetailGroup = new DQP_SOCDetailGroup(); SOCDetailGroup.SOCDetailGroupID = Guid.NewGuid(); SOCDetailGroup.SOCDetailID = groupSOCDetailView.SOCDetailID; SOCDetailGroup.No = (i + 1); this.SetNewStatus(SOCDetailGroup); if (groupSOCDetailView.IsStudentNo) { if (other != 0 && (i + 1) == groupNum) { SOCDetailGroup.CF_Student = new HashSet(stuOrderLoginID.Take((stuNum + other) ?? 0).ToList()); } else { SOCDetailGroup.CF_Student = new HashSet(stuOrderLoginID.Take(stuNum ?? 0).ToList()); stuOrderLoginID = stuOrderLoginID.Skip(stuNum ?? 0).ToList(); } } else if (groupSOCDetailView.IsRandom) { if (other != 0 && (i + 1) == groupNum) { SOCDetailGroup.CF_Student = new HashSet(stuList.Take((stuNum + other) ?? 0).ToList()); } else { SOCDetailGroup.CF_Student = new HashSet(stuList.Take(stuNum ?? 0).ToList()); stuList = stuList.Skip(stuNum ?? 0).ToList(); } } SOCDetailGroupList.Add(SOCDetailGroup); } } } else if (groupSOCDetailView.IsManual) { var dbnum = dbSOCDetailGroupList.Max(x => x.No) == null ? 0 : dbSOCDetailGroupList.Max(x => x.No); var GroupNo = dbnum + 1; var groupNum = groupSOCDetailView.GroupNum; for (var i = 0; i < groupNum; i++) { DQP_SOCDetailGroup SOCDetailGroup = new DQP_SOCDetailGroup(); SOCDetailGroup.SOCDetailGroupID = Guid.NewGuid(); SOCDetailGroup.SOCDetailID = groupSOCDetailView.SOCDetailID; SOCDetailGroup.No = GroupNo; this.SetNewStatus(SOCDetailGroup); SOCDetailGroupList.Add(SOCDetailGroup); GroupNo++; } } UnitOfWork.BulkInsert(SOCDetailGroupList); UnitOfWork.BulkInsert(SOCDetailGroupList, x => x.CF_Student); } } catch (Exception) { throw; } } public SOCDetailGroupView GetSOCDetailGroupView(Guid? SOCDetailGroupID) { var socDetailGroupView = SOCDetailGroupDAL.GetSOCDetailGroupViewQueryable(x => x.SOCDetailGroupID == SOCDetailGroupID).FirstOrDefault(); return socDetailGroupView; } public IGridResultSet GetSOCDetailGroupStudent(Guid? SOCDetailGrpuoID) { return SOCDetailGroupDAL.GetSOCDetailGroupViewStudentList(SOCDetailGrpuoID).OrderBy(x => x.LoginID).ToGridResultSet(); } public void SOCDetailGroupStudentAdd(Guid? SOCDetailGroupID, List listBaseStudentView) { if (SOCDetailGroupID.HasValue) { if (listBaseStudentView.Count > 0) { var SOCDetailGroup = SOCDetailGroupDAL.SOCDetailGroupRepository.GetSingle(x => x.SOCDetailGroupID == SOCDetailGroupID, x => x.CF_Student); var StuIDList = listBaseStudentView.Select(x => x.UserID).ToList(); var studentList = SOCDetailGroupDAL.studentRepository.GetList(x => StuIDList.Contains(x.UserID)).ToList(); if (SOCDetailGroup != null) { SOCDetailGroup.CF_Student = new HashSet(); foreach (var student in studentList) { SOCDetailGroup.CF_Student.Add(student); } } } else { var SOCDetailGroup = SOCDetailGroupDAL.SOCDetailGroupRepository.GetSingle(x => x.SOCDetailGroupID == SOCDetailGroupID, x => x.CF_Student); SOCDetailGroup.CF_Student = new HashSet(); } UnitOfWork.Commit(); } } public IGridResultSet GetNotGroupStudent(ConfiguretView configuretView, Guid? SOCDetailGrpuoID) { var query = SOCDetailGroupDAL.GetNotGroupStudentList(SOCDetailGrpuoID); //查询条件 if (!string.IsNullOrEmpty(configuretView.ConditionValue)) { query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim()); } return query.OrderBy(x => x.LoginID).ToGridResultSet(); } public void SOCDetailGroupDelete(List idList) { try { if (idList.Count > 0) { var socDetailGroupList = SOCDetailGroupDAL.SOCDetailGroupRepository.GetList(x => idList.Contains(x.SOCDetailGroupID), x => x.CF_Student); //删除学生 foreach (var socdetailGroup in socDetailGroupList) { socdetailGroup.CF_Student = new HashSet(); } UnitOfWork.Remove(x => idList.Contains(x.SOCDetailGroupID)); UnitOfWork.Commit(); } } catch (Exception ex) { throw new Exception(ex.Message); } } public List GetUserIDByGroupID(List groupIDList) { var groupList = SOCDetailGroupDAL.SOCDetailGroupRepository.GetList(x => groupIDList.Contains(x.SOCDetailGroupID), (x => x.CF_Student)); List userIDList = new List(); foreach (var group in groupList) { foreach (var stu in group.CF_Student) { userIDList.Add(stu.UserID); } } return userIDList.Distinct().ToList(); } } }