DocterController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 App\Models\TimePeriod;
  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. $distance_field = get_user_distance_field($user);
  36. $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', '<>', '');
  37. $list_type = !empty($req['list_type']) ? $req['list_type'] : 0;
  38. $now_line = (int)date('Hi');
  39. if ($list_type == 1) {
  40. $docter_ids5 = DocterServiceTime::where('type', 1)->where('start_time_line', '<=', $now_line)->where('end_time_line', '>', $now_line)->pluck('docter_id')->toArray();
  41. $builder->where('is_phone', 1)->whereIn('id', $docter_ids5);
  42. }
  43. if ($list_type == 2) {
  44. $docter_ids6 = DocterServiceTime::where('type', 2)->where('start_time_line', '<=', $now_line)->where('end_time_line', '>', $now_line)->pluck('docter_id')->toArray();
  45. $builder->where('is_chat', 1)->whereIn('id', $docter_ids6);
  46. }
  47. if ($list_type == 3) {
  48. $builder->where('is_appoint', 1);
  49. }
  50. if (!empty($req['name'])) {
  51. $name = $req['name'];
  52. $organizations = Organization::with('docter')->select(['id'])->where('name', 'like', '%'.$name.'%')->get()->toArray();
  53. $docterIds = [];
  54. foreach ($organizations as $k => $v) {
  55. $tmpDocterIds = array_column($v['docter'], 'id');
  56. $docterIds = array_merge($docterIds, $tmpDocterIds);
  57. }
  58. $orgDocterIds = array_values(array_unique($docterIds));
  59. $builder->where(function ($query) use($name, $orgDocterIds) {
  60. $query->where('name', 'like', '%'.$name.'%')->orWhereIn('id', $orgDocterIds);
  61. });
  62. }
  63. if (!empty($req['city_id'])) {
  64. $organizations = Organization::with('docter')->select(['id'])->where('city_id', $req['city_id'])->get()->toArray();
  65. $docterIds = [];
  66. foreach ($organizations as $k => $v) {
  67. $tmpDocterIds = array_column($v['docter'], 'id');
  68. $docterIds = array_merge($docterIds, $tmpDocterIds);
  69. }
  70. $cityDocterIds = array_values(array_unique($docterIds));
  71. $builder->whereIn('id', $cityDocterIds);
  72. }
  73. if ($list_type == 3) {
  74. //查询我关注的医生
  75. $docterIds3 = Collection::where('user_id', $user['id'])->where('docter_id', '>', 0)->pluck('docter_id')->toArray();
  76. $builder->whereNotIn('id', $docterIds3);
  77. }
  78. if (!empty($req['is_pack_docter'])) {
  79. $team_ids = ServicePack::pluck('team_id')->toArray();
  80. $team_id_arr = [];
  81. foreach ($team_ids as $k => $v) {
  82. if (!empty($v) && is_array($v)) {
  83. $team_id_arr = array_merge($team_id_arr, $v);
  84. }
  85. }
  86. $team_id_arr = array_values(array_unique($team_id_arr));
  87. $teams = Team::with(['docter'])->whereIn('id', $team_id_arr)->get()->toArray();
  88. $docterIds4 = [];
  89. foreach ($teams as $k => $v) {
  90. foreach ($v['docter'] as $k1 => $v1) {
  91. if (!in_array($v1['id'], $docterIds4)) {
  92. $docterIds4[] = $v1['id'];
  93. }
  94. }
  95. }
  96. $builder->whereIn('id', $docterIds4);
  97. }
  98. if (!empty($req['sort_type'])) {
  99. if ($req['sort_type'] == 1) {
  100. $builder->orderBy('distance', 'asc');
  101. }
  102. elseif ($req['sort_type'] == 2) {
  103. $builder->orderBy('eva_num', 'desc');
  104. }
  105. elseif ($req['sort_type'] == 3) {
  106. $builder->orderBy('service_persons', 'desc');
  107. }
  108. }
  109. $data = $builder->paginate()->toArray();
  110. //组合我关注的医生,放在最前面
  111. $page = empty($req['page']) ? 1 : $req['page'];
  112. if ($list_type == 3 && $page == 1) {
  113. $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);
  114. if (!empty($req['name'])) {
  115. $builder2->where(function ($query) use($name, $orgDocterIds) {
  116. $query->where('name', 'like', '%'.$name.'%')->orWhereIn('id', $orgDocterIds);
  117. });
  118. }
  119. if (!empty($req['city_id'])) {
  120. $builder2->whereIn('id', $cityDocterIds);
  121. }
  122. if (!empty($req['is_pack_docter'])) {
  123. $builder->whereIn('id', $docterIds4);
  124. }
  125. if (!empty($req['sort_type'])) {
  126. if ($req['sort_type'] == 1) {
  127. $builder2->orderBy('distance', 'asc');
  128. }
  129. elseif ($req['sort_type'] == 2) {
  130. $builder2->orderBy('eva_num', 'desc');
  131. }
  132. elseif ($req['sort_type'] == 3) {
  133. $builder2->orderBy('service_persons', 'desc');
  134. }
  135. }
  136. $collectDocters = $builder2->get()->toArray();
  137. if (!empty($collectDocters)) {
  138. $data['data'] = array_merge($collectDocters, $data['data'] );
  139. }
  140. }
  141. return out($data);
  142. }
  143. public function docterDetail()
  144. {
  145. $req = request()->post();
  146. $this->validate(request(), [
  147. 'docter_id' => 'required|integer',
  148. 'list_type' => 'in:0,1,2,3',
  149. 'latitude' => 'numeric',
  150. 'longitude' => 'numeric',
  151. ]);
  152. $user = $this->user;
  153. $distance_field = get_user_distance_field($user);
  154. $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();
  155. $data['organization'] = null;
  156. if (!empty($req['list_type']) && $req['list_type'] == 3) {
  157. $schedulePeriod = SchedulePeriod::with('organization')->select(['organization_id'])->where('docter_id', $req['docter_id'])->where('schedule_date', date('Y-m-d'))->first()->toArray();
  158. $data['organization'] = $schedulePeriod['organization'];
  159. }
  160. return ($data);
  161. }
  162. public function schedulePeriodList()
  163. {
  164. $req = request()->post();
  165. $this->validate(request(), [
  166. 'docter_id' => 'required|integer',
  167. 'organization_id' => 'integer',
  168. 'per_page' => 'integer',
  169. 'latitude' => 'numeric',
  170. 'longitude' => 'numeric',
  171. ]);
  172. $user = $this->user;
  173. //todo Schedule表 考虑是否有必要性,日期可以通过timePeriod取到。
  174. $builder = Schedule::with(['schedulePeriod.timePeriod', 'schedulePeriod.organization'])->where('docter_id', $req['docter_id'])->where('schedule_day', '>=', date('Ymd'));
  175. if (!empty($req['organization_id'])) {
  176. $builder->where('organization_id', $req['organization_id']);
  177. }
  178. $data = $builder->paginate($req['per_page']??15)->toArray();
  179. if (!empty($data)) {
  180. foreach ($data['data'] as $k => &$v) {
  181. foreach ($v['schedule_period'] as $k1 => &$v1) {
  182. if (!empty($v1['organization'])) {
  183. $v1['organization']['distance'] = get_user_distance($user, $v1['organization']['latitude'], $v1['organization']['longitude']);
  184. }
  185. $docterSettings = DocterSetting::select(['service_num'])->where('docter_id', $req['docter_id'])->where('type', 1)->where('org_id', $v1['organization_id'])->first();
  186. if (empty($docterSettings)) {
  187. $v1['can_appoint_num'] = 0;
  188. }
  189. else {
  190. $can_appoint_num = $docterSettings['service_num'] - $v1['order_num'];
  191. $v1['can_appoint_num'] = $can_appoint_num < 0 ? 0 : $can_appoint_num;
  192. }
  193. }
  194. }
  195. }
  196. return out($data);
  197. }
  198. public function timePeriodList()
  199. {
  200. $req = request()->post();
  201. $this->validate(request(), [
  202. 'organization_id' => 'required|integer',
  203. ]);
  204. $data = [];
  205. //todo 门诊排班诊与医生相关
  206. $data['list'] = TimePeriod::select(['id', 'start_time_period', 'end_time_period'])->where('org_id', $req['organization_id'])->get()->toArray();
  207. for ($i = 0; $i < 7; $i++) {
  208. if ($i > 0) {
  209. $data['dates'][] = date('Y-m-d', strtotime("+$i days"));
  210. }
  211. else {
  212. $data['dates'][] = date('Y-m-d');
  213. }
  214. }
  215. return out($data);
  216. }
  217. }