kphcdr 1 rok pred
rodič
commit
6cce33ce4e

+ 24 - 0
app/Events/OrderCreateEvent.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace App\Events;
+
+use App\Models\Order\Order;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class OrderCreateEvent
+{
+    use Dispatchable, InteractsWithSockets, SerializesModels;
+
+    public Order $order;
+
+    /**
+     * @param Order $order
+     */
+    public function __construct(Order $order)
+    {
+        $this->order = $order;
+    }
+}

+ 20 - 0
app/Listeners/OrderCreateListener.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace App\Listeners;
+
+use App\Events\OrderCreateEvent;
+
+class OrderCreateListener
+{
+
+    /**
+     * Handle the event.
+     *
+     * @param OrderCreateEvent $event
+     * @return void
+     */
+    public function handle($event)
+    {
+        logger()->info("ordering event:" . $event->order->id);
+    }
+}

+ 37 - 0
app/Models/Order/Order.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace App\Models\Order;
+
+use App\Base\BaseModel;
+
+/**
+ * App\Models\Order\Order
+ *
+ * @property int                                                                          $id
+ * @property int                                                                          $user_id
+ * @property \Illuminate\Support\Carbon|null                                              $created_at
+ * @property \Illuminate\Support\Carbon|null                                              $updated_at
+ * @method static \Illuminate\Database\Eloquent\Builder|Order newModelQuery()
+ * @method static \Illuminate\Database\Eloquent\Builder|Order newQuery()
+ * @method static \Illuminate\Database\Eloquent\Builder|Order query()
+ * @method static \Illuminate\Database\Eloquent\Builder|Order whereCreatedAt($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|Order whereId($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|Order whereUpdatedAt($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|Order whereUserId($value)
+ * @mixin \Eloquent
+ * @property string                                                                       $source 来源
+ * @method static \Illuminate\Database\Eloquent\Builder|Order whereSource($value)
+ * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Order\OrderGoods[] $goods
+ * @property-read int|null                                                                $goods_count
+ */
+class Order extends BaseModel
+{
+    const SOURCE_GOODS = "goods";
+    const SOURCE_CART = "cart";
+    protected $table = "order";
+
+    public function goods()
+    {
+        return $this->hasMany(OrderGoods::class);
+    }
+}

+ 40 - 0
app/Models/Order/OrderGoods.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace App\Models\Order;
+
+use App\Base\BaseModel;
+
+/**
+ * App\Models\Order\OrderGoods
+ *
+ * @property int                               $id
+ * @property int                               $order_id
+ * @property int                               $goods_id
+ * @property string                            $sn
+ * @property int                               $count
+ * @property \Illuminate\Support\Carbon|null   $created_at
+ * @property \Illuminate\Support\Carbon|null   $updated_at
+ * @method static \Illuminate\Database\Eloquent\Builder|OrderGoods newModelQuery()
+ * @method static \Illuminate\Database\Eloquent\Builder|OrderGoods newQuery()
+ * @method static \Illuminate\Database\Eloquent\Builder|OrderGoods query()
+ * @method static \Illuminate\Database\Eloquent\Builder|OrderGoods whereCount($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|OrderGoods whereCreatedAt($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|OrderGoods whereGoodsId($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|OrderGoods whereId($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|OrderGoods whereOrderId($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|OrderGoods whereSn($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|OrderGoods whereUpdatedAt($value)
+ * @mixin \Eloquent
+ * @property int                               $sku_id
+ * @method static \Illuminate\Database\Eloquent\Builder|OrderGoods whereSkuId($value)
+ * @property-read \App\Models\Order\Order|null $order
+ */
+class OrderGoods extends BaseModel
+{
+    protected $table = "order_goods";
+
+    public function order()
+    {
+        return $this->belongsTo(Order::class);
+    }
+}

+ 3 - 0
app/Models/User/UserFav.php

@@ -24,6 +24,9 @@ use Illuminate\Database\Eloquent\SoftDeletes;
  * @mixin \Eloquent
  * @property string|null                     $deleted_at
  * @method static \Illuminate\Database\Eloquent\Builder|UserFav whereDeletedAt($value)
+ * @method static \Illuminate\Database\Query\Builder|UserFav onlyTrashed()
+ * @method static \Illuminate\Database\Query\Builder|UserFav withTrashed()
+ * @method static \Illuminate\Database\Query\Builder|UserFav withoutTrashed()
  */
 class UserFav extends BaseModel
 {

+ 0 - 28
app/Modules/Admin/Controllers/Admin/OrderController.php

@@ -32,34 +32,6 @@ class OrderController extends BaseController
         ]);
     }
 
-    public function store()
-    {
-        return $this->ok($this->mockCategory());
-    }
-
-    protected function mockCategory()
-    {
-        return [
-            "id" => 1,
-            "name" => "分类名称",
-            "thumb" => "xx" . Image::imageUrl(),
-            "level" => 1,
-            "parent_id" => 1,
-            "parent_name" => "上级名称",
-            "child_category_count" => 1,
-            "child_goods_count" => 1,
-            "weight" => 1,
-            "index_weight" => 1,
-            "category_weight" => 1,
-            "search_weight" => 1,
-        ];
-    }
-
-    public function info()
-    {
-        return $this->ok($this->mockCategory());
-    }
-
     public function del()
     {
         $data = $this->valid([

+ 9 - 5
app/Modules/Mini/Controllers/GoodsController.php

@@ -3,8 +3,10 @@
 namespace App\Modules\Mini\Controllers;
 
 use App\Base\BaseController;
+use App\Models\Order\Order;
 use App\Modules\Mini\Services\FavService;
 use App\Modules\Mini\Services\GoodsService;
+use App\Modules\Mini\Services\OrderService;
 use Faker\Provider\Image;
 use Illuminate\Support\Facades\Auth;
 
@@ -63,11 +65,13 @@ class GoodsController extends BaseController
 
     public function ask()
     {
-        return $this->ok([
-            "id" => 1,
-            "thumb" => Image::imageUrl(),
-            "name" => "商品名称",
-            "url" => "http://baidu.com/xxxx",
+        $params = $this->valid([
+            "id" => "required",
+            "sku_id" => "required",
+            "sku_custom" => "",
         ]);
+        $params['source'] = Order::SOURCE_GOODS;
+
+        return $this->ok(app(OrderService::class)->askOne(Auth::user(), $params));
     }
 }

+ 56 - 0
app/Modules/Mini/Services/OrderService.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace App\Modules\Mini\Services;
+
+use App\Base\BaseService;
+use App\Events\OrderCreateEvent;
+use App\Models\Goods\Goods;
+use App\Models\Goods\GoodsSku;
+use App\Models\Order\Order;
+use App\Models\Order\OrderGoods;
+use App\Models\User\User;
+use App\Models\User\UserFav;
+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();
+
+            $sku = GoodsSku::where("goods_id", $params['id'])->where("id", $params['sku_id'])->first();
+            $og = new OrderGoods();
+            $og->order_id = $order->id;
+            $og->goods_id = $params['id'];
+            $og->sku_id = $params['sku_id'];
+            $og->count = 1;
+            $og->sn = $sku->sn;
+            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);
+        $gid = $order->goods->first()->goods_id;
+        $goods = Goods::find($gid);
+        return [
+            "id" => $order->id,
+            "thumb" => $goods->thumb,
+            "name" => $goods->name,
+            "url" => "http://baidu.com/xxxx",
+        ];
+    }
+
+}

+ 4 - 1
app/Providers/EventServiceProvider.php

@@ -2,10 +2,10 @@
 
 namespace App\Providers;
 
+use App\Events\OrderCreateEvent;
 use Illuminate\Auth\Events\Registered;
 use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
 use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
-use Illuminate\Support\Facades\Event;
 
 class EventServiceProvider extends ServiceProvider
 {
@@ -18,6 +18,9 @@ class EventServiceProvider extends ServiceProvider
         Registered::class => [
             SendEmailVerificationNotification::class,
         ],
+        OrderCreateEvent::class => [
+            \App\Listeners\OrderCreateListener::class,
+        ],
     ];
 
     /**

+ 42 - 0
database/migrations/2023_05_10_084235_create_order_table.php

@@ -0,0 +1,42 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration {
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('order', function (Blueprint $table) {
+            $table->id();
+            $table->unsignedInteger("user_id");
+            $table->string("source")->default("")->comment("来源");
+            $table->timestamps();
+        });
+        Schema::create('order_goods', function (Blueprint $table) {
+            $table->id();
+            $table->unsignedInteger("order_id");
+            $table->unsignedInteger("goods_id");
+            $table->unsignedInteger("sku_id");
+            $table->string("sn")->default("");
+            $table->unsignedInteger("count")->default(0);
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('order');
+        Schema::dropIfExists('order_goods');
+    }
+};