浏览代码

Merge branch 'master' of http://git.9026.com/swdz-WangHaijun/BaoMa

whj 4 年之前
父节点
当前提交
aaca7b2dc0

+ 36 - 1
app/Http/Controllers/Api/V1/DocterController.php

xqd xqd xqd xqd xqd
@@ -14,6 +14,8 @@ use App\Models\DocterServiceTime;
 use App\Models\Organization;
 use App\Models\Schedule;
 use App\Models\SchedulePeriod;
+use App\Models\ServicePack;
+use App\Models\Team;
 use App\Models\TimePeriod;
 use DB;
 
@@ -31,6 +33,7 @@ class DocterController extends AuthController
             'sort_type' => 'in:0,1,2,3',
             'schedule_date' => 'required_if:list_type,3|date',
             'time_period_id' => 'required_if:list_type,3|integer',
+            'is_pack_docter' => 'in:0,1',
         ]);
         $user = $this->user;
 
@@ -83,6 +86,27 @@ class DocterController extends AuthController
             $builder->whereNotIn('id', $docterIds3);
         }
 
+        if (!empty($req['is_pack_docter'])) {
+            $team_ids = ServicePack::pluck('team_id')->toArray();
+            $team_id_arr = [];
+            foreach ($team_ids as $k => $v) {
+                if (!empty($v) && is_array($v)) {
+                    $team_id_arr = array_merge($team_id_arr, $v);
+                }
+            }
+            $team_id_arr = array_values(array_unique($team_id_arr));
+            $teams = Team::with(['docter'])->whereIn('id', $team_id_arr)->get()->toArray();
+            $docterIds4 = [];
+            foreach ($teams as $k => $v) {
+                foreach ($v['docter'] as $k1 => $v1) {
+                    if (!in_array($v1['id'], $docterIds4)) {
+                        $docterIds4[] = $v1['id'];
+                    }
+                }
+            }
+            $builder->whereIn('id', $docterIds4);
+        }
+
         if (!empty($req['sort_type'])) {
             if ($req['sort_type'] == 1) {
                 $builder->orderBy('distance', 'asc');
@@ -108,6 +132,9 @@ class DocterController extends AuthController
             if (!empty($req['city_id'])) {
                 $builder2->whereIn('id', $cityDocterIds);
             }
+            if (!empty($req['is_pack_docter'])) {
+                $builder->whereIn('id', $docterIds4);
+            }
             if (!empty($req['sort_type'])) {
                 if ($req['sort_type'] == 1) {
                     $builder2->orderBy('distance', 'asc');
@@ -160,19 +187,27 @@ class DocterController extends AuthController
         $req = request()->post();
         $this->validate(request(), [
             'docter_id' => 'required|integer',
+            'organization_id' => 'integer',
             'per_page' => 'integer',
             'latitude' => 'numeric',
             'longitude' => 'numeric',
         ]);
         $user = $this->user;
 
-        $data = Schedule::with(['schedulePeriod.timePeriod', 'schedulePeriod.organization'])->where('docter_id', $req['docter_id'])->where('schedule_day', '>=', date('Ymd'))->paginate($req['per_page']??15)->toArray();
+        $builder = Schedule::with(['schedulePeriod.timePeriod', 'schedulePeriod.organization'])->where('docter_id', $req['docter_id'])->where('schedule_day', '>=', date('Ymd'));
+        if (!empty($req['organization_id'])) {
+            $builder->where('organization_id', $req['organization_id']);
+        }
+        $data = $builder->paginate($req['per_page']??15)->toArray();
         if (!empty($data)) {
             foreach ($data['data'] as $k => &$v) {
                 foreach ($v['schedule_period'] as $k1 => &$v1) {
                     if (!empty($v1['organization'])) {
                         $v1['organization']['distance'] = get_user_distance($user, $v1['organization']['latitude'], $v1['organization']['longitude']);
                     }
+
+                    $can_appoint_num = $v['per_time_num'] - $v1['order_num'];
+                    $v1['can_appoint_num'] = $can_appoint_num < 0 ? 0 : $can_appoint_num;
                 }
             }
         }

+ 54 - 4
app/Http/Controllers/Api/V1/OrderController.php

xqd xqd xqd xqd xqd xqd xqd
@@ -8,6 +8,7 @@
 
 namespace App\Http\Controllers\Api\V1;
 
+use App\Models\Docter;
 use App\Models\Nurse;
 use App\Models\Order;
 use App\Models\OrderNurse;
@@ -16,6 +17,7 @@ use App\Models\OrderPatient;
 use App\Models\OrderVaccine;
 use App\Models\Patient;
 use App\Models\Payment;
+use App\Models\SchedulePeriod;
 use App\Models\ServicePack;
 use App\Models\Team;
 use App\Models\TimePeriod;
@@ -24,6 +26,7 @@ use App\Models\UserCoupon;
 use App\Models\Vaccine;
 use EasyWeChat\Factory;
 use DB;
+use Exception;
 
 class OrderController extends AuthController
 {
@@ -166,7 +169,7 @@ class OrderController extends AuthController
             }
 
             DB::commit();
-        } catch (\Exception $e) {
+        } catch (Exception $e) {
             DB::rollBack();
             return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
         }
@@ -341,7 +344,7 @@ class OrderController extends AuthController
             }
 
             DB::commit();
-        } catch (\Exception $e) {
+        } catch (Exception $e) {
             DB::rollBack();
             return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
         }
@@ -488,7 +491,7 @@ class OrderController extends AuthController
             }
 
             DB::commit();
-        } catch (\Exception $e) {
+        } catch (Exception $e) {
             DB::rollBack();
             return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
         }
@@ -618,11 +621,58 @@ class OrderController extends AuthController
             $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
 
             DB::commit();
-        } catch (\Exception $e) {
+        } catch (Exception $e) {
             DB::rollBack();
             return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
         }
 
         return out($config);
     }
+
+    public function orderCancel()
+    {
+        $req = request()->post();
+        $this->validate(request(), [
+            'order_id' => 'required|integer'
+        ]);
+
+        $order = Order::with(['orderPatient'])->where('id', $req['order_id'])->first();
+        if ($order['order_status'] == 4) {
+            return out(null, 10001, '订单已完成,不能取消了');
+        }
+        if ($order['order_status'] == 5) {
+            return out(null, 10002, '订单已取消了,请勿重复操作');
+        }
+
+        if ($order['product_type'] == 3) {
+            if ($order['order_patient']['appoint_start_time'] + 1800 > time()) {
+                return out(null, 10003, '预约时间临近,不能取消订单了');
+            }
+
+            DB::beginTransaction();
+            try {
+                //改变订单状态
+                Order::where('id', $req['order_id'])->update(['order_status' => 5]);
+                //退钱到余额
+                User::changeBalance($order['user_id'], $order['payment_amount'], 4, $order['id'], '取消订单退款');
+                //预约时间段的订单数减1
+                $orderPatient = OrderPatient::select(['time_period_id', 'appoint_start_time'])->where('order_id', $req['order_id'])->first();
+                $schedule_date = date('Y-m-d', $orderPatient['appoint_start_time']);
+                SchedulePeriod::where('docter_id', $order['docter_id'])->where('organization_id', $order['organization_id'])->where('time_period_id', $orderPatient['time_period_id'])->where('schedule_date', $schedule_date)->decrement('order_num');
+                //更新医生的服务人数
+                Docter::where('id', $order['docter_id'])->decrement('service_persons');
+
+                DB::commit();
+            } catch (Exception $e) {
+                DB::rollBack();
+
+                return out(null, 500, '取消订单失败,请稍后重试', $e->getMessage());
+            }
+        }
+        else {
+            return out(null, 10001, '暂时只支持取消门诊预约的订单');
+        }
+
+        return out();
+    }
 }

+ 0 - 5
app/Http/Controllers/Api/V1/PayCallbackController.php

xqd
@@ -61,11 +61,6 @@ class PayCallbackController extends Controller
                             'payment_time' => time(),
                         ]);
 
-                        if (!empty($order['docter_id'])) {
-                            //更新医生的服务人数
-                            Docter::where('id', $order['docter_id'])->increment('service_persons');
-                        }
-
                         Order::payCompletedHandle($order['id']);
 
                         // 提交事务

+ 1 - 1
app/Http/Controllers/Api/V1/VaccineController.php

xqd
@@ -22,7 +22,7 @@ class VaccineController extends AuthController
             'sort_type' => 'in:0,1,2',
         ]);
 
-        $builder = Vaccine::join('organization_vaccines', 'organization_vaccines.vaccine_id', '=', 'vaccines.id')->select(['vaccines.id', 'vaccines.type', 'vaccines.price', 'vaccines.name', 'vaccines.remark', 'vaccines.supplier', 'organization_vaccines.stock']);
+        $builder = Vaccine::join('organization_vaccines', 'organization_vaccines.vaccine_id', '=', 'vaccines.id')->select(['vaccines.id', 'vaccines.type', 'vaccines.price', 'vaccines.name', 'vaccines.remark', 'vaccines.supplier', 'organization_vaccines.stock'])->where('organization_vaccines.stock', '>', 0);
         if (!empty($req['type'])) {
             $builder->where('vacciness.type', $req['type']);
         }

+ 13 - 1
app/Models/Order.php

xqd xqd
@@ -76,7 +76,7 @@ class Order extends BaseModel
     //支付完成的处理方法
     public static function payCompletedHandle($order_id)
     {
-        $order = Order::select(['user_id', 'docter_id', 'product_type', 'total_amount', 'payment_type', 'payment_amount', 'order_sn', 'pay_order_pack_id'])->where('id', $order_id)->first();
+        $order = Order::select(['user_id', 'docter_id', 'product_type', 'total_amount', 'payment_type', 'payment_amount', 'order_sn', 'pay_order_pack_id', 'organization_id'])->where('id', $order_id)->first();
         //发送下单消息
         if ($order['product_type'] < 6) {
             $product_type_text = config('config.product_type_map')[$order['product_type']];
@@ -105,6 +105,18 @@ class Order extends BaseModel
             OrderPack::deductPackData($order['pay_order_pack_id'], $order['payment_type'], $order['payment_amount']);
         }
 
+        if (!empty($order['docter_id'])) {
+            //更新医生的服务人数
+            Docter::where('id', $order['docter_id'])->increment('service_persons');
+        }
+
+        //如果是门诊预约,预约时间段的订单数要加1
+        if ($order['product_type'] == 3) {
+            $orderPatient = OrderPatient::select(['time_period_id', 'appoint_start_time'])->where('order_id', $order_id)->first();
+            $schedule_date = date('Y-m-d', $orderPatient['appoint_start_time']);
+            SchedulePeriod::where('docter_id', $order['docter_id'])->where('organization_id', $order['organization_id'])->where('time_period_id', $orderPatient['time_period_id'])->where('schedule_date', $schedule_date)->increment('order_num');
+        }
+
         return true;
     }