SysDeptController.cs 5.8 KB

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