From f45182c1f215a3dfdadff1c04f0c98ba2e17c2f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Tue, 21 Dec 2021 13:24:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E6=B6=88=E8=AE=A2=E5=8D=95=E6=97=B6,?= =?UTF-8?q?=E9=80=80=E8=BF=98=E8=B5=A0=E9=80=81=E7=9A=84=E8=B5=A0=E5=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/ProductGift.php | 10 ++++ app/Services/OrderService.php | 54 +++++++++++++------ ...2_06_135928_create_product_gifts_table.php | 4 +- ...9_add_remaining_to_product_gifts_table.php | 2 +- 4 files changed, 52 insertions(+), 18 deletions(-) diff --git a/app/Models/ProductGift.php b/app/Models/ProductGift.php index 12b41656..ad9c00c7 100644 --- a/app/Models/ProductGift.php +++ b/app/Models/ProductGift.php @@ -25,4 +25,14 @@ class ProductGift extends Model { return $this->belongsTo(ProductSku::class, 'gift_sku_id'); } + + /** + * 确认此赠品是否有限制 + * + * @return bool + */ + public function isLimit(): bool + { + return $this->limit !== 0; + } } diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 4ea0fedc..d28086bd 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -12,6 +12,7 @@ use App\Helpers\Numeric; use App\Helpers\Order as OrderHelper; use App\Models\Order; use App\Models\OrderProduct; +use App\Models\ProductGift; use App\Models\ProductSku; use App\Models\ShippingAddress; use App\Models\User; @@ -293,28 +294,29 @@ class OrderService continue; } + // 需赠送礼品的总数 + $num = $gift->num * $qty; + // 如果赠品有限,且剩余数量不足时,直接赠送剩余赠品 - if ($gift->limit !== 0 && $gift->remaining < $qty) { - $qty = $gift->remaining; + if ($gift->isLimit() && $num > $gift->remaining) { + // 计算剩余可赠送的份数 + $remainingQty = (int) ($gift->remaining / $gift->num); + + $num = $gift->num * $remainingQty; } - // 如果赠送的份数小于1,则不赠送 - if ($qty < 1) { - continue; - } - - if ($gift->limit === 0) { - $gift->increment('sent', $qty); - } else { + if ($gift->isLimit()) { $gift->update([ - 'remaining' => DB::raw("remaining-{$qty}"), - 'sent' => DB::raw("sent+{$qty}"), + 'remaining' => DB::raw("remaining-{$num}"), + 'sent' => DB::raw("sent+{$num}"), ]); + } else { + $gift->increment('sent', $num); } $gifts[] = [ 'sku' => $gift->giftSku, - 'num' => $qty*$gift->num, // 赠送商品总数 + 'num' => $num, // 赠送商品总数 ]; } @@ -790,9 +792,31 @@ class OrderService $products = $order->products()->get(); foreach ($products->load('sku') as $product) { - // 取消订单时,赠品不退回 + if ($product->sku === null) { + continue; + } + + // 如果商品不是赠品,则直接增加商品库存 if (! $product->isGift()) { - $product->sku?->increment('stock', $product->quantity); + $product->sku->increment('stock', $product->quantity); + continue; + } + + $gift = ProductGift::where('sku_id', $product->gift_for_sku_id) + ->where('gift_sku_id', $product->sku_id) + ->first(); + + if ($gift === null) { + continue; + } + + if ($gift->isLimit()) { + $gift->update([ + 'remaining' => DB::raw("remaining+{$product->quantity}"), + 'sent' => DB::raw("sent-{$product->quantity}"), + ]); + } else { + $gift->decrement('sent', $product->quantity); } } diff --git a/database/migrations/2021_12_06_135928_create_product_gifts_table.php b/database/migrations/2021_12_06_135928_create_product_gifts_table.php index ee38623d..042c0279 100644 --- a/database/migrations/2021_12_06_135928_create_product_gifts_table.php +++ b/database/migrations/2021_12_06_135928_create_product_gifts_table.php @@ -18,8 +18,8 @@ class CreateProductGiftsTable extends Migration $table->unsignedBigInteger('sku_id')->comment('主SKU商品ID'); $table->unsignedBigInteger('gift_sku_id')->comment('赠品SKU商品ID'); $table->unsignedInteger('num')->default(0)->comment('每份赠送数量'); - $table->unsignedInteger('limit')->default(0)->comment('上限(份)'); - $table->unsignedInteger('sent')->default(0)->comment('已送(份)'); + $table->unsignedInteger('limit')->default(0)->comment('上限个数'); + $table->unsignedInteger('sent')->default(0)->comment('已送个数'); $table->timestamps(); }); } diff --git a/database/migrations/2021_12_20_202109_add_remaining_to_product_gifts_table.php b/database/migrations/2021_12_20_202109_add_remaining_to_product_gifts_table.php index 0ef162a5..cb6c24e0 100644 --- a/database/migrations/2021_12_20_202109_add_remaining_to_product_gifts_table.php +++ b/database/migrations/2021_12_20_202109_add_remaining_to_product_gifts_table.php @@ -14,7 +14,7 @@ class AddRemainingToProductGiftsTable extends Migration public function up() { Schema::table('product_gifts', function (Blueprint $table) { - $table->unsignedInteger('remaining')->default(0)->comment('剩余(份)'); + $table->unsignedInteger('remaining')->default(0)->comment('剩余个数'); }); }