コード例 #1
0
ファイル: Cart.php プロジェクト: swcug/bzfshop
 /**
  * 把购物车中的信息保存到数据库订单中,如果订单已经存在,则更新订单数据,
  * 注意:这个方法必须在购物车计算完成之后调用,否则无法正确保存计算之后的数据
  *
  * @return mixed 失败返回 false,成功返回订单记录
  *
  * @param int    $userId   用户的数字 ID
  * @param string $username 用户名
  */
 public function saveOrder($userId, $username)
 {
     // 参数验证
     $validator = new Validator(array('userId' => $userId));
     $userId = $validator->required()->digits()->min(1)->validate('userId');
     $this->validate($validator);
     $orderBasicService = new OrderBasicService();
     $orderId = isset($this->cartContext->orderInfo) ? $this->cartContext->orderInfo['order_id'] : null;
     // 不允许创建空订单
     if (!$orderId && $this->cartContext->isEmpty()) {
         return false;
     }
     // 是否需要新建订单
     $isNewOrder = false;
     //检查这个 order_info 是否合法
     if ($orderId) {
         $orderInfo = $orderBasicService->loadOrderInfoById($orderId);
         // 不存在的 order_info 或者 已经付款了的订单不能再用了
         if ($orderInfo->isEmpty() || OrderBasicService::PS_UNPAYED != $orderInfo->pay_status) {
             $orderId = null;
             $isNewOrder = true;
         }
     } else {
         $isNewOrder = true;
     }
     // 创建 order_info 记录
     $orderInfoArray = $this->cartContext->orderInfoValue;
     $orderInfoArray['user_id'] = $userId;
     $orderInfoArray['order_status'] = OrderBasicService::OS_UNCONFIRMED;
     $orderInfoArray['pay_status'] = OrderBasicService::PS_UNPAYED;
     if (!$orderId) {
         // 新订单,生成 add_time 和 order_sn
         $orderInfoArray['add_time'] = Time::gmTime();
         $orderInfoArray['order_sn'] = OrderBasicService::generateOrderSn();
         $orderInfoArray['system_id'] = Cart::$cartSystemId;
         // 记录订单来自于哪个系统
     } elseif ($this->cartContext->isOrderAmountChange()) {
         // 去支付网关支付,如果订单金额发生变化都需要重新生成一次 order_sn ,因为订单金额如果发生变化,支付网关会拒绝支付
         $orderInfoArray['order_sn'] = OrderBasicService::generateOrderSn();
     } else {
         // do nothing
     }
     // 我们需要数据库事务的保障
     $dbEngine = DataMapper::getDbEngine();
     $dbEngine->begin();
     // 记录余额支付
     $oldSurplus = $this->cartContext->orderInfo ? $this->cartContext->orderInfo['surplus'] : 0;
     $newSurplus = $this->cartContext->getValue('surplus') > 0 ? $this->cartContext->getValue('surplus') : 0;
     // 记录使用红包
     $oldBonusId = $this->cartContext->orderInfo ? $this->cartContext->orderInfo['bonus_id'] : 0;
     $newBonusId = $this->cartContext->getValue('bonus_id') > 0 ? $this->cartContext->getValue('bonus_id') : 0;
     // 创建订单或者更新 order_info
     $orderInfo = $orderBasicService->saveOrderInfo($orderId, $orderInfoArray);
     // 创建订单失败,返回 false
     if ($orderInfo->isEmpty()) {
         goto out_fail;
     }
     // 处理余额支付
     if ($oldSurplus != $newSurplus) {
         // 前后发生了支付余额的改变
         $accountLog = new AccountLog();
         if ($oldSurplus > 0) {
             // 把之前的余额退还
             $accountLog->logChange($userId, $oldSurplus, 0, 0, 0, '退还余额,订单:' . $orderInfo['order_id'], AccountLog::ACT_OTHER);
         }
         if ($newSurplus > 0) {
             // 使用余额
             $accountLog->logChange($userId, -1 * $newSurplus, 0, 0, 0, '使用余额,订单:' . $orderInfo['order_id'], AccountLog::ACT_OTHER);
         }
         // 由于修改了用户信息,需要 reload 用户数据
         AuthHelper::reloadAuthUser();
     }
     // 处理红包支付
     if ($oldBonusId != $newBonusId) {
         // 红包支付前后发生了变化
         $bonusService = new Bonus();
         if ($oldBonusId > 0) {
             //退还之前使用的红包
             $bonusService->unUseBonus($oldBonusId);
         }
         if ($newBonusId > 0) {
             // 使用新的红包
             $bonusService->useBonus($newBonusId, $orderInfo['order_id']);
         }
     }
     // 创建 orderRefer 对象,记录订单的来源
     $orderReferService = new OrderReferService();
     $orderRefer = $orderReferService->loadOrderReferByOrderId($orderInfo['order_id']);
     // order_refer 记录创建一次之后就不会再修改了
     if ($orderRefer->isEmpty()) {
         // 新的 order_refer 记录,设置 order_id 值
         $orderRefer->order_id = $orderInfo['order_id'];
         $orderRefer->create_time = Time::gmTime();
         $orderRefer->login_type = AuthHelper::getLoginType();
         global $f3;
         $orderReferArray = ReferHelper::parseOrderRefer($f3);
         if (!empty($orderReferArray)) {
             unset($orderReferArray['refer_id']);
             //清除掉危险字段
             unset($orderReferArray['order_id']);
             //清除掉危险字段
             $orderRefer->copyFrom($orderReferArray);
         }
         $orderRefer->save();
         // 保存 order_refer 记录
     }
     // 取得数据库中已经存在的 order_goods 列表
     $dbOrderGoodsArray = array();
     if ($orderId > 0) {
         $dbOrderGoodsArray = $orderBasicService->fetchOrderGoodsArray($orderId);
     }
     $stillExistOrderGoodsIdArray = array();
     // 在购物车中仍然存在的 orderGoods 对象
     // 创建 orderInfo 对应的 order_goods 对象
     foreach ($this->cartContext->orderGoodsArray as $orderGoodsItem) {
         $orderGoodsValueArray = array();
         $orderGoodsValueArray['order_id'] = $orderInfo['order_id'];
         //从 goods 中复制属性
         $orderGoodsValueArray['goods_id'] = $orderGoodsItem->goods['goods_id'];
         $orderGoodsValueArray['goods_admin_user_id'] = $orderGoodsItem->goods['admin_user_id'];
         $orderGoodsValueArray['goods_admin_user_name'] = $orderGoodsItem->goods['admin_user_name'];
         $orderGoodsValueArray['goods_name'] = $orderGoodsItem->goods['goods_name_short'];
         $orderGoodsValueArray['goods_sn'] = $orderGoodsItem->goods['goods_sn'];
         $orderGoodsValueArray['warehouse'] = $orderGoodsItem->goods['warehouse'];
         $orderGoodsValueArray['shelf'] = $orderGoodsItem->goods['shelf'];
         $orderGoodsValueArray['market_price'] = $orderGoodsItem->goods['market_price'];
         $orderGoodsValueArray['shop_price'] = $orderGoodsItem->goods['shop_price'];
         $orderGoodsValueArray['shipping_fee'] = $orderGoodsItem->goods['shipping_fee'];
         $orderGoodsValueArray['is_real'] = $orderGoodsItem->goods['is_real'];
         $orderGoodsValueArray['extension_code'] = $orderGoodsItem->goods['extension_code'];
         $orderGoodsValueArray['suppliers_id'] = $orderGoodsItem->goods['suppliers_id'];
         $orderGoodsValueArray['suppliers_price'] = $orderGoodsItem->goods['suppliers_price'];
         $orderGoodsValueArray['suppliers_shipping_fee'] = $orderGoodsItem->goods['suppliers_shipping_fee'];
         // 合并一些因为计算而覆盖的值
         $orderGoodsValueArray = array_merge($orderGoodsValueArray, $orderGoodsItem->orderGoodsValue);
         $orderGoodsId = null;
         if (!$isNewOrder && $orderGoodsItem->orderGoods) {
             // 如果不是新订单,并且已经存在的 order_goods,做更新
             $orderGoodsId = $orderGoodsItem->orderGoods['rec_id'];
             $stillExistOrderGoodsIdArray[] = $orderGoodsId;
         } else {
             // 新建的 order_goods,我们需要取得它的 CPS 信息,CPS 信息只在一开始加入,后面不会再被修改了
             $cpsArray = ReferHelper::getCpsParam($orderRefer, $orderInfo, $orderGoodsValueArray);
             if (!empty($cpsArray)) {
                 $orderGoodsValueArray['cps_rate'] = isset($cpsArray['cps_rate']) ? $cpsArray['cps_rate'] : 0;
                 $orderGoodsValueArray['cps_fix_fee'] = isset($cpsArray['cps_fix_fee']) ? $cpsArray['cps_fix_fee'] : 0;
             }
         }
         $orderGoods = $orderBasicService->saveOrderGoods($orderGoodsId, $orderInfo['order_id'], $orderGoodsValueArray);
         if ($orderGoods->isEmpty()) {
             //创建失败
             goto out_fail;
         }
     }
     // 删除已经被用户删除的 order_goods
     foreach ($dbOrderGoodsArray as $dbOrderGoodsItem) {
         // 这个 order_goods 已经不存在了,删除它
         if (!in_array($dbOrderGoodsItem['rec_id'], $stillExistOrderGoodsIdArray)) {
             $orderBasicService->removeOrderGoods($dbOrderGoodsItem['rec_id']);
         }
     }
     // 记录订单操作日志
     $orderActionService = new OrderActionService();
     $orderActionService->logOrderAction($orderInfo['order_id'], 0, OrderBasicService::OS_UNCONFIRMED, OrderBasicService::PS_UNPAYED, OrderGoodsService::OGS_UNPAY, '订单创建或更新', $username, 0, 0);
     $dbEngine->commit();
     return $orderInfo;
     // 返回创建的订单
     out_fail:
     $dbEngine->rollback();
     return false;
 }