DeptController.php 5.7 KB

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