DoctorController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\admin\controller\medical;
  3. use app\admin\validate\medical\DoctorValidate;
  4. use app\controller\Curd;
  5. use app\model\MedicalDoctor;
  6. use support\exception\BusinessException;
  7. use support\Request;
  8. use support\Response;
  9. class DoctorController extends Curd
  10. {
  11. public function __construct()
  12. {
  13. $this->model = new MedicalDoctor();
  14. $this->validate = true;
  15. $this->validateClass = new DoctorValidate();
  16. }
  17. /**
  18. * @Desc 列表
  19. * @Author Gorden
  20. * @Date 2024/2/29 14:31
  21. *
  22. * @param Request $request
  23. * @return Response
  24. * @throws \support\exception\BusinessException
  25. */
  26. public function select(Request $request): Response
  27. {
  28. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  29. $order = $request->get('order', 'desc');
  30. $field = $field ?? 'doctor_addTime';
  31. $where['doctor_is_del'] = 0;
  32. $query = $this->doSelect($where, $field, $order);
  33. return $this->doFormat($query, $format, $limit);
  34. }
  35. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  36. {
  37. $model = $this->model->with(['dept','user']);
  38. foreach ($where as $column => $value) {
  39. if (is_array($value)) {
  40. if ($value[0] === 'like' || $value[0] === 'not like') {
  41. $model = $model->where($column, $value[0], "%$value[1]%");
  42. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  43. $model = $model->where($column, $value[0], $value[1]);
  44. } elseif ($value[0] == 'in' && !empty($value[1])) {
  45. $valArr = $value[1];
  46. if (is_string($value[1])) {
  47. $valArr = explode(",", trim($value[1]));
  48. }
  49. $model = $model->whereIn($column, $valArr);
  50. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  51. $valArr = $value[1];
  52. if (is_string($value[1])) {
  53. $valArr = explode(",", trim($value[1]));
  54. }
  55. $model = $model->whereNotIn($column, $valArr);
  56. } elseif ($value[0] == 'null') {
  57. $model = $model->whereNull($column);
  58. } elseif ($value[0] == 'not null') {
  59. $model = $model->whereNotNull($column);
  60. } elseif ($value[0] !== '' || $value[1] !== '') {
  61. $model = $model->whereBetween($column, $value);
  62. }
  63. } else {
  64. $model = $model->where($column, $value);
  65. }
  66. }
  67. if ($field) {
  68. $model = $model->orderBy($field, $order);
  69. }
  70. return $model;
  71. }
  72. protected function afterQuery($items)
  73. {
  74. foreach ($items as &$item) {
  75. $item['doctor_avatar'] = getenv('STORAGE_DOMAIN') . $item['doctor_avatar'];
  76. }
  77. return $items;
  78. }
  79. protected function afterInfoQuery($info)
  80. {
  81. $info->doctor_avatar = getenv("STORAGE_DOMAIN") . $info->doctor_avatar;
  82. // 出诊时间
  83. if ($info->doctor_visit_time) {
  84. $doctorVisitTimeArr = explode('~', $info->doctor_visit_time);
  85. foreach ($doctorVisitTimeArr as $key => $item) {
  86. $doctorVisitTimeArr[$key] = date("Y-m-d\TH:i:s\Z", strtotime(date('Y-m-d ') . $item) - 8 * 3600);
  87. }
  88. $info->doctor_visit_time = $doctorVisitTimeArr;
  89. }
  90. // 上班时间
  91. if ($info->doctor_work) {
  92. $doctorWorkArr = explode('~', $info->doctor_work);
  93. foreach ($doctorWorkArr as $key => $item) {
  94. $doctorWorkArr[$key] = date("Y-m-d\TH:i:s\Z", strtotime(date('Y-m-d ') . $item) - 8 * 3600);
  95. }
  96. $info->doctor_work = $doctorWorkArr;
  97. }
  98. return $info;
  99. }
  100. protected function insertInput(Request $request): array
  101. {
  102. $data = $this->inputFilter($request->post());
  103. $data['doctor_avatar'] = str_replace(getenv("STORAGE_DOMAIN"), '', $data['doctor_avatar']);
  104. // 处理时间
  105. if ($data['doctor_visit_time']) {
  106. $doctorVisitTimeStart = date('H:i', strtotime(explode(',', $data['doctor_visit_time'])[0]));
  107. $doctorVisitTimeEnd = date('H:i', strtotime(explode(',', $data['doctor_visit_time'])[1]));
  108. $data['doctor_visit_time'] = $doctorVisitTimeStart . '~' . $doctorVisitTimeEnd;
  109. }
  110. if ($data['doctor_work']) {
  111. $doctorWorkStart = date('H:i', strtotime(explode(',', $data['doctor_work'])[0]));
  112. $doctorWorkEnd = date('H:i', strtotime(explode(',', $data['doctor_work'])[1]));
  113. $data['doctor_work'] = $doctorWorkStart . '~' . $doctorWorkEnd;
  114. }
  115. return $data;
  116. }
  117. protected function updateInput(Request $request): array
  118. {
  119. $primary_key = $this->model->getKeyName();
  120. $id = $request->post($primary_key);
  121. $data = $this->inputFilter($request->post());
  122. $model = $this->model->find($id);
  123. if (!$model) {
  124. throw new BusinessException('记录不存在', 2);
  125. }
  126. unset($data[$primary_key]);
  127. // 头像
  128. $data['doctor_avatar'] = str_replace(getenv("STORAGE_DOMAIN"), '', $data['doctor_avatar']);
  129. // 处理时间
  130. if ($data['doctor_visit_time']) {
  131. $doctorVisitTimeStart = date('H:i', strtotime(explode(',', $data['doctor_visit_time'])[0]));
  132. $doctorVisitTimeEnd = date('H:i', strtotime(explode(',', $data['doctor_visit_time'])[1]));
  133. $data['doctor_visit_time'] = $doctorVisitTimeStart . '~' . $doctorVisitTimeEnd;
  134. }
  135. if ($data['doctor_work']) {
  136. $doctorWorkStart = date('H:i', strtotime(explode(',', $data['doctor_work'])[0]));
  137. $doctorWorkEnd = date('H:i', strtotime(explode(',', $data['doctor_work'])[1]));
  138. $data['doctor_work'] = $doctorWorkStart . '~' . $doctorWorkEnd;
  139. }
  140. return [$id, $data];
  141. }
  142. /**
  143. * @Desc 软删除
  144. * @Author Gorden
  145. * @Date 2024/2/29 14:37
  146. *
  147. * @param Request $request
  148. * @return Response
  149. * @throws \support\exception\BusinessException
  150. */
  151. public function delete(Request $request): Response
  152. {
  153. $ids = $this->deleteInput($request);
  154. $this->doSoftDelete($ids, ['doctor_is_del' => 1]);
  155. return json_success('success');
  156. }
  157. public function relationUser(Request $request)
  158. {
  159. $doctorId = $request->post('doctor_id','');
  160. $userId = $request->post('join_doctor_user_id','');
  161. if (!$doctorId || !$userId){
  162. return json_fail("参数异常");
  163. }
  164. try {
  165. MedicalDoctor::where('doctor_id',$doctorId)->update(['join_doctor_user_id'=>$userId]);
  166. }catch (\Exception $e){
  167. return json_fail("关联用户失败");
  168. }
  169. return json_success("关联用户成功");
  170. }
  171. }