OrganizationController.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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_name = str_replace('市', '', $req['city_name']);
  38. $city_id = Area::where('name', 'like', $city_name.'%')->where('level', 2)->value('id');
  39. $city_id = !empty($city_id) ? $city_id : '';
  40. $builder->where('city_id', $city_id);
  41. }
  42. $data = $builder->orderBy('distance', 'asc')->paginate();
  43. return out($data);
  44. }
  45. public function organizationCityList()
  46. {
  47. $city_ids = Organization::pluck('city_id')->toArray();
  48. $data = Area::select(['id', 'name'])->whereIn('id', $city_ids)->get();
  49. return out($data);
  50. }
  51. }