SysDeptController.cs 6.1 KB

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