using System; using System.Collections.Generic; using System.Text; using System.Linq; using YLShipBuildLandMap.Entity; using YLShipBuildLandMap.Entity.ViewModel; using System.Linq.Expressions; using Bowin.Common.Linq.Entity; using YLShipBuildLandMap.Entity.ViewModel.SystemSetting; using Bowin.Common.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using System.Security.Cryptography; using Microsoft.VisualBasic; using Z.EntityFramework.Plus; namespace YLShipBuildLandMap.Services.SystemSetting { public class UserRoleService : IUserRoleService { private YLShipBuildLandMapContext DbContext { get; set; } public UserRoleService(YLShipBuildLandMapContext dbContext) { DbContext = dbContext; } #region 生成相关View的查询 private IQueryable GetUserRoleViewQueryable(Expression> rexp, Expression> uExp) { var sql = from r in DbContext.SysRole.Where(rexp) from ur in DbContext.SysUserSysRole from u in DbContext.SysUser.Where(uExp) where r.RoleId == ur.RoleId && ur.UserId == u.UserId select new UserRoleView { RoleID = r.RoleId, UserID = u.UserId, LoginID = u.LoginId, Name = u.Name, RoleName = r.RoleName }; return sql; } #endregion public IGridResultSet GetUserViewListByRole(Guid? roleID, string name, int? pageIndex = null, int? pageSize = null) { Expression> rExp = (x => x.RoleId == roleID); Expression> uExp = (x => x.RecordStatus >= 1); if (!string.IsNullOrEmpty(name)) { uExp = uExp.And(x => x.Name.Contains(name)); } var query = GetUserRoleViewQueryable(rExp, uExp); return query.OrderBy(x => x.Name) .ToGridResultSet(pageIndex, pageSize); } public async Task SaveUserRoleList(Guid? roleId, List userIds) { await this.DbContext.SysUserSysRole.Where(x => x.RoleId == roleId && userIds.Contains(x.UserId)).DeleteAsync(); if (roleId.HasValue && userIds.Count > 0) { foreach (Guid id in userIds) { SysUserSysRole userRole = new SysUserSysRole(); userRole.RoleId = roleId.Value; userRole.UserId = id; this.DbContext.Add(userRole); } } await this.DbContext.SaveChangesAsync(); } public Task> SelectUserRole(Guid userId) { var query = from r in this.DbContext.SysUserSysRole where r.UserId == userId select r.RoleId; return Task.FromResult(query.ToList()); } public async Task DeleteUserRole(Guid userId, Guid roleId) { await this.DbContext.SysUserSysRole.Where(x => x.RoleId == roleId && x.UserId == userId).DeleteAsync(); } } }