RoleServices.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Bowin.Common;
  6. using Bowin.Common.Linq;
  7. using Bowin.Common.Linq.Entity;
  8. using EMIS.DataLogic.SystemDAL;
  9. using EMIS.ViewModel.SystemView;
  10. using EMIS.ViewModel;
  11. using EMIS.Entities;
  12. using System.Linq.Expressions;
  13. using EMIS.DataLogic.Repositories;
  14. using EMIS.Utility;
  15. using System.Web;
  16. namespace EMIS.CommonLogic.SystemServices
  17. {
  18. public class RoleServices : BaseServices, IRoleServices
  19. {
  20. public Lazy<RoleDAL> RoleDAL { get; set; }
  21. public Lazy<UserDAL> UserDAL { get; set; }
  22. public Lazy<DataRangeDAL> DataRangeDAL { get; set; }
  23. public RoleRepository roleRepository { get; set; }
  24. /// <summary>
  25. /// 教师角色列表(分页)
  26. /// </summary>
  27. /// <param name="roleView">实体</param>
  28. /// <param name="pageIndex">当前页码</param>
  29. /// <param name="pageSize">显示页码</param>
  30. /// <returns></returns>
  31. public Bowin.Common.Linq.Entity.IGridResultSet<RoleView> GetRoleViewList(ConfiguretView roleView, int? dataRange, int pageIndex, int pageSize)
  32. {
  33. Expression<Func<Sys_Role, bool>> exp = (x => x.TypeID == (int)SYS_RoleType.Teacher);
  34. if (dataRange.HasValue)
  35. {
  36. exp = exp.And(x => x.DefaultDataRange == dataRange.Value);
  37. }
  38. if (!string.IsNullOrEmpty(roleView.ConditionValue) && !string.IsNullOrEmpty(roleView.Attribute))
  39. return RoleDAL.Value.GetRoleView(exp).DynamicWhere(roleView.Attribute, roleView.Condition, roleView.ConditionValue)
  40. .ToGridResultSet<RoleView>(pageIndex, pageSize);
  41. return RoleDAL.Value.GetRoleView(exp).OrderBy(x => x.OrderNo).ThenBy(x => x.RoleName).ToGridResultSet<RoleView>(pageIndex, pageSize);
  42. }
  43. /// <summary>
  44. /// 教师角色列表(不分页)
  45. /// </summary>
  46. /// <param name="roleView">实体</param>
  47. /// <param name="pageIndex">当前页码</param>
  48. /// <param name="pageSize">显示页码</param>
  49. /// <returns></returns>
  50. public IList<RoleView> GetRoleViewList(ConfiguretView roleView, int? dataRange)
  51. {
  52. Expression<Func<Sys_Role, bool>> exp = (x => x.TypeID == (int)SYS_RoleType.Teacher);
  53. if (dataRange.HasValue)
  54. {
  55. exp = exp.And(x => x.DefaultDataRange == dataRange.Value);
  56. }
  57. if (!string.IsNullOrEmpty(roleView.ConditionValue) && !string.IsNullOrEmpty(roleView.Attribute))
  58. return RoleDAL.Value.GetRoleView(exp).DynamicWhere(roleView.Attribute, roleView.Condition, roleView.ConditionValue).ToList();
  59. return RoleDAL.Value.GetRoleView(exp).OrderBy(x => x.OrderNo).ThenBy(x => x.RoleName).ToList();
  60. }
  61. /// <summary>
  62. /// 获取教师角色列表
  63. /// </summary>
  64. /// <param name="roleView">实体</param>
  65. /// <param name="pageIndex">当前页码</param>
  66. /// <param name="pageSize">显示页码</param>
  67. /// <returns></returns>
  68. public IList<RoleView> GetEnabledTeacherRoleViewList()
  69. {
  70. return RoleDAL.Value.GetRoleView(x => x.TypeID == (int)SYS_RoleType.Teacher && x.RecordStatus > (int)SYS_STATUS.UNUSABLE)
  71. .OrderBy(x => x.OrderNo).ThenBy(x => x.RoleName).ToList();
  72. }
  73. /// <summary>
  74. /// 学生角色列表
  75. /// </summary>
  76. /// <param name="pageIndex"></param>
  77. /// <param name="pageSize"></param>
  78. /// <returns></returns>
  79. public Bowin.Common.Linq.Entity.IGridResultSet<RoleView> GetStudentRoleViewList(int pageIndex, int pageSize)
  80. {
  81. Expression<Func<Sys_Role, bool>> exp = (x => x.TypeID == (int)SYS_RoleType.Student);
  82. return RoleDAL.Value.GetRoleView(exp).OrderBy(x => x.OrderNo).ThenBy(x => x.RoleName).ToGridResultSet<RoleView>(pageIndex, pageSize);
  83. }
  84. /// <summary>
  85. /// 学生角色列表
  86. /// </summary>
  87. /// <returns></returns>
  88. public IList<RoleView> GetStudentRoleViewList()
  89. {
  90. Expression<Func<Sys_Role, bool>> exp = (x => x.TypeID == (int)SYS_RoleType.Student);
  91. return RoleDAL.Value.GetRoleView(exp).OrderBy(x => x.OrderNo).ThenBy(x => x.RoleName).ToList();
  92. }
  93. /// <summary>
  94. /// 角色列表
  95. /// </summary>
  96. /// <param name="roleView">实体</param>
  97. /// <param name="pageIndex">当前页码</param>
  98. /// <param name="pageSize">显示页码</param>
  99. /// <returns></returns>
  100. public IList<RoleView> GetEnabledRoleViewList()
  101. {
  102. return RoleDAL.Value.GetRoleView(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE)
  103. .OrderBy(x => x.TypeID).ThenBy(x => x.OrderNo).ToList();
  104. }
  105. /// <summary>
  106. /// 根据ID获取角色详细信息
  107. /// </summary>
  108. /// <param name="roleID">角色ID</param>
  109. /// <returns></returns>
  110. public RoleView GetRoleViewInfo(Guid? roleID)
  111. {
  112. return RoleDAL.Value.GetRoleView(x => x.RoleID == roleID).FirstOrDefault();
  113. }
  114. /// <summary>
  115. /// 新增角色
  116. /// </summary>
  117. /// <param name="role">角色实体</param>
  118. /// <returns></returns>
  119. public void Save(RoleView role)
  120. {
  121. //var roleEntity = role.ParseToEntity();
  122. var roleEntity = RoleDAL.Value.RoleRepository.GetSingle(x => x.RoleID == role.RoleID, x => x.CF_StudentRole, x => x.Sys_User);
  123. var OnlyOne = true;
  124. if (roleEntity != null)
  125. {
  126. if (role.OrderNo != null)
  127. {
  128. if (role.OrderNo < 0)
  129. {
  130. throw new Exception("序号不能为负数!");
  131. }
  132. roleEntity.OrderNo = role.OrderNo;
  133. }
  134. else
  135. {
  136. roleEntity.OrderNo = 1;
  137. }
  138. if (roleRepository.Entities.Any(x => x.RoleName == role.RoleName.Trim() && x.RoleID != role.RoleID))
  139. {
  140. OnlyOne = false;
  141. throw new Exception("已存在相同的类型名称。");
  142. }
  143. roleEntity.RoleName = role.RoleName;
  144. if (roleEntity.CF_StudentRole == null)
  145. {
  146. roleEntity.CF_StudentRole = new CF_StudentRole();
  147. roleEntity.CF_StudentRole.RoleID = role.RoleID;
  148. this.SetNewStatus(roleEntity.CF_StudentRole);
  149. }
  150. else
  151. {
  152. this.SetModifyStatus(roleEntity.CF_StudentRole);
  153. }
  154. roleEntity.CF_StudentRole.StudentType = role.StudentType;
  155. roleEntity.IsSystemRole = false;
  156. roleEntity.DefaultDataRange = role.DefaultDataRange;
  157. roleEntity.Description = role.Description;
  158. this.SetModifyStatus(roleEntity);
  159. }
  160. else
  161. {
  162. if (roleRepository.Entities.Any(x=>x.RoleName==role.RoleName.Trim()))
  163. {
  164. OnlyOne = false;
  165. throw new Exception("已存在相同的类型名称请勿重复添加。");
  166. }
  167. roleEntity = role.ParseToEntity();
  168. roleEntity.OrderNo = role.OrderNo;
  169. roleEntity.RoleID = Guid.NewGuid();
  170. this.SetNewStatus(roleEntity);
  171. roleEntity.IsSystemRole = false;
  172. UnitOfWork.Add(roleEntity);
  173. }
  174. if(OnlyOne==true){
  175. UnitOfWork.Commit();
  176. }
  177. if (roleEntity.CF_StudentRole.StudentType.HasValue)
  178. {
  179. this.SetStudentRoles(roleEntity);
  180. }
  181. }
  182. private void SetStudentRoles(Sys_Role roleEntity)
  183. {
  184. var userList = UserDAL.Value.UserRepository.GetList(x => x.CF_Student.StudentType == roleEntity.CF_StudentRole.StudentType).ToList();
  185. //roleEntity.Sys_User = new HashSet<Sys_User>();
  186. //UnitOfWork.Commit();
  187. roleEntity.Sys_User = new HashSet<Sys_User>(userList);
  188. this.UnitOfWork.Delete(roleEntity, (x => x.Sys_User));
  189. this.UnitOfWork.BulkInsert(new List<Sys_Role>() { roleEntity }, (x => x.Sys_User));
  190. }
  191. /// <summary>
  192. /// 删除角色
  193. /// </summary>
  194. /// <param name="roleID">主键ID</param>
  195. /// <returns></returns>
  196. public void Delete(IList<Guid?> roleID)
  197. {
  198. if (roleID.Count > 0)
  199. {
  200. UnitOfWork.Delete<Sys_Role>(x => roleID.Contains(x.RoleID));
  201. }
  202. }
  203. public IList<RoleDataRangeView> GetRoleDataRange(Guid roleID)
  204. {
  205. return DataRangeDAL.Value.GetRoleDataRange(x => x.RoleID == roleID).ToList();
  206. }
  207. public void SaveDataRange(Guid roleID, IList<RoleDataRangeView> dataRangeList)
  208. {
  209. var role = RoleDAL.Value.RoleRepository.GetSingle(x => x.RoleID == roleID, (x => x.Sys_RoleDataRange));
  210. role.Sys_RoleDataRange = new HashSet<Sys_RoleDataRange>();
  211. dataRangeList.ToList().ForEach(x =>
  212. {
  213. var dataRange = new Sys_RoleDataRange();
  214. dataRange.RoleDataRangeID = Guid.NewGuid();
  215. if (x.MenuNo == null) throw new Exception("MenuNo为必填项。");
  216. dataRange.MenuNo = x.MenuNo;
  217. if (!x.RoleID.HasValue) throw new Exception("RoleID为必填项。");
  218. dataRange.RoleID = x.RoleID.Value;
  219. if (!x.DataRangeID.HasValue) throw new Exception("DataRangeID为必填项。");
  220. dataRange.DataRangeID = x.DataRangeID.Value;
  221. this.SetNewStatus(dataRange);
  222. role.Sys_RoleDataRange.Add(dataRange);
  223. });
  224. this.UnitOfWork.Commit();
  225. }
  226. public void StudentVerification(Guid roleID, int studentTypeID, string roleName)
  227. {
  228. var sameStudentType = RoleDAL.Value.RoleRepository.GetSingle(x => x.CF_StudentRole.StudentType == studentTypeID && x.RoleID != roleID);
  229. if (sameStudentType != null)
  230. {
  231. throw new Exception("已存在相同学生类别的用户类型,不能添加。");
  232. }
  233. var sameName = RoleDAL.Value.RoleRepository.GetSingle(x => x.RoleName == roleName && x.RoleID != roleID);
  234. if (sameName != null)
  235. {
  236. throw new Exception("已存在相同名称的用户类型,不能添加。");
  237. }
  238. }
  239. public int GetDataRange()
  240. {
  241. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  242. var curUserID = curUser.UserID;
  243. if (curUser.IsStudent)
  244. {
  245. return 6;
  246. }
  247. var menuNo = HttpHelper.GetMenuNo();
  248. if (string.IsNullOrEmpty(menuNo))
  249. {
  250. menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU");
  251. }
  252. SYS_DataRange dataRange = DataRangeDAL.Value.GetRoleDataRange(curUser.RoleID, menuNo);
  253. switch (dataRange)
  254. {
  255. case SYS_DataRange.All:
  256. default:
  257. return (int)SYS_DataRange.All;
  258. case SYS_DataRange.Campus:
  259. return (int)SYS_DataRange.Campus;
  260. case SYS_DataRange.College:
  261. return (int)SYS_DataRange.College;
  262. case SYS_DataRange.Department:
  263. return (int)SYS_DataRange.Department;
  264. case SYS_DataRange.Teacher:
  265. return (int)SYS_DataRange.Teacher;
  266. case SYS_DataRange.Assistant:
  267. return (int)SYS_DataRange.Assistant;
  268. }
  269. }
  270. public Guid? GetLoginCollegeID(int DataRange, Guid UserID)
  271. {
  272. if (DataRange == (int)SYS_DataRange.College || DataRange == (int)SYS_DataRange.Department || DataRange == (int)SYS_DataRange.Teacher)
  273. {
  274. var CollegeID = DataRangeDAL.Value.StaffManageCollegeRepository.Entities.Where(x => x.UserID == UserID).Select(w => w.CollegeID).FirstOrDefault();
  275. return CollegeID;
  276. }
  277. else
  278. {
  279. return null;
  280. }
  281. }
  282. public Guid? GetLoginClassmajorID(int DataRange, Guid UserID)
  283. {
  284. if (DataRange == (int)SYS_DataRange.Assistant)
  285. {
  286. var ClassmajorID = DataRangeDAL.Value.ClassmajorRepository.Entities.Where(x => x.UserID == UserID).Select(w => w.ClassmajorID).FirstOrDefault();
  287. return ClassmajorID;
  288. }
  289. else
  290. {
  291. return null;
  292. }
  293. }
  294. public Guid? GetLoginCampusID(int DataRange,Guid UserID)
  295. {
  296. if (DataRange == (int)SYS_DataRange.Campus)
  297. {
  298. var CampusID = DataRangeDAL.Value.StaffManageCampusRepository.Entities.Where(x => x.UserID == UserID).Select(w => w.CampusID).FirstOrDefault();
  299. return CampusID;
  300. }
  301. else
  302. {
  303. return null;
  304. }
  305. }
  306. }
  307. }