DeptController.php 5.5 KB

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