zilong пре 4 година
родитељ
комит
e5e711eab9
2 измењених фајлова са 21 додато и 11 уклоњено
  1. 2 11
      app/Http/Controllers/Api/V1/OrderController.php
  2. 19 0
      app/Models/Order.php

+ 2 - 11
app/Http/Controllers/Api/V1/OrderController.php

xqd
@@ -647,22 +647,13 @@ class OrderController extends AuthController
         }
 
         if ($order['product_type'] == 3) {
-            if ($order['order_patient']['appoint_start_time'] + 1800 > time()) {
+            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');
+                Order::orderCancel($req['order_id']);
 
                 DB::commit();
             } catch (Exception $e) {

+ 19 - 0
app/Models/Order.php

xqd
@@ -152,4 +152,23 @@ class Order extends BaseModel
 
         return '';
     }
+
+    //取消订单,建议是在事务里面去调用
+    public static function orderCancel($order_id)
+    {
+        $order = Order::with(['orderPatient'])->where('id', $order_id)->first();
+        //改变订单状态
+        Order::where('id', $order_id)->update(['order_status' => 5]);
+        //退钱到余额
+        User::changeBalance($order_id, $order['payment_amount'], 4, $order['id'], '取消订单退款');
+        //如果距离预约时间还有半个小时以上,那么预约时间段的订单数减1
+        if ($order['order_patient']['appoint_start_time'] - 1800 > time()) {
+            $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
+            SchedulePeriod::where('docter_id', $order['docter_id'])->where('organization_id', $order['organization_id'])->where('time_period_id', $order['order_patient']['time_period_id'])->where('schedule_date', $schedule_date)->decrement('order_num');
+        }
+        //更新医生的服务人数
+        Docter::where('id', $order['docter_id'])->decrement('service_persons');
+
+        return true;
+    }
 }