DeptValidate.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\tenantapi\validate\dept;
  15. use app\common\model\auth\TenantAdminDept;
  16. use app\common\model\dept\TenantDept;
  17. use app\common\validate\BaseValidate;
  18. /**
  19. * 部门验证器
  20. * Class DeptValidate
  21. * @package app\tenantapi\validate\dept
  22. */
  23. class DeptValidate extends BaseValidate
  24. {
  25. protected $rule = [
  26. 'id' => 'require|checkDept',
  27. 'pid' => 'require|integer',
  28. 'name' => 'require|unique:'.TenantDept::class.'|length:1,30',
  29. 'status' => 'require|in:0,1',
  30. 'sort' => 'egt:0',
  31. ];
  32. protected $message = [
  33. 'id.require' => '参数缺失',
  34. 'name.require' => '请填写部门名称',
  35. 'name.length' => '部门名称长度须在1-30位字符',
  36. 'name.unique' => '部门名称已存在',
  37. 'sort.egt' => '排序值不正确',
  38. 'pid.require' => '请选择上级部门',
  39. 'pid.integer' => '上级部门参数错误',
  40. 'status.require' => '请选择部门状态',
  41. ];
  42. /**
  43. * @notes 添加场景
  44. * @return DeptValidate
  45. * @author 段誉
  46. * @date 2022/5/25 18:16
  47. */
  48. public function sceneAdd()
  49. {
  50. return $this->remove('id', true)->append('pid', 'checkDept');
  51. }
  52. /**
  53. * @notes 详情场景
  54. * @return DeptValidate
  55. * @author 段誉
  56. * @date 2022/5/25 18:16
  57. */
  58. public function sceneDetail()
  59. {
  60. return $this->only(['id']);
  61. }
  62. /**
  63. * @notes 编辑场景
  64. * @return DeptValidate
  65. * @author 段誉
  66. * @date 2022/5/26 18:42
  67. */
  68. public function sceneEdit()
  69. {
  70. return $this->append('pid', 'checkPid');
  71. }
  72. /**
  73. * @notes 删除场景
  74. * @return DeptValidate
  75. * @author 段誉
  76. * @date 2022/5/25 18:16
  77. */
  78. public function sceneDelete()
  79. {
  80. return $this->only(['id'])->append('id', 'checkAbleDetele');
  81. }
  82. /**
  83. * @notes 校验部门
  84. * @param $value
  85. * @return bool|string
  86. * @author 段誉
  87. * @date 2022/5/25 18:17
  88. */
  89. public function checkDept($value)
  90. {
  91. $dept = TenantDept::findOrEmpty($value);
  92. if ($dept->isEmpty()) {
  93. return '部门不存在';
  94. }
  95. return true;
  96. }
  97. /**
  98. * @notes 校验能否删除
  99. * @param $value
  100. * @return bool|string
  101. * @author 段誉
  102. * @date 2022/5/26 14:22
  103. */
  104. public function checkAbleDetele($value)
  105. {
  106. $hasLower = TenantDept::where(['pid' => $value])->findOrEmpty();
  107. if (!$hasLower->isEmpty()) {
  108. return '已关联下级部门,暂不可删除';
  109. }
  110. $check = TenantAdminDept::where(['dept_id' => $value])->findOrEmpty();
  111. if (!$check->isEmpty()) {
  112. return '已关联管理员,暂不可删除';
  113. }
  114. $dept = TenantDept::findOrEmpty($value);
  115. if ($dept['pid'] == 0) {
  116. return '顶级部门不可删除';
  117. }
  118. return true;
  119. }
  120. /**
  121. * @notes 校验部门
  122. * @param $value
  123. * @param $rule
  124. * @param array $data
  125. * @return bool|string
  126. * @author 段誉
  127. * @date 2022/5/26 18:41
  128. */
  129. public function checkPid($value, $rule, $data = [])
  130. {
  131. // 当前编辑的部门id信息是否存在
  132. $dept = TenantDept::findOrEmpty($data['id']);
  133. if ($dept->isEmpty()) {
  134. return '当前部门信息缺失';
  135. }
  136. // 顶级部门不校验上级部门id
  137. if ($dept['pid'] == 0) {
  138. return true;
  139. }
  140. if ($data['id'] == $value) {
  141. return '上级部门不可是当前部门';
  142. }
  143. $leaderDept = TenantDept::findOrEmpty($value);
  144. if ($leaderDept->isEmpty()) {
  145. return '部门不存在';
  146. }
  147. return true;
  148. }
  149. }