feat: #10000 增量订单获取
This commit is contained in:
parent
5475a468af
commit
5ebe7b685c
56
app/Console/Commands/Swoole.php
Normal file
56
app/Console/Commands/Swoole.php
Normal 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();
|
||||
}
|
||||
}
|
||||
@ -7,7 +7,7 @@ use Illuminate\Console\Command;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
class UpdateSuperPermissions extends Command
|
||||
class UpdateSuperAdminPermissions extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Console\Commands\Inventory;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
|
||||
@ -24,8 +25,9 @@ class Kernel extends ConsoleKernel
|
||||
*/
|
||||
protected function schedule(Schedule $schedule)
|
||||
{
|
||||
// $schedule->command('inspire')
|
||||
// ->hourly();
|
||||
// 服务器添加cron入口
|
||||
// * * * * * cd /home/wwwroot/erp.staging.chutang66.com && php artisan schedule:run >> /dev/null 2>&1
|
||||
$schedule->command(Inventory::class)->dailyAt('07:00');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -89,11 +89,9 @@ class ShopsController extends Controller
|
||||
$business->setShop($shop);
|
||||
if ('goods' === $request->get('type')) {
|
||||
$business->bindGoods($request->get('data'));
|
||||
event(new BindBusinessGoods($shop));
|
||||
}
|
||||
if ('orders' === $request->get('type')) {
|
||||
$business->saveOrders();
|
||||
event(new UpdateBusinessGoodsStock($shop));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
34
app/Jobs/ProcessPodcast.php
Normal file
34
app/Jobs/ProcessPodcast.php
Normal 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()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@ -7,28 +7,32 @@ use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
|
||||
class UpdateBusinessGoodsStock
|
||||
class UpdateBusinessGoodsStock implements ShouldQueue
|
||||
{
|
||||
protected $shop;
|
||||
protected $shopId;
|
||||
protected $updateOrders;
|
||||
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Shop $shop)
|
||||
public function __construct($shopId, $updateOrders)
|
||||
{
|
||||
$this->shop = $shop;
|
||||
$this->shopId = $shopId;
|
||||
$this->updateOrders = $updateOrders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param Registered $event
|
||||
* @param Registered $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Registered $event)
|
||||
{
|
||||
//
|
||||
if (empty($this->updateOrders)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,18 +88,21 @@ abstract class BusinessClient
|
||||
$res = (new Client())->request('POST', $url, $headers);
|
||||
$size = $res->getBody()->getSize();
|
||||
$res = json_decode($res->getBody()->getContents(), true);
|
||||
$log = new Log();
|
||||
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->module = 'plat';
|
||||
$log->action = 'POST';
|
||||
$log->target_type = $this->shop->plat_id;
|
||||
$log->target_id = $this->skuId;
|
||||
$log->target_id = $this->getSkuId();
|
||||
$log->target_field = $params['type'];
|
||||
$log->user_id = 1;
|
||||
$log->user_id = $this->shop->id;
|
||||
if ($size < 64000) {
|
||||
$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']);
|
||||
}
|
||||
|
||||
@ -80,13 +80,14 @@ class KuaiTuanTuan extends BusinessClient
|
||||
{
|
||||
if ('increment' === $downloadType) {
|
||||
[$type, $appendParams] = Order::downloadIncrementOrders($beginTime, $endTime, $page);
|
||||
$responseName = 'ktt_increment_order_query_response';
|
||||
} else {
|
||||
[$type, $appendParams] = Order::downloadOrders($beginTime, $endTime, $page);
|
||||
$responseName = 'ktt_order_list_response';
|
||||
}
|
||||
$res = $this->doRequest($type, $appendParams);
|
||||
$this->saveOrders($res['ktt_order_list_response']['order_list']);
|
||||
exit();
|
||||
$pageNum = ceil($res['ktt_order_list_response']['total_count'] / $appendParams['size']);
|
||||
$this->saveOrders($res[$responseName]['order_list']);
|
||||
$pageNum = ceil($res[$responseName]['total_count'] / $appendParams['page_size']);
|
||||
if ($pageNum > $page && 10 >= $page) {
|
||||
$this->downloadOrdersAndSave($beginTime, $endTime, $page + 1);
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services\Business\KuaiTuanTuan;
|
||||
|
||||
use App\Listeners\UpdateBusinessGoodsStock;
|
||||
use App\Models\BusinessOrder;
|
||||
use App\Models\BusinessOrderItem;
|
||||
|
||||
@ -17,7 +18,7 @@ class Order
|
||||
'confirm_at_begin' => $beginTime, // 成交启始时间, 必填,毫秒时间戳
|
||||
'confirm_at_end' => $endTime, // 成交结束时间,必填, 毫秒时间戳,成交结束时间 - 成交启始时间 <= 24h
|
||||
'page_number' => $page, // 页码, 必填
|
||||
'page_size' => 1, // 数量, 必填, 1~100
|
||||
'page_size' => 100, // 数量, 必填, 1~100
|
||||
// 非必填
|
||||
// 'activity_no' => $activityNo, // 团号
|
||||
'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)
|
||||
{
|
||||
$updateOrders = [];
|
||||
foreach ($orders as $order) {
|
||||
unset($order['custom_item_list'], $order['logistics_list'], $order['gift_order_list']);
|
||||
$orderRecord = BusinessOrder::updateOrCreate(
|
||||
['shop_id' => $shopId, 'order_sn' => $order['order_sn']],
|
||||
$order
|
||||
);
|
||||
$updateOrders[] = $order['order_sn'];
|
||||
foreach ($order['sub_order_list'] as $item) {
|
||||
BusinessOrderItem::updateOrCreate(
|
||||
['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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services\Business\MiaoXuan;
|
||||
|
||||
use App\Listeners\UpdateBusinessGoodsStock;
|
||||
use App\Models\BusinessOrder;
|
||||
|
||||
class Order
|
||||
@ -9,11 +10,13 @@ class Order
|
||||
// 下载订单事件之后去统计 然后更新库存
|
||||
public static function saveOrders(array $orders, $shopId)
|
||||
{
|
||||
$updateOrders = [];
|
||||
foreach ($orders as $order) {
|
||||
$orderRecord = BusinessOrder::updateOrCreate(
|
||||
['shop_id' => $shopId, 'order_sn' => $order['order_sn']],
|
||||
$order
|
||||
);
|
||||
$updateOrders[] = $order['order_sn'];
|
||||
foreach ($order['sub_order_list'] as $item) {
|
||||
$orderRecord = BusinessOrder::updateOrCreate(
|
||||
['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
4
composer.lock
generated
@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "5a01163bdf570af5226c567ad02f45d7",
|
||||
"content-hash": "037b06c1b26399725a1d9c0687402942",
|
||||
"packages": [
|
||||
{
|
||||
"name": "aliyuncs/oss-sdk-php",
|
||||
@ -9110,7 +9110,7 @@
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": "^7.2.5|^8.0",
|
||||
"ext-json": "^1.7"
|
||||
"ext-json": "*"
|
||||
},
|
||||
"platform-dev": [],
|
||||
"plugin-api-version": "2.3.0"
|
||||
|
||||
@ -20,8 +20,9 @@ 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->timestamps();
|
||||
// 索引
|
||||
$table->unique(['sku_id', 'day']);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
36
database/migrations/2022_08_10_150657_create_jobs_table.php
Normal file
36
database/migrations/2022_08_10_150657_create_jobs_table.php
Normal 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');
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user