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.

262 lines
8.3 KiB
C#

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 Admin.Core.IService.ISys;
using Admin.Core.Model.Sys;
namespace Admin.Core.Api
{
/// <summary>
/// SysDeptController
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class SysDeptController : BaseApiUserController
{
/// <summary>
/// _sysDeptService
/// </summary>
private readonly ISysDeptService _sysDeptService;
private readonly ISysRoleDeptService _sysRoleDeptService;
private readonly IMapper _mapper;
/// <summary>
/// 构造方法
/// </summary>
/// <param name="SysDeptService"></param>
/// <param name="sysRoleDeptService"></param>
/// <param name="sysUserService"></param>
/// <param name="mapper"></param>
public SysDeptController(ISysDeptService SysDeptService, ISysRoleDeptService sysRoleDeptService, ISysUserService sysUserService,
IMapper mapper) : base(sysUserService)
{
_sysUserService = sysUserService;
_sysDeptService = SysDeptService;
_sysRoleDeptService = sysRoleDeptService;
_mapper = mapper;
}
/// <summary>
/// 分页查询
/// </summary>
/// <param name="pageQuery"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<PageModel<DeptView>>> GetByPage([FromBody] PageQuery<SysDept> pageQuery)
{
if (!pageQuery.Query.IsNotEmptyOrNull())
{
pageQuery.Query = new SysDept();
}
var model = await _sysDeptService.SelectDeptList(pageQuery);
return SuccessPage(model);
}
/// <summary>
/// 查询所有部门信息
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<List<SysDept>>> GetAll()
{
return Success(await _sysDeptService.QueryAsync());
}
/// <summary>
/// 根据ID查询
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<SysDept>> GetByID(int id)
{
return new MessageModel<SysDept>()
{
msg = Resource_SysBase.OprateSuccess,
success = true,
data = await _sysDeptService.QueryByIdAsync(id)
};
}
/// <summary>
/// 新增一条数据
/// </summary>
/// <param name="request">SysDept</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<int>> Add([FromBody] SysDept request)
{
var data = new MessageModel<int>();
if (!await _sysDeptService.CheckDeptNameUnique(request))
{
data.success = false;
data.msg = "部门名称已存在!";
return data;
}
request.CreateBy = CurrentUser.LoginName;
request.UpdateBy = CurrentUser.LoginName;
data.data = await _sysDeptService.InsertDept(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">SysDept</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> Update([FromBody] SysDept request)
{
var data = new MessageModel<bool>();
if (!await _sysDeptService.CheckDeptNameUnique(request))
{
data.success = false;
data.msg = "部门名称已存在!";
return data;
}
else if (request.ParentID == request.DeptID)
{
data.success = false;
data.msg = "上级部门不能是自己!";
return data;
}
else if (request.Status == SysConst.DISABLE)
{
//禁用部门需先禁用所有子部门
var existChildren = await _sysDeptService.SelectNormalChildrenDeptById(request.DeptID);
if (existChildren > 0)
{
data.success = false;
data.msg = "该部门包含未停用的子部门!";
return data;
}
}
data.success = await _sysDeptService.UpdateDept(request);
if (data.success)
{
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 删除一条数据
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> Del(int id)
{
var data = new MessageModel<bool>();
if (await _sysDeptService.HasChildByDeptId(id))
{
data.success = false;
data.msg = "存在下级部门,不允许删除!";
return data;
}
if (await _sysDeptService.CheckDeptExistUser(id))
{
data.success = false;
data.msg = "部门存在用户,不允许删除!";
return data;
}
data.success = await _sysDeptService.DeleteDeptById(id, CurrentUser.LoginName);
if (data.success)
{
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 查询部门列表(排除节点)
/// </summary>
[HttpGet]
public async Task<MessageModel<List<TreeSelect>>> ExcludeChild(int deptId)
{
var detptList = new List<SysDept>();
var depts = await _sysDeptService.SelectDeptAll();
foreach (var item in depts)
{
if (item.DeptID != deptId && item.Ancestors.IsNotEmptyOrNull() && !item.Ancestors.Split(",").Contains(deptId.ToString()))
{
detptList.Add(item);
}
}
return Success(_sysDeptService.BuildDeptTreeSelect(detptList));
}
/// <summary>
/// 获取部门下拉树列表
/// </summary>
[HttpGet]
public async Task<MessageModel<List<TreeSelect>>> TreeSelect()
{
var depts = await _sysDeptService.SelectDeptAll();
return Success(_sysDeptService.BuildDeptTreeSelect(depts));
}
/// <summary>
/// 获取部门下拉树列表
/// </summary>
[HttpGet]
public async Task<MessageModel<List<TreeSelect>>> TreeSelectByPid(int id)
{
var depts = await _sysDeptService.QueryAsync(x => x.DelFlag == false && x.Status == SysConst.ENABLE && x.ParentID == id);
return Success(_sysDeptService.BuildDeptTreeSelect(depts));
}
/// <summary>
/// 加载对应角色部门列表树
/// </summary>
[HttpGet]
public async Task<MessageModel<TreeNode>> RoleDeptTreeSelect(int roleId)
{
var depts = await _sysDeptService.SelectDeptAll();
var roleDepts = await _sysRoleDeptService.QueryAsync(x => x.RoleID == roleId);
TreeNode tree = new TreeNode()
{
CheckedKeys = roleDepts.Select(x => (int)x.DeptID).ToList(),
TreeSelects = _sysDeptService.BuildDeptTreeSelect(depts)
};
return Success(tree);
}
}
}