for mission and webservic harim
This commit is contained in:
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\V3\Dashboard\Mission\Report;
|
||||||
|
|
||||||
|
use App\Exports\V3\Mission\Report\Tradod\CountryReport;
|
||||||
|
use App\Exports\V3\Mission\Report\Tradod\ProvinceReport;
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Traits\ApiResponse;
|
||||||
|
use App\Models\City;
|
||||||
|
use App\Models\Province;
|
||||||
|
use App\Services\Cartables\Mission\Report\ReportTradodService;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Maatwebsite\Excel\Facades\Excel;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Exception;
|
||||||
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||||
|
|
||||||
|
class MachineMissionController extends Controller
|
||||||
|
{
|
||||||
|
use ApiResponse;
|
||||||
|
|
||||||
|
public function Activity(Request $request, ReportTradodService $reportMissionService): JsonResponse
|
||||||
|
{
|
||||||
|
return $this->successResponse([
|
||||||
|
'activities' => $reportMissionService->countryActivity($request),
|
||||||
|
'provinces' => Province::all(['id', 'name_fa']),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function countryExcelActivity(Request $request, ReportTradodService $reportMissionService): BinaryFileResponse
|
||||||
|
{
|
||||||
|
$data = $reportMissionService->countryActivity($request);
|
||||||
|
$name = 'گزارش کل تردد '.verta()->now()->format('Y-m-d H-i').'.xlsx';
|
||||||
|
|
||||||
|
return Excel::download(new CountryReport($data), $name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,10 +2,14 @@
|
|||||||
|
|
||||||
namespace App\Listeners\V3\Dashboard\Harim;
|
namespace App\Listeners\V3\Dashboard\Harim;
|
||||||
|
|
||||||
|
use App\Enums\HarimStates;
|
||||||
use App\Events\V3\Dashboard\Harim\RejectRequestEvent;
|
use App\Events\V3\Dashboard\Harim\RejectRequestEvent;
|
||||||
use App\Services\PanjarehVahed\SendAccessRoadStatusService;
|
use App\Services\PanjarehVahed\SendAccessRoadStatusService;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
|
||||||
class RejectRequestListener implements ShouldQueue
|
class RejectRequestListener implements ShouldQueue
|
||||||
{
|
{
|
||||||
@@ -15,20 +19,28 @@ class RejectRequestListener implements ShouldQueue
|
|||||||
* @var string|null
|
* @var string|null
|
||||||
*/
|
*/
|
||||||
public ?string $queue = 'harim';
|
public ?string $queue = 'harim';
|
||||||
|
public int $tries = 2;
|
||||||
|
public function backoff(): array
|
||||||
|
{
|
||||||
|
return [10, 30];
|
||||||
|
}
|
||||||
|
|
||||||
public function __construct(){}
|
public function __construct(
|
||||||
|
|
||||||
|
private SendAccessRoadStatusService $sendAccessRoadStatusService
|
||||||
|
){}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function handle(RejectRequestEvent $event, SendAccessRoadStatusService $sendAccessRoadStatusService): void
|
public function handle(RejectRequestEvent $event): void
|
||||||
{
|
{
|
||||||
|
|
||||||
$arrayData = json_decode($event->harim->data, true);
|
$arrayData = json_decode($event->harim->data, true);
|
||||||
|
|
||||||
$sendAccessRoadStatusService->makeInputParameters([
|
$this->sendAccessRoadStatusService->makeInputParameters([
|
||||||
'data' => [
|
'data' => [
|
||||||
'actionId' => $arrayData['actionId'],
|
'actionId' => 470,
|
||||||
'ruleId' => $arrayData['ruleId'],
|
'ruleId' => $arrayData['ruleId'],
|
||||||
'activityId' => $arrayData['activityId'],
|
'activityId' => $arrayData['activityId'],
|
||||||
'requestId' => $arrayData['requestId'],
|
'requestId' => $arrayData['requestId'],
|
||||||
@@ -41,6 +53,119 @@ class RejectRequestListener implements ShouldQueue
|
|||||||
'token' => $arrayData['token'],
|
'token' => $arrayData['token'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$sendAccessRoadStatusService->run();
|
$this->sendAccessRoadStatusService->run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function failed(RejectRequestEvent $event, Throwable $e): void
|
||||||
|
{
|
||||||
|
$harim = $event->harim->fresh();
|
||||||
|
|
||||||
|
DB::transaction(function () use ($harim, $e) {
|
||||||
|
if ((int)$harim->state_id === HarimStates::EMKAN_PAZIR_NEMEBASHAD->value) {
|
||||||
|
$harim->update([
|
||||||
|
'state_id' => HarimStates::BARESI_TAVASOT_MODIR_KOL->value, // همون 4
|
||||||
|
'state_name' => HarimStates::BARESI_TAVASOT_MODIR_KOL->label(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
//namespace App\Listeners\V3\Dashboard\Harim;
|
||||||
|
//
|
||||||
|
//use App\Enums\HarimStates;
|
||||||
|
//use App\Events\V3\Dashboard\Harim\RejectRequestEvent;
|
||||||
|
//use App\Services\PanjarehVahed\SendAccessRoadStatusService;
|
||||||
|
//use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
//use Illuminate\Support\Facades\DB;
|
||||||
|
//use Illuminate\Support\Facades\Log;
|
||||||
|
//
|
||||||
|
//class RejectRequestListener implements ShouldQueue
|
||||||
|
//{
|
||||||
|
// public ?string $queue = 'harim';
|
||||||
|
//
|
||||||
|
// public function __construct(
|
||||||
|
// private readonly SendAccessRoadStatusService $sendAccessRoadStatusService
|
||||||
|
// )
|
||||||
|
// {
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public function handle(RejectRequestEvent $event): void
|
||||||
|
// {
|
||||||
|
// $harim = $event->harim;
|
||||||
|
//
|
||||||
|
// $raw = $harim->data;
|
||||||
|
//
|
||||||
|
// // ✅ اگر data خالی/Null یا غیر string بود => 500 نده، فقط revert و خارج شو
|
||||||
|
// if (!is_string($raw) || trim($raw) === '') {
|
||||||
|
// $this->revertHarim($harim->id, 'harim.data is empty or not string');
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// $arrayData = json_decode($raw, true);
|
||||||
|
//
|
||||||
|
// // ✅ اگر json خراب بود => 500 نده، فقط revert و خارج شو
|
||||||
|
// if (!is_array($arrayData) || json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
// $this->revertHarim($harim->id, 'Invalid harim.data JSON: ' . json_last_error_msg());
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// $payload = [
|
||||||
|
// 'data' => [
|
||||||
|
// 'actionId' => $arrayData['actionId'] ?? 470,
|
||||||
|
// 'ruleId' => $arrayData['ruleId'] ?? null,
|
||||||
|
// 'activityId' => $arrayData['activityId'] ?? null,
|
||||||
|
// 'requestId' => $arrayData['requestId'] ?? null,
|
||||||
|
// 'taskId' => $arrayData['taskId'] ?? null,
|
||||||
|
// 'code' => $harim->id,
|
||||||
|
// 'resultStatus' => 3,
|
||||||
|
// 'Description' => $harim->last_description,
|
||||||
|
// ],
|
||||||
|
// 'token' => $arrayData['token'] ?? null,
|
||||||
|
// ];
|
||||||
|
//
|
||||||
|
// // ✅ اگر فیلد حیاتی نبود => revert و خارج شو
|
||||||
|
// if (empty($payload['token']) || empty($payload['data']['requestId'])) {
|
||||||
|
// $this->revertHarim($harim->id, 'Missing token/requestId for external service');
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // ✅ اگر سرویس بیرونی fail شد => 500 نده، revert کن و خارج شو
|
||||||
|
// try {
|
||||||
|
// $this->sendAccessRoadStatusService->makeInputParameters($payload);
|
||||||
|
// $this->sendAccessRoadStatusService->run();
|
||||||
|
//
|
||||||
|
// Log::info('RejectRequest external call succeeded', ['harim_id' => $harim->id]);
|
||||||
|
//
|
||||||
|
// } catch (\Throwable $e) {
|
||||||
|
// $this->revertHarim($harim->id, 'External call failed: ' . $e->getMessage());
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private function revertHarim(int $harimId): void
|
||||||
|
// {
|
||||||
|
// DB::transaction(function () use ($harimId) {
|
||||||
|
// $harim = \App\Models\Harim::query()->lockForUpdate()->find($harimId);
|
||||||
|
//
|
||||||
|
// if (!$harim) {
|
||||||
|
// Log::error('Revert failed: harim not found', ['harim_id' => $harimId]);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if ((int)$harim->state_id === HarimStates::EMKAN_PAZIR_NEMEBASHAD->value) {
|
||||||
|
// $harim->update([
|
||||||
|
// 'state_id' => HarimStates::BARESI_TAVASOT_MODIR_KOL->value,
|
||||||
|
// 'state_name' => HarimStates::BARESI_TAVASOT_MODIR_KOL->label(),
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Log::warning('RejectRequest reverted harim state (sync mode)', [
|
||||||
|
// 'harim_id' => $harimId,
|
||||||
|
// ]);
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services\Cartables\Mission\Report;
|
||||||
|
|
||||||
|
use App\Facades\DataTable\DataTableFacade;
|
||||||
|
use App\Models\MissionViolation;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class MachinMissionService
|
||||||
|
{
|
||||||
|
public function Activity(Request $request): array
|
||||||
|
{
|
||||||
|
$provinceId = $request->province_id ?? auth()->user()->province_id;
|
||||||
|
$from = $request->from_date ?? today()->startOfDay();
|
||||||
|
$to = $request->to_date ? Carbon::parse($request->to_date)->endOfDay() : today()->endOfDay();
|
||||||
|
|
||||||
|
$sql = "
|
||||||
|
SELECT
|
||||||
|
cm.machine_code,
|
||||||
|
cm.car_name,
|
||||||
|
COALESCE(c.name_fa, '---') AS city_name,
|
||||||
|
COUNT(m.id) AS missions_count,
|
||||||
|
COALESCE(SUM(m.in_area_duration), 0) AS gps,
|
||||||
|
COALESCE(SUM(m.mission_duration), 0) AS durations
|
||||||
|
|
||||||
|
FROM cmms_machines cm
|
||||||
|
|
||||||
|
LEFT JOIN missions m
|
||||||
|
ON m.machine_code = cm.machine_code
|
||||||
|
AND m.created_at >= :from
|
||||||
|
AND m.created_at <= :to
|
||||||
|
|
||||||
|
LEFT JOIN cities c
|
||||||
|
ON c.id = cm.city_id
|
||||||
|
|
||||||
|
WHERE (:car_group IS NULL OR cm.car_group = :car_group)
|
||||||
|
AND (:province_id IS NULL OR cm.province_id = :province_id)
|
||||||
|
|
||||||
|
GROUP BY
|
||||||
|
cm.machine_code,
|
||||||
|
cm.car_name,
|
||||||
|
c.name_fa
|
||||||
|
|
||||||
|
ORDER BY
|
||||||
|
missions_count DESC,
|
||||||
|
durations DESC,
|
||||||
|
cm.machine_code;
|
||||||
|
|
||||||
|
";
|
||||||
|
|
||||||
|
return DB::select($sql, [
|
||||||
|
'province_id' => $provinceId,
|
||||||
|
'from' => $from,
|
||||||
|
'to' => $to,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ class SendAccessRoadStatusService
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->url = config('harim_web_services.Reject.url');
|
$this->url = config('harim_web_services.Reject.url');
|
||||||
$this->channelName = '';
|
$this->channelName = 'send_access_road';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class SendPaymentInfoService
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->url = config('harim_web_services.Reject.url');
|
$this->url = config('harim_web_services.Reject.url');
|
||||||
$this->channelName = '';
|
$this->channelName = 'send_payment_info';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -152,5 +152,15 @@ return [
|
|||||||
'path' => storage_path('logs/cmms/daily_moving_machine_service.log'),
|
'path' => storage_path('logs/cmms/daily_moving_machine_service.log'),
|
||||||
'level' => 'info',
|
'level' => 'info',
|
||||||
],
|
],
|
||||||
|
'send_access_road' => [
|
||||||
|
'driver' => 'single',
|
||||||
|
'path' => storage_path('logs/harim/send_access_road_service.log'),
|
||||||
|
'level' => 'info',
|
||||||
|
],
|
||||||
|
'send_payment_info' => [
|
||||||
|
'driver' => 'single',
|
||||||
|
'path' => storage_path('logs/harim/send_payment_info_service.log'),
|
||||||
|
'level' => 'info',
|
||||||
|
],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user