goodsnotices/app/Console/Commands/HandleNotice.php

100 lines
2.7 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-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;
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-24 11:04:29 +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-24 07:55:55 +00:00
$this->line('开始消息队列');
2024-05-24 11:04:29 +00:00
$url = env('NOTICE_URL');
2024-05-26 14:06:21 +00:00
$res = apiRequest::request($url, [
'token' => env('API_TOKEN'),
]);
2024-05-24 11:04:29 +00:00
if (!$res['result'] || $res['errCode'] != '0000') {
$this->error('request err: ' . $res['desc'] ?? '');
return 1;
}
$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]),
2024-05-27 02:21:38 +00:00
'err_message' => '',
2024-05-24 11:04:29 +00:00
'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-25 14:06:58 +00:00
} catch (\Exception $exception) {
$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-24 11:04:29 +00:00
}
}
2024-05-24 07:55:55 +00:00
return 0;
}
}