OrganizationController.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-10-29
  6. * Time: 下午3:43
  7. */
  8. namespace App\Http\Controllers\Api\V1;
  9. use App\Models\Area;
  10. use App\Models\Organization;
  11. use App\Models\Schedule;
  12. use DB;
  13. class OrganizationController extends AuthController
  14. {
  15. public function organizationList()
  16. {
  17. $req = request()->post();
  18. $this->validate(request(), [
  19. 'latitude' => 'numeric',
  20. 'longitude' => 'numeric',
  21. 'city_id' => 'integer',
  22. 'schedule_type' => 'integer',
  23. 'city_name' => 'max:50',
  24. ]);
  25. $user = $this->user;
  26. $schedule_type = !empty($req['schedule_type']) ? $req['schedule_type'] : 0;
  27. $distance_field = get_user_distance_field($user);
  28. $builder = Organization::with('docter')->select(['id', 'type', 'name', 'province_id', 'city_id', 'area_id', 'address', 'latitude', 'longitude','nurse_notice','vaccine_notice', DB::raw($distance_field)]);
  29. if (in_array($schedule_type, [2,3])) {
  30. $organization_ids = Schedule::where('schedule_type', $schedule_type)->where('schedule_day', '>=', date('Ymd'))->pluck('organization_id')->toArray();
  31. $builder->whereIn('id', $organization_ids);
  32. }
  33. if (!empty($req['city_id'])) {
  34. $builder->where('city_id', $req['city_id']);
  35. }
  36. if (!empty($req['city_name'])) {
  37. $city_id = Area::where('name', 'like', '%'.$req['city_name'].'%')->where('level', 2)->value('id');
  38. $city_id = !empty($city_id) ? $city_id : '';
  39. $builder->where('city_id', $city_id);
  40. }
  41. $data = $builder->orderBy('distance', 'asc')->paginate();
  42. return out($data);
  43. }
  44. public function organizationCityList()
  45. {
  46. $city_ids = Organization::pluck('city_id')->toArray();
  47. $data = Area::select(['id', 'name'])->whereIn('id', $city_ids)->get();
  48. return out($data);
  49. }
  50. }