You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
11 KiB
C#

1 year ago
using Admin.Core.Model;
using Admin.Core.Common.Resource;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Admin.Core.Common;
using System.Linq;
using Admin.Core.IService.ISys;
using Admin.Core.Model.Sys;
namespace Admin.Core.Api
{
/// <summary>
/// SysRoleController
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class SysRoleController : BaseApiUserController
{
/// <summary>
/// _sysRoleService
/// </summary>
private readonly ISysRoleService _sysRoleService;
private readonly ISysRoleMenuService _sysRoleMenuService;
private readonly ISysRoleDeptService _sysRoleDeptService;
/// <summary>
/// 构造方法
/// </summary>
/// <param name="SysRoleService"></param>
/// <param name="sysRoleMenuService"></param>
/// <param name="sysRoleDeptService"></param>
/// <param name="sysUserService"></param>
public SysRoleController(ISysRoleService SysRoleService, ISysRoleMenuService sysRoleMenuService, ISysRoleDeptService sysRoleDeptService,
ISysUserService sysUserService) : base(sysUserService)
{
_sysRoleService = SysRoleService;
_sysUserService = sysUserService;
_sysRoleMenuService = sysRoleMenuService;
_sysRoleDeptService = sysRoleDeptService;
}
/// <summary>
/// 分页查询
/// </summary>
/// <param name="pageQuery"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<PageModel<SysRole>>> GetByPage([FromBody] PageQuery<RoleQuery> pageQuery)
{
if (!pageQuery.Query.IsNotEmptyOrNull())
{
pageQuery.Query = new RoleQuery();
}
var userModel = await _sysRoleService.SelectRoleList(pageQuery);
return SuccessPage(userModel);
}
/// <summary>
/// 根据ID查询
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<RoleView>> GetByID(int id)
{
return Success(await _sysRoleService.GetInfo(id));
}
/// <summary>
/// 根据编号获取详细信息
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<RoleView>> GetInfo(int roleId)
{
return Success(await _sysRoleService.GetInfo(roleId));
}
/// <summary>
/// 新增一条数据
/// </summary>
/// <param name="request">RoleView</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<int>> Add([FromBody] RoleView request)
{
MessageModel<int> data = new MessageModel<int>();
var exist = await _sysRoleService.CheckRoleNameUnique(request.SysRole.RoleName);
if (exist)
{
data.success = false;
data.msg = "角色名称已存在!";
return data;
}
exist = await _sysRoleService.CheckRoleKeyUnique(request.SysRole.RoleKey);
if (exist)
{
data.success = false;
data.msg = "角色Key已存在";
return data;
}
request.SysRole.CreateBy = CurrentUser.LoginName;
request.SysRole.UpdateBy = CurrentUser.LoginName;
data.data = await _sysRoleService.InsertRole(request);
if (data.data > 0)
{
data.success = true;
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.success = false;
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 更新一条数据
/// </summary>
/// <param name="request">RoleView</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> Update([FromBody] RoleView request)
{
var data = new MessageModel<bool>();
if (RoleView.IsAdmin(request.SysRole.RoleID))
{
data.success = false;
data.msg = Resource_SysBase.OprateAdmin;
return data;
}
request.SysRole.UpdateBy = CurrentUser.LoginName;
data.data = await _sysRoleService.UpdateRole(request);
if (data.data)
{
data.success = true;
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.success = false;
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="ids">主键</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> Del(List<int> ids)
{
var data = new MessageModel<bool>();
foreach (var item in ids)
{
if (RoleView.IsAdmin(item))
{
data.success = false;
data.msg = Resource_SysBase.OprateAdmin;
return data;
}
}
data.data = await _sysRoleService.DeleteRoleByIds(ids.ToArray(), CurrentUser.LoginName);
if (data.data)
{
data.success = true;
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.success = false;
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 修改保存数据权限
/// </summary>
/// <param name="role"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> DataScope(RoleView role)
{
var data = new MessageModel<bool>();
if (RoleView.IsAdmin(role.SysRole.RoleID))
{
data.success = false;
data.msg = Resource_SysBase.OprateAdmin;
return data;
}
data.data = await _sysRoleService.AuthDataScope(role);
if (data.data)
{
data.success = true;
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.success = false;
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 修改状态
/// </summary>
/// <param name="role"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> ChangeStatus(SysRole role)
{
var data = new MessageModel<bool>();
if (RoleView.IsAdmin(role.RoleID))
{
data.success = false;
data.msg = Resource_SysBase.OprateAdmin;
return data;
}
var updateBy = CurrentUser.LoginName;
data.data = await _sysRoleService.UpdateRoleStatus(role, updateBy);
if (data.data)
{
data.success = true;
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.success = false;
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 数据导出
/// </summary>
/// <param name="role"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<string>> Export(SysRole role)
{
Expression<Func<SysRole, bool>> whereExpression = a => true;
var list = await _sysRoleService.QueryAsync(whereExpression);
return Success<string>(ExcelHelper.ExportToList(list, "角色数据"));
}
/// <summary>
/// 获取角色选择框列表
/// </summary>
[HttpGet]
public async Task<MessageModel<List<SysRole>>> Optionselect()
{
return Success(await _sysRoleService.QueryAsync());
}
/// <summary>
/// 取消授权用户
/// </summary>
/// <param name="userRole"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> CancelAuthUser(SysUserRole userRole)
{
var data = new MessageModel<bool>();
data.data = await _sysRoleService.DeleteAuthUser(userRole);
if (data.data)
{
data.success = true;
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.success = false;
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 批量取消授权用户
/// </summary>
/// <param name="userRoleView"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> CancelAuthUserAll(UserRoleView userRoleView)
{
var data = new MessageModel<bool>();
data.data = await _sysRoleService.DeleteAuthUsers(userRoleView.RoleID, userRoleView.UserIds.ToArray());
if (data.data)
{
data.success = true;
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.success = false;
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 批量选择用户授权
/// </summary>
/// <param name="userRoleView"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> SelectAuthUserAll(UserRoleView userRoleView)
{
var data = new MessageModel<bool>();
data.data = await _sysRoleService.InsertAuthUsers(userRoleView.RoleID, userRoleView.UserIds.ToArray());
if (data.data)
{
data.success = true;
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.success = false;
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
}
}