DocterController.php 8.3 KB

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