create service and command for sending reports

This commit is contained in:
2025-08-03 11:08:14 +03:30
parent 5085f22a1d
commit 23587ae958
9 changed files with 400 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
<?php
namespace App\Services\Commands;
use Exception;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class SendSystemMessageStatsService
{
private string $url;
private string $channelName;
private string $provinceId;
private string $provinceName;
public function __construct()
{
$this->url = config('ProvinceInfo.center_ip');
$this->channelName = 'send_system_message_stats_report';
$this->provinceId = config('ProvinceInfo.id');
$this->provinceName = config('ProvinceInfo.name');
}
/**
* @throws ConnectionException
* @throws RequestException
*/
public function execute(): void
{
try
{
$data = $this->prepare();
Http::post($this->url, $data)->throw();
Log::channel($this->channelName)->info(SendSystemMessageStatsService::class, [$this->provinceName, array_sum($data), 'successful']);
}
catch (Exception $e)
{
Log::channel($this->channelName)->error(SendSystemMessageStatsService::class, [$e->getMessage()]);
throw $e;
}
}
private function prepare(): array
{
$query = "SELECT
{$this->provinceId} AS province_id,
'{$this->provinceName}' AS province_name,
date,
COUNT(CASE WHEN message_quality=1 THEN 1 END) AS message_quality_ok,
COUNT(CASE WHEN message_quality=0 THEN 1 END) AS message_quality_nok,
COUNT(CASE WHEN format_quality=1 THEN 1 END) AS message_format_ok,
COUNT(CASE WHEN format_quality=0 THEN 1 END) AS message_format_nok,
FROM system_messages GROUP BY date ORDER BY date DESC LIMIT 5";
return DB::select($query);
}
}