create service and command for sending reports
This commit is contained in:
64
app/Services/Commands/SendPeopleMessageStatsService.php
Normal file
64
app/Services/Commands/SendPeopleMessageStatsService.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?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 SendPeopleMessageStatsService
|
||||
{
|
||||
private string $url;
|
||||
private string $channelName;
|
||||
private string $provinceId;
|
||||
private string $provinceName;
|
||||
public function __construct()
|
||||
{
|
||||
$this->url = config('ProvinceInfo.center_ip');
|
||||
$this->channelName = 'send_people_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(SendPeopleMessageStatsService::class, [$this->provinceName, array_sum($data), 'successful']);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
Log::channel($this->channelName)->error(SendPeopleMessageStatsService::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 action_id=1 THEN 1 END) AS action1,
|
||||
COUNT(CASE WHEN action_id=2 THEN 1 END) AS action2,
|
||||
COUNT(CASE WHEN action_id=3 THEN 1 END) AS action3,
|
||||
COUNT(CASE WHEN action_id=4 THEN 1 END) AS action4,
|
||||
COUNT(CASE WHEN action_id=5 THEN 1 END) AS action5,
|
||||
COUNT(CASE WHEN is_listen=1 THEN 1 END) AS listens,
|
||||
COUNT(CASE WHEN is_listen=0 THEN 1 END) AS not_listens,
|
||||
COUNT(*) AS total,
|
||||
AVG(review_duration) as review_average
|
||||
FROM system_messages GROUP BY date ORDER BY date DESC LIMIT 3";
|
||||
|
||||
return DB::select($query);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user