DeptController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace app\admin\controller\medical;
  3. use _PHPStan_cc8d35ffb\Nette\Neon\Exception;
  4. use app\admin\validate\medical\DeptValidate;
  5. use app\common\Tree;
  6. use app\controller\Curd;
  7. use app\model\MedicalDept;
  8. use support\Db;
  9. use support\exception\BusinessException;
  10. use support\Request;
  11. use support\Response;
  12. class DeptController extends Curd
  13. {
  14. public function __construct()
  15. {
  16. $this->model = new MedicalDept();
  17. $this->validate = true;
  18. $this->validateClass = new DeptValidate();
  19. }
  20. /**
  21. * @Desc
  22. * @Author Gorden
  23. * @Date 2024/3/1 14:38
  24. *
  25. * @param Request $request
  26. * @return Response
  27. * @throws \support\exception\BusinessException
  28. */
  29. public function select(Request $request): Response
  30. {
  31. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  32. $format = 'tree';
  33. $order = $request->get('order', 'desc');
  34. $field = $field ?? 'dept_sort';
  35. $where['dept_is_del'] = 0;
  36. $query = $this->doSelect($where, $field, $order);
  37. return $this->doFormat($query, $format, $limit);
  38. }
  39. /**
  40. * @Desc 执行写入数据
  41. * @Author Gorden
  42. * @Date 2024/3/1 15:42
  43. *
  44. * @param array $data
  45. * @return mixed|null
  46. * @throws Exception
  47. */
  48. protected function doInsert(array $data)
  49. {
  50. Db::beginTransaction();
  51. try {
  52. $primary_key = $this->model->getKeyName();
  53. $model_class = get_class($this->model);
  54. $model = new $model_class;
  55. foreach ($data as $key => $val) {
  56. $model->{$key} = $val;
  57. }
  58. $model->save();
  59. if ($model->dept_pid != 0) {
  60. $superior = $this->model->getByPrimaryKey($model->dept_pid);
  61. $model->dept_path = $superior->dept_path . $model->dept_id . '/';
  62. } else {
  63. $model->dept_path = '/0/' . $model->dept_id . '/';
  64. }
  65. $model->save();
  66. Db::commit();
  67. } catch (\Exception $e) {
  68. Db::rollBack();
  69. throw new Exception('数据写入失败');
  70. }
  71. return $primary_key ? $model->$primary_key : null;
  72. }
  73. /**
  74. * @Desc 执行更新
  75. * @Author Gorden
  76. * @Date 2024/3/1 15:51
  77. *
  78. * @param $id
  79. * @param $data
  80. * @return void
  81. */
  82. protected function doUpdate($id, $data)
  83. {
  84. Db::beginTransaction();
  85. try {
  86. $model = $this->model->find($id);
  87. $oldDeptPid = $model->dept_pid;
  88. $oldDeptPath = $model->dept_path;
  89. foreach ($data as $key => $val) {
  90. $model->{$key} = $val;
  91. }
  92. // 上级
  93. $superior = $this->model->getByPrimaryKey($model->dept_pid);
  94. $model->dept_path = $superior->dept_path . $model->dept_id . '/';
  95. $model->save();
  96. // pid变动,如果有下级,path应跟着变
  97. if ($data['dept_pid'] != $oldDeptPid) {
  98. $subs = $this->model->getAllSubDept($oldDeptPath);
  99. if ($subs) {
  100. foreach ($subs as $sub) {
  101. $this->model->where('dept_id', $sub['dept_id'])->update(['dept_path' => str_replace($oldDeptPath, $model->dept_path, $sub['dept_path'])]);
  102. }
  103. }
  104. }
  105. Db::commit();
  106. } catch (\Exception $e) {
  107. Db::rollBack();
  108. throw new BusinessException('数据更新失败~');
  109. }
  110. }
  111. /**
  112. * @Desc 软删除
  113. * @Author Gorden
  114. * @Date 2024/3/1 14:39
  115. *
  116. * @param Request $request
  117. * @return Response
  118. * @throws \support\exception\BusinessException
  119. */
  120. public function delete(Request $request): Response
  121. {
  122. $ids = $this->deleteInput($request);
  123. $this->doSoftDelete($ids, ['dept_is_del' => 1]);
  124. return json_success('success');
  125. }
  126. /**
  127. * @Desc 树形
  128. * @Author Gorden
  129. * @Date 2024/3/4 8:31
  130. *
  131. * @param $items
  132. * @return Response
  133. */
  134. protected function formatTree($items): Response
  135. {
  136. $format_items = [];
  137. foreach ($items as $item) {
  138. $format_items[] = [
  139. 'name' => $item->dept_name,
  140. 'value' => (string)$item->dept_id,
  141. 'id' => $item->dept_id,
  142. 'pid' => $item->dept_pid,
  143. 'dept_id' => $item->dept_id,
  144. 'dept_pid' => $item->dept_pid,
  145. 'dept_name' => $item->dept_name,
  146. 'dept_type' => $item->dept_type,
  147. 'dept_sort' => $item->dept_sort,
  148. 'dept_addTime' => $item->dept_addTime,
  149. 'dept_updateTime' => $item->dept_updateTime
  150. ];
  151. }
  152. $tree = new Tree($format_items);
  153. return json_success('success', $tree->getTree());
  154. }
  155. }