71 lines
2.0 KiB
PHP
71 lines
2.0 KiB
PHP
<?php
|
|||
|
|
namespace App\Jobs;
|
||
|
|
|
||
|
|
use App\Models\Customer;
|
||
|
|
use App\Models\RouteInfo;
|
||
|
|
use Carbon\Carbon;
|
||
|
|
use Illuminate\Bus\Queueable;
|
||
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||
|
|
use Illuminate\Queue\InteractsWithQueue;
|
||
|
|
use Illuminate\Queue\SerializesModels;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
|
||
|
|
class LogRouteCustomerJob implements ShouldQueue
|
||
|
|
{
|
||
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
|
|
||
|
|
protected $customer;
|
||
|
|
protected $route;
|
||
|
|
|
||
|
|
public function __construct(Customer $customer, string $route)
|
||
|
|
{
|
||
|
|
$this->customer = $customer;
|
||
|
|
$this->route = $route;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function handle()
|
||
|
|
{
|
||
|
|
$now = now();
|
||
|
|
|
||
|
|
$this->customer->last_using_api = $now;
|
||
|
|
$this->customer->save();
|
||
|
|
|
||
|
|
$parts = explode(' ', $this->route);
|
||
|
|
if (count($parts) < 2) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$method = trim($parts[0]);
|
||
|
|
$path = trim($parts[1]);
|
||
|
|
|
||
|
|
$routeInfo = RouteInfo::where('method', $method)
|
||
|
|
->where('route', $path)
|
||
|
|
->first();
|
||
|
|
|
||
|
|
$lastlog = DB::table('customer_route_logs')
|
||
|
|
->where('customer_id', $this->customer->id)
|
||
|
|
->where('route', $this->route)
|
||
|
|
->where('last_using_api', '<', $now)
|
||
|
|
->orderBy('last_using_api', 'desc')
|
||
|
|
->first();
|
||
|
|
|
||
|
|
if ($lastlog) {
|
||
|
|
$diff = Carbon::parse($lastlog->last_using_api)->diff($now);
|
||
|
|
$timeSpentFormatted = sprintf('%02d:%02d:%02d', $diff->h + ($diff->d * 24), $diff->i, $diff->s);
|
||
|
|
|
||
|
|
DB::table('customer_route_logs')
|
||
|
|
->where('id', $lastlog->id)
|
||
|
|
->update(['time_spent' => $timeSpentFormatted]);
|
||
|
|
}
|
||
|
|
|
||
|
|
DB::table('customer_route_logs')->insert([
|
||
|
|
'customer_id' => $this->customer->id,
|
||
|
|
'route' => $this->route,
|
||
|
|
'route_info_id' => $routeInfo ? $routeInfo->id : null,
|
||
|
|
'last_using_api' => $now,
|
||
|
|
'time_spent' => null,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|