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
{
///
/// SysRoleController
///
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class SysRoleController : BaseApiUserController
{
///
/// _sysRoleService
///
private readonly ISysRoleService _sysRoleService;
private readonly ISysRoleMenuService _sysRoleMenuService;
private readonly ISysRoleDeptService _sysRoleDeptService;
///
/// 构造方法
///
///
///
///
///
public SysRoleController(ISysRoleService SysRoleService, ISysRoleMenuService sysRoleMenuService, ISysRoleDeptService sysRoleDeptService,
ISysUserService sysUserService) : base(sysUserService)
{
_sysRoleService = SysRoleService;
_sysUserService = sysUserService;
_sysRoleMenuService = sysRoleMenuService;
_sysRoleDeptService = sysRoleDeptService;
}
///
/// 分页查询
///
///
///
[HttpPost]
public async Task>> GetByPage([FromBody] PageQuery pageQuery)
{
if (!pageQuery.Query.IsNotEmptyOrNull())
{
pageQuery.Query = new RoleQuery();
}
var userModel = await _sysRoleService.SelectRoleList(pageQuery);
return SuccessPage(userModel);
}
///
/// 根据ID查询
///
/// 主键
///
[HttpGet]
public async Task> GetByID(int id)
{
return Success(await _sysRoleService.GetInfo(id));
}
///
/// 根据编号获取详细信息
///
///
///
[HttpGet]
public async Task> GetInfo(int roleId)
{
return Success(await _sysRoleService.GetInfo(roleId));
}
///
/// 新增一条数据
///
/// RoleView
///
[HttpPost]
public async Task> Add([FromBody] RoleView request)
{
MessageModel data = new MessageModel();
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;
}
///
/// 更新一条数据
///
/// RoleView
///
[HttpPost]
public async Task> Update([FromBody] RoleView request)
{
var data = new MessageModel();
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;
}
///
/// 删除数据
///
/// 主键
///
[HttpPost]
public async Task> Del(List ids)
{
var data = new MessageModel();
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;
}
///
/// 修改保存数据权限
///
///
///
[HttpPost]
public async Task> DataScope(RoleView role)
{
var data = new MessageModel();
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;
}
///
/// 修改状态
///
///
///
[HttpPost]
public async Task> ChangeStatus(SysRole role)
{
var data = new MessageModel();
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;
}
///
/// 数据导出
///
///
///
[HttpPost]
public async Task> Export(SysRole role)
{
Expression> whereExpression = a => true;
var list = await _sysRoleService.QueryAsync(whereExpression);
return Success(ExcelHelper.ExportToList(list, "角色数据"));
}
///
/// 获取角色选择框列表
///
[HttpGet]
public async Task>> Optionselect()
{
return Success(await _sysRoleService.QueryAsync());
}
///
/// 取消授权用户
///
///
///
[HttpPost]
public async Task> CancelAuthUser(SysUserRole userRole)
{
var data = new MessageModel();
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;
}
///
/// 批量取消授权用户
///
///
///
[HttpPost]
public async Task> CancelAuthUserAll(UserRoleView userRoleView)
{
var data = new MessageModel();
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;
}
///
/// 批量选择用户授权
///
///
///
[HttpPost]
public async Task> SelectAuthUserAll(UserRoleView userRoleView)
{
var data = new MessageModel();
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;
}
}
}