zilong 4 年之前
父节点
当前提交
0f31c9f7f1
共有 5 个文件被更改,包括 313 次插入35 次删除
  1. 270 34
      app/Http/Controllers/Api/V1/OrderController.php
  2. 14 0
      app/Models/Nurse.php
  3. 1 1
      app/Models/Order.php
  4. 14 0
      app/Models/ServicePack.php
  5. 14 0
      app/Models/Vaccine.php

+ 270 - 34
app/Http/Controllers/Api/V1/OrderController.php

xqd xqd xqd xqd xqd xqd xqd
@@ -8,22 +8,28 @@
 
 namespace App\Http\Controllers\Api\V1;
 
+use App\Models\Nurse;
 use App\Models\Order;
+use App\Models\OrderNurse;
+use App\Models\OrderPack;
 use App\Models\OrderPatient;
+use App\Models\OrderVaccine;
 use App\Models\Patient;
 use App\Models\Payment;
+use App\Models\ServicePack;
 use App\Models\TimePeriod;
 use App\Models\UserCoupon;
+use App\Models\Vaccine;
 use EasyWeChat\Factory;
 use DB;
 
 class OrderController extends AuthController
 {
-    public function placeOrder()
+    public function consultPlaceOrder()
     {
         $req = request()->post();
         $this->validate(request(), [
-            'product_type' => 'required|in:1,2,3',
+            'product_type' => 'required|in:1,2',
             'docter_id' => 'required|integer',
             'patient_id' => 'required|integer',
             'total_amount' => 'required|integer',
@@ -32,9 +38,6 @@ class OrderController extends AuthController
             'phone_minutes' => 'required_if:product_type,1|integer',
             'symptoms' => 'required_if:product_type,2|max:2000',
             'medical_imgs' => 'required_if:product_type,2|json|max:3000',
-            'schedule_date' => 'required_if:product_type,3|date',
-            'time_period_id' => 'required_if:product_type,3|integer',
-            'organization_id' => 'required_if:product_type,3|integer'
         ]);
         $user = $this->user;
 
@@ -44,7 +47,16 @@ class OrderController extends AuthController
             $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], $req['product_type']);
         }
         $payment_amount = $req['total_amount'] - $discount_amount;
+        $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
 
+        $order_status = $payment_status = 1;
+        $payment_time = 0;
+        if ($payment_amount == 0) {
+            $order_status = $payment_status = 2;
+            $payment_time = time();
+        }
+
+        $config = null;
         DB::beginTransaction();
         try {
             //保存订单数据
@@ -56,6 +68,9 @@ class OrderController extends AuthController
                 'payment_amount' => $payment_amount,
                 'discount_amount' => $discount_amount,
                 'patient_id' => $req['patient_id'],
+                'order_status' => $order_status,
+                'payment_status' => $payment_status,
+                'payment_time' => $payment_time,
             ]);
             $order_sn = build_sn($order['id']);
             Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
@@ -71,40 +86,261 @@ class OrderController extends AuthController
                 $addPatient['symptoms'] = $req['symptoms'];
                 $addPatient['medical_imgs'] = $req['medical_imgs'];
             }
-            elseif ($req['product_type'] == 3) {
-                $addPatient['organization_id'] = $req['organization_id'];
-                $addPatient['time_period_id'] = $req['time_period_id'];
-                $timePeriod = TimePeriod::where('id', $req['time_period_id'])->first();
-                $addPatient['appoint_start_time'] = strtotime($req['schedule_date'].' '.$timePeriod['start_time_period'].':00');
-                $addPatient['appoint_end_time'] = strtotime($req['schedule_date'].' '.$timePeriod['end_time_period'].':00');
-            }
             OrderPatient::create($addPatient);
             //生成支付交易单
-            $trade_sn = build_sn($order['id'], 3, 'T');
-            $payBody = '超级宝妈-'.config('config.product_type_map')[$req['product_type']];
-            Payment::create([
+            if ($payment_amount > 0) {
+                $trade_sn = build_sn($order['id'], 3, 'T');
+                $payBody = '超级宝妈-'.config('config.product_type_map')[$req['product_type']];
+                Payment::create([
+                    'user_id' => $user['id'],
+                    'order_id' => $order['id'],
+                    'trade_sn' => $trade_sn,
+                    'amount' => $payment_amount,
+                    'remark' => $payBody,
+                ]);
+                //请求支付
+                $payment = Factory::payment(config('config.wechat_pay'));
+                $result = $payment->order->unify([
+                    'body' => $payBody,
+                    'out_trade_no' => $trade_sn,
+                    'total_fee' => $payment_amount,
+                    'trade_type' => 'JSAPI',
+                    'openid' => $user['openid'],
+                ]);
+
+                if (empty($result['prepay_id'])) {
+                    $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
+                    return out(null, 702, $errorMsg);
+                }
+
+                $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
+            }
+
+            DB::commit();
+        } catch (\Exception $e) {
+            DB::rollBack();
+            return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
+        }
+
+        return out($config);
+    }
+
+    public function appointPlaceOrder()
+    {
+        $req = request()->post();
+        $this->validate(request(), [
+            'product_type' => 'required|in:3,4,5',
+            'patient_id' => 'required|integer',
+            'organization_id' => 'required|integer',
+            'schedule_date' => 'required|date',
+            'time_period_id' => 'required|integer',
+            'total_amount' => 'required|integer',
+            'user_coupon_id' => 'integer',
+            'docter_id' => 'required_if:product_type,3|integer',
+            'vaccine_id' => 'required_if:product_type,4|integer',
+            'nurse_ids' => 'required_if:product_type,5|json',
+        ]);
+        $user = $this->user;
+
+        $product_type = $req['product_type'];
+
+        if ($req['product_type'] == 4) {
+            $vaccine = Vaccine::select(['type'])->where('id', $req['vaccine_id'])->first();
+            if ($vaccine['type'] == 2) {
+                if (empty($req['total_amount'])) {
+                    return out(null, 10001, '总价不能为0');
+                }
+            }
+        }
+
+        $discount_amount = 0;
+        if (!empty($req['user_coupon_id'])) {
+            //计算优惠金额
+            $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], $product_type);
+        }
+        $payment_amount = $req['total_amount'] - $discount_amount;
+        $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
+
+        $order_status = $payment_status = 1;
+        $payment_time = 0;
+        if ($payment_amount == 0) {
+            $order_status = $payment_status = 2;
+            $payment_time = time();
+        }
+
+        $config = null;
+        DB::beginTransaction();
+        try {
+            //保存订单数据
+            $order = Order::create([
                 'user_id' => $user['id'],
-                'order_id' => $order['id'],
-                'trade_sn' => $trade_sn,
-                'amount' => $payment_amount,
-                'remark' => $payBody,
-            ]);
-            //请求支付
-            $payment = Factory::payment(config('config.wechat_pay'));
-            $result = $payment->order->unify([
-                'body' => $payBody,
-                'out_trade_no' => $trade_sn,
-                'total_fee' => $payment_amount,
-                'trade_type' => 'JSAPI',
-                'openid' => $user['openid'],
+                'docter_id' => $req['docter_id'] ?? 0,
+                'patient_id' => $req['patient_id'],
+                'organization_id' => $req['organization_id'],
+                'product_type' => $product_type,
+                'order_status' => $order_status,
+                'payment_status' => $payment_status,
+                'total_amount' => $req['total_amount'],
+                'payment_amount' => $payment_amount,
+                'discount_amount' => $discount_amount,
+                'payment_time' => $payment_time,
             ]);
+            $order_sn = build_sn($order['id']);
+            Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
+            //保存订单患者信息
+            $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship', 'info', 'card_type', 'card_number'])->where('id', $req['patient_id'])->first()->toArray();
+            $addPatient['order_id'] = $order['id'];
+            $addPatient['patient_id'] = $req['patient_id'];
+            $addPatient['organization_id'] = $req['organization_id'];
+            $addPatient['time_period_id'] = $req['time_period_id'];
+            $timePeriod = TimePeriod::where('id', $req['time_period_id'])->first();
+            $addPatient['appoint_start_time'] = strtotime($req['schedule_date'].' '.$timePeriod['start_time_period'].':00');
+            $addPatient['appoint_end_time'] = strtotime($req['schedule_date'].' '.$timePeriod['end_time_period'].':00');
+            $orderPatient = OrderPatient::create($addPatient);
+            //保存订单疫苗信息
+            if ($req['product_type'] == 4) {
+                $addVaccine = Vaccine::select(['id vaccine_id', 'type vaccine_type', 'price vaccine_price', 'name vaccine_name', 'remark vaccine_remark', 'supplier vaccine_supplier'])->where('id', $req['vaccine_id'])->first()->toArray();
+                $addVaccine['order_id'] = $order['id'];
+                $addVaccine['order_patient_id'] = $orderPatient['id'];
+                OrderVaccine::create($addVaccine);
+            }
+            //保存儿保订单信息
+            if ($req['product_type'] == 5) {
+                $nurse_ids = json_decode($req['nurse_ids'], true);
+                foreach ($nurse_ids as $k => $v) {
+                    $addNurse = Nurse::select(['id nurse_id', 'price nurse_price', 'name nurse_name', 'remark nurse_remark'])->where('id', $v)->first()->toArray();
+                    $addNurse['order_id'] = $order['id'];
+                    $addNurse['order_patient_id'] = $orderPatient['id'];
+                    OrderNurse::create($addNurse);
+                }
+            }
+            //生成支付交易单
+            if ($payment_amount > 0) {
+                $trade_sn = build_sn($order['id'], 3, 'T');
+                $payBody = '超级宝妈-'.config('config.product_type_map')[$product_type];
+                Payment::create([
+                    'user_id' => $user['id'],
+                    'order_id' => $order['id'],
+                    'trade_sn' => $trade_sn,
+                    'amount' => $payment_amount,
+                    'remark' => $payBody,
+                ]);
+                //请求支付
+                $payment = Factory::payment(config('config.wechat_pay'));
+                $result = $payment->order->unify([
+                    'body' => $payBody,
+                    'out_trade_no' => $trade_sn,
+                    'total_fee' => $payment_amount,
+                    'trade_type' => 'JSAPI',
+                    'openid' => $user['openid'],
+                ]);
+
+                if (empty($result['prepay_id'])) {
+                    $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
+                    return out(null, 702, $errorMsg);
+                }
 
-            if (empty($result['prepay_id'])) {
-                $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
-                return out(null, 702, $errorMsg);
+                $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
             }
 
-            $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
+            DB::commit();
+        } catch (\Exception $e) {
+            DB::rollBack();
+            return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
+        }
+
+        return out($config);
+    }
+
+    public function packPlaceOrder()
+    {
+        $req = request()->post();
+        $this->validate(request(), [
+            'patient_id' => 'required|integer',
+            'total_amount' => 'required|integer',
+            'user_coupon_id' => 'integer',
+            'service_pack_id' => 'required|integer',
+            'is_security' => 'required|in:0,1',
+            'guardian_name' => 'required|max:50',
+            'relationship_type' => 'required|integer',
+        ]);
+        $user = $this->user;
+
+        $discount_amount = 0;
+        if (!empty($req['user_coupon_id'])) {
+            //计算优惠金额
+            $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], 6);
+        }
+        $payment_amount = $req['total_amount'] - $discount_amount;
+        $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
+
+        $order_status = $payment_status = 1;
+        $payment_time = 0;
+        if ($payment_amount == 0) {
+            $order_status = $payment_status = 2;
+            $payment_time = time();
+        }
+
+        $config = null;
+        DB::beginTransaction();
+        try {
+            //保存订单数据
+            $order = Order::create([
+                'user_id' => $user['id'],
+                'patient_id' => $req['patient_id'],
+                'product_type' => 6,
+                'docter_id' => $req['docter_id'],
+                'total_amount' => $req['total_amount'],
+                'payment_amount' => $payment_amount,
+                'discount_amount' => $discount_amount,
+                'order_status' => $order_status,
+                'payment_status' => $payment_status,
+                'payment_time' => $payment_time,
+            ]);
+            $order_sn = build_sn($order['id']);
+            Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
+            //保存订单患者信息
+            $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship', 'info', 'card_type', 'card_number'])->where('id', $req['patient_id'])->first()->toArray();
+            $addPatient['order_id'] = $order['id'];
+            $addPatient['patient_id'] = $req['patient_id'];
+            OrderPatient::create($addPatient);
+            //保存订单服务包表
+            $addPack = ServicePack::select(['id service_pack_id', 'name pack_name', 'intro pack_intro', 'price pack_price', 'team_id', 'phone_minutes', 'chat_num', 'appoint_num', 'vaccine_limit_amount', 'nurses_limit_amount', 'effective_days'])->where('id', $req['service_pack_id'])->first()->toArray();
+            $addPack['order_id'] = $order['id'];
+            $addPack['is_security'] = $req['is_security'];
+            $addPack['guardian_name'] = $req['guardian_name'];
+            $addPack['relationship_type'] = $req['relationship_type'];
+            $addPack['start_time'] = time();
+            $addPack['end_time'] = time() + $addPack['effective_days']*24*3600;
+            OrderPack::create($addPack);
+            //生成支付交易单
+            if ($payment_amount > 0) {
+                $trade_sn = build_sn($order['id'], 3, 'T');
+                $payBody = '超级宝妈-'.config('config.product_type_map')[6];
+                Payment::create([
+                    'user_id' => $user['id'],
+                    'order_id' => $order['id'],
+                    'trade_sn' => $trade_sn,
+                    'amount' => $payment_amount,
+                    'remark' => $payBody,
+                ]);
+                //请求支付
+                $payment = Factory::payment(config('config.wechat_pay'));
+                $result = $payment->order->unify([
+                    'body' => $payBody,
+                    'out_trade_no' => $trade_sn,
+                    'total_fee' => $payment_amount,
+                    'trade_type' => 'JSAPI',
+                    'openid' => $user['openid'],
+                ]);
+
+                if (empty($result['prepay_id'])) {
+                    $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
+                    return out(null, 702, $errorMsg);
+                }
+
+                $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
+            }
 
             DB::commit();
         } catch (\Exception $e) {
@@ -126,7 +362,7 @@ class OrderController extends AuthController
         ]);
         $user = $this->user;
 
-        $builder = Order::with(['docter' => ['office', 'qualification'], 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine'])->where('user_id', $user['id']);
+        $builder = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine'])->where('user_id', $user['id']);
         if (!empty($req['product_type'])) {
             $builder->where('product_type', $req['product_type']);
         }
@@ -164,7 +400,7 @@ class OrderController extends AuthController
         ]);
         $user = $this->user;
 
-        $data = Order::with(['docter' => ['office', 'qualification'], 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine'])->where('id', $req['order_id'])->where('user_id', $user['id'])->first();
+        $data = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine'])->where('id', $req['order_id'])->where('user_id', $user['id'])->first();
 
         return out($data);
     }

+ 14 - 0
app/Models/Nurse.php

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

+ 1 - 1
app/Models/Order.php

xqd
@@ -17,7 +17,7 @@ class Order extends BaseModel
 
     public function orderPatient()
     {
-        return $this->hasOne(orderPatient::class);
+        return $this->hasOne(OrderPatient::class);
     }
 
     public function orderPack()

+ 14 - 0
app/Models/ServicePack.php

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

+ 14 - 0
app/Models/Vaccine.php

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