goodsnotices/app/Console/Commands/HandleNotice.php

102 lines
2.9 KiB
PHP
Raw Normal View History

2024-05-24 07:55:55 +00:00
<?php
namespace App\Console\Commands;
2024-05-25 14:06:58 +00:00
use App\helpers\apiRequest;
2024-05-24 11:04:29 +00:00
use App\Models\Notice;
2024-05-27 08:03:31 +00:00
use Exception;
2024-05-25 14:06:58 +00:00
use GuzzleHttp\Exception\GuzzleException;
2024-05-24 07:55:55 +00:00
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Isolatable;
2024-05-27 03:45:15 +00:00
use Illuminate\Support\Facades\Log;
2024-05-24 07:55:55 +00:00
class HandleNotice extends Command implements Isolatable
{
/**
* The name and signature of the console command.
*
* @var string
*/
2024-05-26 14:06:21 +00:00
protected $signature = 'handle:notice {chunk=1000 : 分片}';
2024-05-24 07:55:55 +00:00
/**
* The console command description.
*
* @var string
*/
protected $description = '处理消息';
/**
* Create a new command instance.
*
* @return void
*/
2024-05-25 14:06:58 +00:00
public function __construct()
2024-05-24 07:55:55 +00:00
{
2024-05-27 08:03:31 +00:00
parent::__construct();
2024-05-24 07:55:55 +00:00
}
/**
* Execute the console command.
*
* @return int
2024-05-25 14:06:58 +00:00
* @throws GuzzleException
2024-05-24 07:55:55 +00:00
*/
2024-05-25 14:06:58 +00:00
public function handle(): int
2024-05-24 07:55:55 +00:00
{
2024-05-26 14:06:21 +00:00
$chunk = $this->argument('chunk');
2024-05-27 07:15:51 +00:00
$this->info(date('Y-m-d H:i:s') . ' 开始消息队列');
2024-05-27 08:03:31 +00:00
$res = apiRequest::api('/Notic/getNotics');
2024-05-24 11:04:29 +00:00
if (!$res['result'] || $res['errCode'] != '0000') {
$this->error('request err: ' . $res['desc'] ?? '');
return 1;
}
2024-05-27 06:27:46 +00:00
$count = 0;
2024-05-24 11:04:29 +00:00
$arr = $res['data']['notice_list'] ?? [];
2024-05-26 14:06:21 +00:00
$typeIds = array_flip(range(1, 5));
while ($items = array_splice($arr, 0, $chunk)) {
2024-05-24 11:04:29 +00:00
$ids = array_column($items, 'notice_id');
if (!$ids) {
continue;
}
2024-05-25 14:06:58 +00:00
$data = array_column($items, null, 'notice_id');
$hadIds = Notice::query()->select('notice_id')->whereIn('notice_id', $ids)->get()->toArray();
2024-05-24 11:04:29 +00:00
2024-05-25 14:06:58 +00:00
$hadIds = array_column($hadIds, 'notice_id');
2024-05-24 11:04:29 +00:00
$newIds = array_diff($ids, $hadIds);
2024-05-25 14:06:58 +00:00
if (!$newIds) {
continue;
}
2024-05-24 11:04:29 +00:00
$notices = [];
$time = date('Y-m-d H:i:s');
2024-05-25 14:06:58 +00:00
$queue = [];
2024-05-24 11:04:29 +00:00
foreach ($newIds as $id) {
$notices[] = [
'notice_id' => $id,
2024-05-27 02:21:38 +00:00
'notice_type' => $data[$id]['type'],
2024-05-24 11:04:29 +00:00
'raw_content' => json_encode($data[$id]),
'created_at' => $time,
];
2024-05-26 14:06:21 +00:00
if (isset($typeIds[$data[$id]['type']])) {
$queue[$data[$id]['type']][] = $data[$id];
}
2024-05-24 11:04:29 +00:00
}
2024-05-25 14:06:58 +00:00
try {
2024-05-24 11:04:29 +00:00
Notice::insert($notices);
2024-05-27 08:03:31 +00:00
} catch (Exception $exception) {
2024-05-25 14:06:58 +00:00
$this->error('insert err:' . $exception->getMessage());
continue;
2024-05-24 11:04:29 +00:00
}
2024-05-25 14:06:58 +00:00
foreach ($queue as $type => $item) {
\App\Jobs\notice::dispatch($type, $item);
2024-05-27 03:45:15 +00:00
Log::info('添加 type' . $type . '的队列', $item);
2024-05-24 11:04:29 +00:00
}
2024-05-27 06:27:46 +00:00
$count += count($queue);
2024-05-24 11:04:29 +00:00
}
2024-05-27 07:15:51 +00:00
$this->info(date('Y-m-d H:i:s') . ' 添加了' . $count . '个任务');
2024-05-24 07:55:55 +00:00
return 0;
}
}