RoleService.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Bowin.Common.Linq;
  2. using Bowin.Common.Linq.Entity;
  3. using YLShipBuildLandMap.Entity;
  4. using YLShipBuildLandMap.Entity.ViewModel.SystemSetting;
  5. using Microsoft.EntityFrameworkCore;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Linq.Expressions;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace YLShipBuildLandMap.Services.SystemSetting
  13. {
  14. public class RoleService : IRoleService
  15. {
  16. private YLShipBuildLandMapContext DbContext { get; set; }
  17. public RoleService(YLShipBuildLandMapContext dbContext)
  18. {
  19. DbContext = dbContext;
  20. }
  21. private IQueryable<RoleView> GetRoleViewQueryable(Expression<Func<SysRole, bool>> exp)
  22. {
  23. var sql = (from r in this.DbContext.SysRole.Where(exp)
  24. select new RoleView
  25. {
  26. RoleID = r.RoleId,
  27. RoleName = r.RoleName,
  28. Description = r.Description,
  29. IsSystemRole = r.IsSystemRole,
  30. CreateTime = r.CreateTime
  31. });
  32. return sql;
  33. }
  34. public IGridResultSet<RoleView> GetRoleViewsForSelect(string roleName, int? pageIndex = null, int? pageSize = null)
  35. {
  36. Expression<Func<SysRole, bool>> exp = (x => x.RecordStatus > 0);
  37. if (!string.IsNullOrEmpty(roleName))
  38. {
  39. exp = exp.And(x => x.RoleName.Contains(roleName));
  40. }
  41. var sql = GetRoleViewQueryable(exp).OrderBy(x => x.RoleName);
  42. return sql.ToGridResultSet(pageIndex, pageSize);
  43. }
  44. public IGridResultSet<RoleView> GetRoleViewList(string roleName, int? pageIndex = null, int? pageSize = null)
  45. {
  46. Expression<Func<SysRole, bool>> exp = (x => x.RecordStatus > 0);
  47. if (!string.IsNullOrEmpty(roleName))
  48. {
  49. exp = exp.And(x => x.RoleName.Contains(roleName));
  50. }
  51. var query = GetRoleViewQueryable(exp);
  52. return query.OrderBy(x => x.RoleName)
  53. .ToGridResultSet<RoleView>(pageIndex, pageSize);
  54. }
  55. public async Task Save(RoleView roleView, List<string> functionCodes, Guid operatorID)
  56. {
  57. DateTime nowTime = DateTime.Now;
  58. SysRole role = new SysRole();
  59. if (roleView.RoleID.HasValue)
  60. {
  61. //先删除角色所有权限,后面再添加
  62. var delCodes = this.DbContext.SysRoleSysFunctionCode.Where(x => x.RoleId == roleView.RoleID).ToArrayAsync();
  63. this.DbContext.SysRoleSysFunctionCode.RemoveRange(delCodes.Result);
  64. role = DbContext.SysRole
  65. .FirstOrDefault(x => x.RoleId == roleView.RoleID);
  66. }
  67. else
  68. {
  69. role.RoleId = Guid.NewGuid();
  70. role.RoleName = roleView.RoleName;
  71. role.Description = roleView.Description;
  72. role.IsSystemRole = false;
  73. role.RecordStatus = 1;
  74. role.CreateUserId = operatorID;
  75. role.CreateTime = nowTime;
  76. this.DbContext.Add(role);
  77. }
  78. role.RoleName = roleView.RoleName;
  79. role.Description = roleView.Description;
  80. role.IsSystemRole = roleView.IsSystemRole;
  81. role.ModifyUserId = operatorID;
  82. role.ModifyTime = nowTime;
  83. foreach(string code in functionCodes)
  84. {
  85. SysRoleSysFunctionCode rf = new SysRoleSysFunctionCode();
  86. rf.RoleId = role.RoleId;
  87. rf.FunctionCode = code;
  88. this.DbContext.Add(rf);
  89. }
  90. await this.DbContext.SaveChangesAsync();
  91. }
  92. public RoleView GetRoleView(Guid roleID)
  93. {
  94. return this.GetRoleViewQueryable(x => x.RoleId == roleID).FirstOrDefault();
  95. }
  96. public async Task Delete(List<Guid> roleIDList)
  97. {
  98. var delRoles = this.DbContext.SysRole.Where(x => roleIDList.Contains(x.RoleId)).ToArrayAsync();
  99. this.DbContext.SysRole.RemoveRange(delRoles.Result);
  100. await this.DbContext.SaveChangesAsync();
  101. }
  102. public RoleView GetRoleViewByName(string roleName)
  103. {
  104. return this.GetRoleViewQueryable(x => x.RoleName == roleName).FirstOrDefault();
  105. }
  106. public List<string> GetRoleMenuList(Guid roleID)
  107. {
  108. throw new NotImplementedException();
  109. }
  110. }
  111. }