DeptController.php 5.6 KB

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