DocterController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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\DocterSetting;
  13. use App\Models\Organization;
  14. use App\Models\Schedule;
  15. use App\Models\SchedulePeriod;
  16. use App\Models\ServicePack;
  17. use App\Models\Team;
  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. 'is_pack_docter' => 'in:0,1',
  32. ]);
  33. $user = $this->user;
  34. $distance_field = get_user_distance_field($user);
  35. $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', '<>', '');
  36. $list_type = !empty($req['list_type']) ? $req['list_type'] : 0;
  37. $now_line = (int)date('Hi');
  38. if ($list_type == 1) {
  39. $docter_ids5 = DocterServiceTime::where('type', 1)->where('start_time_line', '<=', $now_line)->where('end_time_line', '>', $now_line)->pluck('docter_id')->toArray();
  40. $builder->where('is_phone', 1)->whereIn('id', $docter_ids5);
  41. }
  42. elseif ($list_type == 2) {
  43. $docter_ids6 = DocterServiceTime::where('type', 2)->where('start_time_line', '<=', $now_line)->where('end_time_line', '>', $now_line)->pluck('docter_id')->toArray();
  44. $builder->where('is_chat', 1)->whereIn('id', $docter_ids6);
  45. }
  46. elseif ($list_type == 3) {
  47. $builder->where('is_appoint', 1);
  48. }
  49. else {
  50. $builder->where(function ($query) {
  51. $query->where('is_phone', 1)->orWhere('is_chat', 1)->orWhere('is_appoint', 1);
  52. });
  53. }
  54. if (!empty($req['name'])) {
  55. $name = $req['name'];
  56. $organizations = Organization::with('docter')->select(['id'])->where('name', 'like', '%'.$name.'%')->get()->toArray();
  57. $docterIds = [];
  58. foreach ($organizations as $k => $v) {
  59. $tmpDocterIds = array_column($v['docter'], 'id');
  60. $docterIds = array_merge($docterIds, $tmpDocterIds);
  61. }
  62. $orgDocterIds = array_values(array_unique($docterIds));
  63. $builder->where(function ($query) use($name, $orgDocterIds) {
  64. $query->where('name', 'like', '%'.$name.'%')->orWhereIn('id', $orgDocterIds);
  65. });
  66. }
  67. if (!empty($req['city_id'])) {
  68. $organizations = Organization::with('docter')->select(['id'])->where('city_id', $req['city_id'])->get()->toArray();
  69. $docterIds = [];
  70. foreach ($organizations as $k => $v) {
  71. $tmpDocterIds = array_column($v['docter'], 'id');
  72. $docterIds = array_merge($docterIds, $tmpDocterIds);
  73. }
  74. $cityDocterIds = array_values(array_unique($docterIds));
  75. $builder->whereIn('id', $cityDocterIds);
  76. }
  77. if ($list_type == 3) {
  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)->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. 'latitude' => 'numeric',
  153. 'longitude' => 'numeric',
  154. ]);
  155. $user = $this->user;
  156. $distance_field = get_user_distance_field($user);
  157. $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();
  158. $organization_ids = SchedulePeriod::where('docter_id', $req['docter_id'])->groupBy('organization_id')->pluck('organization_id')->toArray();
  159. $data['organization'] = [];
  160. if (!empty($organization_ids)) {
  161. $data['organization'] = Organization::select(['id', 'name', 'address', 'latitude', 'longitude', DB::raw($distance_field)])->whereIn('id', $organization_ids)->get()->toArray();
  162. }
  163. return ($data);
  164. }
  165. public function schedulePeriodList()
  166. {
  167. $req = request()->post();
  168. $this->validate(request(), [
  169. 'docter_id' => 'required|integer',
  170. 'per_page' => 'integer',
  171. 'latitude' => 'numeric',
  172. 'longitude' => 'numeric',
  173. ]);
  174. $user = $this->user;
  175. $data = Schedule::with(['schedulePeriod.timePeriod', 'schedulePeriod.organization'])->where('docter_id', $req['docter_id'])->where('schedule_type', 1)->where('schedule_day', '>=', date('Ymd'))->orderBy('schedule_day', 'asc')->paginate($req['per_page']??15)->toArray();
  176. if (!empty($data)) {
  177. foreach ($data['data'] as $k => &$v) {
  178. foreach ($v['schedule_period'] as $k1 => &$v1) {
  179. if (!empty($v1['organization'])) {
  180. $v1['organization']['distance'] = get_user_distance($user, $v1['organization']['latitude'], $v1['organization']['longitude']);
  181. }
  182. $docterSettings = DocterSetting::select(['service_num'])->where('docter_id', $req['docter_id'])->where('type', 1)->where('org_id', $v1['organization_id'])->first();
  183. if (empty($docterSettings)) {
  184. $v1['can_appoint_num'] = 0;
  185. }
  186. else {
  187. $can_appoint_num = $docterSettings['service_num'] - $v1['order_num'];
  188. $v1['can_appoint_num'] = $can_appoint_num < 0 ? 0 : $can_appoint_num;
  189. }
  190. //预约时间已经过了,就不展示了
  191. $schedule_time = strtotime($v1['schedule_date'].' '.$v1['time_period']['end_time_period'].':00');
  192. if ($schedule_time < time()) {
  193. unset($v['schedule_period'][$k1]);
  194. }
  195. }
  196. $v['schedule_period'] = array_values($v['schedule_period']);
  197. }
  198. }
  199. return out($data);
  200. }
  201. public function timePeriodList()
  202. {
  203. $req = request()->post();
  204. $this->validate(request(), [
  205. 'organization_id' => 'integer',
  206. 'schedule_type' => 'integer',
  207. 'per_page' => 'integer',
  208. 'latitude' => 'numeric',
  209. 'longitude' => 'numeric',
  210. ]);
  211. $builder = Schedule::with(['schedulePeriod.timePeriod', 'schedulePeriod.organization'])->where('schedule_day', '>=', date('Ymd'));
  212. if (!empty($req['organization_id'])) {
  213. $builder->where('organization_id', $req['organization_id']);
  214. }
  215. if (!empty($req['schedule_type'])) {
  216. $builder->where('schedule_type', $req['schedule_type']);
  217. }
  218. $data = $builder->orderBy('schedule_day', 'asc')->paginate($req['per_page']??15)->toArray();
  219. $docterSettings = DocterSetting::select(['service_num'])->where('org_id', $req['organization_id'])->where('type', $req['schedule_type'])->first();
  220. if (!empty($data)) {
  221. foreach ($data['data'] as $k => &$v) {
  222. if (empty($v['schedule_period'])) {
  223. unset($data['data'][$k]);
  224. }
  225. foreach ($v['schedule_period'] as $k1 => &$v1) {
  226. if (empty($docterSettings)) {
  227. $v1['can_appoint_num'] = 0;
  228. }
  229. else {
  230. $can_appoint_num = $docterSettings['service_num'] - $v1['order_num'];
  231. $v1['can_appoint_num'] = $can_appoint_num < 0 ? 0 : $can_appoint_num;
  232. }
  233. //预约时间已经过了,就不展示了
  234. $schedule_time = strtotime($v1['schedule_date'].' '.$v1['time_period']['end_time_period'].':00');
  235. if ($schedule_time < time()) {
  236. unset($v['schedule_period'][$k1]);
  237. }
  238. }
  239. $v['schedule_period'] = array_values($v['schedule_period']);
  240. }
  241. }
  242. $data['data'] = array_values($data['data']);
  243. return out($data);
  244. }
  245. }