dept.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # -*- coding: utf-8 -*-
  2. # @Author : YY
  3. from ruoyi_common.constant import UserConstants
  4. from ruoyi_common.base.model import AjaxResponse
  5. from ruoyi_common.domain.entity import SysDept
  6. from ruoyi_common.domain.enum import BusinessType
  7. from ruoyi_common.descriptor.serializer import JsonSerializer
  8. from ruoyi_common.descriptor.validator import BodyValidator, QueryValidator,PathValidator
  9. from ruoyi_common.utils import security_util as SecurityUtil
  10. from ruoyi_system.service.sys_dept import SysDeptService
  11. from ruoyi_framework.descriptor.log import Log
  12. from ruoyi_framework.descriptor.permission import HasPerm, PreAuthorize
  13. from ... import reg
  14. @reg.api.route("/system/dept/list", methods=["GET"])
  15. @QueryValidator()
  16. @PreAuthorize(HasPerm("system:dept:list"))
  17. @JsonSerializer()
  18. def system_dept_list(dto:SysDept):
  19. '''
  20. 获取部门列表
  21. '''
  22. rows = SysDeptService.select_dept_list(dto)
  23. ajax_response = AjaxResponse.from_success(data=rows)
  24. return ajax_response
  25. @reg.api.route("/system/dept/list/exclude/<int:id>", methods=["GET"])
  26. @PathValidator()
  27. @PreAuthorize(HasPerm("system:dept:list"))
  28. @JsonSerializer()
  29. def system_dept_list_exclude(id:int):
  30. '''
  31. 获取部门列表(排除节点)
  32. '''
  33. rows = SysDeptService.select_dept_list(None)
  34. rows_copy = []
  35. for row in rows:
  36. if row.dept_id != id:
  37. rows_copy.append(row)
  38. ajax_response = AjaxResponse.from_success(data=rows_copy)
  39. return ajax_response
  40. @reg.api.route("/system/dept/treeselect", methods=["GET"])
  41. @QueryValidator()
  42. @JsonSerializer()
  43. def system_dept_treeselect(dto:SysDept):
  44. '''
  45. 获取部门下拉树
  46. '''
  47. rows = SysDeptService.select_dept_list(dto)
  48. ajax_response = AjaxResponse.from_success(
  49. data=SysDeptService.build_dept_tree_select(rows)
  50. )
  51. return ajax_response
  52. @reg.api.route("/system/dept/roleDeptTreeselect/<int:roleId>", methods=["GET"])
  53. @PathValidator()
  54. @JsonSerializer()
  55. def system_dept_roletreeselect(role_id:int):
  56. '''
  57. 获取角色的部门下拉树
  58. '''
  59. rows = SysDeptService.select_dept_list(None)
  60. ajax_response = AjaxResponse.from_success()
  61. setattr(ajax_response, "checked_keys", SysDeptService.select_dept_list_by_role_id(role_id))
  62. setattr(ajax_response, "depts", SysDeptService.build_dept_tree_select(rows))
  63. return ajax_response
  64. @reg.api.route("/system/dept/<int:id>", methods=["GET"])
  65. @PathValidator()
  66. @PreAuthorize(HasPerm("system:dept:query"))
  67. @JsonSerializer()
  68. def system_dept(id:int):
  69. '''
  70. 获取部门详情
  71. '''
  72. SysDeptService.check_dept_data_scope(id)
  73. dept = SysDeptService.select_dept_by_id(id)
  74. ajax_response = AjaxResponse.from_success(data=dept)
  75. return ajax_response
  76. @reg.api.route("/system/dept", methods=["POST"])
  77. @BodyValidator()
  78. @PreAuthorize(HasPerm("system:dept:add"))
  79. @Log(title="部门管理",business_type=BusinessType.INSERT)
  80. @JsonSerializer()
  81. def system_dept_create(dto:SysDept):
  82. '''
  83. 新增部门
  84. '''
  85. if UserConstants.NOT_UNIQUE == SysDeptService.check_dept_name_unique(dto):
  86. ajax_response = AjaxResponse.from_error("部门名称已存在")
  87. return ajax_response
  88. dto.create_by_user(SecurityUtil.get_username())
  89. SysDeptService.insert_dept(dto)
  90. ajax_response = AjaxResponse.from_success()
  91. return ajax_response
  92. @reg.api.route("/system/dept", methods=["PUT"])
  93. @BodyValidator()
  94. @PreAuthorize(HasPerm("system:dept:edit"))
  95. @Log(title="部门管理",business_type=BusinessType.UPDATE)
  96. @JsonSerializer()
  97. def system_dept_update(dto:SysDept):
  98. '''
  99. 修改部门
  100. '''
  101. SysDeptService.check_dept_data_scope(dto.dept_id)
  102. if UserConstants.UNIQUE != SysDeptService.check_dept_name_unique(dto):
  103. return AjaxResponse.from_error("部门名称已存在")
  104. elif dto.parent_id == dto.dept_id:
  105. return AjaxResponse.from_error("上级部门不能是自己")
  106. elif UserConstants.DEPT_DISABLE == dto.status and \
  107. SysDeptService.select_normal_children_dept_by_id(dto.id) > 0:
  108. return AjaxResponse.from_error("该部门包含未停用的子部门!")
  109. dto.update_by_user(SecurityUtil.get_username())
  110. flag = SysDeptService.update_dept(dto)
  111. return AjaxResponse.from_success() if flag else AjaxResponse.from_error()
  112. @reg.api.route("/system/dept/<int:id>", methods=["DELETE"])
  113. @PathValidator()
  114. @PreAuthorize(HasPerm("system:dept:remove"))
  115. @Log(title="部门管理",business_type=BusinessType.DELETE)
  116. @JsonSerializer()
  117. def system_dept_delete(id:int):
  118. '''
  119. 删除部门
  120. '''
  121. if SysDeptService.has_child_by_dept_id(id):
  122. return AjaxResponse.from_error("该部门存在下级部门,不允许删除")
  123. if SysDeptService.check_dept_exist_user(id):
  124. return AjaxResponse.from_error("该部门存在用户,不允许删除")
  125. SysDeptService.check_dept_data_scope(id)
  126. flag = SysDeptService.delete_dept_by_id(id)
  127. return AjaxResponse.from_success() if flag else AjaxResponse.from_error()