123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- 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<UserRoleView> GetUserRoleViewQueryable(Expression<Func<SysRole, bool>> rexp, Expression<Func<SysUser, bool>> 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<UserRoleView> GetUserViewListByRole(Guid? roleID, string name, int? pageIndex = null, int? pageSize = null)
- {
- Expression<Func<SysRole, bool>> rExp = (x => x.RoleId == roleID);
- Expression<Func<SysUser, bool>> 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<UserRoleView>(pageIndex, pageSize);
- }
- public async Task SaveUserRoleList(Guid? roleId, List<Guid> 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<List<Guid>> 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();
- }
- }
- }
|