From 15d95f0e9fccdda111f0ecb6d2d8a09b4a029caa 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, 4 Nov 2022 15:01:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20#10000=20=E5=BF=AB=E5=9B=A2=E5=9B=A2?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/Test.php | 8 ++++++- app/Imports/InventoryImport.php | 3 ++- app/Listeners/StockUpdateListener.php | 23 ++++++++++++------- app/Services/Business/BusinessClient.php | 18 +++++++++++++++ .../Business/KuaiTuanTuan/KuaiTuanTuan.php | 15 ++++++++++++ app/Services/Business/MiaoXuan/MiaoXuan.php | 4 ++++ 6 files changed, 61 insertions(+), 10 deletions(-) diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index 7a1804e..38da580 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -2,6 +2,8 @@ namespace App\Console\Commands; +use App\Events\StockUpdateEvent; +use App\Models\BusinessGoodsSku; use App\Models\GoodsSku; use App\Models\Log; use App\Models\Shop; @@ -9,6 +11,7 @@ use App\Services\Business\BusinessFactory; use App\Utils\DateTimeUtils; use Illuminate\Console\Command; use Illuminate\Support\Facades\DB; +use App\Jobs\BusinessGoodsSkuIncrQuantity; class Test extends Command { @@ -43,6 +46,9 @@ class Test extends Command */ public function handle() { + $onSkuIds = [2823]; + event(new StockUpdateEvent($onSkuIds)); + exit(); $shop = Shop::query()->find(1); $business = BusinessFactory::init()->make($shop->plat_id); $business->setShop($shop); @@ -66,7 +72,7 @@ class Test extends Command // $business->incrQuantity(1); // 订单下载 - $beginTime = DateTimeUtils::getMicroTime('2022-08-30 19:17:03'); + $beginTime = DateTimeUtils::getMicroTime('2022-08-30 19:17:03'); $endTime = DateTimeUtils::getMicroTime('2022-08-30 19:19:03'); $business->downloadOrdersAndSave($beginTime, $endTime); diff --git a/app/Imports/InventoryImport.php b/app/Imports/InventoryImport.php index 58c2886..ae433e2 100644 --- a/app/Imports/InventoryImport.php +++ b/app/Imports/InventoryImport.php @@ -41,6 +41,7 @@ class InventoryImport implements ToCollection, SkipsEmptyRows continue; } $goodsSku = GoodsSku::query() + ->select(['id', 'status', 'stock', 'cost']) ->where('goods_id', $hasGoods[$row[0]]['id']) ->where('sku_code', $row[4]) ->first(); @@ -67,7 +68,7 @@ class InventoryImport implements ToCollection, SkipsEmptyRows ->toArray(); $downSkuIds = array_diff($onSkuIds, $updateIds); foreach ($downSkuIds as $downSkuId) { - $goodsSku = GoodsSku::query()->find($downSkuId); + $goodsSku = GoodsSku::query()->select(['id', 'yesterday_num', 'stock'])->find($downSkuId); $goodsSku->yesterday_num -= $goodsSku->stock; $goodsSku->stock = 0; $goodsSku->save(); diff --git a/app/Listeners/StockUpdateListener.php b/app/Listeners/StockUpdateListener.php index e468ebb..9bf7b67 100644 --- a/app/Listeners/StockUpdateListener.php +++ b/app/Listeners/StockUpdateListener.php @@ -8,6 +8,7 @@ use App\Models\BusinessGoodsSku; use App\Models\Shop; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; +use App\Services\Business\BusinessFactory; class StockUpdateListener { @@ -36,18 +37,24 @@ class StockUpdateListener foreach ($shops as $shop) { if (isset($event->goodsSku)) { $num = $event->goodsSku->stock; - $businessGoodsSkus = BusinessGoodsSku::query()->where('shop_id', $shop->id)->where('is_sync', 1)->where('external_sku_id', $event->goodsSku->goods['goods_code'] . '_' . $event->goodsSku->sku_code)->get(); - foreach ($businessGoodsSkus as $businessGoodsSku) { - BusinessGoodsSkuIncrQuantity::dispatch($shop, $businessGoodsSku->toArray(), $num, false); - } + $businessGoodsSkus = BusinessGoodsSku::query() + ->select(['goods_id', 'sku_id']) + ->where('shop_id', $shop->id) + ->where('is_sync', 1) + ->where('external_sku_id', $event->goodsSku->goods['goods_code'] . '_' . $event->goodsSku->sku_code) + ->get(); + BusinessFactory::init()->make($shop['plat_id'])->setShopWithId($shop['id'])->batchIncrQuantity($businessGoodsSkus->toArray(), $num, false); } if (isset($event->goodsSkus)) { foreach ($event->goodsSkus as $goodsSku) { $num = $goodsSku->stock; - $businessGoodsSkus = BusinessGoodsSku::query()->where('shop_id', $shop->id)->where('is_sync', 1)->where('external_sku_id', $goodsSku->goods['goods_code'] . '_' . $goodsSku->sku_code)->get(); - foreach ($businessGoodsSkus as $businessGoodsSku) { - BusinessGoodsSkuIncrQuantity::dispatch($shop, $businessGoodsSku->toArray(), $num, false); - } + $businessGoodsSkus = BusinessGoodsSku::query() + ->select(['goods_id', 'sku_id']) + ->where('shop_id', $shop->id) + ->where('is_sync', 1) + ->where('external_sku_id', $goodsSku->goods['goods_code'] . '_' . $goodsSku->sku_code) + ->get(); + BusinessFactory::init()->make($shop['plat_id'])->setShopWithId($shop['id'])->batchIncrQuantity($businessGoodsSkus->toArray(), $num, false); } } } diff --git a/app/Services/Business/BusinessClient.php b/app/Services/Business/BusinessClient.php index bfb31b8..38de67a 100644 --- a/app/Services/Business/BusinessClient.php +++ b/app/Services/Business/BusinessClient.php @@ -11,6 +11,7 @@ use App\Models\Log; use App\Models\Shop; use GuzzleHttp\Client; use Illuminate\Support\Facades\Auth; +use GuzzleHttp\Promise; abstract class BusinessClient { @@ -159,4 +160,21 @@ abstract class BusinessClient return $res; } + + public function batchAsyncPostRequest($url, $batchParams) + { + $client = new Client(); + $promises = []; + foreach ($batchParams as $param) { + $options = [ + 'headers' => ['Content-type' => 'application/x-www-form-urlencoded;'], + 'form_params' => $param + ]; + $promises[] = $client->postAsync($url, $options); + } +// $res = Promise\Utils::unwrap($promises); +// foreach ($res as $item) { +// var_dump($item->getBody()->getContents()); +// } + } } diff --git a/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php b/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php index 7835f0a..2d05bf6 100644 --- a/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php +++ b/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php @@ -63,6 +63,21 @@ class KuaiTuanTuan extends BusinessClient $this->doRequest($type, $appendParams); } + public function batchIncrQuantity($businessGoodsSkus, $num, $incremental) + { + $batchAppendParams = []; + foreach ($businessGoodsSkus as $businessGoodsSku) { + [$type, $appendParams] = Goods::incrQuantity($businessGoodsSku['goods_id'], $businessGoodsSku['sku_id'], $num, $incremental ? 1 : 2); + $appendParams['type'] = $type; + $appendParams['client_id'] = $this->clientId; + $appendParams['timestamp'] = time(); + $appendParams['access_token'] = $this->getShop()->access_token; + $appendParams['sign'] = $this->getSign($appendParams); + $batchAppendParams[] = $appendParams; + } + $this->batchAsyncPostRequest('https://gw-api.pinduoduo.com/api/router', $batchAppendParams); + } + /** * 下载订单 * diff --git a/app/Services/Business/MiaoXuan/MiaoXuan.php b/app/Services/Business/MiaoXuan/MiaoXuan.php index 31a60c4..f2fd30a 100644 --- a/app/Services/Business/MiaoXuan/MiaoXuan.php +++ b/app/Services/Business/MiaoXuan/MiaoXuan.php @@ -33,6 +33,10 @@ class MiaoXuan extends BusinessClient { } + public function batchIncrQuantity($businessGoodsSkus, $num, $incremental) + { + } + public function downloadGoods($skuId) { // TODO: Implement downloadGoods() method.