From 274918cb8f24ecf5ef91115845979795d3d3ab6c Mon Sep 17 00:00:00 2001 From: Sam Chen Date: Sun, 2 Jan 2022 22:50:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E5=A4=9A=E8=A7=84=E6=A0=BC?= =?UTF-8?q?=E8=A1=A8=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Admin/ProductsController.php | 6 +- app/Models/Product.php | 6 +- app/Models/ProductSku.php | 6 +- app/Services/ProductService.php | 27 ++- .../2021_12_26_111435_create_tables.php | 24 ++- .../admin/pages/products/form/form.blade.php | 203 +++++++++++++++--- .../admin/pages/products/index.blade.php | 2 +- 7 files changed, 224 insertions(+), 50 deletions(-) diff --git a/app/Http/Controllers/Admin/ProductsController.php b/app/Http/Controllers/Admin/ProductsController.php index 314babf9..d7ea1f68 100644 --- a/app/Http/Controllers/Admin/ProductsController.php +++ b/app/Http/Controllers/Admin/ProductsController.php @@ -48,9 +48,11 @@ class ProductsController extends Controller return view('admin.pages.products.form.form', $data); } - public function update(Request $request, $id) + public function update(Request $request, Product $product) { - // + $product = (new ProductService)->update($product, $request->all()); + + return redirect()->route('admin.products.index')->with('success', 'product updated'); } public function destroy($id) diff --git a/app/Models/Product.php b/app/Models/Product.php index ebf05e31..e1e43679 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -9,15 +9,15 @@ class Product extends Model { use HasFactory; - protected $fillable = ['image', 'video', 'sort_order', 'status', 'variable']; + protected $fillable = ['image', 'video', 'position', 'active', 'variables']; public function skus() { return $this->hasMany(ProductSku::class); } - public function getVariableDecodedAttribute() + public function getVariablesDecodedAttribute() { - return json_decode($this->variable, true); + return json_decode($this->variables, true); } } diff --git a/app/Models/ProductSku.php b/app/Models/ProductSku.php index 167e78e2..2f61ab08 100644 --- a/app/Models/ProductSku.php +++ b/app/Models/ProductSku.php @@ -9,5 +9,9 @@ class ProductSku extends Model { use HasFactory; - protected $fillable = ['product_id', 'image', 'model', 'sku', 'price', 'quantity', 'is_default']; + protected $fillable = ['product_id', 'variants', 'position', 'image', 'model', 'sku', 'price', 'origin_price', 'cost_price', 'quantity', 'is_default']; + + protected $casts = [ + 'variants' => 'array', + ]; } diff --git a/app/Services/ProductService.php b/app/Services/ProductService.php index d46d9bf2..48901bd8 100644 --- a/app/Services/ProductService.php +++ b/app/Services/ProductService.php @@ -7,24 +7,35 @@ use Illuminate\Support\Facades\DB; class ProductService { - public function create($data) + public function create(array $data): Product { + $product = new Product; + return $this->createOrUpdate($product, $data); + } + + public function update(Product $product, array $data): Product + { + return $this->createOrUpdate($product, $data); + } + + protected function createOrUpdate(Product $product, array $data): Product + { + $isUpdating = $product->id > 0; + try { DB::beginTransaction(); - $product = new Product($data); - if (isset($data['variant'])) { - $product->variable = json_encode($data['variant']); - } + $product->fill($data); $product->saveOrFail(); $skus = []; foreach ($data['skus'] as $index => $rawSku) { - $sku = $rawSku; - $sku['is_default'] = $index == 0; - $skus[] = $sku; + $skus[] = $rawSku; } + if ($isUpdating) { + $product->skus()->delete(); + } $product->skus()->createMany($skus); DB::commit(); diff --git a/database/migrations/2021_12_26_111435_create_tables.php b/database/migrations/2021_12_26_111435_create_tables.php index 175e08b7..c2869adc 100644 --- a/database/migrations/2021_12_26_111435_create_tables.php +++ b/database/migrations/2021_12_26_111435_create_tables.php @@ -15,11 +15,11 @@ class CreateTables extends Migration { Schema::create('products', function (Blueprint $table) { $table->id()->startingValue(100_000); - $table->string('image'); - $table->string('video'); - $table->integer('sort_order'); - $table->boolean('status'); - $table->json('variable'); + $table->string('image')->default(''); + $table->string('video')->default(''); + $table->integer('position')->default(0); + $table->boolean('active'); + $table->json('variables')->nullable(); $table->timestamps(); $table->softDeletes(); }); @@ -27,11 +27,15 @@ class CreateTables extends Migration Schema::create('product_skus', function (Blueprint $table) { $table->id()->startingValue(100_000); $table->unsignedBigInteger('product_id'); - $table->string('image'); - $table->string('model'); - $table->string('sku'); - $table->double('price'); - $table->integer('quantity'); + $table->string('variants')->default(0); + $table->integer('position')->default(0); + $table->string('image')->default(''); + $table->string('model')->default(''); + $table->string('sku')->default(''); + $table->double('price')->default(0); + $table->double('origin_price')->default(0); + $table->double('cost_price')->default(0); + $table->integer('quantity')->default(0); $table->boolean('is_default'); $table->timestamps(); }); diff --git a/resources/views/admin/pages/products/form/form.blade.php b/resources/views/admin/pages/products/form/form.blade.php index 18088790..8d7c02d0 100644 --- a/resources/views/admin/pages/products/form/form.blade.php +++ b/resources/views/admin/pages/products/form/form.blade.php @@ -6,14 +6,15 @@ @section('content')

product

-
+ @csrf + @method(($product ?? null) ? 'PUT' : 'POST') - - + +

skus

@@ -21,35 +22,72 @@ 多规格
-
- - - +
+
+ + +
+ +
+ +
+ +
-
+
+ - - - - - - - - + + + + + + + + + + + + + + + + + + + + + +
+ @{{ variant.name || 'No name' }} + imagemodelskupriceorgin pricecost pricequantity
+ + + + +
- - - - - - + + + + + + + + + +
@@ -60,17 +98,132 @@ @push('footer') @endpush \ No newline at end of file diff --git a/resources/views/admin/pages/products/index.blade.php b/resources/views/admin/pages/products/index.blade.php index 0ab8f58c..8659783c 100644 --- a/resources/views/admin/pages/products/index.blade.php +++ b/resources/views/admin/pages/products/index.blade.php @@ -6,7 +6,7 @@ @foreach ($products as $product) {{ $product->id }} - {{ $product->variable ? '多规格' : '单规格' }} + {{ $product->variables ? '多规格' : '单规格' }} 编辑