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
{
///
/// SysDeptController
///
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class SysDeptController : BaseApiUserController
{
///
/// _sysDeptService
///
private readonly ISysDeptService _sysDeptService;
private readonly ISysRoleDeptService _sysRoleDeptService;
private readonly IMapper _mapper;
///
/// 构造方法
///
///
///
///
///
public SysDeptController(ISysDeptService SysDeptService, ISysRoleDeptService sysRoleDeptService, ISysUserService sysUserService,
IMapper mapper) : base(sysUserService)
{
_sysUserService = sysUserService;
_sysDeptService = SysDeptService;
_sysRoleDeptService = sysRoleDeptService;
_mapper = mapper;
}
///
/// 分页查询
///
///
///
[HttpPost]
public async Task>> GetByPage([FromBody] PageQuery pageQuery)
{
if (!pageQuery.Query.IsNotEmptyOrNull())
{
pageQuery.Query = new SysDept();
}
var model = await _sysDeptService.SelectDeptList(pageQuery);
return SuccessPage(model);
}
///
/// 查询所有部门信息
///
///
[HttpGet]
public async Task>> GetAll()
{
return Success(await _sysDeptService.QueryAsync());
}
///
/// 根据ID查询
///
/// 主键
///
[HttpGet]
public async Task> GetByID(int id)
{
return new MessageModel()
{
msg = Resource_SysBase.OprateSuccess,
success = true,
data = await _sysDeptService.QueryByIdAsync(id)
};
}
///
/// 新增一条数据
///
/// SysDept
///
[HttpPost]
public async Task> Add([FromBody] SysDept request)
{
var data = new MessageModel();
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;
}
///
/// 更新一条数据
///
/// SysDept
///
[HttpPost]
public async Task> Update([FromBody] SysDept request)
{
var data = new MessageModel();
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;
}
///
/// 删除一条数据
///
/// 主键
///
[HttpPost]
public async Task> Del(int id)
{
var data = new MessageModel();
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;
}
///
/// 查询部门列表(排除节点)
///
[HttpGet]
public async Task>> ExcludeChild(int deptId)
{
var detptList = new List();
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));
}
///
/// 获取部门下拉树列表
///
[HttpGet]
public async Task>> TreeSelect()
{
var depts = await _sysDeptService.SelectDeptAll();
return Success(_sysDeptService.BuildDeptTreeSelect(depts));
}
///
/// 获取部门下拉树列表
///
[HttpGet]
public async Task>> TreeSelectByPid(int id)
{
var depts = await _sysDeptService.QueryAsync(x => x.DelFlag == false && x.Status == SysConst.ENABLE && x.ParentID == id);
return Success(_sysDeptService.BuildDeptTreeSelect(depts));
}
///
/// 加载对应角色部门列表树
///
[HttpGet]
public async Task> 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);
}
}
}