DeptController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. if ($model->dept_pid != 0){
  94. $superior = $this->model->getByPrimaryKey($model->dept_pid);
  95. $model->dept_path = $superior->dept_path . $model->dept_id . '/';
  96. }
  97. $model->save();
  98. // pid变动,如果有下级,path应跟着变
  99. if ($data['dept_pid'] != $oldDeptPid) {
  100. $subs = $this->model->getAllSubDept($oldDeptPath);
  101. if ($subs) {
  102. foreach ($subs as $sub) {
  103. $this->model->where('dept_id', $sub['dept_id'])->update(['dept_path' => str_replace($oldDeptPath, $model->dept_path, $sub['dept_path'])]);
  104. }
  105. }
  106. }
  107. Db::commit();
  108. } catch (\Exception $e) {
  109. Db::rollBack();
  110. throw new BusinessException('数据更新失败~');
  111. }
  112. }
  113. /**
  114. * @Desc 软删除
  115. * @Author Gorden
  116. * @Date 2024/3/1 14:39
  117. *
  118. * @param Request $request
  119. * @return Response
  120. * @throws \support\exception\BusinessException
  121. */
  122. public function delete(Request $request): Response
  123. {
  124. $ids = $this->deleteInput($request);
  125. $this->doSoftDelete($ids, ['dept_is_del' => 1]);
  126. return json_success('success');
  127. }
  128. /**
  129. * @Desc 树形
  130. * @Author Gorden
  131. * @Date 2024/3/4 8:31
  132. *
  133. * @param $items
  134. * @return Response
  135. */
  136. protected function formatTree($items): Response
  137. {
  138. $format_items = [];
  139. foreach ($items as $item) {
  140. $format_items[] = [
  141. 'name' => $item->dept_name,
  142. 'value' => (string)$item->dept_id,
  143. 'id' => $item->dept_id,
  144. 'pid' => $item->dept_pid,
  145. 'dept_id' => $item->dept_id,
  146. 'dept_pid' => $item->dept_pid,
  147. 'dept_name' => $item->dept_name,
  148. 'dept_type' => $item->dept_type,
  149. 'dept_sort' => $item->dept_sort,
  150. 'dept_addTime' => $item->dept_addTime,
  151. 'dept_updateTime' => $item->dept_updateTime
  152. ];
  153. }
  154. $tree = new Tree($format_items);
  155. return json_success('success', $tree->getTree());
  156. }
  157. }