UserRoleService.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5. using YLShipBuildLandMap.Entity;
  6. using YLShipBuildLandMap.Entity.ViewModel;
  7. using System.Linq.Expressions;
  8. using Bowin.Common.Linq.Entity;
  9. using YLShipBuildLandMap.Entity.ViewModel.SystemSetting;
  10. using Bowin.Common.Linq;
  11. using System.Threading.Tasks;
  12. using Microsoft.EntityFrameworkCore;
  13. using System.Security.Cryptography;
  14. using Microsoft.VisualBasic;
  15. using Z.EntityFramework.Plus;
  16. namespace YLShipBuildLandMap.Services.SystemSetting
  17. {
  18. public class UserRoleService : IUserRoleService
  19. {
  20. private YLShipBuildLandMapContext DbContext { get; set; }
  21. public UserRoleService(YLShipBuildLandMapContext dbContext)
  22. {
  23. DbContext = dbContext;
  24. }
  25. #region 生成相关View的查询
  26. private IQueryable<UserRoleView> GetUserRoleViewQueryable(Expression<Func<SysRole, bool>> rexp, Expression<Func<SysUser, bool>> uExp)
  27. {
  28. var sql = from r in DbContext.SysRole.Where(rexp)
  29. from ur in DbContext.SysUserSysRole
  30. from u in DbContext.SysUser.Where(uExp)
  31. where r.RoleId == ur.RoleId && ur.UserId == u.UserId
  32. select new UserRoleView
  33. {
  34. RoleID = r.RoleId,
  35. UserID = u.UserId,
  36. LoginID = u.LoginId,
  37. Name = u.Name,
  38. RoleName = r.RoleName
  39. };
  40. return sql;
  41. }
  42. #endregion
  43. public IGridResultSet<UserRoleView> GetUserViewListByRole(Guid? roleID, string name, int? pageIndex = null, int? pageSize = null)
  44. {
  45. Expression<Func<SysRole, bool>> rExp = (x => x.RoleId == roleID);
  46. Expression<Func<SysUser, bool>> uExp = (x => x.RecordStatus >= 1);
  47. if (!string.IsNullOrEmpty(name))
  48. {
  49. uExp = uExp.And(x => x.Name.Contains(name));
  50. }
  51. var query = GetUserRoleViewQueryable(rExp, uExp);
  52. return query.OrderBy(x => x.Name)
  53. .ToGridResultSet<UserRoleView>(pageIndex, pageSize);
  54. }
  55. public async Task SaveUserRoleList(Guid? roleId, List<Guid> userIds)
  56. {
  57. await this.DbContext.SysUserSysRole.Where(x => x.RoleId == roleId && userIds.Contains(x.UserId)).DeleteAsync();
  58. if (roleId.HasValue && userIds.Count > 0)
  59. {
  60. foreach (Guid id in userIds)
  61. {
  62. SysUserSysRole userRole = new SysUserSysRole();
  63. userRole.RoleId = roleId.Value;
  64. userRole.UserId = id;
  65. this.DbContext.Add(userRole);
  66. }
  67. }
  68. await this.DbContext.SaveChangesAsync();
  69. }
  70. public Task<List<Guid>> SelectUserRole(Guid userId)
  71. {
  72. var query = from r in this.DbContext.SysUserSysRole
  73. where r.UserId == userId
  74. select r.RoleId;
  75. return Task.FromResult(query.ToList());
  76. }
  77. public async Task DeleteUserRole(Guid userId, Guid roleId)
  78. {
  79. await this.DbContext.SysUserSysRole.Where(x => x.RoleId == roleId && x.UserId == userId).DeleteAsync();
  80. }
  81. }
  82. }