|
|
@ -2,16 +2,18 @@ |
|
|
|
|
|
|
|
namespace api\logic; |
|
|
|
|
|
|
|
use antgoods\goods\models\ars\Goods; |
|
|
|
use antgoods\goods\models\ars\GoodsSku; |
|
|
|
use common\models\ars\Address; |
|
|
|
use common\models\ars\Cart; |
|
|
|
use common\models\ars\Order; |
|
|
|
use common\models\ars\OrderGoods; |
|
|
|
use common\models\ars\TakingSite; |
|
|
|
use backend\modules\goods\models\ars\Goods; |
|
|
|
use backend\modules\goods\models\ars\GoodsSku; |
|
|
|
use backend\modules\shop\models\ars\Address; |
|
|
|
use backend\modules\shop\models\ars\Cart; |
|
|
|
use backend\modules\shop\models\ars\Order; |
|
|
|
use backend\modules\shop\models\ars\OrderGoods; |
|
|
|
use backend\modules\shop\models\ars\TakingSite; |
|
|
|
use yii\base\Component; |
|
|
|
use Throwable; |
|
|
|
use Yii; |
|
|
|
use yii\db\ActiveRecord; |
|
|
|
use yii\base\Exception; |
|
|
|
use yii\web\BadRequestHttpException; |
|
|
|
use yii\web\NotFoundHttpException; |
|
|
|
use yii\web\ServerErrorHttpException; |
|
|
@ -25,9 +27,6 @@ use yii\web\ServerErrorHttpException; |
|
|
|
class OrderLogic extends Component |
|
|
|
{ |
|
|
|
public $viewAction = 'view'; |
|
|
|
/*创建途径类型*/ |
|
|
|
const TYPE_ADD_GOODS_PURCHASE = 1; |
|
|
|
const TYPE_ADD_GOODS_CART = 2; |
|
|
|
/*订单操作类型*/ |
|
|
|
const TYPE_CONFIRM = 1; |
|
|
|
const TYPE_CANCEL = 2; |
|
|
@ -41,12 +40,58 @@ class OrderLogic extends Component |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param $data |
|
|
|
* @return bool|\Exception|\yii\base\Exception |
|
|
|
* @return Order |
|
|
|
* @throws BadRequestHttpException |
|
|
|
* @throws Exception |
|
|
|
* 创建订单 |
|
|
|
*/ |
|
|
|
public function update($data) |
|
|
|
public function create() |
|
|
|
{ |
|
|
|
$type = Yii::$app->request->getQueryParam('type'); |
|
|
|
//立即购物需传参数
|
|
|
|
$goodsId = Yii::$app->request->getBodyParam('goodsId');/*int 商品id*/ |
|
|
|
$count = Yii::$app->request->getBodyParam('count');/*int 数量*/ |
|
|
|
$skuId = Yii::$app->request->getBodyParam('skuId');/*int 商品sku id*/ |
|
|
|
//购物车提交订单需传参数
|
|
|
|
$cartIds = Yii::$app->request->getBodyParam('cartIds');/*array 购物车商品id*/ |
|
|
|
|
|
|
|
if ((empty($cartIds) && empty($goodsId)) || ($goodsId && empty($count))) { |
|
|
|
throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); |
|
|
|
} |
|
|
|
$tra = Yii::$app->db->beginTransaction(); |
|
|
|
try { |
|
|
|
$order = new Order(); |
|
|
|
$order->user_id = Yii::$app->user->getId(); |
|
|
|
$order->type = Order::TYPE_SHOPPING; |
|
|
|
if (!$order->save()) { |
|
|
|
throw new ServerErrorHttpException('服务器创建订单失败'); |
|
|
|
} |
|
|
|
if ($goodsId && $count) { |
|
|
|
$this->addGoods($order->id, $goodsId, $skuId, $count);/*添加订单商品*/ |
|
|
|
} else { |
|
|
|
$this->addGoodsByCart($cartIds, $order->id);/*通过购物车添加订单商品*/ |
|
|
|
} |
|
|
|
Helper::checkStock($goodsId, $count, $skuId); /*检查库存*/ |
|
|
|
$this->saveGoodsInfo($order);/*保存订单商品信息*/ |
|
|
|
$tra->commit(); |
|
|
|
Helper::createdResponse($order, $this->viewAction); |
|
|
|
return $order; |
|
|
|
} catch (Exception $e) { |
|
|
|
$tra->rollBack(); |
|
|
|
throw $e; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @return bool |
|
|
|
* @throws Exception |
|
|
|
* @throws Throwable |
|
|
|
* @throws yii\base\InvalidConfigException |
|
|
|
* 根据操作类型更新订单 |
|
|
|
*/ |
|
|
|
public function update() |
|
|
|
{ |
|
|
|
$data = Yii::$app->request->getBodyParams(); |
|
|
|
$type = Yii::$app->request->getBodyParam('type'); |
|
|
|
$order = $this->findOrder(); |
|
|
|
$tra = Yii::$app->db->beginTransaction(); |
|
|
|
try { |
|
|
@ -61,7 +106,7 @@ class OrderLogic extends Component |
|
|
|
$this->take($order); |
|
|
|
break; |
|
|
|
case self::TYPE_CHANGE_SHIPPING_TYPE: |
|
|
|
$this->updateShippingType($order, $data); |
|
|
|
$this->switchShippingType($order, $data); |
|
|
|
break; |
|
|
|
case self::TYPE_CHANGE_ADDRESS: |
|
|
|
$this->changeAddress($order, $data); |
|
|
@ -75,17 +120,19 @@ class OrderLogic extends Component |
|
|
|
} |
|
|
|
$tra->commit(); |
|
|
|
return true; |
|
|
|
} catch (\yii\base\Exception $e) { |
|
|
|
} catch (Exception $e) { |
|
|
|
$tra->rollBack(); |
|
|
|
return $e; |
|
|
|
throw $e; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*----------------------------------------- 华丽的分割线 -----------------------------------------*/ |
|
|
|
/** |
|
|
|
* @param Order $order |
|
|
|
* @param array $data |
|
|
|
* @throws BadRequestHttpException |
|
|
|
* @throws NotFoundHttpException |
|
|
|
* 添加地址到订单 |
|
|
|
*/ |
|
|
|
private function changeAddress($order, $data) |
|
|
|
{ |
|
|
@ -95,9 +142,7 @@ class OrderLogic extends Component |
|
|
|
if ($order->shipping_type !== Order::SHIPPING_TYPE_EXPRESS) { |
|
|
|
throw new BadRequestHttpException('配送方式异常'); |
|
|
|
} |
|
|
|
$address = Address::find() |
|
|
|
->where(['id' => $data['address_id'], 'user_id' => Yii::$app->user->getId()]) |
|
|
|
->one(); |
|
|
|
$address = Address::findOne(['id' => $data['address_id'], 'user_id' => Yii::$app->user->getId()]); |
|
|
|
if (!$address) { |
|
|
|
throw new NotFoundHttpException('收货地址未找到'); |
|
|
|
} |
|
|
@ -114,6 +159,7 @@ class OrderLogic extends Component |
|
|
|
* @param array $data |
|
|
|
* @throws BadRequestHttpException |
|
|
|
* @throws NotFoundHttpException |
|
|
|
* 修改自提点 |
|
|
|
*/ |
|
|
|
private function changeTakingSite($order, $data) |
|
|
|
{ |
|
|
@ -135,8 +181,9 @@ class OrderLogic extends Component |
|
|
|
* @param Order $order |
|
|
|
* @param array $data |
|
|
|
* @throws BadRequestHttpException |
|
|
|
* 切换配送方式 |
|
|
|
*/ |
|
|
|
private function updateShippingType($order, $data) |
|
|
|
private function switchShippingType($order, $data) |
|
|
|
{ |
|
|
|
if (!isset($data['shipping_type'])) { |
|
|
|
throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); |
|
|
@ -153,6 +200,9 @@ class OrderLogic extends Component |
|
|
|
* @throws BadRequestHttpException |
|
|
|
* @throws NotFoundHttpException |
|
|
|
* @throws ServerErrorHttpException |
|
|
|
* @throws Throwable |
|
|
|
* @throws yii\db\StaleObjectException |
|
|
|
* 确定订单 |
|
|
|
*/ |
|
|
|
private function confirm($order, $data) |
|
|
|
{ |
|
|
@ -160,9 +210,8 @@ class OrderLogic extends Component |
|
|
|
throw new BadRequestHttpException('订单状态异常'); |
|
|
|
} |
|
|
|
if ($order->shipping_type === Order::SHIPPING_TYPE_PICKED_UP) { |
|
|
|
if (!isset($data['consignee']) || !isset($data['phone']) |
|
|
|
|| empty($data['consignee']) || empty($data['phone'])) { |
|
|
|
throw new BadRequestHttpException('(联系人)参数无效'); |
|
|
|
if ($data['consignee'] ?? false || $data['phone'] ?? false) { |
|
|
|
throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); |
|
|
|
} |
|
|
|
$order->consignee = $data['consignee']; |
|
|
|
$order->phone = $data['phone']; |
|
|
@ -173,15 +222,35 @@ class OrderLogic extends Component |
|
|
|
if (isset($data['remarks'])) { |
|
|
|
$order->remarks = $data['remarks']; |
|
|
|
} |
|
|
|
Helper::delCarts($this->getCartsIds($order)); //删除购物车对应商品
|
|
|
|
$order->status = Order::STATUS_NONPAYMENT; |
|
|
|
$this->updateStock($order, self::TYPE_SUB_STOCK); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $order |
|
|
|
* @return array |
|
|
|
* 获取订单商品对应购物车商品id |
|
|
|
*/ |
|
|
|
private function getCartsIds($order) |
|
|
|
{ |
|
|
|
$allGoods = $this->findOrderGoods($order->id); |
|
|
|
$ids = []; |
|
|
|
foreach ($allGoods as $goods) { |
|
|
|
$cart = Cart::findOne(['goods_id' => $goods->goods_id, 'user_id' => Yii::$app->user->getId()]); |
|
|
|
if ($cart) { |
|
|
|
$ids[] = $cart->id; |
|
|
|
} |
|
|
|
} |
|
|
|
return $ids; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param Order $order |
|
|
|
* @throws BadRequestHttpException |
|
|
|
* @throws NotFoundHttpException |
|
|
|
* @throws ServerErrorHttpException |
|
|
|
* 取消订单 |
|
|
|
*/ |
|
|
|
private function cancel($order) |
|
|
|
{ |
|
|
@ -189,12 +258,13 @@ class OrderLogic extends Component |
|
|
|
throw new BadRequestHttpException('订单状态异常'); |
|
|
|
} |
|
|
|
$order->status = Order::STATUS_CANCEL; |
|
|
|
$this->updateStock($order, self::TYPE_ADD_STOCK); |
|
|
|
$this->updateStock($order, self::TYPE_ADD_STOCK);/*更新库存*/ |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param Order $order |
|
|
|
* @throws BadRequestHttpException |
|
|
|
* 确认收货 |
|
|
|
*/ |
|
|
|
private function take($order) |
|
|
|
{ |
|
|
@ -210,6 +280,7 @@ class OrderLogic extends Component |
|
|
|
* @throws BadRequestHttpException |
|
|
|
* @throws NotFoundHttpException |
|
|
|
* @throws ServerErrorHttpException |
|
|
|
* 更新库存 |
|
|
|
*/ |
|
|
|
private function updateStock($id, $type) |
|
|
|
{ |
|
|
@ -234,57 +305,27 @@ class OrderLogic extends Component |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $originId |
|
|
|
* @param $count |
|
|
|
* @param $skuId |
|
|
|
* @return bool |
|
|
|
* @throws BadRequestHttpException |
|
|
|
* @param array $cartIds 购物车商品id |
|
|
|
* @param int $id 订单id |
|
|
|
* @throws Exception |
|
|
|
* @throws NotFoundHttpException |
|
|
|
* 创建订单并添加商品 |
|
|
|
* 通过购物车添加商品 |
|
|
|
*/ |
|
|
|
public function create($originId, $count, $skuId) |
|
|
|
private function addGoodsByCart($cartIds, $id) |
|
|
|
{ |
|
|
|
$type = Yii::$app->request->getQueryParam('type'); |
|
|
|
if (empty($type) || ($type == self::TYPE_ADD_GOODS_CART && empty($originId)) || |
|
|
|
($type == self::TYPE_ADD_GOODS_PURCHASE && (empty($count) || empty($originId)))) { |
|
|
|
throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); |
|
|
|
} |
|
|
|
if ($type == self::TYPE_ADD_GOODS_PURCHASE) { |
|
|
|
$goodsId = $originId; |
|
|
|
} else { |
|
|
|
$cart = Cart::findOne($originId); |
|
|
|
foreach ($cartIds as $cartId) { |
|
|
|
$cart = Cart::findOne($cartId); |
|
|
|
if (!$cart) { |
|
|
|
throw new NotFoundHttpException('购物车未找到'); |
|
|
|
} |
|
|
|
$goodsId = $cart->goods_id; |
|
|
|
$skuId = $cart->sku_id; |
|
|
|
$count = $cart->goods_count; |
|
|
|
} |
|
|
|
/*检查库存*/ |
|
|
|
Helper::checkStock($goodsId, $count, $skuId); |
|
|
|
$tra = Yii::$app->db->beginTransaction(); |
|
|
|
try { |
|
|
|
$order = new Order(); |
|
|
|
// $order->user_id = Yii::$app->user->getId();
|
|
|
|
$order->user_id = 1; |
|
|
|
$order->type = Order::TYPE_SHOPPING; |
|
|
|
if (!$order->save()) { |
|
|
|
throw new ServerErrorHttpException('服务器创建订单失败'); |
|
|
|
} |
|
|
|
$this->addGoods($order->id, $goodsId, $skuId, $count);/*创建订单商品*/ |
|
|
|
$this->saveGoodsInfo($order);/*保存订单商品信息*/ |
|
|
|
$tra->commit(); |
|
|
|
Helper::createdResponse($order, $this->viewAction); |
|
|
|
return $order; |
|
|
|
} catch (\Exception $e) { |
|
|
|
$tra->rollBack(); |
|
|
|
throw $e; |
|
|
|
$this->addGoods($id, $cart->goods_id, $cart->sku_id, $cart->goods_count); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param Order $order |
|
|
|
* @throws ServerErrorHttpException |
|
|
|
* @throws Exception |
|
|
|
* 保存商品总价格和总数量信息到 |
|
|
|
*/ |
|
|
|
private function saveGoodsInfo($order) |
|
|
|
{ |
|
|
@ -294,26 +335,23 @@ class OrderLogic extends Component |
|
|
|
$order->goods_count += $goods->goods_count; |
|
|
|
} |
|
|
|
if (!$order->save()) { |
|
|
|
throw new ServerErrorHttpException('服务器创建订单失败'); |
|
|
|
throw new Exception('服务器创建订单失败'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param int $id |
|
|
|
* @param $id |
|
|
|
* @param int $goodsId |
|
|
|
* @param int $skuId |
|
|
|
* @param int $count |
|
|
|
* @param $skuId |
|
|
|
* @param $count |
|
|
|
* @throws Exception |
|
|
|
* @throws NotFoundHttpException |
|
|
|
* @throws ServerErrorHttpException |
|
|
|
* 创建订单商品 |
|
|
|
* 添加商品到订单 |
|
|
|
*/ |
|
|
|
private function addGoods($id, $goodsId, $skuId, $count) |
|
|
|
{ |
|
|
|
$goods = Goods::findOne($goodsId); |
|
|
|
if (!$goods) { |
|
|
|
throw new NotFoundHttpException('商品未找到'); |
|
|
|
} |
|
|
|
$orderGoods = new OrderGoods(); |
|
|
|
$orderGoods->order_id = $id; |
|
|
|
$orderGoods->goods_id = $goodsId; |
|
|
@ -322,15 +360,15 @@ class OrderLogic extends Component |
|
|
|
$orderGoods->goods_count = $count; |
|
|
|
$orderGoods->goods_img = $goods->image; |
|
|
|
$orderGoods->goods_name = $goods->name; |
|
|
|
$orderGoods->price = Helper::countPrice($goodsId, $skuId); |
|
|
|
$orderGoods->price = Helper::goodsPrice($goodsId, $skuId); |
|
|
|
if (!$orderGoods->save()) { |
|
|
|
throw new ServerErrorHttpException('服务器添加订单商品失败'); |
|
|
|
throw new Exception('服务器添加订单商品失败'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @return array|Order|null |
|
|
|
* 获取订单 |
|
|
|
* 根据id获取订单 |
|
|
|
*/ |
|
|
|
private function findOrder() |
|
|
|
{ |
|
|
@ -344,6 +382,7 @@ class OrderLogic extends Component |
|
|
|
/** |
|
|
|
* @param $id |
|
|
|
* @return array|ActiveRecord[]|OrderGoods[] |
|
|
|
* 根据订单 id 获取所有订单商品 |
|
|
|
*/ |
|
|
|
private function findOrderGoods($id) |
|
|
|
{ |
|
|
|