Files
car_info/app/controller/VehicleController.php
T
2025-04-30 11:30:08 +08:00

62 lines
2.0 KiB
PHP

<?php
namespace app\controller;
use app\model\Vehicle;
use plugin\admin\app\model\Location;
use util\Util;
use support\Request;
class VehicleController
{
protected $status = ['未知','待发车','在途(发往fbo港口)','抵达发货港口','在途(发往目的地仓库)','抵达仓库','售出'];
/**
* Mask VIN number by showing only first 2 and last 6 characters
*/
private function maskVin($vin)
{
if (strlen($vin) <= 8) {
return $vin;
}
return substr($vin, 0, 2) . str_repeat('*', strlen($vin) - 8) . substr($vin, -6);
}
/**
* Get all vehicles with their locations
*/
public function index(Request $request)
{
$vehicles = Vehicle::with('location')->with('finalLocation')->get();
$vehicles->each(function ($vehicle) {
if (isset($vehicle->vin)) {
$vehicle->vin = $this->maskVin($vehicle->vin);
}
$vehicle->status = $this->status[$vehicle->status]??$this->status[0];
// $location = Location::find($vehicle->locations_id);
// $vehicle->location_name = $location ? $location->name : '';
// $final_location = Location::find($vehicle->final_locations_id);
// $vehicle->final_location_name = $final_location ? $final_location->name : '';
$fbo_location = Location::find($vehicle->fbo_locations_id);
$vehicle->fbo_destination = $fbo_location ? $fbo_location->name : '';
});
$result = ['list' => $vehicles,'count'=>count($vehicles)];
return Util::success($result);
}
/**
* Get a specific vehicle with its location
*/
public function show(Request $request, $id)
{
$vehicle = Vehicle::with('location')->find($id);
if (!$vehicle) {
return Util::fail($vehicle,'Vehicle not found');
}
if (isset($vehicle->vin)) {
$vehicle->vin = $this->maskVin($vehicle->vin);
}
return Util::success($vehicle);
}
}