DocterController.php 10 KB

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