SystemDeptLogic.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\logic\system;
  8. use plugin\saiadmin\basic\think\BaseLogic;
  9. use plugin\saiadmin\exception\ApiException;
  10. use plugin\saiadmin\app\model\system\SystemDept;
  11. use plugin\saiadmin\app\model\system\SystemUser;
  12. use plugin\saiadmin\utils\Helper;
  13. use plugin\saiadmin\utils\Arr;
  14. /**
  15. * 部门逻辑层
  16. */
  17. class SystemDeptLogic extends BaseLogic
  18. {
  19. /**
  20. * 构造函数
  21. */
  22. public function __construct()
  23. {
  24. $this->model = new SystemDept();
  25. }
  26. /**
  27. * 添加数据
  28. */
  29. public function add($data): mixed
  30. {
  31. $data = $this->handleData($data);
  32. $this->model->save($data);
  33. return $this->model->getKey();
  34. }
  35. /**
  36. * 修改数据
  37. */
  38. public function edit($id, $data): mixed
  39. {
  40. $oldLevel = $data['level'] . $id . ',';
  41. $data = $this->handleData($data);
  42. if ($data['parent_id'] == $id) {
  43. throw new ApiException('上级部门和当前部门不能相同');
  44. }
  45. if (in_array($id, explode(',', $data['level']))) {
  46. throw new ApiException('不能将上级部门设置为当前部门的子部门');
  47. }
  48. $newLevel = $data['level'] . $id . ',';
  49. $deptIds = $this->model->where('level', 'like', $oldLevel . '%')->column('id');
  50. return $this->transaction(function () use ($deptIds, $oldLevel, $newLevel, $data, $id) {
  51. $this->model->whereIn('id', $deptIds)->exp('level', "REPLACE(level, '$oldLevel', '$newLevel')")->update([]);
  52. return $this->model->update($data, ['id' => $id]);
  53. });
  54. }
  55. /**
  56. * 数据删除
  57. */
  58. public function destroy($ids): bool
  59. {
  60. $num = $this->model->where('parent_id', 'in', $ids)->count();
  61. if ($num > 0) {
  62. throw new ApiException('该部门下存在子部门,请先删除子部门');
  63. } else {
  64. $count = SystemUser::where('dept_id', 'in', $ids)->count();
  65. if ($count > 0) {
  66. throw new ApiException('该部门下存在用户,请先删除或者转移用户');
  67. }
  68. return $this->model->destroy($ids);
  69. }
  70. }
  71. /**
  72. * 数据处理
  73. */
  74. protected function handleData($data)
  75. {
  76. // 处理上级部门
  77. if (empty($data['parent_id']) || $data['parent_id'] == 0) {
  78. $data['level'] = '0';
  79. $data['parent_id'] = 0;
  80. } else {
  81. $parentMenu = SystemDept::findOrEmpty($data['parent_id']);
  82. $data['level'] = $parentMenu['level'] . $parentMenu['id'] . ',';
  83. }
  84. return $data;
  85. }
  86. /**
  87. * 数据树形化
  88. * @param array $where
  89. * @return array
  90. */
  91. public function tree(array $where = []): array
  92. {
  93. $query = $this->search($where);
  94. $request = request();
  95. if ($request && $request->input('tree', 'false') === 'true') {
  96. $query->field('id, id as value, name as label, parent_id');
  97. }
  98. $query->order('sort', 'desc');
  99. $query->with(['leader']);
  100. $data = $this->getAll($query);
  101. return Helper::makeTree($data);
  102. }
  103. /**
  104. * 可操作部门
  105. * @param array $where
  106. * @return array
  107. */
  108. public function accessDept(array $where = []): array
  109. {
  110. $query = $this->search($where);
  111. $query->auth($this->adminInfo['deptList']);
  112. $query->field('id, id as value, name as label, parent_id');
  113. $query->order('sort', 'desc');
  114. $data = $this->getAll($query);
  115. return Helper::makeTree($data);
  116. }
  117. }