6
0
Fork 0
jiqu-library-server/app/Console/Commands/PushMessageCommand.php

61 lines
1.5 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Message;
use App\Models\PushMessageTask;
use App\Services\Push\MallUnipushService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Throwable;
class PushMessageCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'message:push';
/**
* The console command description.
*
* @var string
*/
protected $description = '推送消息';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
PushMessageTask::with('message')->where('is_pushed', 0)->chunkById(100, function ($tasks) {
$mallPushService = new MallUnipushService();
foreach ($tasks as $task) {
try {
DB::beginTransaction();
$status = 2; //默认失败
//如果是系统消息
if ($task->message instanceof Message) {
$status = $mallPushService->push($task->sn, $task->message) ? 1 : 2;
}
$task->update([
'is_pushed'=> 1,
'status'=> $status,
]);
DB::commit();
} catch (Throwable $th) {
DB::rollBack();
report($th);
}
}
});
return Command::SUCCESS;
}
}