PHP + Mysql实现防止货品超卖逻辑

 1 public function NotifyProcess($data, &$msg)
 2     {
 3 //        $data = $this->data;
 4         if ($data['result_code'] == 'SUCCESS') {
 5             $orderNo = $data['out_trade_no'];
 6             Db::startTrans();
 7             try {
 8                 $order = Order::where('order_no', '=', $orderNo)->lock(true)->find();
 9                 if ($order->status == 1) {
10                     $service = new OrderService();
11                     $status = $service->checkOrderStock($order->id);
12                     if ($status['pass']) {
13                         $this->updateOrderStatus($order->id, true);
14                         $this->reduceStock($status);
15                     } else {
16                         $this->updateOrderStatus($order->id, false);
17                     }
18                 }
19                 Db::commit();
20             } catch (Exception $ex) {
21                 Db::rollback();
22                 Log::error($ex);
23                 // 如果出现异常,向微信返回false,请求重新发送通知
24                 return false;
25             }
26         }
27         return true;
28     }