DocterController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-9-29
  6. * Time: 上午11:09
  7. */
  8. namespace App\Http\Controllers\Api\V1;
  9. use App\Models\Collection;
  10. use App\Models\Docter;
  11. use App\Models\DocterServiceTime;
  12. use App\Models\Organization;
  13. use App\Models\Schedule;
  14. use App\Models\SchedulePeriod;
  15. use App\Models\TimePeriod;
  16. use DB;
  17. class DocterController extends AuthController
  18. {
  19. public function docterList()
  20. {
  21. $req = request()->post();
  22. $this->validate(request(), [
  23. 'list_type' => 'in:0,1,2,3',
  24. 'city_id' => 'integer',
  25. 'name' => 'max:255',
  26. 'latitude' => 'numeric',
  27. 'longitude' => 'numeric',
  28. 'sort_type' => 'in:0,1,2,3',
  29. 'schedule_date' => 'required_if:list_type,3|date',
  30. 'time_period_id' => 'required_if:list_type,3|integer',
  31. ]);
  32. $user = $this->user;
  33. $distance_field = get_user_distance_field($user);
  34. $builder = Docter::with('office', 'qualification')->select(['id', 'type', 'name', 'phone', 'sex', 'birthday', 'avatar', 'status', 'label', 'sign', 'intro', 'office_id', 'qualification_id', 'score', 'service_persons', 'eva_num', 'service_days', 'phone_minutes', 'chat_price', 'phone_price', 'appoint_price', 'is_chat', 'is_phone', 'is_appoint', 'latitude', 'longitude', DB::raw($distance_field)])->where('status', 1)->where('is_then', 1)->where('phone', '<>', '');
  35. $list_type = !empty($req['list_type']) ? $req['list_type'] : 0;
  36. $now_line = (int)date('Hi');
  37. if ($list_type == 1) {
  38. $ids = DocterServiceTime::where('type', 1)->where('start_time_line', '<=', $now_line)->where('end_time_line', '>', $now_line)->pluck('docter_id')->toArray();
  39. $builder->where('is_phone', 1)->whereIn('id', $ids);
  40. }
  41. if ($list_type == 2) {
  42. $ids = DocterServiceTime::where('type', 2)->where('start_time_line', '<=', $now_line)->where('end_time_line', '>', $now_line)->pluck('docter_id')->toArray();
  43. $builder->where('is_phone', 1)->whereIn('id', $ids);
  44. $builder->where('is_chat', 1);
  45. }
  46. if ($list_type == 3) {
  47. $builder->where('is_appoint', 1);
  48. }
  49. if (!empty($req['name'])) {
  50. $name = $req['name'];
  51. $organizations = Organization::with('docter')->select(['id'])->where('name', 'like', '%'.$name.'%')->get()->toArray();
  52. $docterIds = [];
  53. foreach ($organizations as $k => $v) {
  54. $tmpDocterIds = array_column($v['docter'], 'id');
  55. $docterIds = array_merge($docterIds, $tmpDocterIds);
  56. }
  57. $orgDocterIds = array_values(array_unique($docterIds));
  58. $builder->where(function ($query) use($name, $orgDocterIds) {
  59. $query->where('name', 'like', '%'.$name.'%')->orWhereIn('id', $orgDocterIds);
  60. });
  61. }
  62. if (!empty($req['city_id'])) {
  63. $organizations = Organization::with('docter')->select(['id'])->where('city_id', $req['city_id'])->get()->toArray();
  64. $docterIds = [];
  65. foreach ($organizations as $k => $v) {
  66. $tmpDocterIds = array_column($v['docter'], 'id');
  67. $docterIds = array_merge($docterIds, $tmpDocterIds);
  68. }
  69. $cityDocterIds = array_values(array_unique($docterIds));
  70. $builder->whereIn('id', $cityDocterIds);
  71. }
  72. if ($list_type == 3) {
  73. $docterIds2 = SchedulePeriod::where('time_period_id', $req['time_period_id'])->where('schedule_date', $req['schedule_date'])->pluck('docter_id')->toArray();
  74. $builder->whereIn('id', $docterIds2);
  75. //查询我关注的医生
  76. $docterIds3 = Collection::where('user_id', $user['id'])->where('docter_id', '>', 0)->pluck('docter_id')->toArray();
  77. $builder->whereNotIn('id', $docterIds3);
  78. }
  79. if (!empty($req['sort_type'])) {
  80. if ($req['sort_type'] == 1) {
  81. $builder->orderBy('distance', 'asc');
  82. }
  83. elseif ($req['sort_type'] == 2) {
  84. $builder->orderBy('eva_num', 'desc');
  85. }
  86. elseif ($req['sort_type'] == 3) {
  87. $builder->orderBy('service_persons', 'desc');
  88. }
  89. }
  90. $data = $builder->paginate()->toArray();
  91. //组合我关注的医生,放在最前面
  92. $page = empty($req['page']) ? 1 : $req['page'];
  93. if ($list_type == 3 && $page == 1) {
  94. $builder2 = Docter::with('office', 'qualification')->select(['id', 'type', 'name', 'phone', 'sex', 'birthday', 'avatar', 'status', 'label', 'sign', 'intro', 'office_id', 'qualification_id', 'score', 'service_persons', 'eva_num', 'service_days', 'phone_minutes', 'chat_price', 'phone_price', 'appoint_price', 'is_chat', 'is_phone', 'is_appoint', 'latitude', 'longitude', DB::raw($distance_field)])->whereIn('id', $docterIds3)->whereIn('id', $docterIds2)->where('status', 1)->where('is_then', 1)->where('phone', '<>', '')->where('is_appoint', 1);
  95. if (!empty($req['name'])) {
  96. $builder2->where(function ($query) use($name, $orgDocterIds) {
  97. $query->where('name', 'like', '%'.$name.'%')->orWhereIn('id', $orgDocterIds);
  98. });
  99. }
  100. if (!empty($req['city_id'])) {
  101. $builder2->whereIn('id', $cityDocterIds);
  102. }
  103. if (!empty($req['sort_type'])) {
  104. if ($req['sort_type'] == 1) {
  105. $builder2->orderBy('distance', 'asc');
  106. }
  107. elseif ($req['sort_type'] == 2) {
  108. $builder2->orderBy('eva_num', 'desc');
  109. }
  110. elseif ($req['sort_type'] == 3) {
  111. $builder2->orderBy('service_persons', 'desc');
  112. }
  113. }
  114. $collectDocters = $builder2->get()->toArray();
  115. if (!empty($collectDocters)) {
  116. $data['data'] = array_merge($collectDocters, $data['data'] );
  117. }
  118. }
  119. return out($data);
  120. }
  121. public function docterDetail()
  122. {
  123. $req = request()->post();
  124. $this->validate(request(), [
  125. 'docter_id' => 'required|integer',
  126. 'list_type' => 'in:0,1,2,3',
  127. 'schedule_date' => 'required_if:list_type,3|date',
  128. 'time_period_id' => 'required_if:list_type,3|integer',
  129. 'latitude' => 'numeric',
  130. 'longitude' => 'numeric',
  131. ]);
  132. $user = $this->user;
  133. $distance_field = get_user_distance_field($user);
  134. $data = Docter::with('office', 'qualification', 'evaluate.user')->select(['id', 'type', 'name', 'phone', 'sex', 'birthday', 'avatar', 'status', 'label', 'sign', 'intro', 'office_id', 'qualification_id', 'score', 'service_persons', 'eva_num', 'service_days', 'phone_minutes', 'chat_price', 'phone_price', 'appoint_price', 'is_chat', 'is_phone', 'is_appoint', 'latitude', 'longitude', DB::raw($distance_field)])->where('id', $req['docter_id'])->where('status', 1)->first()->toArray();
  135. $data['organization'] = null;
  136. if (!empty($req['list_type']) && $req['list_type'] == 3) {
  137. $schedulePeriod = SchedulePeriod::with('organization')->select(['organization_id'])->where('docter_id', $req['docter_id'])->where('time_period_id', $req['time_period_id'])->where('schedule_date', $req['schedule_date'])->first()->toArray();
  138. $data['organization'] = $schedulePeriod['organization'];
  139. }
  140. return ($data);
  141. }
  142. public function schedulePeriodList()
  143. {
  144. $req = request()->post();
  145. $this->validate(request(), [
  146. 'docter_id' => 'required|integer',
  147. 'per_page' => 'integer',
  148. 'latitude' => 'numeric',
  149. 'longitude' => 'numeric',
  150. ]);
  151. $user = $this->user;
  152. $data = Schedule::with(['schedulePeriod.timePeriod', 'schedulePeriod.organization'])->where('docter_id', $req['docter_id'])->where('schedule_day', '>=', date('Ymd'))->paginate($req['per_page']??15)->toArray();
  153. if (!empty($data)) {
  154. foreach ($data['data'] as $k => &$v) {
  155. foreach ($v['schedule_period'] as $k1 => &$v1) {
  156. if (!empty($v1['organization'])) {
  157. $v1['organization']['distance'] = get_user_distance($user, $v1['organization']['latitude'], $v1['organization']['longitude']);
  158. }
  159. }
  160. }
  161. }
  162. return out($data);
  163. }
  164. public function timePeriodList()
  165. {
  166. $req = request()->post();
  167. $this->validate(request(), [
  168. 'organization_id' => 'required|integer',
  169. ]);
  170. $data = [];
  171. $data['list'] = TimePeriod::select(['id', 'start_time_period', 'end_time_period'])->where('org_id', $req['organization_id'])->get()->toArray();
  172. for ($i = 0; $i < 7; $i++) {
  173. if ($i > 0) {
  174. $data['dates'][] = date('Y-m-d', strtotime("+$i days"));
  175. }
  176. else {
  177. $data['dates'][] = date('Y-m-d');
  178. }
  179. }
  180. return out($data);
  181. }
  182. }