DocterController.php 12 KB

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