123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- 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> RoleDAL { get; set; }
- public Lazy<UserDAL> UserDAL { get; set; }
- public Lazy<DataRangeDAL> DataRangeDAL { get; set; }
- public RoleRepository roleRepository { get; set; }
- /// <summary>
- /// 教师角色列表(分页)
- /// </summary>
- /// <param name="roleView">实体</param>
- /// <param name="pageIndex">当前页码</param>
- /// <param name="pageSize">显示页码</param>
- /// <returns></returns>
- public Bowin.Common.Linq.Entity.IGridResultSet<RoleView> GetRoleViewList(ConfiguretView roleView, int? dataRange, int pageIndex, int pageSize)
- {
- Expression<Func<Sys_Role, bool>> 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<RoleView>(pageIndex, pageSize);
- return RoleDAL.Value.GetRoleView(exp).OrderBy(x => x.OrderNo).ThenBy(x => x.RoleName).ToGridResultSet<RoleView>(pageIndex, pageSize);
- }
- /// <summary>
- /// 教师角色列表(不分页)
- /// </summary>
- /// <param name="roleView">实体</param>
- /// <param name="pageIndex">当前页码</param>
- /// <param name="pageSize">显示页码</param>
- /// <returns></returns>
- public IList<RoleView> GetRoleViewList(ConfiguretView roleView, int? dataRange)
- {
- Expression<Func<Sys_Role, bool>> 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();
- }
- /// <summary>
- /// 获取教师角色列表
- /// </summary>
- /// <param name="roleView">实体</param>
- /// <param name="pageIndex">当前页码</param>
- /// <param name="pageSize">显示页码</param>
- /// <returns></returns>
- public IList<RoleView> 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();
- }
- /// <summary>
- /// 学生角色列表
- /// </summary>
- /// <param name="pageIndex"></param>
- /// <param name="pageSize"></param>
- /// <returns></returns>
- public Bowin.Common.Linq.Entity.IGridResultSet<RoleView> GetStudentRoleViewList(int pageIndex, int pageSize)
- {
- Expression<Func<Sys_Role, bool>> exp = (x => x.TypeID == (int)SYS_RoleType.Student);
- return RoleDAL.Value.GetRoleView(exp).OrderBy(x => x.OrderNo).ThenBy(x => x.RoleName).ToGridResultSet<RoleView>(pageIndex, pageSize);
- }
-
- /// <summary>
- /// 学生角色列表
- /// </summary>
- /// <returns></returns>
- public IList<RoleView> GetStudentRoleViewList()
- {
- Expression<Func<Sys_Role, bool>> exp = (x => x.TypeID == (int)SYS_RoleType.Student);
- return RoleDAL.Value.GetRoleView(exp).OrderBy(x => x.OrderNo).ThenBy(x => x.RoleName).ToList();
- }
-
- /// <summary>
- /// 角色列表
- /// </summary>
- /// <param name="roleView">实体</param>
- /// <param name="pageIndex">当前页码</param>
- /// <param name="pageSize">显示页码</param>
- /// <returns></returns>
- public IList<RoleView> GetEnabledRoleViewList()
- {
- return RoleDAL.Value.GetRoleView(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE)
- .OrderBy(x => x.TypeID).ThenBy(x => x.OrderNo).ToList();
- }
- /// <summary>
- /// 根据ID获取角色详细信息
- /// </summary>
- /// <param name="roleID">角色ID</param>
- /// <returns></returns>
- public RoleView GetRoleViewInfo(Guid? roleID)
- {
- return RoleDAL.Value.GetRoleView(x => x.RoleID == roleID).FirstOrDefault();
- }
- /// <summary>
- /// 新增角色
- /// </summary>
- /// <param name="role">角色实体</param>
- /// <returns></returns>
- 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<Sys_User>();
- //UnitOfWork.Commit();
- roleEntity.Sys_User = new HashSet<Sys_User>(userList);
- this.UnitOfWork.Delete(roleEntity, (x => x.Sys_User));
- this.UnitOfWork.BulkInsert(new List<Sys_Role>() { roleEntity }, (x => x.Sys_User));
- }
- /// <summary>
- /// 删除角色
- /// </summary>
- /// <param name="roleID">主键ID</param>
- /// <returns></returns>
- public void Delete(IList<Guid?> roleID)
- {
- if (roleID.Count > 0)
- {
- UnitOfWork.Delete<Sys_Role>(x => roleID.Contains(x.RoleID));
- }
- }
- public IList<RoleDataRangeView> GetRoleDataRange(Guid roleID)
- {
- return DataRangeDAL.Value.GetRoleDataRange(x => x.RoleID == roleID).ToList();
- }
- public void SaveDataRange(Guid roleID, IList<RoleDataRangeView> dataRangeList)
- {
- var role = RoleDAL.Value.RoleRepository.GetSingle(x => x.RoleID == roleID, (x => x.Sys_RoleDataRange));
- role.Sys_RoleDataRange = new HashSet<Sys_RoleDataRange>();
- 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;
- }
- }
- }
- }
|