using Bowin.Common.Linq; using Bowin.Common.Linq.Entity; using YLShipBuildLandMap.Entity; using YLShipBuildLandMap.Entity.ViewModel.SystemSetting; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace YLShipBuildLandMap.Services.SystemSetting { public class RoleService : IRoleService { private YLShipBuildLandMapContext DbContext { get; set; } public RoleService(YLShipBuildLandMapContext dbContext) { DbContext = dbContext; } private IQueryable GetRoleViewQueryable(Expression> exp) { var sql = (from r in this.DbContext.SysRole.Where(exp) select new RoleView { RoleID = r.RoleId, RoleName = r.RoleName, Description = r.Description, IsSystemRole = r.IsSystemRole, CreateTime = r.CreateTime }); return sql; } public IGridResultSet GetRoleViewsForSelect(string roleName, int? pageIndex = null, int? pageSize = null) { Expression> exp = (x => x.RecordStatus > 0); if (!string.IsNullOrEmpty(roleName)) { exp = exp.And(x => x.RoleName.Contains(roleName)); } var sql = GetRoleViewQueryable(exp).OrderBy(x => x.RoleName); return sql.ToGridResultSet(pageIndex, pageSize); } public IGridResultSet GetRoleViewList(string roleName, int? pageIndex = null, int? pageSize = null) { Expression> exp = (x => x.RecordStatus > 0); if (!string.IsNullOrEmpty(roleName)) { exp = exp.And(x => x.RoleName.Contains(roleName)); } var query = GetRoleViewQueryable(exp); return query.OrderBy(x => x.RoleName) .ToGridResultSet(pageIndex, pageSize); } public async Task Save(RoleView roleView, List functionCodes, Guid operatorID) { DateTime nowTime = DateTime.Now; SysRole role = new SysRole(); if (roleView.RoleID.HasValue) { //先删除角色所有权限,后面再添加 var delCodes = this.DbContext.SysRoleSysFunctionCode.Where(x => x.RoleId == roleView.RoleID).ToArrayAsync(); this.DbContext.SysRoleSysFunctionCode.RemoveRange(delCodes.Result); role = DbContext.SysRole .FirstOrDefault(x => x.RoleId == roleView.RoleID); } else { role.RoleId = Guid.NewGuid(); role.RoleName = roleView.RoleName; role.Description = roleView.Description; role.IsSystemRole = false; role.RecordStatus = 1; role.CreateUserId = operatorID; role.CreateTime = nowTime; this.DbContext.Add(role); } role.RoleName = roleView.RoleName; role.Description = roleView.Description; role.IsSystemRole = roleView.IsSystemRole; role.ModifyUserId = operatorID; role.ModifyTime = nowTime; foreach(string code in functionCodes) { SysRoleSysFunctionCode rf = new SysRoleSysFunctionCode(); rf.RoleId = role.RoleId; rf.FunctionCode = code; this.DbContext.Add(rf); } await this.DbContext.SaveChangesAsync(); } public RoleView GetRoleView(Guid roleID) { return this.GetRoleViewQueryable(x => x.RoleId == roleID).FirstOrDefault(); } public async Task Delete(List roleIDList) { var delRoles = this.DbContext.SysRole.Where(x => roleIDList.Contains(x.RoleId)).ToArrayAsync(); this.DbContext.SysRole.RemoveRange(delRoles.Result); await this.DbContext.SaveChangesAsync(); } public RoleView GetRoleViewByName(string roleName) { return this.GetRoleViewQueryable(x => x.RoleName == roleName).FirstOrDefault(); } public List GetRoleMenuList(Guid roleID) { throw new NotImplementedException(); } } }