user_id = $user->id; $order->source = $params['source']; $order->save(); $goods = Goods::findOrFail($params['id']); $og = new OrderGoods(); $og->order_id = $order->id; $og->goods_id = $params['id']; $og->sku_id = 0; $og->count = $params['count'] ?? 1; $og->sn = $goods->sn; $og->custom = $params['custom']; if ($skuCustom = Arr::get($params, "sku_custom")) { $og->sn = $og->sn . "-" . implode("-", Arr::pluck($skuCustom, "spec_attr_custom")); } $og->save(); return $order; }); OrderCreateEvent::dispatch($order); return [ "id" => $order->id, ]; } public function askByCart(User $user, $params) { /** @var Order $order */ $order = DB::transaction(function () use ($user, $params) { $order = new Order(); $order->user_id = $user->id; $order->source = $params['source']; $order->save(); foreach ($params['ids'] as $param) { $cart = Cart::where("user_id", $user->id)->where("id", $param['id'])->first(); if (is_null($cart)) { continue; } $og = new OrderGoods(); $og->order_id = $order->id; $og->goods_id = $cart->goods_id; $og->sku_id = 0; $og->count = $cart->count; $og->custom = $cart->custom; $og->save(); } return $order; }); OrderCreateEvent::dispatch($order); return [ "id" => $order->id, ]; } public function paginate($uid, $params) { $p = Order::where("user_id", $uid)->orderByDesc("id")->paginate($params['page_size']); return [ "total" => $p->total(), "page_total" => $p->lastPage(), "list" => array_map(function (Order $model) { /** @var OrderGoods $firstGoods */ $firstGoods = $model->goods->first(); return [ "id" => $model->id, "no" => $model->goods->pluck("sn")->implode("\sn"), "goods_name" => $firstGoods->goods->name, "goods_sn" => $firstGoods->sn, "user_id" => Utils::idNo4($model->user_id), "thumb" => $firstGoods->goods->thumb, "email" => $model->user->email, "created_at" => $model->created_at->format("Y-m-d H:i:s"), "goods" => $model->goods->map(function (OrderGoods $goods) { return [ "goods_id" => $goods->id, "goods_name" => $goods->goods->name, "goods_en_name" => $goods->goods->en_name, "goods_thumb" => $goods->goods->thumb, "goods_sn" => $goods->sn, "goods_count" => $goods->count, "custom" => array_map(function ($custom) { $custom = GoodsCustom::where("id", $custom['custom_id'])->first(); return [ "custom_id" => $custom['custom_id'], "title" => $custom->title, "en_title" => $custom->en_title, "value" => $custom['value'] ?? "", ]; }, $goods->custom ?? []), ]; }), "phone" => $model->user->phone, "name" => $model->user->name, "source" => $model->source, ]; }, $p->items()), ]; } }