Browse Source

支持attr

kphcdr 1 year ago
parent
commit
30a2cd8ef6

+ 17 - 6
app/Models/Goods/Goods.php

@@ -38,15 +38,21 @@ use Illuminate\Support\Arr;
  * @method static \Illuminate\Database\Query\Builder|Goods withTrashed()
  * @method static \Illuminate\Database\Query\Builder|Goods withoutTrashed()
  * @mixin \Eloquent
- * @property int                             $status 1 上架 2下架
+ * @property int                                                                                $status 1 上架 2下架
  * @method static \Illuminate\Database\Eloquent\Builder|Goods whereStatus($value)
  * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Goods\GoodsCategoryMap[] $map
- * @property-read int|null $map_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Goods\GoodsSku[] $sku
- * @property-read int|null $sku_count
- * @property-read mixed $thumb
- * @property int $view_total
+ * @property-read int|null                                                                      $map_count
+ * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Goods\GoodsSku[]         $sku
+ * @property-read int|null                                                                      $sku_count
+ * @property-read mixed                                                                         $thumb
+ * @property int                                                                                $view_total
  * @method static \Illuminate\Database\Eloquent\Builder|Goods whereViewTotal($value)
+ * @property string                                                                             $sn
+ * @property string                                                                             $url_3d
+ * @method static \Illuminate\Database\Eloquent\Builder|Goods whereSn($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|Goods whereUrl3d($value)
+ * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Goods\GoodsSpecMap[]     $specMap
+ * @property-read int|null                                                                      $spec_map_count
  */
 class Goods extends BaseModel
 {
@@ -79,4 +85,9 @@ class Goods extends BaseModel
     {
         return Arr::first($this->image_list);
     }
+
+    public function specMap()
+    {
+        return $this->hasMany(GoodsSpecMap::class, "goods_id", "id");
+    }
 }

+ 1 - 0
app/Models/Goods/GoodsSku.php

@@ -33,6 +33,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
  * @method static \Illuminate\Database\Query\Builder|GoodsSku withTrashed()
  * @method static \Illuminate\Database\Query\Builder|GoodsSku withoutTrashed()
  * @mixin \Eloquent
+ * @deprecated
  */
 class GoodsSku extends BaseModel
 {

+ 37 - 0
app/Models/Goods/GoodsSpecMap.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace App\Models\Goods;
+
+use App\Base\BaseModel;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+/**
+ * App\Models\Goods\GoodsSpecMap
+ *
+ * @property int                              $id
+ * @property int                              $goods_id
+ * @property int                              $spec_id
+ * @property string                           $value
+ * @property \Illuminate\Support\Carbon|null  $created_at
+ * @property \Illuminate\Support\Carbon|null  $updated_at
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSpecMap newModelQuery()
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSpecMap newQuery()
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSpecMap query()
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSpecMap whereCreatedAt($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSpecMap whereGoodsId($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSpecMap whereId($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSpecMap whereSpecId($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSpecMap whereUpdatedAt($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSpecMap whereValue($value)
+ * @mixin \Eloquent
+ * @property-read \App\Models\Goods\Spec|null $spec
+ */
+class GoodsSpecMap extends BaseModel
+{
+    protected $table = "goods_spec_map";
+
+    public function spec()
+    {
+        return $this->belongsTo(Spec::class);
+    }
+}

+ 4 - 2
app/Modules/Admin/Controllers/Admin/GoodsController.php

@@ -68,12 +68,14 @@ class GoodsController extends BaseController
             "name" => "required|max:50",
             "image_list" => "required|array",
             "category" => "required|array",
-            "spec" => 'required|array',
-            "sku" => 'required|array',
+            "spec" => 'array',
+            "spec_map" => "array",
             'weight' => "int",
             "desc_html" => "",
             "spec_attr_html" => "",
             "faq_html" => "",
+            "url_3d" => "",
+            "sn" => "",
         ]);
         return $this->ok($this->service->store($params));
     }

+ 28 - 49
app/Modules/Admin/Services/GoodsService.php

@@ -7,6 +7,7 @@ use App\Exceptions\ClientException;
 use App\Models\Goods\Goods;
 use App\Models\Goods\GoodsCategoryMap;
 use App\Models\Goods\GoodsSku;
+use App\Models\Goods\GoodsSpecMap;
 use App\Models\Goods\Spec;
 use App\Models\Goods\SpecAttr;
 use Illuminate\Database\Eloquent\Builder;
@@ -57,6 +58,8 @@ class GoodsService extends BaseService
         DB::transaction(function () use ($params, $goods, $specIdArr) {
 
             $goods->image_list = Arr::get($params, "image_list", []);
+            $goods->sn = Arr::get($params, "sn", []);
+            $goods->url_3d = Arr::get($params, "url_3d", []);
             $goods->spec = $specIdArr;
             $goods->name = Arr::get($params, "name", []);
             $goods->weight = Arr::get($params, "weight", 0);
@@ -79,44 +82,24 @@ class GoodsService extends BaseService
                     'is_public' => $c['is_public'],
                 ]);
             }
-            $skuArr = Arr::get($params, "sku", []);
+            $specMap = Arr::get($params, "spec_map", []);
             $doNotDelIdArr = [];
-            foreach ($skuArr as $c) {
-                /**
-                 * [
-                 * "id" => 0
-                 * "sn" => "规格型号"
-                 * "url_3d" => null
-                 * "spec_attr" => array:2 [
-                 * 0 => array:2 [
-                 * "id" => 1
-                 * "name" => "属性一"
-                 * ]
-                 * 1 => array:2 [
-                 * "id" => 2
-                 * "name" => "属性二"
-                 * ]
-                 * ]
-                 * "is_use" => 1
-                 * ]
-                 */
-                if ($id = Arr::get($c, "id")) {
-                    $sku = GoodsSku::where("goods_id", $goods->id)->where("id", $id)->first();
-                } else {
-                    $sku = new GoodsSku();
-                    $sku->goods_id = $goods->id;
-                }
-                $sku->sn = Arr::get($c, "sn", "");
-                if (empty($sku->sn)) {
-                    $sku->is_use = 0;
+            foreach ($specMap as $c) {
+
+                $model = GoodsSpecMap::where("goods_id", $goods->id)->where("spec_id", $c['spec_id'])->first();
+                if (is_null($model)) {
+                    $model = new GoodsSpecMap();
+                    $model->goods_id = $goods->id;
+                    $model->spec_id = $c['spec_id'];
                 }
-                $sku->url_3d = Arr::get($c, "url_3d", "");
-                $sku->spec_attr_list = Arr::get($c, "spec_attr", []);
-                $sku->save();
-                $doNotDelIdArr[] = $sku->id;
+
+                $model->value = $c['value'];
+
+                $model->save();
+                $doNotDelIdArr[] = $model->id;
             }
 
-            GoodsSku::where("goods_id", $goods->id)->whereNotIn("id", $doNotDelIdArr)->delete();
+            GoodsSpecMap::where("goods_id", $goods->id)->whereNotIn("id", $doNotDelIdArr)->delete();
         });
 
     }
@@ -138,25 +121,21 @@ class GoodsService extends BaseService
                 ];
             }),
             "spec" => $model->spec,
-            "sku" => $model->sku->map(function (GoodsSku $sku) {
-                return [
-                    "id" => $sku->id,
-                    "sn" => $sku->sn,
-                    "url_3d" => $sku->url_3d,
-                    "is_use" => $sku->is_use,
-                    "spec_attr" => array_map(function ($specAttr) {
-                        $id = Arr::get($specAttr, "id");
-                        return [
-                            "id" => $id,
-                            "name" => SpecAttr::where("id", $id)->value("name"),
-                        ];
-                    }, $sku->spec_attr_list),
-                ];
-            }),
+
             "weight" => $model->weight,
             "desc_html" => $model->desc_html,
             "spec_attr_html" => $model->spec_attr_html,
             "faq_html" => $model->faq_html,
+            "sn" => $model->sn,
+            "url_3d" => $model->url_3d,
+            "spec_map" => $model->specMap->map(function (GoodsSpecMap $map) {
+                return [
+                    "id" => $map->id,
+                    "spec_id" => $map->spec_id,
+                    "value" => $map->value,
+                    "name" => $map->spec->name,
+                ];
+            }),
         ];
 
         return $return;

+ 41 - 0
database/migrations/2023_05_19_144032_alert_goods_table_sn.php

@@ -0,0 +1,41 @@
+<?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::table("goods", function (Blueprint $table) {
+            $table->string("sn");
+            $table->string("url_3d");
+        });
+        Schema::create("goods_spec_map", function (Blueprint $table) {
+            $table->id();
+            $table->unsignedBigInteger("goods_id");
+            $table->unsignedBigInteger("spec_id");
+            $table->string("value")->default("");
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists("goods_spec_map");
+        Schema::table("goods", function (Blueprint $table) {
+            $table->dropColumn("sn");
+            $table->dropColumn("url_3d");
+        });
+    }
+};