From a2867b0ea874231cc5cda35e28a348529dbc303b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=B8=96=E7=95=8C?= <642747453@qq.com> Date: Fri, 21 Apr 2023 19:43:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20#10000=20=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Events/BusinessOrdersUpdate.php | 1 - app/Events/StockUpdateEvent.php | 4 +- .../Business/BusinessGoodsSkusController.php | 1 - app/Imports/TodayPriceImport.php | 3 +- .../CombinationGoodsStockUpdateListener.php | 95 +++++++++++++++++++ app/Models/CombinationGood.php | 5 + app/Providers/EventServiceProvider.php | 3 + ...04_20_091631_create_today_prices_table.php | 1 - .../frontend/src/views/goods/combination.vue | 27 ++++++ 9 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 app/Listeners/CombinationGoodsStockUpdateListener.php diff --git a/app/Events/BusinessOrdersUpdate.php b/app/Events/BusinessOrdersUpdate.php index b44823c..38f7bff 100644 --- a/app/Events/BusinessOrdersUpdate.php +++ b/app/Events/BusinessOrdersUpdate.php @@ -19,7 +19,6 @@ class BusinessOrdersUpdate public $num; public $businessGoods; public $goodsSku; - public $goodsSkus; /** * Create a new event instance. diff --git a/app/Events/StockUpdateEvent.php b/app/Events/StockUpdateEvent.php index 14b34a4..fb8acf8 100644 --- a/app/Events/StockUpdateEvent.php +++ b/app/Events/StockUpdateEvent.php @@ -18,6 +18,7 @@ class StockUpdateEvent public $goodsSku; public $goodsSkus; public $isBatch; + public $combinationGoodsUpdate; /** * Create a new event instance. @@ -26,9 +27,10 @@ class StockUpdateEvent * * @return void */ - public function __construct($data, $isBatch = 0) + public function __construct($data, $isBatch = 0, $combinationGoodsUpdate = false) { $this->isBatch = $isBatch; + $this->combinationGoodsUpdate = $combinationGoodsUpdate; if (is_array($data)) { // ids集合 $this->goodsSkus = GoodsSku::query()->whereIn('id', $data)->get(); diff --git a/app/Http/Controllers/Business/BusinessGoodsSkusController.php b/app/Http/Controllers/Business/BusinessGoodsSkusController.php index 264bca9..b00820d 100644 --- a/app/Http/Controllers/Business/BusinessGoodsSkusController.php +++ b/app/Http/Controllers/Business/BusinessGoodsSkusController.php @@ -134,7 +134,6 @@ class BusinessGoodsSkusController extends Controller $todayGoodsPrice = []; foreach ($todayPrice as $item) { $todayGoodsPrice[$item['external_sku_id']] = [ - 'today_price' => $item['price'], 'goods_name' => $item['goodsSku']['goods']['title'] . $item['goodsSku']['title'], 'external_sku_id' => $item['external_sku_id'], 'shop_price' => json_decode($item['shop_price'], true), diff --git a/app/Imports/TodayPriceImport.php b/app/Imports/TodayPriceImport.php index 7438845..f3cf12a 100644 --- a/app/Imports/TodayPriceImport.php +++ b/app/Imports/TodayPriceImport.php @@ -20,14 +20,13 @@ class TodayPriceImport implements ToArray, SkipsEmptyRows }, $row); $shopPrice = []; foreach ($row as $i => $v) { - if ($i > 2) { + if ($i > 1) { $shopPrice[$header[$i]] = $v; } } $data[] = [ 'day' => $day, 'external_sku_id' => $row[1], - 'price' => $row[2], 'shop_price' => json_encode($shopPrice, 256) ]; } diff --git a/app/Listeners/CombinationGoodsStockUpdateListener.php b/app/Listeners/CombinationGoodsStockUpdateListener.php new file mode 100644 index 0000000..6796ad1 --- /dev/null +++ b/app/Listeners/CombinationGoodsStockUpdateListener.php @@ -0,0 +1,95 @@ +combinationGoodsUpdate) { + return false; + } + $updateIds = $combinationGoodsIds = $combinationGoodsItemIds = []; + if ($event->goodsSku) { + if ($event->goodsSku->is_combination) { + $combinationGoodsIds[] = $event->goodsSku->id; + } else { + $combinationGoodsItemIds[$event->goodsSku->id] = $event->goodsSku->stock; + } + } + if ($event->goodsSkus) { + foreach ($event->goodsSkus as $sku) { + if ($sku->is_combination) { + $combinationGoodsIds[] = $sku->id; + } else { + $combinationGoodsItemIds[$sku->id] = $sku->stock; + } + } + } + // 减子商品库存 + if ($combinationGoodsIds) { + $combinationGoods = CombinationGood::query() + ->with('goodsSku:id,stock') + ->whereIn('goods_sku_id', $combinationGoodsIds) + ->get(); + foreach ($combinationGoods as $item) { + $sku = GoodsSku::query()->find($item['item_id']); + $sku->stock -= $item['item_num']; + $sku->save(); + $updateIds[] = $sku->id; + } + } + // 计算主商品库存 + if ($combinationGoodsItemIds) { + $itemIds = array_keys($combinationGoodsItemIds); + $goodsSkuIds = CombinationGood::query() + ->whereIn('item_id', $itemIds) + ->pluck('goods_sku_id') + ->toArray(); + if (empty($goodsSkuIds)) { + return false; + } + $goodsSkus = GoodsSku::query() + ->whereIn('id', $goodsSkuIds) + ->pluck('stock', 'id') + ->toArray(); + foreach ($combinationGoodsItemIds as $itemId => $stock) { + $combinationGoods = CombinationGood::query() + ->where('item_id', $itemId) + ->get(); + foreach ($combinationGoods as $goods) { + $stock = $combinationGoodsItemIds[$goods['item_id']] / $goods['item_num']; + if ($stock < $goodsSkus[$goods['goods_sku_id']]) { + GoodsSku::query()->where('id', $goods['goods_sku_id'])->update(['stock' => $stock]); + $updateIds[] = $goods['goods_sku_id']; + } + } + } + } + if ($updateIds) { + event(new StockUpdateEvent($updateIds, 1, true)); + } + } +} diff --git a/app/Models/CombinationGood.php b/app/Models/CombinationGood.php index bb39ad1..1ca09cd 100644 --- a/app/Models/CombinationGood.php +++ b/app/Models/CombinationGood.php @@ -12,4 +12,9 @@ class CombinationGood extends Model { return $this->belongsTo(GoodsSku::class, 'item_id', 'id'); } + + public function goodsSku() + { + return $this->belongsTo(GoodsSku::class, 'goods_sku_id', 'id'); + } } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 882f12e..fdd269f 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -8,6 +8,7 @@ use App\Events\StockUpdateEvent; use App\Listeners\GroupQueryListener; use App\Listeners\StockUpdateListener; use App\Listeners\StockWarning; +use App\Listeners\CombinationGoodsStockUpdateListener; use App\Listeners\UpdateBusinessGoodsStock; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; @@ -22,10 +23,12 @@ class EventServiceProvider extends ServiceProvider BusinessOrdersUpdate::class => [ UpdateBusinessGoodsStock::class, StockWarning::class, + CombinationGoodsStockUpdateListener::class, ], StockUpdateEvent::class => [ StockUpdateListener::class, StockWarning::class, + CombinationGoodsStockUpdateListener::class, ], GroupSetEvent::class => [ GroupQueryListener::class, diff --git a/database/migrations/2023_04_20_091631_create_today_prices_table.php b/database/migrations/2023_04_20_091631_create_today_prices_table.php index 2713516..9b5eca4 100644 --- a/database/migrations/2023_04_20_091631_create_today_prices_table.php +++ b/database/migrations/2023_04_20_091631_create_today_prices_table.php @@ -21,7 +21,6 @@ class CreateTodayPricesTable extends Migration $table->bigIncrements('id'); $table->date('day'); $table->string('external_sku_id'); - $table->float('price'); $table->text('shop_price'); $table->index(['external_sku_id', 'day']); $table->timestamps(); diff --git a/resources/frontend/src/views/goods/combination.vue b/resources/frontend/src/views/goods/combination.vue index 83ff1f4..f3ae8ae 100644 --- a/resources/frontend/src/views/goods/combination.vue +++ b/resources/frontend/src/views/goods/combination.vue @@ -12,6 +12,11 @@ 筛选 重置筛选 + + 商品货架导入 + @@ -227,6 +232,28 @@ export default { item_num: 1, }], }; + }, + beforeUpload() { + this.loadingModule = this.$loading({ + lock: true, + text: '表格导入中...', + spinner: 'el-icon-loading', + background: 'rgba(0, 0, 0, 0.7)' + }); + }, + uploadSuccess(response) { + this.$message({ + message: response.message, + type: "success", + }); + this.loadingModule.close(); + }, + uploadError(err) { + this.$message({ + message: err.errorMessage, + type: "error", + }); + this.loadingModule.close(); } },