From 4b73ad7570787d92bcb43629c6e719a3a48d557b Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Mon, 14 Feb 2022 14:39:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=89=B9=E9=9B=B6=E7=AB=AF?= =?UTF-8?q?=E8=B4=AD=E7=89=A9=E8=BD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Dealer/OrderController.php | 48 ++++++++- .../Dealer/ShoppingCartItemController.php | 101 ++++++++++++++++++ app/Endpoint/Api/routes.php | 7 ++ app/Models/DealerShoppingCartItem.php | 25 +++++ app/Models/User.php | 9 ++ app/Services/Dealer/OrderService.php | 74 +++++++++---- ...reate_dealer_shopping_cart_items_table.php | 38 +++++++ 7 files changed, 279 insertions(+), 23 deletions(-) create mode 100644 app/Endpoint/Api/Http/Controllers/Dealer/ShoppingCartItemController.php create mode 100644 app/Models/DealerShoppingCartItem.php create mode 100644 database/migrations/2022_02_14_103211_create_dealer_shopping_cart_items_table.php diff --git a/app/Endpoint/Api/Http/Controllers/Dealer/OrderController.php b/app/Endpoint/Api/Http/Controllers/Dealer/OrderController.php index 600e010b..a23af247 100644 --- a/app/Endpoint/Api/Http/Controllers/Dealer/OrderController.php +++ b/app/Endpoint/Api/Http/Controllers/Dealer/OrderController.php @@ -58,7 +58,53 @@ class OrderController extends Controller $product = DealerProduct::online()->findOrFail($input['product_id']); try { DB::beginTransaction(); - $order = $orderService->createOrder($request->user(), $product, $input['num'], $input['shipping_address_id']); + $order = $orderService->quickCreateOrder($request->user(), $product, $input['num'], $input['shipping_address_id']); + DB::commit(); + } catch (BizException $e) { + DB::rollBack(); + throw $e; + } catch (Throwable $th) { + DB::rollBack(); + report($th); + throw new BizException('下单失败,请稍后再试'); + } + + return OrderResource::make($order); + } + + /** + * 新下单接口 + */ + public function newStore(Request $request, OrderService $orderService) + { + $isQuick = $request->filled('product'); + + $rules = $isQuick ? [ + 'product.id' => ['bail', 'required', 'int'], + 'product.quantity' => ['bail', 'required', 'int', 'min:1'], + 'shipping_address_id' => ['bail', 'required', 'int'], + ] : [ + 'shopping_cart' => ['bail', 'required', 'array'], + 'shipping_address_id' => ['bail', 'required', 'int'], + ]; + + $input = $request->validate($rules, [], [ + 'product.id' => '商品', + 'product.quantity' => '数量', + 'shopping_cart' => '购物车商品', + 'shipping_address_id' => '收货地址', + ]); + + try { + DB::beginTransaction(); + + if ($isQuick) { + $product = DealerProduct::online()->findOrFail($input['product']['id']); + $order = $orderService->quickCreateOrder($request->user(), $product, $input['product']['quantity'], $input['shipping_address_id']); + } else { + $order = $orderService->cartCreateOrder($request->user(), $input['shopping_cart'], $input['shipping_address_id']); + } + DB::commit(); } catch (BizException $e) { DB::rollBack(); diff --git a/app/Endpoint/Api/Http/Controllers/Dealer/ShoppingCartItemController.php b/app/Endpoint/Api/Http/Controllers/Dealer/ShoppingCartItemController.php new file mode 100644 index 00000000..085001f3 --- /dev/null +++ b/app/Endpoint/Api/Http/Controllers/Dealer/ShoppingCartItemController.php @@ -0,0 +1,101 @@ +user(); + $items = $user->dealerShoppingCartItems()->latest('id')->get(); + $items->load('product'); + $totalQty = $items->sum('quantity'); + $data = []; + foreach ($items as $item) { + $data[] = [ + 'id'=>$item->id, + 'name' =>$item->name, + 'cover'=>$item->cover, + 'sell_price'=>$item->sell_price, + 'dealer_price' =>$orderService->getSalePrice($user, $item->product, $totalQty), + 'quantity'=>$item->quantity, + ]; + } + + return response()->json(['data'=>$data]); + } + + /** + * 加入购物车 + */ + public function store(Request $request) + { + $input = $request->validate([ + 'product_id' => ['bail', 'required', 'int'], + 'quantity' => ['bail', 'required', 'int', 'min:1'], + ]); + + $product = DealerProduct::online()->findOrFail($input['product_id']); + + $shoppingCartItem = $request->user()->dealerShoppingCartItems()->firstOrCreate([ + 'product_id' => $product->id, + ], [ + 'name' => $product->name, + 'cover' => $product->cover, + 'sell_price' => $product->price, + 'quantity' => $input['quantity'], + ]); + if (!$shoppingCartItem->wasRecentlyCreated) { + $shoppingCartItem->increment('quantity', $input['quantity']); + } + + return response()->noContent(); + } + + /** + * 购物车变动 + */ + public function update($id, Request $request) + { + $input = $request->validate([ + 'quantity' => ['bail', 'required', 'int', 'min:1'], + ]); + + $shoppingCartItem = $request->user()->dealerShoppingCartItems()->findOrFail($id); + + $product = DealerProduct::online()->findOrFail($shoppingCartItem->product_id); + + $shoppingCartItem->update(array_merge($input, [ + 'name' => $product->name, + 'cover' => $product->cover, + 'sell_price' => $product->price, + ])); + + return response()->noContent(); + } + + /** + * 移出购物车 + */ + public function delete(Request $request) + { + $input = $request->validate([ + 'ids' => ['bail', 'required', 'array'], + ]); + + $request->user()->dealerShoppingCartItems()->whereIn('id', $input['ids'])->delete(); + + return response()->noContent(); + } +} diff --git a/app/Endpoint/Api/routes.php b/app/Endpoint/Api/routes.php index 47ea910d..4370afd0 100644 --- a/app/Endpoint/Api/routes.php +++ b/app/Endpoint/Api/routes.php @@ -238,12 +238,19 @@ Route::group([ Route::post('user-products/offline-out', [Dealer\UserProductController::class, 'offlineOutQty']); Route::post('user-products/offline-out-revoke/{log}', [Dealer\UserProductController::class, 'revokeQtyLog']); + //购物车 + Route::apiResource('shopping-cart-items', Dealer\ShoppingCartItemController::class)->only( + ['index', 'store', 'update'] + ); + Route::delete('shopping-cart-items', [Dealer\ShoppingCartItemController::class, 'delete']); + //计算商品下单价格 Route::get('orders/total-amount', [Dealer\OrderController::class, 'totalAmount']); //订单列表 Route::get('orders', [Dealer\OrderController::class, 'index']); //下单 Route::post('orders', [Dealer\OrderController::class, 'store']); + Route::post('orders-new', [Dealer\OrderController::class, 'newStore']); //订单详情 Route::get('orders/{order}', [Dealer\OrderController::class, 'show']); diff --git a/app/Models/DealerShoppingCartItem.php b/app/Models/DealerShoppingCartItem.php new file mode 100644 index 00000000..49c2d434 --- /dev/null +++ b/app/Models/DealerShoppingCartItem.php @@ -0,0 +1,25 @@ +belongsTo(DealerProduct::class, 'product_id'); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index a1f2734d..0a0b16dd 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -104,6 +104,15 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac return $this->hasMany(ShoppingCartItem::class); } + /** + * 属于此用户的批零购物车商品 + * + */ + public function dealerShoppingCartItems() + { + return $this->hasMany(DealerShoppingCartItem::class); + } + /** * 属于此用户的收货地址 */ diff --git a/app/Services/Dealer/OrderService.php b/app/Services/Dealer/OrderService.php index 32a8529e..3370bf71 100644 --- a/app/Services/Dealer/OrderService.php +++ b/app/Services/Dealer/OrderService.php @@ -18,13 +18,13 @@ use Illuminate\Database\QueryException; class OrderService { /** - * 计算订单价格 + * 获取单个商品实际价格 * + * @param User $user * @param DealerProduct $product * @param integer $number - * @return string */ - public function totalAmount(User $user, DealerProduct $product, int $number = 0) + public function getSalePrice(User $user, DealerProduct $product, int $number = 0) { //获取等级规则,判断当前用户等级是否配置等级价格 $salePrice = $product->price; @@ -45,9 +45,19 @@ class OrderService break; } } - // dd($salePrice, $number); + return $salePrice; + } - return bcmul($salePrice, $number, 2); + /** + * 计算订单价格 + * + * @param DealerProduct $product + * @param integer $number + * @return string + */ + public function totalAmount(User $user, DealerProduct $product, int $number = 0) + { + return bcmul($this->getSalePrice($user, $product, $number), $number, 2); } /** @@ -59,7 +69,7 @@ class OrderService * @param integer $shippingAddressId * @return DealerOrder $order */ - public function createOrder(User $user, DealerProduct $product, int $number = 0, int $shippingAddressId) + public function quickCreateOrder(User $user, DealerProduct $product, int $number = 0, int $shippingAddressId) { //判断是否满足当前等级最低补货价 $totalAmount = $this->totalAmount($user, $product, $number); @@ -69,6 +79,42 @@ class OrderService } } + $order = $this->createOrder($user, $totalAmount, $shippingAddressId); + + //保存订单商品-----一个订单对应一个商品 + $order->products()->create([ + 'order_id' => $order->id, + 'product_id'=> $product->id, + 'name'=> $product->name, + 'subtitle'=> $product->subtitle, + 'cover'=> $product->cover, + 'price' => $product->price, + 'sale_price'=> bcdiv($totalAmount, $number, 2), + 'qty'=> $number, + ]); + + if (!$order->consignor) {//如果订单分配给公司,则直接确认 + $this->confirmOrder($order); + } + + return $order; + } + + public function cartCreateOrder(User $user, $cartIds, int $shippingAddressId) + { + return null; // + } + + /** + * 创建订单 + * + * @param User $user + * @param [type] $totalAmount + * @param integer $shippingAddressId + * @return DealerOrder $order + */ + protected function createOrder(User $user, $totalAmount, int $shippingAddressId) + { //找到发货人 $consignor = $this->getConsignor($user, $totalAmount); @@ -97,22 +143,6 @@ class OrderService } } while (true); - //保存订单商品-----一个订单对应一个商品 - $order->products()->create([ - 'order_id' => $order->id, - 'product_id'=> $product->id, - 'name'=> $product->name, - 'subtitle'=> $product->subtitle, - 'cover'=> $product->cover, - 'price' => $product->price, - 'sale_price'=> bcdiv($totalAmount, $number, 2), - 'qty'=> $number, - ]); - - if (!$order->consignor) {//如果订单分配给公司,则直接确认 - $this->confirmOrder($order); - } - return $order; } diff --git a/database/migrations/2022_02_14_103211_create_dealer_shopping_cart_items_table.php b/database/migrations/2022_02_14_103211_create_dealer_shopping_cart_items_table.php new file mode 100644 index 00000000..fb0c00be --- /dev/null +++ b/database/migrations/2022_02_14_103211_create_dealer_shopping_cart_items_table.php @@ -0,0 +1,38 @@ +id(); + $table->unsignedBigInteger('user_id')->comment('用户ID'); + $table->unsignedBigInteger('product_id')->comment('商品ID'); + $table->string('name')->comment('商品名称'); + $table->string('cover')->nullable()->comment('封面图'); + $table->unsignedDecimal('sell_price', 10, 2)->default(0.00)->comment('销售价格:元'); + $table->unsignedDecimal('dealer_price', 10, 2)->default(0.00)->comment('经销商价格:元'); + $table->unsignedInteger('quantity')->default(0)->comment('购买数量'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('dealer_shopping_cart_items'); + } +}