DocterController.php 5.4 KB

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