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 AutoMapper;
using Microsoft.AspNetCore.Http;
using System.IO;
using Admin.Core.IService.ISys;
using Admin.Core.Model.Sys;
namespace Admin.Core.Api
{
///
/// SysUserController
///
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class SysUserController : BaseApiUserController
{
///
/// _sysUserService
///
private readonly ISysUserRoleService _sysUserRoleService;
private readonly ISysRoleService _sysRoleService;
private readonly ISysPostService _sysPostService;
private readonly ISysUserPostService _sysUserPostService;
private readonly ISysDeptService _sysDeptService;
private readonly IMapper _mapper;
///
/// 构造方法
///
///
///
///
///
///
///
///
public SysUserController(ISysUserService sysUserService, ISysUserRoleService sysUserRoleService, ISysRoleService sysRoleService, ISysPostService sysPostService,
ISysUserPostService sysUserPostService, ISysDeptService sysDeptService, IMapper mapper) : base(sysUserService)
{
_sysUserService = sysUserService;
_sysUserRoleService = sysUserRoleService;
_sysRoleService = sysRoleService;
_sysPostService = sysPostService;
_sysUserPostService = sysUserPostService;
_sysDeptService = sysDeptService;
_mapper = mapper;
}
///
/// 分页查询
///
///
///
[HttpPost]
public async Task>> GetByPage([FromBody] PageQuery pageQuery)
{
if (!pageQuery.Query.IsNotEmptyOrNull())
{
pageQuery.Query = new UserQuery();
}
var model = await _sysUserService.SelectUserList(pageQuery);
return SuccessPage(model);
}
///
/// 获取所有用户
///
///
[HttpGet]
public async Task>> GetAllUser(string roleKey = "")
{
return Success(await _sysUserRoleService.GetAllUser(roleKey));
}
///
/// 根据ID查询
///
/// 主键
///
[HttpGet]
public async Task> GetByID(int id)
{
return Success(await _sysUserService.GetByID(id));
}
///
/// 新增一条数据
///
/// SysUser
///
[HttpPost]
public async Task> Add([FromBody] UserView request)
{
MessageModel data = new MessageModel();
var exist = await _sysUserService.CheckLoginNameUnique(request.SysUser.LoginName);
if (exist)
{
data.success = false;
data.msg = "账号已存在!";
return data;
}
request.SysUser.CreateBy = CurrentUser.LoginName;
request.SysUser.UpdateBy = CurrentUser.LoginName;
data.data = await _sysUserService.InsertUser(request);
if (data.data > 0)
{
data.success = true;
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.success = false;
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
///
/// 更新一条数据
///
/// SysUser
///
[HttpPost]
public async Task> Update([FromBody] UserView request)
{
var data = new MessageModel();
if (UserView.IsAdmin(request.SysUser.UserID))
{
data.success = false;
data.msg = Resource_SysBase.OprateAdmin;
return data;
}
request.SysUser.UpdateBy = CurrentUser.LoginName;
data.data = await _sysUserService.UpdateUser(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 userId in ids)
{
if (UserView.IsAdmin(userId))
{
data.success = false;
data.msg = Resource_SysBase.OprateAdmin;
return data;
}
}
data.data = await _sysUserService.DeleteUserByIds(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> ChangeStatus(SysUser user)
{
var data = new MessageModel();
if (UserView.IsAdmin(user.UserID))
{
data.success = false;
data.msg = Resource_SysBase.OprateAdmin;
return data;
}
user.UpdateBy = CurrentUser.LoginName;
data.data = await _sysUserService.UpdateUserStatus(user);
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(SysUser user)
{
Expression> whereExpression = a => true;
var list = await _sysUserService.QueryAsync(whereExpression);
return Success(ExcelHelper.ExportToList(list, "用户数据"));
}
///
/// 用户数据导入
///
///
///
///
[HttpPost]
public MessageModel ImportData(string file, bool updateSupport)
{
return Success(true);
}
///
/// 用户数据导入模板
///
///
[HttpGet]
public MessageModel ImportTemplate()
{
return Success(string.Empty);
}
///
/// 根据用户编号获取详细信息
///
///
///
[HttpGet]
public async Task> GetInfo(int userId)
{
return Success(await _sysUserService.GetInfo(userId));
}
///
/// 重置密码
///
///
///
[HttpPost]
public async Task> ResetPwd(SysUser user)
{
var data = new MessageModel();
user.UpdateBy = CurrentUser.LoginName;
data.success = await _sysUserService.ResetPwd(user);
if (data.success)
{
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
///
/// 根据用户编号获取授权角色
///
///
///
[HttpGet]
public async Task> AuthRole(int userId)
{
return Success(await _sysUserService.AuthRole(userId));
}
///
/// 查询用户已分配角色列表
///
///
///
[HttpGet]
public async Task> AllocatedList(int userId)
{
return Success(await _sysUserService.AllocatedList(userId));
}
///
/// 查询用户未分配角色列表
///
///
///
[HttpGet]
public async Task> UnallocatedList(int userId)
{
return Success(await _sysUserService.UnallocatedList(userId));
}
///
/// 用户授权角色
///
///
///
[HttpPost]
public async Task> InsertAuthRole(UserRoleView view)
{
return Success(await _sysUserService.InsertUserAuth(view.UserID, view.RoleIds.ToArray()));
}
///
/// 个人信息
///
[HttpGet]
public async Task> Profile()
{
return Success(await _sysUserService.GetInfo(CurrentUser.UserID));
}
///
/// 修改用户基础信息
///
[HttpPost]
public async Task> UpdateProfile(SysUser user)
{
user.UpdateBy = CurrentUser.LoginName;
return Success(await _sysUserService.UpdateUserProfile(user));
}
///
/// 重置密码
///
[HttpPost]
public async Task> UpdatePwd(string oldPassword, string newPassword)
{
MessageModel model = new MessageModel();
var user = await _sysUserService.QueryByIdAsync(CurrentUser.UserID);
if (MD5Helper.MD5Encrypt32(oldPassword) == user.Password)
{
user.Password = MD5Helper.MD5Encrypt32(newPassword);
user.UpdateBy = CurrentUser.LoginName;
user.UpdateTime = DateTime.Now;
model.success = await _sysUserService.UpdateAsync(user);
}
else
{
model.success = false;
model.msg = Resource_SysBase.OprateFail;
}
return model;
}
///
/// 头像上传
///
[HttpPost]
public async Task> Avatar()
{
MessageModel model = new MessageModel();
//获取Form提交的文件
var files = Request.Form.Files;
if (files.Count == 0)
{
model.success = false;
model.msg = "文件不存在!";
return model;
}
model = await FileHelper.FileSaveByBlob(files);
if (model.success)
{
var user = await _sysUserService.QueryByIdAsync(CurrentUser.UserID);
user.Avatar = model.data;
user.UpdateBy = CurrentUser.LoginName;
user.UpdateTime = DateTime.Now;
await _sysUserService.UpdateAsync(user);
}
return model;
}
}
}