filter($request->all())->where('source_type', OrderPre::class)->latest('id')->simplePaginate($perPage); return OrderResourceCollection::make($orders); } /** * 线下订单详细 */ public function show($id) { $order = Order::where('source_type', OrderPre::class)->findOrFail($id); $order->load('products'); return OrderResource::make($order); } /** * 发货 */ public function package($id, Request $request) { $request->validate([ 'products' => 'required|array', 'products.*.id' => 'required', 'products.*.amount' => 'required', ], [ 'products.required' => '发货商品必填', ]); $order = Order::where('source_type', OrderPre::class)->findOrFail($id); // 订单来源为 帮客户下单 order_pres try { DB::beginTransaction(); $order_products = $order->products; // 根据 order_pres 发货数量, 自动发货 $service_package = new \App\Admin\Services\OrderPackageService(); // order_product_id: 订单商品ID, quantity: 发货数量 $package_params = []; foreach($request->input('products') as $item) { $order_product = $order_products->firstWhere('id', $item['id']); if ($order_product) { array_push($package_params, [ 'order_product_id' => $order_product->id, 'quantity' => $item['amount'], ]); } } // 发货 if (count($package_params) === 0) { throw new BizException('提货商品不存在'); } $service_package->createPackage($order, [ 'shipping_company' => '自提', 'shipping_number' => '0', 'packages' => $package_params ]); DB::commit(); return response()->noContent(); } catch (Throwable $th) { DB::rollBack(); report($th); throw new BizException($th->getMessage()); } } }