feat: #10000 增量订单获取

This commit is contained in:
赵世界 2022-08-10 16:39:25 +08:00
parent 5475a468af
commit 5ebe7b685c
13 changed files with 165 additions and 22 deletions

View File

@ -0,0 +1,56 @@
<?php
namespace App\Console\Commands;
use App\Services\Business\BusinessFactory;
use App\Utils\DateTimeUtils;
use Illuminate\Console\Command;
use App\Models\Shop;
use Swoole\Timer;
use Swoole\Event;
class Swoole extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'swoole:timer';
/**
* The console command description.
*
* @var string
*/
protected $description = '每秒钟获取增量订单';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
Timer::tick(1000, function () {
$shops = Shop::query()->where('plat_id', 1)->where('status', 1)->get();
$endTime = DateTimeUtils::getMicroTime();
$beginTime = $endTime - 1000;
foreach ($shops as $shop) {
BusinessFactory::init()->make($shop->plat_id)->setShop($shop)->downloadOrdersAndSave($beginTime, $endTime, 1, '', 'increment');
}
});
Event::wait();
}
}

View File

@ -7,7 +7,7 @@ use Illuminate\Console\Command;
use Spatie\Permission\Models\Permission; use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role; use Spatie\Permission\Models\Role;
class UpdateSuperPermissions extends Command class UpdateSuperAdminPermissions extends Command
{ {
/** /**
* The name and signature of the console command. * The name and signature of the console command.

View File

@ -2,6 +2,7 @@
namespace App\Console; namespace App\Console;
use App\Console\Commands\Inventory;
use Illuminate\Console\Scheduling\Schedule; use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@ -24,8 +25,9 @@ class Kernel extends ConsoleKernel
*/ */
protected function schedule(Schedule $schedule) protected function schedule(Schedule $schedule)
{ {
// $schedule->command('inspire') // 服务器添加cron入口
// ->hourly(); // * * * * * cd /home/wwwroot/erp.staging.chutang66.com && php artisan schedule:run >> /dev/null 2>&1
$schedule->command(Inventory::class)->dailyAt('07:00');
} }
/** /**

View File

@ -89,11 +89,9 @@ class ShopsController extends Controller
$business->setShop($shop); $business->setShop($shop);
if ('goods' === $request->get('type')) { if ('goods' === $request->get('type')) {
$business->bindGoods($request->get('data')); $business->bindGoods($request->get('data'));
event(new BindBusinessGoods($shop));
} }
if ('orders' === $request->get('type')) { if ('orders' === $request->get('type')) {
$business->saveOrders(); $business->saveOrders();
event(new UpdateBusinessGoodsStock($shop));
} }
} }
} }

View File

@ -0,0 +1,34 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class ProcessPodcast implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//
}
}

View File

@ -7,18 +7,20 @@ use Illuminate\Auth\Events\Registered;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
class UpdateBusinessGoodsStock class UpdateBusinessGoodsStock implements ShouldQueue
{ {
protected $shop; protected $shopId;
protected $updateOrders;
/** /**
* Create the event listener. * Create the event listener.
* *
* @return void * @return void
*/ */
public function __construct(Shop $shop) public function __construct($shopId, $updateOrders)
{ {
$this->shop = $shop; $this->shopId = $shopId;
$this->updateOrders = $updateOrders;
} }
/** /**
@ -29,6 +31,8 @@ class UpdateBusinessGoodsStock
*/ */
public function handle(Registered $event) public function handle(Registered $event)
{ {
// if (empty($this->updateOrders)) {
return;
}
} }
} }

View File

@ -88,18 +88,21 @@ abstract class BusinessClient
$res = (new Client())->request('POST', $url, $headers); $res = (new Client())->request('POST', $url, $headers);
$size = $res->getBody()->getSize(); $size = $res->getBody()->getSize();
$res = json_decode($res->getBody()->getContents(), true); $res = json_decode($res->getBody()->getContents(), true);
if ('pdd.ktt.increment.order.query' === $params['type']) {
$log = Log::query()->where('user_id', $this->shop->id)->where('target_field', $params['type'])->first();
} else {
$log = new Log(); $log = new Log();
}
$log->module = 'plat'; $log->module = 'plat';
$log->action = 'POST'; $log->action = 'POST';
$log->target_type = $this->shop->plat_id; $log->target_type = $this->shop->plat_id;
$log->target_id = $this->skuId; $log->target_id = $this->getSkuId();
$log->target_field = $params['type']; $log->target_field = $params['type'];
$log->user_id = 1; $log->user_id = $this->shop->id;
if ($size < 64000) { if ($size < 64000) {
$log->message = json_encode($res, 256); $log->message = json_encode($res, 256);
} }
$log->save(); $log->save();
if (isset($res['error_response'])) { if (isset($res['error_response'])) {
throw new \Exception($res['error_response']['error_msg'], $res['error_response']['error_code']); throw new \Exception($res['error_response']['error_msg'], $res['error_response']['error_code']);
} }

View File

@ -80,13 +80,14 @@ class KuaiTuanTuan extends BusinessClient
{ {
if ('increment' === $downloadType) { if ('increment' === $downloadType) {
[$type, $appendParams] = Order::downloadIncrementOrders($beginTime, $endTime, $page); [$type, $appendParams] = Order::downloadIncrementOrders($beginTime, $endTime, $page);
$responseName = 'ktt_increment_order_query_response';
} else { } else {
[$type, $appendParams] = Order::downloadOrders($beginTime, $endTime, $page); [$type, $appendParams] = Order::downloadOrders($beginTime, $endTime, $page);
$responseName = 'ktt_order_list_response';
} }
$res = $this->doRequest($type, $appendParams); $res = $this->doRequest($type, $appendParams);
$this->saveOrders($res['ktt_order_list_response']['order_list']); $this->saveOrders($res[$responseName]['order_list']);
exit(); $pageNum = ceil($res[$responseName]['total_count'] / $appendParams['page_size']);
$pageNum = ceil($res['ktt_order_list_response']['total_count'] / $appendParams['size']);
if ($pageNum > $page && 10 >= $page) { if ($pageNum > $page && 10 >= $page) {
$this->downloadOrdersAndSave($beginTime, $endTime, $page + 1); $this->downloadOrdersAndSave($beginTime, $endTime, $page + 1);
} }

View File

@ -2,6 +2,7 @@
namespace App\Services\Business\KuaiTuanTuan; namespace App\Services\Business\KuaiTuanTuan;
use App\Listeners\UpdateBusinessGoodsStock;
use App\Models\BusinessOrder; use App\Models\BusinessOrder;
use App\Models\BusinessOrderItem; use App\Models\BusinessOrderItem;
@ -17,7 +18,7 @@ class Order
'confirm_at_begin' => $beginTime, // 成交启始时间, 必填,毫秒时间戳 'confirm_at_begin' => $beginTime, // 成交启始时间, 必填,毫秒时间戳
'confirm_at_end' => $endTime, // 成交结束时间,必填, 毫秒时间戳,成交结束时间 - 成交启始时间 <= 24h 'confirm_at_end' => $endTime, // 成交结束时间,必填, 毫秒时间戳,成交结束时间 - 成交启始时间 <= 24h
'page_number' => $page, // 页码, 必填 'page_number' => $page, // 页码, 必填
'page_size' => 1, // 数量, 必填, 1100 'page_size' => 100, // 数量, 必填, 1100
// 非必填 // 非必填
// 'activity_no' => $activityNo, // 团号 // 'activity_no' => $activityNo, // 团号
'after_sales_status' => 0, // 售后状态, 可选 0-未发起售后 1-退款中 2-退款成功 3-待处理 4-拒绝退款 6-待(顾客)退货 7-待(团长)确认退货 8-(顾客)撤销 9-(系统)关闭 'after_sales_status' => 0, // 售后状态, 可选 0-未发起售后 1-退款中 2-退款成功 3-待处理 4-拒绝退款 6-待(顾客)退货 7-待(团长)确认退货 8-(顾客)撤销 9-(系统)关闭
@ -54,12 +55,14 @@ class Order
// 下载订单事件之后去统计 然后更新库存 // 下载订单事件之后去统计 然后更新库存
public static function saveOrders(array $orders, $shopId) public static function saveOrders(array $orders, $shopId)
{ {
$updateOrders = [];
foreach ($orders as $order) { foreach ($orders as $order) {
unset($order['custom_item_list'], $order['logistics_list'], $order['gift_order_list']); unset($order['custom_item_list'], $order['logistics_list'], $order['gift_order_list']);
$orderRecord = BusinessOrder::updateOrCreate( $orderRecord = BusinessOrder::updateOrCreate(
['shop_id' => $shopId, 'order_sn' => $order['order_sn']], ['shop_id' => $shopId, 'order_sn' => $order['order_sn']],
$order $order
); );
$updateOrders[] = $order['order_sn'];
foreach ($order['sub_order_list'] as $item) { foreach ($order['sub_order_list'] as $item) {
BusinessOrderItem::updateOrCreate( BusinessOrderItem::updateOrCreate(
['shop_id' => $shopId, 'business_order_id' => $orderRecord->id, 'goods_id' => $item['goods_id'], 'sku_id' => $item['sku_id']], ['shop_id' => $shopId, 'business_order_id' => $orderRecord->id, 'goods_id' => $item['goods_id'], 'sku_id' => $item['sku_id']],
@ -67,6 +70,7 @@ class Order
); );
} }
} }
event(new UpdateBusinessGoodsStock($shopId, $updateOrders));
} }
} }

View File

@ -2,6 +2,7 @@
namespace App\Services\Business\MiaoXuan; namespace App\Services\Business\MiaoXuan;
use App\Listeners\UpdateBusinessGoodsStock;
use App\Models\BusinessOrder; use App\Models\BusinessOrder;
class Order class Order
@ -9,11 +10,13 @@ class Order
// 下载订单事件之后去统计 然后更新库存 // 下载订单事件之后去统计 然后更新库存
public static function saveOrders(array $orders, $shopId) public static function saveOrders(array $orders, $shopId)
{ {
$updateOrders = [];
foreach ($orders as $order) { foreach ($orders as $order) {
$orderRecord = BusinessOrder::updateOrCreate( $orderRecord = BusinessOrder::updateOrCreate(
['shop_id' => $shopId, 'order_sn' => $order['order_sn']], ['shop_id' => $shopId, 'order_sn' => $order['order_sn']],
$order $order
); );
$updateOrders[] = $order['order_sn'];
foreach ($order['sub_order_list'] as $item) { foreach ($order['sub_order_list'] as $item) {
$orderRecord = BusinessOrder::updateOrCreate( $orderRecord = BusinessOrder::updateOrCreate(
['shop_id' => $shopId, 'business_order_id' => $orderRecord->id, 'goods_id' => $item['goods_id'], 'sku_id' => $item['sku_id']], ['shop_id' => $shopId, 'business_order_id' => $orderRecord->id, 'goods_id' => $item['goods_id'], 'sku_id' => $item['sku_id']],
@ -21,6 +24,7 @@ class Order
); );
} }
} }
event(new UpdateBusinessGoodsStock($shopId, $updateOrders));
} }
} }

4
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "5a01163bdf570af5226c567ad02f45d7", "content-hash": "037b06c1b26399725a1d9c0687402942",
"packages": [ "packages": [
{ {
"name": "aliyuncs/oss-sdk-php", "name": "aliyuncs/oss-sdk-php",
@ -9110,7 +9110,7 @@
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {
"php": "^7.2.5|^8.0", "php": "^7.2.5|^8.0",
"ext-json": "^1.7" "ext-json": "*"
}, },
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "2.3.0" "plugin-api-version": "2.3.0"

View File

@ -20,8 +20,9 @@ class CreateDailyStockRecordsTable extends Migration
$table->unsignedInteger('arrived_today_num')->default(0)->comment('今日到货'); $table->unsignedInteger('arrived_today_num')->default(0)->comment('今日到货');
$table->unsignedInteger('loss_num')->default(0)->comment('损耗'); $table->unsignedInteger('loss_num')->default(0)->comment('损耗');
$table->unsignedInteger('inventory')->default(0)->comment('库存盘点'); $table->unsignedInteger('inventory')->default(0)->comment('库存盘点');
$table->unique(['sku_id', 'day']);
$table->timestamps(); $table->timestamps();
// 索引
$table->unique(['sku_id', 'day']);
}); });
} }

View File

@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateJobsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('jobs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedTinyInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('jobs');
}
}