DocterController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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');
  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. \Log::info($data);
  178. if (!empty($data)) {
  179. foreach ($data['data'] as $k => &$v) {
  180. foreach ($v['schedule_period'] as $k1 => &$v1) {
  181. if (!empty($v1['organization'])) {
  182. $v1['organization']['distance'] = get_user_distance($user, $v1['organization']['latitude'], $v1['organization']['longitude']);
  183. }
  184. $docterSettings = DocterSetting::select(['service_num'])->where('docter_id', $req['docter_id'])->where('type', 1)->where('org_id', $v1['organization_id'])->first();
  185. if (empty($docterSettings)) {
  186. $v1['can_appoint_num'] = 0;
  187. }
  188. else {
  189. $can_appoint_num = $docterSettings['service_num'] - $v1['order_num'];
  190. $v1['can_appoint_num'] = $can_appoint_num < 0 ? 0 : $can_appoint_num;
  191. }
  192. }
  193. }
  194. }
  195. return out($data);
  196. }
  197. // public function schedulePeriodList()
  198. // {
  199. // $req = request()->post();
  200. // $this->validate(request(), [
  201. // 'docter_id' => 'required|integer',
  202. // 'organization_id' => 'integer',
  203. // 'per_page' => 'integer',
  204. // 'latitude' => 'numeric',
  205. // 'longitude' => 'numeric',
  206. // ]);
  207. // $user = $this->user;
  208. // $builder = schedulePeriod::with(['timePeriod', 'organization'])->where('docter_id', $req['docter_id'])->where('schedule_date', '>=', date('Y-m-d'));
  209. // if (!empty($req['organization_id'])) {
  210. // $builder->where('organization_id', $req['organization_id']);
  211. // }
  212. // $data = $builder->paginate($req['per_page']??15)->toArray();
  213. // //日期有重复,不同机构排班
  214. // $newdata = array();
  215. // if (!empty($data)) {
  216. //// foreach ($data['data'] as $k => &$v) {
  217. // foreach ($data as $k1 => $v1) {
  218. // if (!empty($v1['organization'])) {
  219. // $v1['organization']['distance'] = get_user_distance($user, $v1['organization']['latitude'], $v1['organization']['longitude']);
  220. // }
  221. // $docterSettings = DocterSetting::select(['service_num'])->where('docter_id', $req['docter_id'])->where('type', 1)->where('org_id', $v1['organization_id'])->first();
  222. // if (empty($docterSettings)) {
  223. // $v1['can_appoint_num'] = 0;
  224. // }
  225. // else {
  226. // $can_appoint_num = $docterSettings['service_num'] - $v1['order_num'];
  227. // $v1['can_appoint_num'] = $can_appoint_num < 0 ? 0 : $can_appoint_num;
  228. // }
  229. //
  230. // if(empty($newdata[$v1['time_period']])){
  231. // $newdata[$v1['schedule_date']] = $v1;
  232. // }else{
  233. // $newd = $newdata[$v1['schedule_date']];
  234. // foreach ($newd['time_period'] as $k2 => $v2) {
  235. // array_push($v1['time_period'],$v2);
  236. // }
  237. // $newdata[$v['schedule_date']] = $v1;
  238. //
  239. // }
  240. // }
  241. //
  242. //// }
  243. // }
  244. // $res = array();
  245. // foreach ($newdata as $v) {
  246. // if (!empty($v['time_period']))$res[] = $v;
  247. // }
  248. //
  249. // return out($res);
  250. // }
  251. public function timePeriodList()
  252. {
  253. $req = request()->post();
  254. $this->validate(request(), [
  255. 'organization_id' => 'integer',
  256. 'schedule_type' => 'integer',
  257. 'per_page' => 'integer',
  258. 'latitude' => 'numeric',
  259. 'longitude' => 'numeric',
  260. ]);
  261. $builder = Schedule::with(['schedulePeriod.timePeriod', 'schedulePeriod.organization'])->where('organization_id', $req['organization_id'])->where('schedule_type', $req['schedule_type'])->where('schedule_day', '>=', date('Ymd'))->orderBy('schedule_day');
  262. $data = $builder->paginate($req['per_page']??15)->toArray();
  263. $docterSettings = DocterSetting::select(['service_num'])->where('org_id', $req['organization_id'])->where('type',$req['schedule_type'])->first();
  264. if (!empty($data)) {
  265. foreach ($data['data'] as $k => &$v) {
  266. if(empty($v['schedule_period'])) unset($data['data'][$k]);
  267. foreach ($v['schedule_period'] as $k1 => &$v1) {
  268. if (empty($docterSettings)) {
  269. $v1['can_appoint_num'] = 0;
  270. }
  271. else {
  272. $can_appoint_num = $docterSettings['service_num'] - $v1['order_num'];
  273. $v1['can_appoint_num'] = $can_appoint_num < 0 ? 0 : $can_appoint_num;
  274. }
  275. }
  276. }
  277. }
  278. $data['data'] = array_values($data['data']);
  279. return out($data);
  280. }
  281. }