DocterController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Docter;
  10. use App\Models\Organization;
  11. use App\Models\Schedule;
  12. use App\Models\SchedulePeriod;
  13. use App\Models\TimePeriod;
  14. use DB;
  15. class DocterController extends AuthController
  16. {
  17. public function docterList()
  18. {
  19. $req = request()->post();
  20. $this->validate(request(), [
  21. 'list_type' => 'in:1,2,3',
  22. 'city_id' => 'integer',
  23. 'name' => 'max:255',
  24. 'latitude' => 'numeric',
  25. 'longitude' => 'numeric',
  26. 'sort_type' => 'in:1,2,3',
  27. 'schedule_date' => 'required_if:list_type,3|date',
  28. 'time_period_id' => 'required_if:list_type,3|integer',
  29. ]);
  30. $user = $this->user;
  31. $latitude = !empty($req['latitude']) ? $req['latitude'] : $user['latitude'];
  32. $longitude = !empty($req['longitude']) ? $req['longitude'] : $user['longitude'];
  33. $distance_field = get_distance_field($latitude, $longitude);
  34. $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);
  35. if ($req['list_type'] == 1) {
  36. $builder->where('is_phone', 1);
  37. }
  38. if ($req['list_type'] == 2) {
  39. $builder->where('is_chat', 1);
  40. }
  41. if ($req['list_type'] == 3) {
  42. $builder->where('is_appoint', 1);
  43. }
  44. if (!empty($req['name'])) {
  45. $name = $req['name'];
  46. $organizations = Organization::with('docter')->select(['id'])->where('name', 'like', '%'.$name.'%')->get()->toArray();
  47. $docterIds = [];
  48. foreach ($organizations as $k => $v) {
  49. $tmpDocterIds = array_column($v['docter'], 'id');
  50. $docterIds = array_merge($docterIds, $tmpDocterIds);
  51. }
  52. $docterIds = array_values(array_unique($docterIds));
  53. $builder->where(function ($query) use($name, $docterIds) {
  54. $query->where('name', 'like', '%'.$name.'%')->orWhereIn('id', $docterIds);
  55. });
  56. }
  57. if ($req['list_type'] == 3) {
  58. $docterIds2 = SchedulePeriod::where('time_period_id', $req['time_period_id'])->where('schedule_date', $req['schedule_date'])->pluck('docter_id');
  59. $builder->whereIn('id', $docterIds2);
  60. }
  61. if (!empty($req['sort_type'])) {
  62. if ($req['sort_type'] == 1) {
  63. $builder->orderBy('distance', 'asc');
  64. }
  65. elseif ($req['sort_type'] == 2) {
  66. $builder->orderBy('eva_num', 'desc');
  67. }
  68. elseif ($req['sort_type'] == 3) {
  69. $builder->orderBy('service_persons', 'desc');
  70. }
  71. }
  72. $data = $builder->paginate();
  73. return out($data);
  74. }
  75. public function docterDetail()
  76. {
  77. $req = request()->post();
  78. $this->validate(request(), [
  79. 'docter_id' => 'required|integer',
  80. 'list_type' => 'in:1,2,3',
  81. 'schedule_date' => 'required_if:list_type,3|date',
  82. 'time_period_id' => 'required_if:list_type,3|integer',
  83. 'latitude' => 'numeric',
  84. 'longitude' => 'numeric',
  85. ]);
  86. $user = $this->user;
  87. $latitude = !empty($req['latitude']) ? $req['latitude'] : $user['latitude'];
  88. $longitude = !empty($req['longitude']) ? $req['longitude'] : $user['longitude'];
  89. $distance_field = get_distance_field($latitude, $longitude);
  90. $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();
  91. $data['organization'] = null;
  92. if (!empty($req['list_type']) && $req['list_type'] == 3) {
  93. $schedulePeriod = SchedulePeriod::with('organization')->select(['organization_id'])->where('docter_id', $req['docter_id'])->where('time_period_id', $req['time_period_id'])->where('schedule_date', $req['schedule_date'])->first()->toArray();
  94. $data['organization'] = $schedulePeriod['organization'];
  95. }
  96. return ($data);
  97. }
  98. public function schedulePeriodList()
  99. {
  100. $req = request()->post();
  101. $this->validate(request(), [
  102. 'docter_id' => 'required|integer',
  103. 'per_page' => 'integer',
  104. ]);
  105. $data = Schedule::with('schedulePeriod.timePeriod')->where('docter_id', $req['docter_id'])->where('schedule_day', '>=', date('Ymd'))->paginate($req['per_page']??15);
  106. return out($data);
  107. }
  108. public function timePeriodList()
  109. {
  110. $data = TimePeriod::select(['id', 'start_time_period', 'end_time_period'])->get();
  111. return out($data);
  112. }
  113. }