zilong před 4 roky
rodič
revize
0a4520c6a0

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

xqd
@@ -566,9 +566,8 @@ class OrderController extends AuthController
                 return out(null, 601, '余额不足');
             }
         }
-        if ($order['payment_type'] == 3) {
-            OrderPack::checkUserServicePack($order['pay_order_pack_id'], $user['id'], $order['product_type']);
-        }
+
+        Order::checkOrder($req['order_id']);
 
         $config = null;
         DB::beginTransaction();

+ 57 - 1
app/Models/Order.php

xqd xqd
@@ -39,7 +39,7 @@ class Order extends BaseModel
     }
     /**
      * 用户医生关注表
-     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
+     * @return
      * @author Liu-Yh
      * Create By 2020/11/18 11:06
      */
@@ -286,4 +286,60 @@ class Order extends BaseModel
 
         return !empty($secret_no) ? $secret_no : '';
     }
+
+    public static function checkOrder($order_id)
+    {
+        $order = Order::with(['orderPatient', 'orderVaccine'])->where('id', $order_id)->first()->toArray();
+        $product_type = $order['product_type'];
+        if (in_array($product_type, [1,2])) {
+            //判断是否在服务时间内
+            $now_line = (int)date('Hi');
+            if (!DocterServiceTime::where('docter_id', $order['docter_id'])->where('type', $product_type)->where('start_time_line', '<=', $now_line)->where('end_time_line', '>', $now_line)->exists()) {
+                exit_out(null, 10011, '当前不在医生服务时间内,不能下单');
+            }
+
+            //图文咨询订单未结束时不能针对同一医生再次下图文订单
+            if ($product_type == 2 && Order::where('docter_id', $order['docter_id'])->where('product_type', 2)->where('user_id', $order['user_id'])->whereIn('order_status', [2,3])->exists()) {
+                exit_out(null, 10012, '您已经下过该医生的图文订单了,并且订单还未完成');
+            }
+        }
+        //检查号源
+        elseif ($product_type == 3) {
+            $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
+            $schedulePeriod = SchedulePeriod::where('docter_id', $order['docter_id'])->where('time_period_id', $order['order_patient']['time_period_id'])->where('schedule_date', $schedule_date)->where('schedule_type', 1)->first();
+            if (empty($schedulePeriod)) {
+                exit_out(null, 10012, '医生无该时间段的排班');
+            }
+            $docterSettings = DocterSetting::select(['service_num'])->where('docter_id', $order['docter_id'])->where('type', 1)->first();
+            if ($docterSettings['service_num'] <= $schedulePeriod['order_sn']) {
+                exit_out(null, 10014, '医生该时间段已经预约满了');
+            }
+        }
+        elseif (in_array($product_type, [4,5])) {
+            $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
+            $schedule_type_map = [4 => 2, 5 => 3];
+            $schedulePeriod = SchedulePeriod::where('time_period_id', $order['order_patient']['time_period_id'])->where('schedule_date', $schedule_date)->where('organization_id', $order['organization_id'])->where('schedule_type', $schedule_type_map[$product_type])->first();
+            if (empty($schedulePeriod)) {
+                exit_out(null, 10013, '机构无该时间段的排班');
+            }
+            $docterSettings = DocterSetting::select(['service_num'])->where('org_id', $order['organization_id'])->where('type', $schedule_type_map[$product_type])->first();
+            if ($docterSettings['service_num'] <= $schedulePeriod['order_sn']) {
+                exit_out(null, 10015, '机构该时间段已经预约满了');
+            }
+        }
+
+        //疫苗预约检查库存是否足够
+        if ($product_type == 4) {
+            $stock = OrganizationVaccine::where('org_id', $order['organization_id'])->where('vaccine_id', $order['order_vaccine']['vaccine_id'])->value('stock');
+            if ($stock <= 0) {
+                exit_out(null, 10009, '该疫苗库存不足');
+            }
+        }
+
+        if ($order['payment_type'] == 3) {
+            OrderPack::checkUserServicePack($order['pay_order_pack_id'], $order['user_id'], $product_type);
+        }
+
+        return true;
+    }
 }