ADD: Agrega modelo y migración para 'folio_tag' y 'tag_number' en 'vehicle_fakes', y actualiza el controlador para registrar vehículos robados
This commit is contained in:
parent
6c31941803
commit
b27f360109
@ -3,9 +3,8 @@
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Vehicle;
|
||||
use App\Models\Detection;
|
||||
use App\Models\VehicleFake;
|
||||
use App\Services\VehicleService;
|
||||
use App\Services\ConsultaEstatalService;
|
||||
use App\Services\ReporteRoboService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Notsoweb\ApiResponse\Enums\ApiResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@ -16,13 +15,11 @@
|
||||
class VehicleController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private VehicleService $vehicleService,
|
||||
private ConsultaEstatalService $consultaEstatal,
|
||||
private ReporteRoboService $reporteRobo
|
||||
private VehicleService $vehicleService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Consultar y registrar vehículo por PLACA o VIN
|
||||
* Consultar y registrar vehículo por PLACA o VIN (Alta de vehículo robado)
|
||||
*/
|
||||
public function consultarVehiculo(Request $request): JsonResponse
|
||||
{
|
||||
@ -42,91 +39,77 @@ public function consultarVehiculo(Request $request): JsonResponse
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$placa = $request->placa;
|
||||
$vin = $request->vin;
|
||||
$placa = $request->placa;
|
||||
$vin = $request->vin;
|
||||
|
||||
// Consultar padrón estatal (obtener datos completos del vehículo)
|
||||
Log::info('Consultando padrón estatal', ['placa' => $placa, 'vin' => $vin]);
|
||||
// Buscar vehículo en vehicle_fakes
|
||||
$vehiculo = VehicleFake::when($vin, function($query) use ($vin) {
|
||||
return $query->where('vin', $vin);
|
||||
})
|
||||
->when($placa, function($query) use ($placa) {
|
||||
return $query->orWhere('placa', $placa);
|
||||
})
|
||||
->first();
|
||||
|
||||
// Si viene VIN, usar VIN, si no usar placa
|
||||
$criterio = $vin ?: $placa;
|
||||
$datosEstatal = $this->consultaEstatal->consultarPorEpc($criterio);
|
||||
|
||||
if (!$datosEstatal || (!$datosEstatal['vin'] && !$datosEstatal['placa'])) {
|
||||
return ApiResponse::BAD_REQUEST->response([
|
||||
'success' => false,
|
||||
'message' => 'No se encontró información del vehículo en el padrón estatal'
|
||||
]);
|
||||
}
|
||||
|
||||
// Obtener VIN y Placa completos
|
||||
$vinCompleto = $datosEstatal['vin'] ?: $vin;
|
||||
$placaCompleta = $datosEstatal['placa'] ?: $placa;
|
||||
|
||||
Log::info('Datos del padrón estatal obtenidos', [
|
||||
'vin' => $vinCompleto,
|
||||
'placa' => $placaCompleta
|
||||
]);
|
||||
|
||||
// Consultar REPUVE
|
||||
$reporteRobo = $this->reporteRobo->consultarPorVin($vinCompleto, $placaCompleta);
|
||||
|
||||
$tieneReporte = $reporteRobo['tiene_reporte'] ?? false;
|
||||
|
||||
// Si tiene reporte de robo → Guardar en Redis
|
||||
if ($tieneReporte) {
|
||||
$epc = 'MANUAL_' . ($vinCompleto ?: $placaCompleta);
|
||||
|
||||
$this->guardarEnRedis($epc, $datosEstatal, $reporteRobo['datos']);
|
||||
|
||||
Log::warning('¡VEHÍCULO CON REPORTE DE ROBO REGISTRADO!', [
|
||||
'vin' => $vinCompleto,
|
||||
'placa' => $placaCompleta,
|
||||
'autoridad' => $reporteRobo['datos']['autoridad'] ?? null
|
||||
]);
|
||||
|
||||
return ApiResponse::OK->response([
|
||||
'success' => true,
|
||||
'tiene_reporte_robo' => true,
|
||||
'message' => '¡ALERTA! El vehículo tiene reporte de robo',
|
||||
'vehiculo' => [
|
||||
'vin' => $vinCompleto,
|
||||
'placa' => $placaCompleta,
|
||||
],
|
||||
'reporte_robo' => $reporteRobo['datos']
|
||||
]);
|
||||
}
|
||||
|
||||
// No tiene reporte de robo
|
||||
Log::info('Vehículo consultado sin reporte de robo', [
|
||||
'vin' => $vinCompleto,
|
||||
'placa' => $placaCompleta
|
||||
]);
|
||||
|
||||
return ApiResponse::OK->response([
|
||||
'success' => true,
|
||||
'tiene_reporte_robo' => false,
|
||||
'message' => 'Vehículo sin reporte de robo',
|
||||
'vehiculo' => [
|
||||
'vin' => $vinCompleto,
|
||||
'placa' => $placaCompleta,
|
||||
]
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Error al consultar vehículo', [
|
||||
'placa' => $request->placa,
|
||||
'vin' => $request->vin,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
|
||||
return ApiResponse::INTERNAL_ERROR->response([
|
||||
if (!$vehiculo) {
|
||||
return ApiResponse::NOT_FOUND->response([
|
||||
'success' => false,
|
||||
'message' => 'Error al consultar vehículo',
|
||||
'error' => $e->getMessage()
|
||||
'message' => 'No se encontró el vehículo en la base de datos'
|
||||
]);
|
||||
}
|
||||
|
||||
// Generar EPC
|
||||
$epc = 'E280117000000' . $vehiculo->vin;
|
||||
|
||||
// Verificar si ya está en Redis
|
||||
$key = "vehiculo:robado:{$placa}-{$vin}";
|
||||
if (Redis::exists($key)) {
|
||||
return ApiResponse::BAD_REQUEST->response([
|
||||
'success' => false,
|
||||
'message' => 'El vehículo ya está registrado como robado'
|
||||
]);
|
||||
}
|
||||
|
||||
// Guardar en Redis como robado
|
||||
$datos = [
|
||||
'epc' => $epc,
|
||||
'folio_tag' => $vehiculo->folio_tag,
|
||||
'tag_number' => $vehiculo->tag_number,
|
||||
'vin' => $vehiculo->vin,
|
||||
'placa' => $vehiculo->placa,
|
||||
'marca' => $vehiculo->marca,
|
||||
'modelo' => $vehiculo->modelo,
|
||||
'color' => $vehiculo->color,
|
||||
'fecha_robo' => now()->format('Y-m-d'),
|
||||
'acta' => 'MANUAL-' . now()->format('Ymd-His'),
|
||||
'denunciante' => 'SISTEMA',
|
||||
'fecha_acta' => now()->format('Y-m-d'),
|
||||
'primera_deteccion' => now()->toIso8601String(),
|
||||
'ultima_deteccion' => now()->toIso8601String(),
|
||||
];
|
||||
|
||||
Redis::set($key, json_encode($datos));
|
||||
|
||||
Log::warning('Vehículo dado de alta como robado (MANUAL)', [
|
||||
'epc' => $epc,
|
||||
'vin' => $vehiculo->vin,
|
||||
'placa' => $vehiculo->placa
|
||||
]);
|
||||
|
||||
return ApiResponse::OK->response([
|
||||
'success' => true,
|
||||
'message' => 'Vehículo registrado como robado exitosamente',
|
||||
'vehiculo' => [
|
||||
'epc' => $epc,
|
||||
'folio_tag' => $vehiculo->folio_tag,
|
||||
'tag_number' => $vehiculo->tag_number,
|
||||
'vin' => $vehiculo->vin,
|
||||
'placa' => $vehiculo->placa,
|
||||
'marca' => $vehiculo->marca,
|
||||
'modelo' => $vehiculo->modelo,
|
||||
'color' => $vehiculo->color
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -203,12 +186,11 @@ public function recuperarVehiculo(Request $request): JsonResponse
|
||||
// 2. Guardar en MySQL como recuperado
|
||||
$vehiculoRecuperado = Vehicle::create([
|
||||
'epc' => $epc,
|
||||
'vin' => $datosRedis['vin'],
|
||||
'placa' => $datosRedis['placa'],
|
||||
'fecha_robo' => $datosRedis['fecha_robo'],
|
||||
'autoridad_robo' => $datosRedis['autoridad'],
|
||||
'acta_robo' => $datosRedis['acta'],
|
||||
'denunciante' => $datosRedis['denunciante'],
|
||||
'vin' => $datosRedis['vin'] ?? null,
|
||||
'placa' => $datosRedis['placa'] ?? null,
|
||||
'fecha_robo' => $datosRedis['fecha_robo'] ?? null,
|
||||
'acta_robo' => $datosRedis['acta'] ?? null,
|
||||
'denunciante' => $datosRedis['denunciante'] ?? null,
|
||||
'fecha_acta' => $datosRedis['fecha_acta'] ?? null,
|
||||
'fecha_recuperacion' => now(),
|
||||
'datos_robo_original' => $datosRedis
|
||||
@ -233,7 +215,6 @@ public function recuperarVehiculo(Request $request): JsonResponse
|
||||
'placa' => $vehiculoRecuperado->placa,
|
||||
'fecha_robo' => $vehiculoRecuperado->fecha_robo,
|
||||
'fecha_recuperacion' => $vehiculoRecuperado->fecha_recuperacion,
|
||||
'autoridad_robo' => $vehiculoRecuperado->autoridad_robo,
|
||||
'acta_robo' => $vehiculoRecuperado->acta_robo,
|
||||
]
|
||||
]);
|
||||
|
||||
@ -16,6 +16,8 @@
|
||||
class VehicleFake extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'folio_tag',
|
||||
'tag_number',
|
||||
'placa',
|
||||
'vin',
|
||||
'marca',
|
||||
|
||||
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('vehicle_fakes', function (Blueprint $table) {
|
||||
$table->string('folio_tag')->unique()->after('id');
|
||||
$table->string('tag_number')->unique()->after('folio_tag');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('vehicle_fakes', function (Blueprint $table) {
|
||||
$table->dropColumn('folio_tag');
|
||||
$table->dropColumn('tag_number');
|
||||
});
|
||||
}
|
||||
};
|
||||
@ -22,6 +22,5 @@ public function run(): void
|
||||
$this->call(RoleSeeder::class);
|
||||
$this->call(UserSeeder::class);
|
||||
$this->call(SettingSeeder::class);
|
||||
$this->call(VehicleTestSeeder::class);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user