DocterController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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\SystemConfig;
  18. use App\Models\Team;
  19. use DB;
  20. class DocterController extends AuthController
  21. {
  22. public function docterList()
  23. {
  24. $req = request()->post();
  25. $this->validate(request(), [
  26. 'list_type' => 'in:0,1,2,3',
  27. 'city_id' => 'integer',
  28. 'name' => 'max:255',
  29. 'latitude' => 'numeric',
  30. 'longitude' => 'numeric',
  31. 'sort_type' => 'in:0,1,2,3',
  32. 'is_pack_docter' => 'in:0,1',
  33. ]);
  34. $user = $this->user;
  35. $is_show = SystemConfig::get('docter_config', 'is_show');
  36. if ($is_show == 0) {
  37. return out();
  38. }
  39. $distance_field = get_user_distance_field($user);
  40. $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', '<>', '');
  41. $list_type = !empty($req['list_type']) ? $req['list_type'] : 0;
  42. $now_line = (int)date('Hi');
  43. if ($list_type == 1) {
  44. $docter_ids5 = DocterServiceTime::where('type', 1)->where('start_time_line', '<=', $now_line)->where('end_time_line', '>', $now_line)->pluck('docter_id')->toArray();
  45. $builder->where('is_phone', 1)->whereIn('id', $docter_ids5);
  46. }
  47. elseif ($list_type == 2) {
  48. $docter_ids6 = DocterServiceTime::where('type', 2)->where('start_time_line', '<=', $now_line)->where('end_time_line', '>', $now_line)->pluck('docter_id')->toArray();
  49. $builder->where('is_chat', 1)->whereIn('id', $docter_ids6);
  50. }
  51. elseif ($list_type == 3) {
  52. $builder->where('is_appoint', 1);
  53. }
  54. else {
  55. $builder->where(function ($query) {
  56. $query->where('is_phone', 1)->orWhere('is_chat', 1)->orWhere('is_appoint', 1);
  57. });
  58. }
  59. if (!empty($req['name'])) {
  60. $name = $req['name'];
  61. $organizations = Organization::with('docter')->select(['id'])->where('name', 'like', '%'.$name.'%')->get()->toArray();
  62. $docterIds = [];
  63. foreach ($organizations as $k => $v) {
  64. $tmpDocterIds = array_column($v['docter'], 'id');
  65. $docterIds = array_merge($docterIds, $tmpDocterIds);
  66. }
  67. $orgDocterIds = array_values(array_unique($docterIds));
  68. $builder->where(function ($query) use($name, $orgDocterIds) {
  69. $query->where('name', 'like', '%'.$name.'%')->orWhereIn('id', $orgDocterIds);
  70. });
  71. }
  72. if (!empty($req['city_id'])) {
  73. $organizations = Organization::with('docterOrganization')->select(['id'])->where('city_id', $req['city_id'])->get()->toArray();
  74. $docterIds = [];
  75. foreach ($organizations as $k => $v) {
  76. $tmpDocterIds = array_column($v['docter_organization'], 'docter_id');
  77. $docterIds = array_merge($docterIds, $tmpDocterIds);
  78. }
  79. $cityDocterIds = array_values(array_unique($docterIds));
  80. $builder->whereIn('id', $cityDocterIds);
  81. }
  82. if ($list_type == 3) {
  83. if (!empty($user)) {
  84. //查询我关注的医生
  85. $docterIds3 = Collection::where('user_id', $user['id'])->where('docter_id', '>', 0)->pluck('docter_id')->toArray();
  86. $builder->whereNotIn('id', $docterIds3);
  87. }
  88. }
  89. if (!empty($req['is_pack_docter'])) {
  90. $team_ids = ServicePack::pluck('team_id')->toArray();
  91. $team_id_arr = [];
  92. foreach ($team_ids as $k => $v) {
  93. if (!empty($v) && is_array($v)) {
  94. $team_id_arr = array_merge($team_id_arr, $v);
  95. }
  96. }
  97. $team_id_arr = array_values(array_unique($team_id_arr));
  98. $teams = Team::with(['docter'])->whereIn('id', $team_id_arr)->get()->toArray();
  99. $docterIds4 = [];
  100. foreach ($teams as $k => $v) {
  101. foreach ($v['docter'] as $k1 => $v1) {
  102. if (!in_array($v1['id'], $docterIds4)) {
  103. $docterIds4[] = $v1['id'];
  104. }
  105. }
  106. }
  107. $builder->whereIn('id', $docterIds4);
  108. }
  109. if (!empty($req['sort_type'])) {
  110. if ($req['sort_type'] == 1) {
  111. $builder->orderBy('distance', 'asc');
  112. }
  113. elseif ($req['sort_type'] == 2) {
  114. $builder->orderBy('eva_num', 'desc');
  115. }
  116. elseif ($req['sort_type'] == 3) {
  117. $builder->orderBy('service_persons', 'desc');
  118. }
  119. }
  120. $data = $builder->paginate()->toArray();
  121. //组合我关注的医生,放在最前面
  122. $page = empty($req['page']) ? 1 : $req['page'];
  123. if ($list_type == 3 && $page == 1 && !empty($docterIds3)) {
  124. $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);
  125. if (!empty($req['name'])) {
  126. $builder2->where(function ($query) use($name, $orgDocterIds) {
  127. $query->where('name', 'like', '%'.$name.'%')->orWhereIn('id', $orgDocterIds);
  128. });
  129. }
  130. if (!empty($req['city_id'])) {
  131. $builder2->whereIn('id', $cityDocterIds);
  132. }
  133. if (!empty($req['is_pack_docter'])) {
  134. $builder->whereIn('id', $docterIds4);
  135. }
  136. if (!empty($req['sort_type'])) {
  137. if ($req['sort_type'] == 1) {
  138. $builder2->orderBy('distance', 'asc');
  139. }
  140. elseif ($req['sort_type'] == 2) {
  141. $builder2->orderBy('eva_num', 'desc');
  142. }
  143. elseif ($req['sort_type'] == 3) {
  144. $builder2->orderBy('service_persons', 'desc');
  145. }
  146. }
  147. $collectDocters = $builder2->get()->toArray();
  148. if (!empty($collectDocters)) {
  149. $data['data'] = array_merge($collectDocters, $data['data'] );
  150. }
  151. }
  152. return out($data);
  153. }
  154. public function docterDetail()
  155. {
  156. $req = request()->post();
  157. $this->validate(request(), [
  158. 'docter_id' => 'required|integer',
  159. 'latitude' => 'numeric',
  160. 'longitude' => 'numeric',
  161. ]);
  162. $user = $this->user;
  163. $distance_field = get_user_distance_field($user);
  164. $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();
  165. if (empty($data)) {
  166. return out(null, 10001, '医生不存在');
  167. }
  168. $data = $data->toArray();
  169. $organization_ids = SchedulePeriod::where('docter_id', $req['docter_id'])->groupBy('organization_id')->pluck('organization_id')->toArray();
  170. $data['organization'] = [];
  171. if (!empty($organization_ids)) {
  172. $data['organization'] = Organization::select(['id', 'name', 'address', 'latitude', 'longitude', DB::raw($distance_field)])->whereIn('id', $organization_ids)->get()->toArray();
  173. }
  174. return ($data);
  175. }
  176. public function schedulePeriodList()
  177. {
  178. $req = request()->post();
  179. $this->validate(request(), [
  180. 'docter_id' => 'required|integer',
  181. 'latitude' => 'numeric',
  182. 'longitude' => 'numeric',
  183. ]);
  184. $user = $this->user;
  185. $show_days = DocterSetting::where('docter_id', $req['docter_id'])->where('type', 1)->value('show_days');
  186. $per_page = !empty($show_days) ? $show_days : 15;
  187. $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($per_page)->toArray();
  188. if (!empty($data)) {
  189. foreach ($data['data'] as $k => &$v) {
  190. foreach ($v['schedule_period'] as $k1 => &$v1) {
  191. if (!empty($v1['organization'])) {
  192. $v1['organization']['distance'] = get_user_distance($user, $v1['organization']['latitude'], $v1['organization']['longitude']);
  193. }
  194. $docterSettings = DocterSetting::select(['service_num'])->where('docter_id', $req['docter_id'])->where('type', 1)->where('org_id', $v1['organization_id'])->first();
  195. if (empty($docterSettings)) {
  196. $v1['can_appoint_num'] = 0;
  197. }
  198. else {
  199. $can_appoint_num = $docterSettings['service_num'] - $v1['order_num'];
  200. $v1['can_appoint_num'] = $can_appoint_num < 0 ? 0 : $can_appoint_num;
  201. }
  202. //预约时间已经过了,就不展示了
  203. $schedule_time = strtotime($v1['schedule_date'].' '.$v1['time_period']['end_time_period'].':00');
  204. if ($schedule_time < time()) {
  205. unset($v['schedule_period'][$k1]);
  206. }
  207. }
  208. $v['schedule_period'] = array_values($v['schedule_period']);
  209. }
  210. }
  211. $data['can_appoint_days'] = $per_page;
  212. return out($data);
  213. }
  214. public function timePeriodList()
  215. {
  216. $req = request()->post();
  217. $this->validate(request(), [
  218. 'organization_id' => 'required|integer',
  219. 'schedule_type' => 'required|integer',
  220. 'latitude' => 'numeric',
  221. 'longitude' => 'numeric',
  222. ]);
  223. $docterSettings = DocterSetting::select(['service_num', 'show_days'])->where('org_id', $req['organization_id'])->where('type', $req['schedule_type'])->first();
  224. $per_page = !empty($docterSettings['show_days']) ? $docterSettings['show_days'] : 15;
  225. $data = Schedule::with(['schedulePeriod.timePeriod', 'schedulePeriod.organization'])->where('schedule_day', '>=', date('Ymd'))->where('organization_id', $req['organization_id'])->where('schedule_type', $req['schedule_type'])->orderBy('schedule_day', 'asc')->paginate($per_page)->toArray();
  226. if (!empty($data)) {
  227. foreach ($data['data'] as $k => &$v) {
  228. if (empty($v['schedule_period'])) {
  229. unset($data['data'][$k]);
  230. }
  231. foreach ($v['schedule_period'] as $k1 => &$v1) {
  232. if (empty($docterSettings)) {
  233. $v1['can_appoint_num'] = 0;
  234. }
  235. else {
  236. $can_appoint_num = $docterSettings['service_num'] - $v1['order_num'];
  237. $v1['can_appoint_num'] = $can_appoint_num < 0 ? 0 : $can_appoint_num;
  238. }
  239. //预约时间已经过了,就不展示了
  240. $schedule_time = strtotime($v1['schedule_date'].' '.$v1['time_period']['end_time_period'].':00');
  241. if ($schedule_time < time()) {
  242. unset($v['schedule_period'][$k1]);
  243. }
  244. }
  245. $v['schedule_period'] = array_values($v['schedule_period']);
  246. }
  247. }
  248. $data['data'] = array_values($data['data']);
  249. $data['can_appoint_days'] = $per_page;
  250. return out($data);
  251. }
  252. }