using System; using System.Collections.Generic; using System.Linq; using System.Text; using EMIS.DataLogic.Repositories; using EMIS.ViewModel.TeachingMaterial; using System.Linq.Expressions; using EMIS.Entities; using EMIS.ViewModel; using EMIS.ViewModel.Students; using Bowin.Common.Linq; using EMIS.DataLogic.Common.Studentsetting; using EMIS.DataLogic.Common.Students; namespace EMIS.DataLogic.Common.TeachingMaterial { public class StudentDistributeDAL { #region --0.0 定义-- public StudentDistributeRepository StudentDistributeRepository { get; set; } public TeachingMaterialPoolRepository TeachingMaterialPoolRepository { get; set; } public SchoolyearRepository SchoolyearRepository { get; set; } public UserRepository UserRepository { get; set; } public CoursematerialRepository CoursematerialRepository { get; set; } public SpecialtyPlanRepository SpecialtyPlanRepository { get; set; } public CollegeRepository CollegeRepository { get; set; } public DictionaryItemRepository DictionaryItemRepository { get; set; } public GrademajorRepository GrademajorRepository { get; set; } public FacultymajorRepository FacultymajorRepository { get; set; } public SpecialtyCourseRepository SpecialtyCourseRepository { get; set; } public ClassmajorRepository ClassmajorRepository { get; set; } public PublishRepository PublishRepository { get; set; } public TeachingMateriaInventoryRepository TeachingMateriaInventoryRepository { get; set; } public StudentRepository StudentRepository { get; set; } public RecruitstudentsRepository RecruitstudentsRepository { get; set; } public StudentProfileRepository StudentProfileRepository { get; set; } public StudentsOrderRepository StudentsOrderRepository { get; set; } public StaffRepository StaffRepository { get; set; } public InSchoolSettingRepository inSchoolSettingRepository { get; set; } #endregion /// /// 获取学生发放信息列表 /// /// /// public IQueryable GetStudentDistributeGridView(Expression> exporder) { var teaquery = from tmi in TeachingMateriaInventoryRepository.Entities group tmi by tmi.TeachingMaterialPoolID into t select new InventoryView { TeachingMaterialPoolID = t.Key, PresentInventory = t.Sum(x => x.PresentInventory), }; var inSchoolStatusIDList = inSchoolSettingRepository.GetList(x => x.IsSelected == true).Select(w => w.InSchoolStatusID).ToList();//获取选中的在校设定状态、 var view = (from so in StudentDistributeRepository.GetList(exporder) join u in UserRepository.Entities on so.CreateUserID equals u.UserID join tmp in TeachingMaterialPoolRepository.Entities on so.TeachingMaterialPoolID equals tmp.TeachingMaterialPoolID into ttmp from ctmp in ttmp.DefaultIfEmpty() join kc in teaquery on so.TeachingMaterialPoolID equals kc.TeachingMaterialPoolID into kctmp from kcmp in kctmp.DefaultIfEmpty() join fdi in DictionaryItemRepository.Entities on new { so.EM_SpecialtyPlan.CF_Grademajor.CF_Facultymajor.StandardID, DictionaryCode = typeof(CF_Standard).Name } equals new { StandardID = fdi.Value, fdi.DictionaryCode } into tfdi from cfdi in tfdi.DefaultIfEmpty() join sct in DictionaryItemRepository.Entities on new { so.EM_SpecialtyPlan.CourseCategoryID, DictionaryCode =typeof(CF_CourseCategory).Name } equals new { CourseCategoryID = sct.Value, sct.DictionaryCode } into tsct from csct in tsct.DefaultIfEmpty() join sctt in DictionaryItemRepository.Entities on new { so.EM_SpecialtyPlan.CourseTypeID, DictionaryCode = typeof(CF_CourseType).Name } equals new { CourseTypeID = sctt.Value, sctt.DictionaryCode } into tsctt from csctt in tsctt.DefaultIfEmpty() join scq in DictionaryItemRepository.Entities on new { so.EM_SpecialtyPlan.CourseQualityID, DictionaryCode = typeof(CF_CourseQuality).Name } equals new { CourseQualityID = scq.Value, scq.DictionaryCode } into tscq from cscq in tscq.DefaultIfEmpty() join sdord in StudentsOrderRepository.Entities on new { SpecialtyPlanID = (Guid)so.SpecialtyPlanID, so.TeachingMaterialPoolID } equals new { SpecialtyPlanID = sdord.SpecialtyPlanID, sdord.TeachingMaterialPoolID } into tsdord from ctsdord in tsdord.DefaultIfEmpty() select new StudentDistributeView { GrademajorID = so.EM_SpecialtyPlan.CF_Grademajor.GrademajorID, StudentDistributeID = so.StudentDistributeID, SchoolyearID = so.EM_SpecialtyPlan.SchoolyearID, SchoolyearCode = so.EM_SpecialtyPlan.CF_Schoolyear.Code, TeachingMaterialPoolID = ctmp.TeachingMaterialPoolID, CollegeID = so.EM_SpecialtyPlan.CF_Grademajor.CF_Facultymajor.CF_College.CollegeID, CollegeName = so.EM_SpecialtyPlan.CF_Grademajor.CF_Facultymajor.CF_College.Name, CollegeNo = so.EM_SpecialtyPlan.CF_Grademajor.CF_Facultymajor.CF_College.No, GradeSpecialtyCode = so.EM_SpecialtyPlan.CF_Grademajor.Code, GradeSpecialtyName = so.EM_SpecialtyPlan.CF_Grademajor.Name, GrademajorCode = so.EM_SpecialtyPlan.CF_Grademajor.Code, GrademajorName = so.EM_SpecialtyPlan.CF_Grademajor.Name, Years = so.EM_SpecialtyPlan.CF_Grademajor.GradeID, GradeID = so.EM_SpecialtyPlan.CF_Grademajor.GradeID, StandardName = cfdi.Name, StudentQty = so.EM_SpecialtyPlan.CF_Grademajor.CF_Classmajor.Sum(w => w.CF_Student.Where(q => inSchoolStatusIDList.Contains(q.InSchoolStatusID)).Count()), OrderQty = ctsdord.OrderQty, CourseCode = so.EM_SpecialtyPlan.EM_Coursematerial.CourseCode, CourseName = so.EM_SpecialtyPlan.EM_Coursematerial.CourseName, CourseCategoryID = so.EM_SpecialtyPlan.CourseCategoryID, CourseCategoryName = csct.Name, // Years = sy.Years, CourseTypeID = so.EM_SpecialtyPlan.CourseTypeID, CourseTypeName = csctt.Name, CourseQualityID = so.EM_SpecialtyPlan.CourseQualityID, CourseQualityName = cscq.Name, CampusID = so.EM_SpecialtyPlan.CF_Grademajor.CF_Facultymajor.CF_College.CF_Campus.CampusID, StandardID = so.EM_SpecialtyPlan.CF_Grademajor.CF_Facultymajor.StandardID, SpecialtyValue = so.EM_SpecialtyPlan.CF_Grademajor.CF_Facultymajor.StandardID, CoursematerialID = so.EM_SpecialtyPlan.EM_Coursematerial.CoursematerialID, IsDistribute = (int)so.RecordStatus, IsDistributeName = so.RecordStatus == (int)CF_YesOrNoStatus.Yes ? "是" : "否", TeachingMaterialCode = ctmp.TeachingMaterialCode, TeachingMaterialName = ctmp.TeachingMaterialName, CreateTime = so.CreateTime, CreateUserName = u.Name, //Discount = kcmp.Discount == null ? 0 : (kcmp.Discount ?? 1) / (ctmp.Price ?? 1), //DiscountPrice = kcmp.Discount ?? 0, PresentInventory = kcmp.PresentInventory ?? 0, Price = ctmp.Price??0, RemainingQty = so.ET_StudentDistribute_CF_Student.Where(x => inSchoolStatusIDList.Contains(x.InSchoolStatusID)).Count(),//获取未发放表学生数 DistributeQty = so.DistributeQty - so.ET_StudentDistribute_CF_Student.Where(x => inSchoolStatusIDList.Contains(x.InSchoolStatusID)).Count(), //DistributeQty = so.EM_SpecialtyPlan.CF_Grademajor.CF_Classmajor.Sum(w => w.CF_Student.Where(q => inSchoolStatusIDList.Contains(q.InSchoolStatusID)).Count()) - so.CF_Student.Where(x => inSchoolStatusIDList.Contains(x.InSchoolStatusID)).Count(), ISBN = ctmp.ISBN, PublishTime = ctmp.PublishTime,//版本时间 Author = ctmp.Author, PublishName = ctmp.CF_Publish.UnitName, TeachingMaterialTypeID = ctmp.TeachingMaterialTypeID ?? -1 }); return view; } public IQueryable GetAllUsableStudentDistribute() { var query = from sd in StudentDistributeRepository.GetList(x => x.RecordStatus == (int)SYS_STATUS.USABLE) select new StudentDistributeView { StudentDistributeID = sd.StudentDistributeID, SpecialtyPlanID = (Guid)sd.SpecialtyPlanID, }; return query; } public IList GetStudentDistributeIDsBySpecialtyPlan(Guid schoolyearID) { var planList = (from sd in StudentDistributeRepository.Entities.Where(x => x.RecordStatus == (int)CF_YesOrNoStatus.No && x.TeachingMaterialPoolID == null) join sy in SchoolyearRepository.Entities.Where(x => x.SchoolyearID == schoolyearID) on sd.EM_SpecialtyPlan.SchoolyearID equals sy.SchoolyearID select new StudentOrderPrePlanView { SpecialtyPlanID = (Guid)sd.SpecialtyPlanID }).ToList(); //var planList = (from sp in SpecialtyPlanRepository.Entities.Where(x => x.IsNeedMaterial == true) // join sy in SchoolyearRepository.Entities.Where(x => x.SchoolyearID == schoolyearID) on sp.SchoolyearID equals sy.SchoolyearID // join so in StudentDistributeRepository.Entities.Where(x => x.RecordStatus == (int)CF_YesOrNoStatus.No) on sp.SpecialtyPlanID equals so.SpecialtyPlanID into tso // from cso in tso.DefaultIfEmpty() // select new StudentOrderPrePlanView // { // SpecialtyPlanID = sp.SpecialtyPlanID // }).ToList(); return planList; } /// /// 获取专业计划对应的需要征订的信息 /// /// /// public IQueryable GetStudentDistributeRepositoryBySpecialtyPlan(Guid schoolyearID) { var inSchoolStatusIDList = inSchoolSettingRepository.GetList(x => x.IsSelected == true).Select(w => w.InSchoolStatusID).ToList();//获取选中的在校设定状态、 //TODO:增加生成学生发放数据时、以院系所为限制条件生成对应院系数据。 //var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; Expression> exp = x => x.IsNeedMaterial == true; var planList = (from sp in SpecialtyPlanRepository.GetList(exp) join sy in SchoolyearRepository.Entities.Where(x => x.SchoolyearID == schoolyearID) on sp.SchoolyearID equals sy.SchoolyearID join gj in GrademajorRepository.Entities on sp.GrademajorID equals gj.GrademajorID join cj in ClassmajorRepository.Entities on gj.GrademajorID equals cj.GrademajorID into tcj from ccj in tcj.DefaultIfEmpty() from ctlpv in sp.EM_Coursematerial.CF_TeachingMaterialPool.DefaultIfEmpty() join so in StudentDistributeRepository.Entities on sp.SpecialtyPlanID equals so.SpecialtyPlanID into tso from cso in tso.DefaultIfEmpty() where cso == null group new { sp, ccj, ctlpv } // group new { sp, sd = ccj.CF_Student, ctlpv } by new { sp.SpecialtyPlanID, ctlpv.TeachingMaterialPoolID, sp.CF_Grademajor.CF_Facultymajor.CollegeID } into g select new StudentDistributePrePlanView { SpecialtyPlanID = g.Key.SpecialtyPlanID, OrderQty = g.Sum(x => x.ccj.CF_Student.Where(e => inSchoolStatusIDList.Contains(e.InSchoolStatusID)).Count()),//只获取在校学生 CollegeID = g.Key.CollegeID, TeachingMaterialPoolID = g.Key.TeachingMaterialPoolID }); var ss = planList; //以学生征订提交数据为基准 (废弃) //var planList = (from sp in SpecialtyPlanRepository.Entities.Where(x => x.IsNeedMaterial == true) // join sy in SchoolyearRepository.Entities.Where(x => x.SchoolyearID == schoolyearID) on sp.SchoolyearID equals sy.SchoolyearID // join sd in StudentsOrderRepository.Entities.Where(x => x.IsOrdered == true) on sp.SpecialtyPlanID equals sd.SpecialtyPlanID // join gj in GrademajorRepository.Entities on sp.GrademajorID equals gj.GrademajorID // join sc in SpecialtyCourseRepository.Entities on sp.SpecialtyCourseID equals sc.SpecialtyCourseID // join cj in ClassmajorRepository.Entities on gj.GrademajorID equals cj.GrademajorID into tcj // from ccj in tcj.DefaultIfEmpty() // group new { sd, ccj } // by new { sd.SpecialtyPlanID, sd.TeachingMaterialPoolID } into g // select new StudentDistributePrePlanView // { // SpecialtyPlanID = g.Key.SpecialtyPlanID, // OrderQty = g.Sum(x => x.ccj.ClassNum ?? 0), // TeachingMaterialPoolID = (Guid)g.Key.TeachingMaterialPoolID // }); return planList; } /// /// 根据征订ID获取发放数据 /// /// /// public IList GetStudentDistributeListByStudentDistributeIDs(List studentDistributeIDs) { return StudentDistributeRepository.GetList(x => studentDistributeIDs.Contains(x.StudentDistributeID)).ToList(); } /// /// 获取当前学年学期所有未发学生信息 /// /// /// public IList GetStudentQueryable(Expression> filter) { var query = from a in StudentDistributeRepository.GetList(filter) from c in a.ET_StudentDistribute_CF_Student.Where(x => x.InSchoolStatusID == 1) join sp in SpecialtyPlanRepository.Entities on a.SpecialtyPlanID equals sp.SpecialtyPlanID join cos in CoursematerialRepository.Entities on sp.CoursematerialID equals cos.CoursematerialID join recruitStudent in RecruitstudentsRepository.Entities on c.UserID equals recruitStudent.UserID join u in UserRepository.Entities on a.CreateUserID equals u.UserID from b in c.EM_StudentReport.AsQueryable().Where(w => w.CF_Schoolyear.IsCurrent == true).DefaultIfEmpty() //当前学年学期的报到情况 select new StudentDistribute_UserView { UserID = c.UserID, LoginID = c.Sys_User.LoginID, Name = c.Sys_User.Name, TeachingMaterialCode = a.CF_TeachingMaterialPool.TeachingMaterialCode, TeachingMaterialName = a.CF_TeachingMaterialPool.TeachingMaterialName, Remark = a.Remark, CreateTime = (DateTime)a.CreateTime, CreateUserName = u.Name, CourseName = cos.CourseName, CourseCode = cos.CourseCode }; return query.ToList(); } public StudentDistributeView GetSingleStudentDistribute(Expression> filter) { var view = from a in StudentDistributeRepository.GetList(filter) select new StudentDistributeView { IsDistribute = (int)a.RecordStatus, StudentDistributeID = a.StudentDistributeID }; return view.FirstOrDefault(); } /// /// 获取当前学年学期所有学生信息 /// /// /// public IQueryable GetStudentQueryable_User(Expression> filter) { var query = from so in StudentDistributeRepository.Entities.Where(filter) join sp in SpecialtyPlanRepository.Entities on so.SpecialtyPlanID equals sp.SpecialtyPlanID join sy in SchoolyearRepository.Entities on sp.SchoolyearID equals sy.SchoolyearID join gj in GrademajorRepository.Entities on sp.GrademajorID equals gj.GrademajorID join fj in FacultymajorRepository.Entities on gj.FacultymajorID equals fj.FacultymajorID join co in CollegeRepository.Entities on fj.CollegeID equals co.CollegeID join cos in CoursematerialRepository.Entities on sp.CoursematerialID equals cos.CoursematerialID join u in UserRepository.Entities on so.CreateUserID equals u.UserID join tmp in TeachingMaterialPoolRepository.Entities on so.TeachingMaterialPoolID equals tmp.TeachingMaterialPoolID into ttmp from ctmp in ttmp.DefaultIfEmpty() from bj in gj.CF_Classmajor from sd in bj.CF_Student.Where(x => x.InSchoolStatusID == 1) select new StudentDistribute_UserView { UserID = sd.UserID, LoginID = sd.Sys_User.LoginID, Name = sd.Sys_User.Name, Sex = sd.SexID, ClassMajorName = sd.CF_Classmajor.Name, InSchoolStatusID = sd.InSchoolStatusID, StudentStatus = sd.StudentStatus, TeachingMaterialCode = ctmp.TeachingMaterialCode, TeachingMaterialName = ctmp.TeachingMaterialName, Remark = so.Remark, CreateTime = (DateTime)so.CreateTime, CreateUserName = u.Name, CourseName = cos.CourseName, CourseCode = cos.CourseCode }; return query; } /// /// 获取已发放学生 /// /// /// public IQueryable GetStudentQueryableByStudentDistributeID(Expression> filter) { var inSchoolStatusIDList = inSchoolSettingRepository.GetList(x => x.IsSelected == true).Select(w => w.InSchoolStatusID).ToList();//获取选中的在校设定状态、 //1.0 获取未发学生 var userIdlist = (from a in StudentDistributeRepository.Entities.Where(filter) from sd in a.ET_StudentDistribute_CF_Student //.Where(x => inSchoolStatusIDList.Contains(x.InSchoolStatusID)) select new StudentDistributeView { RecipientUserID = sd.UserID }).ToList(); List userList = new List(); if (userIdlist.Count > 0) { foreach (var Id in userIdlist) { userList.Add(Id.RecipientUserID); } } //2.0 获取发放学生 var query = from a in StudentDistributeRepository.Entities.Where(filter) join tmp in TeachingMaterialPoolRepository.Entities on a.TeachingMaterialPoolID equals tmp.TeachingMaterialPoolID into ttmp from ctmp in ttmp.DefaultIfEmpty() // from sdnt in a.CF_Student //未发学生 join u in UserRepository.Entities on a.ModifyUserID equals u.UserID into tu from ctu in tu.DefaultIfEmpty() from spl in a.EM_SpecialtyPlan.CF_Grademajor.CF_Classmajor from cls in spl.CF_Student //.Where(x => inSchoolStatusIDList.Contains(x.InSchoolStatusID)) //班级对应的学生 where cls != null join tp in TeachingMateriaInventoryRepository.Entities on ctmp.TeachingMaterialPoolID equals tp.TeachingMaterialPoolID into ttp from cttp in ttp.DefaultIfEmpty() select new StudentDistributeView { TeachingMaterialCode = ctmp.TeachingMaterialCode, TeachingMaterialName = ctmp.TeachingMaterialName, StudentDistributeNo = a.StudentDistributeNo, DistributeQty = 1,//a.DistributeQty == null ? 1 : a.DistributeQty, Price = ctmp.Price, Discount = cttp.Discount, DiscountPrice = cttp.DiscountPrice, TotalPrice = ctmp.Price, //ctmp.Price * a.DistributeQty, TotalDollar = cttp.DiscountPrice, StockOutUserName = ctu.Name, CreateTime = (DateTime)a.CreateTime, ModifyTime = (DateTime)a.ModifyTime, StudentNo = cls.Sys_User.LoginID, StudentName = cls.Sys_User.Name, RecipientUser = a.RecipientUser, RecipientUserID = cls.UserID, Remark = a.Remark }; return query.Where(x => !userList.Contains(x.RecipientUserID)).Distinct(); } public IQueryable GetStudentDistributeGenerateView(Expression> exp) { var query = from order in StudentsOrderRepository.GetList(exp) join plan in SpecialtyPlanRepository.Entities on order.SpecialtyPlanID equals plan.SpecialtyPlanID join grade in ( from grade in GrademajorRepository.Entities join classmajor in ClassmajorRepository.Entities on grade.GrademajorID equals classmajor.GrademajorID join student in StudentRepository.Entities on classmajor.ClassmajorID equals student.ClassmajorID join inschool in inSchoolSettingRepository.Entities on student.InSchoolStatusID equals inschool.InSchoolStatusID where inschool.IsSelected == true group student by grade.GrademajorID into g select new { GrademajorID = g.Key, StudentCount = (int?)g.Count() } ) on plan.GrademajorID equals grade.GrademajorID join distribute in StudentDistributeRepository.Entities.Where(x => x.RecordStatus == (int)SYS_STATUS.UNUSABLE) on new { SpecialtyPlanID = order.SpecialtyPlanID, order.TeachingMaterialPoolID } equals new { SpecialtyPlanID = (Guid)distribute.SpecialtyPlanID, distribute.TeachingMaterialPoolID } into ddistribute from distribute in ddistribute.DefaultIfEmpty() join submited in StudentDistributeRepository.Entities.Where(x => x.RecordStatus == (int)SYS_STATUS.USABLE) on new { order.SpecialtyPlanID, order.TeachingMaterialPoolID } equals new { SpecialtyPlanID = (Guid)submited.SpecialtyPlanID, submited.TeachingMaterialPoolID } into dsubmited from submited in dsubmited.DefaultIfEmpty() where (grade.StudentCount ?? 0) > 0 && submited.StudentDistributeID == null select new StudentDistributeGenerateView { SpecialtyPlanID = order.SpecialtyPlanID, TeachingMaterialPoolID = order.TeachingMaterialPoolID, StudentCount = grade.StudentCount ?? 0, OldStudentDistributeID = distribute.StudentDistributeID }; return query; } public IQueryable GetInventory(Guid poolID) { var query = from tmi in TeachingMateriaInventoryRepository.GetList(x => x.TeachingMaterialPoolID == poolID && x.PresentInventory > 0) select new InventoryView { TeachingMaterialPoolID = tmi.TeachingMaterialPoolID, PresentInventory = tmi.PresentInventory, CreateTime = tmi.CreateTime, }; return query; } } }