zilong 4 éve
szülő
commit
54c0697b1a

+ 1 - 1
app/Exceptions/Handler.php

xqd
@@ -57,7 +57,7 @@ class Handler extends ExceptionHandler
             if ($exception instanceof ExitOutException){
                 $msg = $exception->getMessage();
                 $out = json_decode($msg, true);
-                return out($out['data'], $out['code'], $out['message']);
+                return out($out['data'], $out['status'], $out['message']);
             }
         }
 

+ 16 - 4
app/Helpers/functions.php

xqd xqd xqd
@@ -11,9 +11,9 @@ use App\Exceptions\ExitOutException;
 
 //统一输出格式话的json数据
 if (!function_exists('out')) {
-    function out($data = null, $code = 200, $message = 'success', $exceptionData = false)
+    function out($data = null, $status = 0, $message = 'success', $exceptionData = false)
     {
-        $out = ['code' => $code, 'message' => $message, 'data' => $data];
+        $out = ['status' => $status, 'message' => $message, 'data' => $data];
 
         if ($exceptionData !== false) {
             trace([$message => $exceptionData], 'error');
@@ -25,9 +25,9 @@ if (!function_exists('out')) {
 
 //统一异常输出格式话的json数据
 if (!function_exists('exit_out')) {
-    function exit_out($data = null, $code = 200, $message = 'success', $exceptionData = false)
+    function exit_out($data = null, $status = 0, $message = 'success', $exceptionData = false)
     {
-        $out = ['code' => $code, 'message' => $message, 'data' => $data];
+        $out = ['status' => $status, 'message' => $message, 'data' => $data];
 
         if ($exceptionData !== false) {
             trace([$message => $exceptionData], 'error');
@@ -79,3 +79,15 @@ if (!function_exists('aes_decrypt')) {
         return json_decode($original_plaintext, true);
     }
 }
+
+//获取distance的sql字段
+if (!function_exists('get_distance_field')) {
+    function get_distance_field($latitude, $longitude)
+    {
+        if (empty($latitude) || empty($longitude)) {
+            return '999999999 distance';
+        }
+
+        return 'if(longitude=0 and latitude=0,999999999,round(6378.138*2*asin(sqrt(pow(sin( (' . $latitude . '*pi()/180-latitude*pi()/180)/2),2)+cos(' . $latitude . '*pi()/180)*cos(latitude*pi()/180)* pow(sin((' . $longitude . '*pi()/180-longitude*pi()/180)/2),2)))*1000)) distance';
+    }
+}

+ 110 - 4
app/Http/Controllers/Api/V1/DocterController.php

xqd
@@ -8,17 +8,123 @@
 
 namespace App\Http\Controllers\Api\V1;
 
+use App\Models\Docter;
+use App\Models\Organization;
+use App\Models\Schedule;
+use App\Models\SchedulePeriod;
+use App\Models\TimePeriod;
+use Illuminate\Support\Facades\DB;
+
 class DocterController extends AuthController
 {
-    public function docterConsultList()
+    public function docterList()
     {
         $req = request()->post();
         $this->validate(request(), [
-            'type' => 'required|in:1,2',
-            'docter_name' => 'max:50',
-            'organization_name' => 'max:255',
+            'list_type' => 'in:1,2,3',
+            'city_id' => 'integer',
+            'name' => 'max:255',
+            'latitude' => 'numeric',
+            'longitude' => 'numeric',
+            'sort_type' => 'in:1,2,3',
+            'schedule_date' => 'required_if:list_type,3|date',
+            'time_period_id' => 'required_if:list_type,3|integer',
         ]);
+        $user = $this->user;
+
+        $latitude = !empty($req['latitude']) ? $req['latitude'] : $user['latitude'];
+        $longitude = !empty($req['longitude']) ? $req['longitude'] : $user['longitude'];
+        $distance_field = get_distance_field($latitude, $longitude);
+
+        $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);
+        if ($req['list_type'] == 1) {
+            $builder->where('is_phone', 1);
+        }
+        if ($req['list_type'] == 2) {
+            $builder->where('is_chat', 1);
+        }
+        if ($req['list_type'] == 3) {
+            $builder->where('is_appoint', 1);
+        }
+        if (!empty($req['name'])) {
+            $name = $req['name'];
+            $organizations = Organization::with('docter')->select(['id'])->where('name', 'like', '%'.$name.'%')->get()->toArray();
+            $docterIds = [];
+            foreach ($organizations as $k => $v) {
+                $tmpDocterIds = array_column($v['docter'], 'id');
+                $docterIds = array_merge($docterIds, $tmpDocterIds);
+            }
+            $docterIds = array_values(array_unique($docterIds));
+            $builder->where(function ($query) use($name, $docterIds) {
+                $query->where('name', 'like', '%'.$name.'%')->orWhereIn('id', $docterIds);
+            });
+        }
+
+        if ($req['list_type'] == 3) {
+            $docterIds2 = SchedulePeriod::where('time_period_id', $req['time_period_id'])->where('schedule_date', $req['schedule_date'])->pluck('docter_id');
+            $builder->whereIn('id', $docterIds2);
+        }
+
+        if (!empty($req['sort_type'])) {
+            if ($req['sort_type'] == 1) {
+                $builder->orderBy('distance', 'asc');
+            }
+            elseif ($req['sort_type'] == 2) {
+                $builder->orderBy('eva_num', 'desc');
+            }
+            elseif ($req['sort_type'] == 3) {
+                $builder->orderBy('service_persons', 'desc');
+            }
+        }
+        $data = $builder->paginate();
+
+        return out($data);
+    }
+
+    public function docterDetail()
+    {
+        $req = request()->post();
+        $this->validate(request(), [
+            'docter_id' => 'required|integer',
+            'list_type' => 'in:1,2,3',
+            'schedule_date' => 'required_if:list_type,3|date',
+            'time_period_id' => 'required_if:list_type,3|integer',
+            'latitude' => 'numeric',
+            'longitude' => 'numeric',
+        ]);
+        $user = $this->user;
+
+        $latitude = !empty($req['latitude']) ? $req['latitude'] : $user['latitude'];
+        $longitude = !empty($req['longitude']) ? $req['longitude'] : $user['longitude'];
+        $distance_field = get_distance_field($latitude, $longitude);
 
+        $data = 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('id', $req['docter_id'])->where('status', 1)->first()->toArray();
 
+        $data['organization'] = null;
+        if (!empty($req['list_type']) && $req['list_type'] == 3) {
+            $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();
+            $data['organization'] = $schedulePeriod['organization'];
+        }
+
+        return ($data);
+    }
+
+    public function schedulePeriodList()
+    {
+        $req = request()->post();
+        $this->validate(request(), [
+            'docter_id' => 'required|integer',
+            'per_page' => 'integer',
+        ]);
+
+        $data = Schedule::with('schedulePeriod.timePeriod')->where('docter_id', $req['docter_id'])->where('schedule_day', '>=', date('Ymd'))->paginate($req['per_page']??15);
+
+        return out($data);
+    }
+
+    public function timePeriodList()
+    {
+        $data = TimePeriod::select(['id', 'start_time_period', 'end_time_period'])->get();
+        return out($data);
     }
 }

+ 17 - 0
app/Models/Docter.php

xqd
@@ -10,5 +10,22 @@ namespace App\Models;
 
 class Docter extends BaseModel
 {
+    protected $casts = [
+        'label' => 'json',
+    ];
 
+    public function office()
+    {
+        return $this->belongsTo(Office::class)->select(['id', 'name']);
+    }
+
+    public function qualification()
+    {
+        return $this->belongsTo(Qualification::class)->select(['id', 'name']);
+    }
+
+    public function organization()
+    {
+        return $this->belongsToMany(Organization::class);
+    }
 }

+ 14 - 0
app/Models/Office.php

xqd
@@ -0,0 +1,14 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: zilongs
+ * Date: 20-9-30
+ * Time: 下午2:20
+ */
+
+namespace App\Models;
+
+class Office extends BaseModel
+{
+
+}

+ 17 - 0
app/Models/Organization.php

xqd
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: zilongs
+ * Date: 20-9-29
+ * Time: 下午2:35
+ */
+
+namespace App\Models;
+
+class Organization extends BaseModel
+{
+    public function docter()
+    {
+        return $this->belongsToMany(Docter::class);
+    }
+}

+ 14 - 0
app/Models/Qualification.php

xqd
@@ -0,0 +1,14 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: zilongs
+ * Date: 20-9-30
+ * Time: 下午2:20
+ */
+
+namespace App\Models;
+
+class Qualification extends BaseModel
+{
+
+}

+ 17 - 0
app/Models/Schedule.php

xqd
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: zilongs
+ * Date: 20-9-30
+ * Time: 下午2:47
+ */
+
+namespace App\Models;
+
+class Schedule extends BaseModel
+{
+    public function schedulePeriod()
+    {
+        return $this->hasMany(SchedulePeriod::class)->select(['id', 'schedule_id', 'organization_id', 'time_period_id', 'order_num']);
+    }
+}

+ 22 - 0
app/Models/SchedulePeriod.php

xqd
@@ -0,0 +1,22 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: zilongs
+ * Date: 20-9-30
+ * Time: 下午2:51
+ */
+
+namespace App\Models;
+
+class SchedulePeriod extends BaseModel
+{
+    public function organization()
+    {
+        return $this->belongsTo(Organization::class);
+    }
+
+    public function timePeriod()
+    {
+        return $this->belongsTo(TimePeriod::class)->select(['id', 'start_time_period', 'end_time_period']);
+    }
+}

+ 14 - 0
app/Models/TimePeriod.php

xqd
@@ -0,0 +1,14 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: zilongs
+ * Date: 20-9-30
+ * Time: 下午4:32
+ */
+
+namespace App\Models;
+
+class TimePeriod extends BaseModel
+{
+
+}