using System; using System.Collections.Generic; using System.Linq; using System.Text; using Bowin.Common; using Bowin.Common.Linq; using Bowin.Common.Linq.Entity; using EMIS.DataLogic.SystemDAL; using EMIS.ViewModel.SystemView; using EMIS.ViewModel; using EMIS.Entities; using System.Linq.Expressions; using EMIS.DataLogic.Repositories; using EMIS.Utility; using System.Web; namespace EMIS.CommonLogic.SystemServices { public class RoleServices : BaseServices, IRoleServices { public Lazy RoleDAL { get; set; } public Lazy UserDAL { get; set; } public Lazy DataRangeDAL { get; set; } public RoleRepository roleRepository { get; set; } /// /// 教师角色列表(分页) /// /// 实体 /// 当前页码 /// 显示页码 /// public Bowin.Common.Linq.Entity.IGridResultSet GetRoleViewList(ConfiguretView roleView, int? dataRange, int pageIndex, int pageSize) { Expression> exp = (x => x.TypeID == (int)SYS_RoleType.Teacher); if (dataRange.HasValue) { exp = exp.And(x => x.DefaultDataRange == dataRange.Value); } if (!string.IsNullOrEmpty(roleView.ConditionValue) && !string.IsNullOrEmpty(roleView.Attribute)) return RoleDAL.Value.GetRoleView(exp).DynamicWhere(roleView.Attribute, roleView.Condition, roleView.ConditionValue) .ToGridResultSet(pageIndex, pageSize); return RoleDAL.Value.GetRoleView(exp).OrderBy(x => x.OrderNo).ThenBy(x => x.RoleName).ToGridResultSet(pageIndex, pageSize); } /// /// 教师角色列表(不分页) /// /// 实体 /// 当前页码 /// 显示页码 /// public IList GetRoleViewList(ConfiguretView roleView, int? dataRange) { Expression> exp = (x => x.TypeID == (int)SYS_RoleType.Teacher); if (dataRange.HasValue) { exp = exp.And(x => x.DefaultDataRange == dataRange.Value); } if (!string.IsNullOrEmpty(roleView.ConditionValue) && !string.IsNullOrEmpty(roleView.Attribute)) return RoleDAL.Value.GetRoleView(exp).DynamicWhere(roleView.Attribute, roleView.Condition, roleView.ConditionValue).ToList(); return RoleDAL.Value.GetRoleView(exp).OrderBy(x => x.OrderNo).ThenBy(x => x.RoleName).ToList(); } /// /// 获取教师角色列表 /// /// 实体 /// 当前页码 /// 显示页码 /// public IList GetEnabledTeacherRoleViewList() { return RoleDAL.Value.GetRoleView(x => x.TypeID == (int)SYS_RoleType.Teacher && x.RecordStatus > (int)SYS_STATUS.UNUSABLE) .OrderBy(x => x.OrderNo).ThenBy(x => x.RoleName).ToList(); } /// /// 学生角色列表 /// /// /// /// public Bowin.Common.Linq.Entity.IGridResultSet GetStudentRoleViewList(int pageIndex, int pageSize) { Expression> exp = (x => x.TypeID == (int)SYS_RoleType.Student); return RoleDAL.Value.GetRoleView(exp).OrderBy(x => x.OrderNo).ThenBy(x => x.RoleName).ToGridResultSet(pageIndex, pageSize); } /// /// 学生角色列表 /// /// public IList GetStudentRoleViewList() { Expression> exp = (x => x.TypeID == (int)SYS_RoleType.Student); return RoleDAL.Value.GetRoleView(exp).OrderBy(x => x.OrderNo).ThenBy(x => x.RoleName).ToList(); } /// /// 角色列表 /// /// 实体 /// 当前页码 /// 显示页码 /// public IList GetEnabledRoleViewList() { return RoleDAL.Value.GetRoleView(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE) .OrderBy(x => x.TypeID).ThenBy(x => x.OrderNo).ToList(); } /// /// 根据ID获取角色详细信息 /// /// 角色ID /// public RoleView GetRoleViewInfo(Guid? roleID) { return RoleDAL.Value.GetRoleView(x => x.RoleID == roleID).FirstOrDefault(); } /// /// 新增角色 /// /// 角色实体 /// public void Save(RoleView role) { //var roleEntity = role.ParseToEntity(); var roleEntity = RoleDAL.Value.RoleRepository.GetSingle(x => x.RoleID == role.RoleID, x => x.CF_StudentRole, x => x.Sys_User); var OnlyOne = true; if (roleEntity != null) { if (role.OrderNo != null) { if (role.OrderNo < 0) { throw new Exception("序号不能为负数!"); } roleEntity.OrderNo = role.OrderNo; } else { roleEntity.OrderNo = 1; } if (roleRepository.Entities.Any(x => x.RoleName == role.RoleName.Trim() && x.RoleID != role.RoleID)) { OnlyOne = false; throw new Exception("已存在相同的类型名称。"); } roleEntity.RoleName = role.RoleName; if (roleEntity.CF_StudentRole == null) { roleEntity.CF_StudentRole = new CF_StudentRole(); roleEntity.CF_StudentRole.RoleID = role.RoleID; this.SetNewStatus(roleEntity.CF_StudentRole); } else { this.SetModifyStatus(roleEntity.CF_StudentRole); } roleEntity.CF_StudentRole.StudentType = role.StudentType; roleEntity.IsSystemRole = false; roleEntity.DefaultDataRange = role.DefaultDataRange; roleEntity.Description = role.Description; this.SetModifyStatus(roleEntity); } else { if (roleRepository.Entities.Any(x=>x.RoleName==role.RoleName.Trim())) { OnlyOne = false; throw new Exception("已存在相同的类型名称请勿重复添加。"); } roleEntity = role.ParseToEntity(); roleEntity.OrderNo = role.OrderNo; roleEntity.RoleID = Guid.NewGuid(); this.SetNewStatus(roleEntity); roleEntity.IsSystemRole = false; UnitOfWork.Add(roleEntity); } if(OnlyOne==true){ UnitOfWork.Commit(); } if (roleEntity.CF_StudentRole.StudentType.HasValue) { this.SetStudentRoles(roleEntity); } } private void SetStudentRoles(Sys_Role roleEntity) { var userList = UserDAL.Value.UserRepository.GetList(x => x.CF_Student.StudentType == roleEntity.CF_StudentRole.StudentType).ToList(); //roleEntity.Sys_User = new HashSet(); //UnitOfWork.Commit(); roleEntity.Sys_User = new HashSet(userList); this.UnitOfWork.Delete(roleEntity, (x => x.Sys_User)); this.UnitOfWork.BulkInsert(new List() { roleEntity }, (x => x.Sys_User)); } /// /// 删除角色 /// /// 主键ID /// public void Delete(IList roleID) { if (roleID.Count > 0) { UnitOfWork.Delete(x => roleID.Contains(x.RoleID)); } } public IList GetRoleDataRange(Guid roleID) { return DataRangeDAL.Value.GetRoleDataRange(x => x.RoleID == roleID).ToList(); } public void SaveDataRange(Guid roleID, IList dataRangeList) { var role = RoleDAL.Value.RoleRepository.GetSingle(x => x.RoleID == roleID, (x => x.Sys_RoleDataRange)); role.Sys_RoleDataRange = new HashSet(); dataRangeList.ToList().ForEach(x => { var dataRange = new Sys_RoleDataRange(); dataRange.RoleDataRangeID = Guid.NewGuid(); if (x.MenuNo == null) throw new Exception("MenuNo为必填项。"); dataRange.MenuNo = x.MenuNo; if (!x.RoleID.HasValue) throw new Exception("RoleID为必填项。"); dataRange.RoleID = x.RoleID.Value; if (!x.DataRangeID.HasValue) throw new Exception("DataRangeID为必填项。"); dataRange.DataRangeID = x.DataRangeID.Value; this.SetNewStatus(dataRange); role.Sys_RoleDataRange.Add(dataRange); }); this.UnitOfWork.Commit(); } public void StudentVerification(Guid roleID, int studentTypeID, string roleName) { var sameStudentType = RoleDAL.Value.RoleRepository.GetSingle(x => x.CF_StudentRole.StudentType == studentTypeID && x.RoleID != roleID); if (sameStudentType != null) { throw new Exception("已存在相同学生类别的用户类型,不能添加。"); } var sameName = RoleDAL.Value.RoleRepository.GetSingle(x => x.RoleName == roleName && x.RoleID != roleID); if (sameName != null) { throw new Exception("已存在相同名称的用户类型,不能添加。"); } } public int GetDataRange() { var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; var curUserID = curUser.UserID; if (curUser.IsStudent) { return 6; } var menuNo = HttpHelper.GetMenuNo(); if (string.IsNullOrEmpty(menuNo)) { menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU"); } SYS_DataRange dataRange = DataRangeDAL.Value.GetRoleDataRange(curUser.RoleID, menuNo); switch (dataRange) { case SYS_DataRange.All: default: return (int)SYS_DataRange.All; case SYS_DataRange.Campus: return (int)SYS_DataRange.Campus; case SYS_DataRange.College: return (int)SYS_DataRange.College; case SYS_DataRange.Department: return (int)SYS_DataRange.Department; case SYS_DataRange.Teacher: return (int)SYS_DataRange.Teacher; case SYS_DataRange.Assistant: return (int)SYS_DataRange.Assistant; } } public Guid? GetLoginCollegeID(int DataRange, Guid UserID) { if (DataRange == (int)SYS_DataRange.College || DataRange == (int)SYS_DataRange.Department || DataRange == (int)SYS_DataRange.Teacher) { var CollegeID = DataRangeDAL.Value.StaffManageCollegeRepository.Entities.Where(x => x.UserID == UserID).Select(w => w.CollegeID).FirstOrDefault(); return CollegeID; } else { return null; } } public Guid? GetLoginClassmajorID(int DataRange, Guid UserID) { if (DataRange == (int)SYS_DataRange.Assistant) { var ClassmajorID = DataRangeDAL.Value.ClassmajorRepository.Entities.Where(x => x.UserID == UserID).Select(w => w.ClassmajorID).FirstOrDefault(); return ClassmajorID; } else { return null; } } public Guid? GetLoginCampusID(int DataRange,Guid UserID) { if (DataRange == (int)SYS_DataRange.Campus) { var CampusID = DataRangeDAL.Value.StaffManageCampusRepository.Entities.Where(x => x.UserID == UserID).Select(w => w.CampusID).FirstOrDefault(); return CampusID; } else { return null; } } } }