155 lines
6.1 KiB
C#
155 lines
6.1 KiB
C#
using AutoMapper;
|
|
using AutoMapper.QueryableExtensions;
|
|
using IRaCIS.Application.Interfaces;
|
|
using IRaCIS.Application.ViewModels;
|
|
using IRaCIS.Core.Application.Contracts.RequestAndResponse;
|
|
using IRaCIS.Core.Domain.Interfaces;
|
|
using IRaCIS.Core.Domain.Models;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
namespace IRaCIS.Application.Services
|
|
{
|
|
public class RoleService : IRoleService
|
|
{
|
|
private readonly IRoleRepository _roleRepository;
|
|
private readonly IUserRoleRepository _userRoleRepository;
|
|
private readonly IMapper _mapper;
|
|
public RoleService(IRoleRepository roleRepository,
|
|
IUserRoleRepository userRoleRepository, IMapper mapper
|
|
)
|
|
{
|
|
_roleRepository = roleRepository;
|
|
_userRoleRepository = userRoleRepository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
public IResponseOutput AddOrUpdateRole(RoleCommand role, Guid userId)
|
|
{
|
|
if (role.Id == Guid.Empty|| role.Id ==null)
|
|
{
|
|
var exist = _roleRepository.GetAll().FirstOrDefault(u => u.RoleName == role.RoleName);
|
|
if (exist != null)
|
|
{
|
|
return ResponseOutput.NotOk("Same role name is existed");
|
|
}
|
|
var result = _roleRepository.Add(_mapper.Map<Role>(role));
|
|
|
|
return ResponseOutput.Result(_roleRepository.SaveChanges());
|
|
|
|
}
|
|
else
|
|
{
|
|
var exist = _roleRepository.GetAll().FirstOrDefault(u => u.RoleName == role.RoleName && u.Id != role.Id);
|
|
if (exist != null)
|
|
{
|
|
return ResponseOutput.NotOk("Same role name is existed");
|
|
}
|
|
var success = _roleRepository.Update(t => t.Id == role.Id, u => new Role()
|
|
{
|
|
UpdateTime = DateTime.Now,
|
|
UpdateUserId = userId,
|
|
RoleName = role.RoleName,
|
|
PrivilegeLevel = role.PrivilegeLevel,
|
|
RoleDescription = role.RoleDescription
|
|
});
|
|
|
|
return ResponseOutput.Result(success);
|
|
}
|
|
}
|
|
|
|
//public IResponseOutput<Guid> CopyRole(Guid sourceRoleId, RoleAddOrUpdateModel role)
|
|
//{
|
|
// var exist = _roleRepository.GetAll().FirstOrDefault(u => u.RoleName == role.RoleName);
|
|
// if (exist != null)
|
|
// {
|
|
// return new IResponseOutput<Guid> { IsSuccess = false, ErrorMessage = "Role Name is existed", Result = Guid.Empty };
|
|
// }
|
|
// var result = _roleRepository.Add(_mapper.Map<Role>(role));
|
|
|
|
// var roleMenu = _roleMenuRepository.Find(u => u.RoleId == sourceRoleId);
|
|
// //var roleFunction = _roleFunctionRepository.Find(u => u.RoleId == sourceRoleId);
|
|
|
|
|
|
// if (_roleRepository.SaveChanges())
|
|
// {
|
|
// foreach (var item in roleMenu)
|
|
// {
|
|
// item.Id = Guid.Empty;
|
|
// item.RoleId = result.Id;
|
|
// _roleMenuRepository.Add(item);
|
|
// };
|
|
// _roleMenuRepository.SaveChanges();
|
|
|
|
// //foreach (var roleFunctionItem in roleFunction)
|
|
// //{
|
|
// // roleFunctionItem.Id = Guid.Empty;
|
|
// // roleFunctionItem.RoleId = result.Id;
|
|
// // _roleFunctionRepository.Add(roleFunctionItem);
|
|
// //};
|
|
// //_roleFunctionRepository.SaveChanges();
|
|
|
|
// return new IResponseOutput<Guid> { IsSuccess = true, Result = result.Id };
|
|
// }
|
|
// return new IResponseOutput<Guid> { IsSuccess = false, ErrorMessage = ConstClass.AddFailed, Result = Guid.Empty };
|
|
//}
|
|
|
|
public IResponseOutput DeleteRole(Guid roleId)
|
|
{
|
|
//删除角色,需要判断该角色是否被使用
|
|
var exist = _userRoleRepository.GetAll().FirstOrDefault(u => u.RoleId == roleId);
|
|
if (exist != null)
|
|
return ResponseOutput.NotOk("This role is used,can not delete.");
|
|
|
|
var result = _roleRepository.Delete(u => u.Id == roleId);
|
|
return ResponseOutput.Result(result);
|
|
}
|
|
|
|
|
|
public PageOutput<RoleDTO> GetRoleListByPage(int pageIndex, int pageSize)
|
|
{
|
|
var count = _roleRepository.GetCount();
|
|
var data = _roleRepository.Find(pageSize, pageIndex, true, u => u.RoleName).ProjectTo<RoleDTO>(_mapper.ConfigurationProvider).ToList();
|
|
return new PageOutput<RoleDTO>(pageIndex, pageSize, count, data);
|
|
}
|
|
|
|
public PageOutput<UserSelectRoleDTO> GetRoleListByPage(Guid userId, int pageIndex, int pageSize)
|
|
{
|
|
var query = from role in _roleRepository.GetAll()
|
|
join userRoleItem in _userRoleRepository.GetAll().Where(t => t.UserId == userId) on role.Id equals userRoleItem.RoleId into t
|
|
from userRole in t.DefaultIfEmpty()
|
|
select new UserSelectRoleDTO()
|
|
{
|
|
Id = role.Id,
|
|
IsSelect = userRole == null ? false : true,
|
|
RoleDescription = role.RoleDescription,
|
|
RoleName = role.RoleName
|
|
};
|
|
var count = _roleRepository.GetCount();
|
|
var data = query.ToList();
|
|
return new PageOutput<UserSelectRoleDTO>(pageIndex, pageSize, count, data);
|
|
}
|
|
|
|
public IResponseOutput UpdateUserRole(Guid userId, Guid roleId, bool isSelect)
|
|
{
|
|
var success = false;
|
|
if (isSelect)
|
|
{
|
|
_userRoleRepository.Add(new UserRole()
|
|
{
|
|
RoleId = roleId,
|
|
UserId = userId
|
|
});
|
|
success = _userRoleRepository.SaveChanges();
|
|
}
|
|
else
|
|
{
|
|
success = _userRoleRepository.Delete(t => t.UserId == userId && t.RoleId == roleId);
|
|
}
|
|
|
|
return ResponseOutput.Result(success);
|
|
|
|
}
|
|
}
|
|
}
|