SysDeptController.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using Infrastructure;
  2. using Infrastructure.Attribute;
  3. using Infrastructure.Enums;
  4. using Microsoft.AspNetCore.Mvc;
  5. using System.Collections;
  6. using ZR.Admin.WebApi.Filters;
  7. using ZR.Common;
  8. using ZR.Model.System;
  9. using ZR.Service.System.IService;
  10. namespace ZR.Admin.WebApi.Controllers.System
  11. {
  12. /// <summary>
  13. /// 部门
  14. /// </summary>
  15. [Verify]
  16. [Route("system/dept")]
  17. public class SysDeptController : BaseController
  18. {
  19. public ISysDeptService DeptService;
  20. public ISysUserService UserService;
  21. public SysDeptController(ISysDeptService deptService
  22. , ISysUserService userService)
  23. {
  24. DeptService = deptService;
  25. UserService = userService;
  26. }
  27. /// <summary>
  28. /// 获取部门列表
  29. /// </summary>
  30. /// <returns></returns>
  31. [ActionPermissionFilter(Permission = "system:dept:list")]
  32. [HttpGet("list")]
  33. public IActionResult List([FromQuery] SysDept dept)
  34. {
  35. return SUCCESS(DeptService.GetSysDepts(dept), TIME_FORMAT_FULL);
  36. }
  37. /// <summary>
  38. /// 查询部门列表(排除节点)
  39. /// </summary>
  40. /// <param name="deptId"></param>
  41. /// <returns></returns>
  42. [HttpGet("list/exclude/{deptId}")]
  43. public IActionResult ExcludeChild(long deptId)
  44. {
  45. var depts = DeptService.GetSysDepts(new SysDept());
  46. for (int i = 0; i < depts.Count; i++)
  47. {
  48. SysDept d = depts[i];
  49. long[] deptIds = Tools.SpitLongArrary(d.Ancestors);
  50. if (d.DeptId == deptId || ((IList)deptIds).Contains(deptId))
  51. {
  52. depts.Remove(d);
  53. }
  54. }
  55. return SUCCESS(depts);
  56. }
  57. /// <summary>
  58. /// 获取部门下拉树列表
  59. /// </summary>
  60. /// <param name="dept"></param>
  61. /// <returns></returns>
  62. [HttpGet("treeselect")]
  63. public IActionResult TreeSelect(SysDept dept)
  64. {
  65. var depts = DeptService.GetSysDepts(dept);
  66. return SUCCESS(DeptService.BuildDeptTreeSelect(depts), TIME_FORMAT_FULL);
  67. }
  68. /// <summary>
  69. /// 获取角色部门信息
  70. /// 加载对应角色部门列表树
  71. /// </summary>
  72. /// <param name="roleId"></param>
  73. /// <returns></returns>
  74. [HttpGet("roleDeptTreeselect/{roleId}")]
  75. public IActionResult RoleMenuTreeselect(int roleId)
  76. {
  77. var depts = DeptService.GetSysDepts(new SysDept());
  78. var checkedKeys = DeptService.SelectRoleDepts(roleId);
  79. return SUCCESS(new
  80. {
  81. checkedKeys,
  82. depts = DeptService.BuildDeptTreeSelect(depts),
  83. });
  84. }
  85. /// <summary>
  86. /// 根据部门编号获取详细信息
  87. /// </summary>
  88. /// <returns></returns>
  89. [HttpGet("{deptId}")]
  90. [ActionPermissionFilter(Permission = "system:dept:query")]
  91. public IActionResult GetInfo(long deptId)
  92. {
  93. var info = DeptService.GetFirst(f => f.DeptId == deptId);
  94. return SUCCESS(info);
  95. }
  96. /// <summary>
  97. /// 新增部门
  98. /// </summary>
  99. /// <param name="dept"></param>
  100. /// <returns></returns>
  101. [HttpPost]
  102. [Log(Title = "部门管理", BusinessType = BusinessType.INSERT)]
  103. [ActionPermissionFilter(Permission = "system:dept:add")]
  104. public IActionResult Add([FromBody] SysDept dept)
  105. {
  106. if (UserConstants.NOT_UNIQUE.Equals(DeptService.CheckDeptNameUnique(dept)))
  107. {
  108. return ToResponse(GetApiResult(ResultCode.CUSTOM_ERROR, $"新增部门{dept.DeptName}失败,部门名称已存在"));
  109. }
  110. dept.Create_by = User.Identity.Name;
  111. return ToResponse(ToJson(DeptService.InsertDept(dept)));
  112. }
  113. /// <summary>
  114. /// 修改部门
  115. /// </summary>
  116. /// <param name="dept"></param>
  117. /// <returns></returns>
  118. [HttpPut]
  119. [Log(Title = "部门管理", BusinessType = BusinessType.UPDATE)]
  120. [ActionPermissionFilter(Permission = "system:dept:update")]
  121. public IActionResult Update([FromBody] SysDept dept)
  122. {
  123. if (UserConstants.NOT_UNIQUE.Equals(DeptService.CheckDeptNameUnique(dept)))
  124. {
  125. return ToResponse(GetApiResult(ResultCode.CUSTOM_ERROR, $"修改部门{dept.DeptName}失败,部门名称已存在"));
  126. }
  127. else if (dept.ParentId.Equals(dept.DeptId))
  128. {
  129. return ToResponse(GetApiResult(ResultCode.CUSTOM_ERROR, $"修改部门{dept.DeptName}失败,上级部门不能是自己"));
  130. }
  131. dept.Update_by = User.Identity.Name;
  132. return ToResponse(ToJson(DeptService.UpdateDept(dept)));
  133. }
  134. /// <summary>
  135. /// 删除部门
  136. /// </summary>
  137. /// <returns></returns>
  138. [HttpDelete("{deptId}")]
  139. [ActionPermissionFilter(Permission = "system:dept:remove")]
  140. [Log(Title = "部门管理", BusinessType = BusinessType.DELETE)]
  141. public IActionResult Remove(long deptId)
  142. {
  143. if (DeptService.Queryable().Count(it => it.ParentId == deptId && it.DelFlag == "0") > 0)
  144. {
  145. return ToResponse(GetApiResult(ResultCode.CUSTOM_ERROR, $"存在下级部门,不允许删除"));
  146. }
  147. if (UserService.Queryable().Count(it => it.DeptId == deptId && it.DelFlag == "0") > 0)
  148. {
  149. return ToResponse(GetApiResult(ResultCode.CUSTOM_ERROR, $"部门存在用户,不允许删除"));
  150. }
  151. return SUCCESS(DeptService.Delete(deptId));
  152. }
  153. }
  154. }