/** * 获取订单的商品列表. * @param $orderId */ public function getOrderProductByOrderId($orderId) { if (empty($orderId)) { return ''; } $list = OrderProduct::model()->findAllByAttributes(array('order_id' => $orderId)); if (empty($list)) { return ''; } // todo 图片待处理 $newList = array(); foreach ($list as $row) { $temp = $row->getAttributes(); if ($row->type == 1) { $temp['images']['cover'] = '/images/public/tab.png'; } elseif ($row->type == 2) { $zineInfo = Zine::model()->findByPk($row['product_id']); if ($zineInfo) { $temp['images']['cover'] = '/images/zine/' . $zineInfo['mcover']; } else { $temp['images']['cover'] = ''; } } else { $temp['images'] = Product::model()->getProductFaceImageByProductId($row->product_id); } $temp['total_price'] = $temp['sell_price'] * $temp['quantity']; $newList[$row->id] = $temp; } return $newList; }
public function actionChange() { $res = array('statusCode' => 200, 'message' => '修改成功!'); $info = Zine::model()->findByPk($_REQUEST['id']); try { if (empty($info)) { throw new exception('记录不存在了!'); } $info->is_show = $_REQUEST['is_show']; $flag = $info->save(); if (empty($flag)) { throw new exception('修改状态失败!'); } } catch (Exception $e) { $res['statusCode'] = 300; $res['message'] = '删除失败【' . $e->getMessage() . '】'; } $res['callbackType'] = 'reloadTab'; $res['forwardUrl'] = '/manage/zine/index'; $this->ajaxDwzReturn($res); }
/** * 保存订单商品信息 * @param $oid * @param $cartinfo * @return bool * @throws Exception */ protected function saveOrderProduct($userId, $order_id, $cartInfo) { $list = $cartInfo['list']; foreach ($list as $row) { if ($row['type'] == 1) { $pInfo = Tab::model()->findByPk($row['product_id']); if (empty($pInfo)) { throw new Exception("谱子不存在!", 500); } } elseif ($row['type'] == 2) { $pInfo = Zine::model()->findByPk($row['product_id']); if (empty($pInfo)) { throw new Exception("杂志商品不存在!", 500); } } else { $pInfo = Product::model()->findByPk($row['product_id']); if (empty($pInfo)) { throw new Exception("订单商品不存在!", 500); } } if ($row['sell_price'] != $pInfo['sell_price']) { throw new exception('价格错误,请重试'); } $m = new OrderProduct(); $m->order_id = $order_id; $m->user_id = $userId; $m->product_id = $row['product_id']; $m->type = $row['type']; $m->product_sn = empty($row['type']) ? $pInfo['product_sn'] : ''; $m->product_name = empty($row['type']) ? $pInfo['product_name'] : ($row['type'] == 1 ? $pInfo['tabname'] : $pInfo['name']); $m->size_id = $row['size_id']; $m->brand_id = $row['type'] == 0 ? $pInfo['brand_id'] : 0; $m->sell_price = $row['sell_price']; $m->quantity = $row['quantity']; //$m->shipping_id = 0; //$m->shipping_code = ''; //$m->shipping_time = 0; $opid = $m->save(); if (empty($opid)) { throw new Exception("订单商品生成失败!", 1); } $this->updateProductStock($pInfo, $row); //更新商品的库存 } return true; }
/** * 处理取消订单的库存 * @param $orderId */ public function dealWithCancelOrderProductStock($orderId) { if (!$orderId) { throw new exception('订单的id不能为空!'); } $list = OrderProduct::model()->findAllByAttributes(array('order_id' => $orderId)); if (empty($list)) { throw new exception('订单商品为空!'); } foreach ($list as $row) { if ($row['type'] == 1) { $pInfo = Tab::model()->findByPk($row['product_id']); } elseif ($row['type'] == 2) { $pInfo = Zine::model()->findByPk($row['product_id']); } else { $pInfo = Product::model()->findByPk($row['product_id']); } if (empty($pInfo)) { throw new exception('商品【' . $row['product_name'] . '】不存在!'); } $pInfo->quantity += $row['quantity']; $flag = $pInfo->save(); if (empty($flag)) { throw new exception('商品【' . $row['product_name'] . '】库存恢复失败!'); } if (!$row['type']) { $stockInfo = ProductStock::model()->findByAttributes(array('product_id' => $row['product_id'], 'attr_id' => $row['size_id'])); if (empty($stockInfo)) { throw new exception('商品【' . $row['product_name'] . '】库存不存在!'); } $stockInfo->quantity += $row['quantity']; $flag = $stockInfo->save(); if (empty($flag)) { throw new exception('订单【' . $row['product_name'] . '】库存恢复失败!'); } } } return true; }