123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace App\Modules\Mini\Services;
- use App\Base\BaseService;
- use App\Base\Utils;
- use App\Events\OrderCreateEvent;
- use App\Models\Cart;
- use App\Models\Goods\Goods;
- use App\Models\Goods\GoodsCustom;
- use App\Models\Order\Order;
- use App\Models\Order\OrderGoods;
- use App\Models\User\User;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\DB;
- class OrderService extends BaseService
- {
- /**
- * 询价单个商品
- */
- public function askOne(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();
- $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()),
- ];
- }
- }
|