DocterController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. if ($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. 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. //查询我关注的医生
  74. $docterIds3 = Collection::where('user_id', $user['id'])->where('docter_id', '>', 0)->pluck('docter_id')->toArray();
  75. $builder->whereNotIn('id', $docterIds3);
  76. }
  77. if (!empty($req['is_pack_docter'])) {
  78. $team_ids = ServicePack::pluck('team_id')->toArray();
  79. $team_id_arr = [];
  80. foreach ($team_ids as $k => $v) {
  81. if (!empty($v) && is_array($v)) {
  82. $team_id_arr = array_merge($team_id_arr, $v);
  83. }
  84. }
  85. $team_id_arr = array_values(array_unique($team_id_arr));
  86. $teams = Team::with(['docter'])->whereIn('id', $team_id_arr)->get()->toArray();
  87. $docterIds4 = [];
  88. foreach ($teams as $k => $v) {
  89. foreach ($v['docter'] as $k1 => $v1) {
  90. if (!in_array($v1['id'], $docterIds4)) {
  91. $docterIds4[] = $v1['id'];
  92. }
  93. }
  94. }
  95. $builder->whereIn('id', $docterIds4);
  96. }
  97. if (!empty($req['sort_type'])) {
  98. if ($req['sort_type'] == 1) {
  99. $builder->orderBy('distance', 'asc');
  100. }
  101. elseif ($req['sort_type'] == 2) {
  102. $builder->orderBy('eva_num', 'desc');
  103. }
  104. elseif ($req['sort_type'] == 3) {
  105. $builder->orderBy('service_persons', 'desc');
  106. }
  107. }
  108. $data = $builder->paginate()->toArray();
  109. //组合我关注的医生,放在最前面
  110. $page = empty($req['page']) ? 1 : $req['page'];
  111. if ($list_type == 3 && $page == 1) {
  112. $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);
  113. if (!empty($req['name'])) {
  114. $builder2->where(function ($query) use($name, $orgDocterIds) {
  115. $query->where('name', 'like', '%'.$name.'%')->orWhereIn('id', $orgDocterIds);
  116. });
  117. }
  118. if (!empty($req['city_id'])) {
  119. $builder2->whereIn('id', $cityDocterIds);
  120. }
  121. if (!empty($req['is_pack_docter'])) {
  122. $builder->whereIn('id', $docterIds4);
  123. }
  124. if (!empty($req['sort_type'])) {
  125. if ($req['sort_type'] == 1) {
  126. $builder2->orderBy('distance', 'asc');
  127. }
  128. elseif ($req['sort_type'] == 2) {
  129. $builder2->orderBy('eva_num', 'desc');
  130. }
  131. elseif ($req['sort_type'] == 3) {
  132. $builder2->orderBy('service_persons', 'desc');
  133. }
  134. }
  135. $collectDocters = $builder2->get()->toArray();
  136. if (!empty($collectDocters)) {
  137. $data['data'] = array_merge($collectDocters, $data['data'] );
  138. }
  139. }
  140. return out($data);
  141. }
  142. public function docterDetail()
  143. {
  144. $req = request()->post();
  145. $this->validate(request(), [
  146. 'docter_id' => 'required|integer',
  147. 'list_type' => 'in:0,1,2,3',
  148. 'latitude' => 'numeric',
  149. 'longitude' => 'numeric',
  150. ]);
  151. $user = $this->user;
  152. $distance_field = get_user_distance_field($user);
  153. $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();
  154. $data['organization'] = null;
  155. if (!empty($req['list_type']) && $req['list_type'] == 3) {
  156. $schedulePeriod = SchedulePeriod::with('organization')->select(['organization_id'])->where('docter_id', $req['docter_id'])->where('schedule_date', date('Y-m-d'))->first()->toArray();
  157. $data['organization'] = $schedulePeriod['organization'];
  158. }
  159. return ($data);
  160. }
  161. public function schedulePeriodList()
  162. {
  163. $req = request()->post();
  164. $this->validate(request(), [
  165. 'docter_id' => 'required|integer',
  166. 'organization_id' => 'integer',
  167. 'per_page' => 'integer',
  168. 'latitude' => 'numeric',
  169. 'longitude' => 'numeric',
  170. ]);
  171. $user = $this->user;
  172. $builder = 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');
  173. if (!empty($req['organization_id'])) {
  174. $builder->where('organization_id', $req['organization_id']);
  175. }
  176. $data = $builder->paginate($req['per_page']??15)->toArray();
  177. if (!empty($data)) {
  178. foreach ($data['data'] as $k => &$v) {
  179. foreach ($v['schedule_period'] as $k1 => &$v1) {
  180. if (!empty($v1['organization'])) {
  181. $v1['organization']['distance'] = get_user_distance($user, $v1['organization']['latitude'], $v1['organization']['longitude']);
  182. }
  183. $docterSettings = DocterSetting::select(['service_num'])->where('docter_id', $req['docter_id'])->where('type', 1)->where('org_id', $v1['organization_id'])->first();
  184. if (empty($docterSettings)) {
  185. $v1['can_appoint_num'] = 0;
  186. }
  187. else {
  188. $can_appoint_num = $docterSettings['service_num'] - $v1['order_num'];
  189. $v1['can_appoint_num'] = $can_appoint_num < 0 ? 0 : $can_appoint_num;
  190. }
  191. }
  192. }
  193. }
  194. return out($data);
  195. }
  196. public function timePeriodList()
  197. {
  198. $req = request()->post();
  199. $this->validate(request(), [
  200. 'organization_id' => 'integer',
  201. 'schedule_type' => 'integer',
  202. 'per_page' => 'integer',
  203. 'latitude' => 'numeric',
  204. 'longitude' => 'numeric',
  205. ]);
  206. $builder = Schedule::with(['schedulePeriod.timePeriod', 'schedulePeriod.organization'])->where('schedule_day', '>=', date('Ymd'));
  207. if (!empty($req['organization_id'])) {
  208. $builder->where('organization_id', $req['organization_id']);
  209. }
  210. if (!empty($req['schedule_type'])) {
  211. $builder->where('schedule_type', $req['schedule_type']);
  212. }
  213. $data = $builder->orderBy('schedule_day', 'asc')->paginate($req['per_page']??15)->toArray();
  214. $docterSettings = DocterSetting::select(['service_num'])->where('org_id', $req['organization_id'])->where('type', $req['schedule_type'])->first();
  215. if (!empty($data)) {
  216. foreach ($data['data'] as $k => &$v) {
  217. if (empty($v['schedule_period'])) {
  218. unset($data['data'][$k]);
  219. }
  220. foreach ($v['schedule_period'] as $k1 => &$v1) {
  221. if (empty($docterSettings)) {
  222. $v1['can_appoint_num'] = 0;
  223. }
  224. else {
  225. $can_appoint_num = $docterSettings['service_num'] - $v1['order_num'];
  226. $v1['can_appoint_num'] = $can_appoint_num < 0 ? 0 : $can_appoint_num;
  227. }
  228. }
  229. }
  230. }
  231. $data['data'] = array_values($data['data']);
  232. return out($data);
  233. }
  234. }