DeptController.php 4.8 KB

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