From 942d46f7a9de094389f9eb21aaa8d706c6165388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=B8=96=E7=95=8C?= <642747453@qq.com> Date: Mon, 8 Aug 2022 18:53:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20#10000=20=E5=AF=B9=E6=8E=A5=E5=BF=AB?= =?UTF-8?q?=E5=9B=A2=E5=9B=A2=E6=B5=8B=E8=AF=95=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/Test.php | 10 ++++-- app/Services/Business/BusinessClient.php | 31 +++++++++++++------ app/Services/Business/KuaiTuanTuan/Goods.php | 11 +++++-- .../Business/KuaiTuanTuan/KuaiTuanTuan.php | 28 +++++++++++++---- app/Services/Business/MiaoXuan/MiaoXuan.php | 7 ++++- ...03559_create_daily_stock_records_table.php | 2 +- 6 files changed, 66 insertions(+), 23 deletions(-) diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index cda01ab..b8ce985 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -2,6 +2,7 @@ namespace App\Console\Commands; +use App\Models\GoodsSku; use App\Models\Shop; use App\Services\Business\BusinessFactory; use Illuminate\Console\Command; @@ -42,7 +43,12 @@ class Test extends Command $shop = Shop::query()->find(2); $business = BusinessFactory::init()->make($shop->plat_id); $business->setShop($shop); - $res = $business->downloadGoods(); - var_dump($res); + // 下载商品 + $business->downloadGoods(1); + + // 库存修改 +// $business->incrQuantity(1); + + $this->info('执行测试成功'); } } diff --git a/app/Services/Business/BusinessClient.php b/app/Services/Business/BusinessClient.php index 4ef76d3..dd7b1c5 100644 --- a/app/Services/Business/BusinessClient.php +++ b/app/Services/Business/BusinessClient.php @@ -1,6 +1,7 @@ request('POST', $url, $headers); $res = json_decode($res->getBody()->getContents(), true); - $log = new Log(); - $log->module = 'plat'; - $log->action = 'POST'; - $log->target_type = $this->shop->plat_id; - $log->target_id = $this->skuId; - $log->target_field = $params['type']; - $log->user_id = 1; - $log->message = json_encode($res, 256); - $log->save(); + $disableLogType = [ + 'pdd.ktt.goods.query.list', + 'pdd.ktt.order.list', + 'pdd.ktt.increment.order.query' + ]; + if (!in_array($params['type'], $disableLogType, true)) { + $log = new Log(); + $log->module = 'plat'; + $log->action = 'POST'; + $log->target_type = $this->shop->plat_id; + $log->target_id = $this->skuId; + $log->target_field = $params['type']; + $log->user_id = 1; + $log->message = json_encode($res, 256); + $log->save(); + } + if (isset($res['error_response'])) { throw new \Exception($res['error_response']['error_msg'], $res['error_response']['error_code']); } diff --git a/app/Services/Business/KuaiTuanTuan/Goods.php b/app/Services/Business/KuaiTuanTuan/Goods.php index 7f4e540..90cc3cd 100644 --- a/app/Services/Business/KuaiTuanTuan/Goods.php +++ b/app/Services/Business/KuaiTuanTuan/Goods.php @@ -10,7 +10,7 @@ class Goods { $type = 'pdd.ktt.goods.query.list'; $appendParams = [ - 'activity_no' => $activityNo, // 非必填,团号(团号和创建时间只能传一个) +// 'activity_no' => $activityNo, // 非必填,团号(团号和创建时间只能传一个) 'page' => $page, 'size' => $size, // 'update_time_end' => '', // 非必填,结束最后更新时间(毫秒级时间戳) @@ -27,6 +27,7 @@ class Goods foreach ($goodsList as $businessGood) { $skuList = $businessGood['sku_list']; unset($businessGood['sku_list']); + $businessGood['goods_image_list'] = json_encode($businessGood['goods_image_list'], 256); foreach ($skuList as $sku) { $sku['spec_list'] = json_encode($sku['spec_list'], 256); $data = array_merge($businessGood, $sku); @@ -38,12 +39,16 @@ class Goods } } - public function downloadSingle($goodsId) + public static function downloadSingle($goodsId) { $type = 'pdd.ktt.goods.query.single'; $appendParams = [ - 'goods_id' => $goodsId + 'goods_id' => $goodsId, + 'page' => 1, + 'size' => 100, ]; + + return [$type, $appendParams]; } public static function incrQuantity($goodsId, $quantity, $skuId, $modifyType = 2) diff --git a/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php b/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php index 143f07d..08611d9 100644 --- a/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php +++ b/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php @@ -38,13 +38,13 @@ class KuaiTuanTuan extends BusinessClient return $this->shop; } - public function downloadGoods($page = 1) + public function downloadGoodsList($page = 1) { - [$type, $appendParams] = Goods::downloadGoods($this->shop->owner_name, $page); + [$type, $appendParams] = Goods::downloadGoods($this->shop->owner_id, $page); $res = $this->doRequest($type, $appendParams); $goods = $res['ktt_goods_query_list_response']['goods_list']; $this->bindGoods($goods); - $pageNum = ceil($res['total'] / $appendParams['size']); + $pageNum = ceil($res['ktt_goods_query_list_response']['total'] / $appendParams['size']); if ($pageNum > $page && 10 >= $page) { $this->downloadGoods($page + 1); } @@ -57,9 +57,12 @@ class KuaiTuanTuan extends BusinessClient public function incrQuantity($skuId) { - $goodsSku = GoodsSku::query()->find($skuId); - $business = BusinessGoodsSku::query()->where('shop_id', $this->shop->id)->where('self_sku_id', $skuId)->first(['goods_id', 'sku_id']); - [$type, $appendParams] = Goods::incrQuantity($business->goodsId, $goodsSku->stock, $business->sku_id); + $goodsSku = GoodsSku::query() + ->with(['goods:id,goods_code']) + ->find($skuId); + $code = $goodsSku->goods->goods_code . '_' . $goodsSku->sku_code; + $business = BusinessGoodsSku::query()->where('shop_id', $this->shop->id)->where('external_sku_id', $code)->first(['goods_id', 'sku_id']); + [$type, $appendParams] = Goods::incrQuantity($business->goods_id, $goodsSku->stock, $business->sku_id); $this->doRequest($type, $appendParams); } @@ -124,4 +127,17 @@ class KuaiTuanTuan extends BusinessClient return "https://oauth.pinduoduo.com/authorize/ktt?client_id={$this->clientId}&redirect_uri={$this->redirectUri}&state={$state}"; } + + public function downloadGoods($skuId) + { + $goodsSku = GoodsSku::query() + ->with(['goods:id,goods_code']) + ->find($skuId); + $code = $goodsSku->goods->goods_code . '_' . $goodsSku->sku_code; + $business = BusinessGoodsSku::query()->where('shop_id', $this->shop->id)->where('external_sku_id', $code)->first(['goods_id', 'sku_id']); + [$type, $appendParams] = Goods::downloadSingle($business->goods_id); + $res = $this->doRequest($type, $appendParams); + $goods = $res['response']['result']; + $this->bindGoods([$goods]); + } } diff --git a/app/Services/Business/MiaoXuan/MiaoXuan.php b/app/Services/Business/MiaoXuan/MiaoXuan.php index 300b967..afd6faf 100644 --- a/app/Services/Business/MiaoXuan/MiaoXuan.php +++ b/app/Services/Business/MiaoXuan/MiaoXuan.php @@ -15,7 +15,7 @@ class MiaoXuan extends BusinessClient // TODO: Implement auth() method. } - public function downloadGoods() + public function downloadGoodsList() { } @@ -41,4 +41,9 @@ class MiaoXuan extends BusinessClient { Order::saveOrders($orders, $this->shop->id); } + + public function downloadGoods($skuId) + { + // TODO: Implement downloadGoods() method. + } } diff --git a/database/migrations/2022_07_26_103559_create_daily_stock_records_table.php b/database/migrations/2022_07_26_103559_create_daily_stock_records_table.php index 7d97f34..894332f 100644 --- a/database/migrations/2022_07_26_103559_create_daily_stock_records_table.php +++ b/database/migrations/2022_07_26_103559_create_daily_stock_records_table.php @@ -20,7 +20,7 @@ class CreateDailyStockRecordsTable extends Migration $table->unsignedInteger('arrived_today_num')->default(0)->comment('今日到货'); $table->unsignedInteger('loss_num')->default(0)->comment('损耗'); $table->unsignedInteger('inventory')->default(0)->comment('库存盘点'); - $table->unique('sku_id', 'day'); + $table->unique(['sku_id', 'day']); $table->timestamps(); }); }