|
- using System.Collections.Generic;
- using Permission.Service.IServices;
- using Permission.Infrastructure.Repositories;
- using Permission.Entity.System;
- using System;
- using Permission.Entity.DbContext;
- using Microsoft.EntityFrameworkCore;
- using System.Linq;
- using Permission.Infrastructure.WebControls;
- using Permission.Utils.Validate;
-
- namespace Permission.Service.Services
- {
- public class SysRoleService : ISysRoleService
- {
- private readonly IUnitOfWork _unitOfWork;
- private readonly PermissionContext _dbContext;
- private readonly IBaseRepository<SysRole> _sysRoleRepository;
- private readonly IBaseRepository<SysMenu> _sysMenuRepository;
-
- public SysRoleService(IUnitOfWork unitOfWork, PermissionContext dbContext, IBaseRepository<SysRole> sysRoleRepository, IBaseRepository<SysMenu> sysMenuRepository)
- {
- this._unitOfWork = unitOfWork;
- this._dbContext = dbContext;
- this._sysRoleRepository = sysRoleRepository;
- this._sysMenuRepository = sysMenuRepository;
- }
-
- public bool AddModel(SysRole model)
- {
- try
- {
- model.GuidId = Guid.NewGuid().ToString();
- model.IsEnabled = model.IsEnabled != null;
- model.DeleteMark = false;
- model.CreateTime = DateTime.Now;
- var res = _sysRoleRepository.Save(model);
- return res;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
- public bool ModifyModel(SysRole model)
- {
- try
- {
- var role = _sysRoleRepository.Get(model.Id);
- role.EnCode = model.EnCode;
- role.Type = model.Type;
- role.OrganizeId = model.OrganizeId;
- role.RoleName = model.RoleName;
- role.AllowEdit = model.AllowEdit;
- role.IsEnabled = model.IsEnabled;
- role.ModifyTime = DateTime.Now;
- role.ModifyUser = model.ModifyUser;
- role.Remark = model.Remark;
- var res = _sysRoleRepository.Update(role);
- return res;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
- public SysRole GetRole(int key)
- {
- try
- {
- if (!key.IsEmpty() || key > 0)
- {
- var data = _sysRoleRepository.Get(key);
- return data;
- }
- return null;
- }
- catch (Exception e)
- {
- throw e;
- }
- }
-
- public SysRole GetRoleByKey(int key)
- {
- try
- {
- if (!key.IsEmpty() || key > 0)
- {
- var data = _customerOrgById(_dbContext, key);
- return data;
- }
- return null;
- }
- catch (Exception e)
- {
- throw e;
- }
- }
-
- public List<int> GetMenuIdListByRoleId(int roleId)
- {
- try
- {
- var currentRole = _customerById(_dbContext, roleId);
- //var data = currentRole.RoleAuthorizeList.Where(m => m.RoleId == roleId).Select(m => m.MenuId).ToList();
- var data = currentRole.RoleAuthorizes.Where(m => m.RoleId == roleId).Select(m => m.MenuId).ToList();
- return data;
- }
- catch (Exception e)
- {
- throw e;
- }
- }
-
- public Page<SysRole> PageList(string keyword, int pagesize, int pageindex)
- {
- try
- {
- int total = 0;
- if (keyword.IsEmpty())
- {
- var data = _sysRoleRepository.PageList(pageindex, pagesize, us => us.OrderBy(ur => ur.SortCode),
- u => _customerOrgById(_dbContext, u.Id),
- out total,
- u => u.DeleteMark == false);
-
- return data;
- }
- else
- {
- var data = _sysRoleRepository.PageList(pageindex, pagesize, us => us.OrderBy(ur => ur.SortCode), u => _customerOrgById(_dbContext, u.Id),
- out total,
- u => (u.RoleName.Contains(keyword) || u.EnCode.Contains(keyword)) && u.DeleteMark == false);
- return data;
- }
- }
- catch (Exception e)
- {
- throw e;
- }
- }
-
- public List<SysRole> GetSysRoleList()
- {
- try
- {
- var data = _sysRoleRepository.LoadAll(m => m.IsEnabled.Value == true && m.DeleteMark.Value == false)
- .OrderBy(ms => ms.SortCode)
- .AsNoTracking()
- .ToList();
-
- return data;
- }
- catch (Exception e)
- {
- throw e;
- }
- }
-
- public void SetRoleAuthorize(int roleId, params int[] menuIds)
- {
- var currentRole = _customerById(_dbContext, roleId);
-
- if (menuIds == null)
- {
- //清空当前角色下所有权限
- currentRole.RoleAuthorizes.Clear();
- }
- else
- {
- //a.角色需要重新设置的权限ID集合。
- var newMenuIdList = menuIds.ToList();
- var newMenuList = _sysMenuRepository.LoadAll(m => newMenuIdList.Contains(m.Id) && m.IsEnable == true && m.DeleteMark == false).ToList();
- //b.角色原有的授权信息。
- //var oldMenuList = currentRole.RoleAuthorizeList.Where(m => m.RoleId == roleId).ToList();
-
- //c.清空当前角色下所有权限
- currentRole.RoleAuthorizes.Clear();
-
- //d.新集合中剩下的授权信息新增到数据库。
- RoleAuthorize model;
- newMenuList.ForEach((menu) =>
- {
- model = new RoleAuthorize
- {
- GuidId = Guid.NewGuid().ToString(),
- RoleId = roleId,
- MenuId = menu.Id,
- SortCode = menu.SortCode,
- CreateTime = DateTime.Now
- //CreateUser = "",
- };
- currentRole.RoleAuthorizes.Add(model);
- });
- }
- _unitOfWork.SaveChanges();
- }
-
- #region 私有方法
-
- private static Func<PermissionContext, int, SysRole> _customerById =
- EF.CompileQuery((PermissionContext db, int roleId) => db.SysRoles
- .Include(u => u.RoleAuthorizes)
- .FirstOrDefault(c => c.Id == roleId));
-
-
- private static Func<PermissionContext, int, SysRole> _customerOrgById =
- EF.CompileQuery((PermissionContext db, int roleId) => db.SysRoles
- .Include(u => u.Organize)
- .FirstOrDefault(c => c.Id == roleId));
-
- #endregion
- }
- }
|