12 changed files with 488 additions and 2 deletions
@ -0,0 +1,230 @@ |
|||
<?php |
|||
|
|||
namespace App\Http\Controllers\Admin; |
|||
|
|||
use App\Exceptions\CustomException; |
|||
use App\Models\AdminFloorRegion; |
|||
use App\Models\ParkingInformation; |
|||
use App\Models\ParkingLicensePlate; |
|||
use App\Models\ParkingSpace; |
|||
use App\Models\ParkingSpaceType; |
|||
use App\Services\ApiResponseService; |
|||
use App\Services\ParkingInformationService; |
|||
use Exception; |
|||
use Illuminate\Http\JsonResponse; |
|||
use Illuminate\Http\Request; |
|||
use Illuminate\Support\Facades\Validator; |
|||
use Illuminate\Validation\ValidationException; |
|||
use Psr\SimpleCache\InvalidArgumentException; |
|||
|
|||
class ParkingInformationController extends BaseController |
|||
{ |
|||
/** |
|||
* @var ParkingInformationService |
|||
*/ |
|||
protected ParkingInformationService $service; |
|||
|
|||
/** |
|||
* 构造函数 |
|||
* @param ApiResponseService $responseService |
|||
* @param ParkingInformationService $service |
|||
*/ |
|||
public function __construct( |
|||
ApiResponseService $responseService, |
|||
ParkingInformationService $service |
|||
) { |
|||
parent::__construct($responseService); |
|||
$this->service = $service; |
|||
} |
|||
|
|||
/** |
|||
* Display a listing of the resource. |
|||
*/ |
|||
public function index(Request $request): JsonResponse |
|||
{ |
|||
try { |
|||
$query = ParkingInformation::query(); |
|||
|
|||
// 车位号码搜索 |
|||
if ($request->has('parking_space_number')) { |
|||
$parking_space_number = $request->input('parking_space_number'); |
|||
if ($parking_space_number) { |
|||
$space_ids = ParkingSpace::getId($parking_space_number); |
|||
if ($space_ids) { |
|||
$query->whereIn('space_id', $space_ids); |
|||
} else { |
|||
$query->where('id', 0); |
|||
} |
|||
} |
|||
} |
|||
|
|||
if ($request->has('license_plate')) { |
|||
$license_plate = $request->input('license_plate'); |
|||
if ($license_plate) { |
|||
$license_plate_ids = ParkingLicensePlate::getId( |
|||
$license_plate |
|||
); |
|||
if ($license_plate_ids) { |
|||
$query->whereIn('license_plate_id', $license_plate_ids); |
|||
} else { |
|||
$query->where('id', 0); |
|||
} |
|||
} |
|||
} |
|||
|
|||
if ($request->has('entry_start_date')) { |
|||
$entry_start_date = $request->input('entry_start_date'); |
|||
if ($entry_start_date && strtotime($entry_start_date)) { |
|||
$query->where('entry_time', '>=', $entry_start_date); |
|||
} |
|||
} |
|||
|
|||
if ($request->has('entry_end_date')) { |
|||
$entry_end_date = $request->input('entry_end_date'); |
|||
if ($entry_end_date && strtotime($entry_end_date)) { |
|||
$query->where('entry_time', '<=', $entry_end_date); |
|||
} |
|||
} |
|||
|
|||
if ($request->has('parking_space_type')) { |
|||
$type = $request->input('parking_space_type'); |
|||
if ($type) { |
|||
$query->where('space_type_id', '=', $type); |
|||
} |
|||
} |
|||
|
|||
// 分页 |
|||
$page = $request->input('page', 1); |
|||
$perPage = $request->input('per_page', 10); |
|||
|
|||
$total = $query->count(); |
|||
$items = $query->latest()->forPage($page, $perPage)->select() |
|||
->get()->each(function ($item) { |
|||
$item['floor_region_name'] = AdminFloorRegion::getName( |
|||
$item['floor_region_id'] |
|||
); |
|||
$item['parking_space'] = ParkingSpace::getNumber( |
|||
$item['space_id'] |
|||
); |
|||
$item['license_plate'] = ParkingLicensePlate::getNumber( |
|||
$item['license_plate_id'] |
|||
); |
|||
$item['parking_space_type'] = ParkingSpaceType::getName( |
|||
$item['space_type_id'] |
|||
); |
|||
unset($item['floor_region_id'], $item['space_id'], $item['license_plate_id'], $item['space_type_id']); |
|||
return $item; |
|||
}); |
|||
|
|||
return $this->responseService->success([ |
|||
'items' => $items, |
|||
'total' => $total, |
|||
'page' => $page, |
|||
'per_page' => $perPage, |
|||
'last_page' => ceil($total / $perPage), |
|||
]); |
|||
} catch (Exception $e) { |
|||
$m_prefix = __('exception.exception_handler.resource'); |
|||
return $this->responseService->systemError( |
|||
$m_prefix . ':' . $e->getMessage() |
|||
); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 列表搜索数据 |
|||
* @return JsonResponse |
|||
*/ |
|||
public function search(): JsonResponse |
|||
{ |
|||
try { |
|||
$data = [ |
|||
'parking_space_type_list' => ParkingSpaceType::getData() |
|||
]; |
|||
return $this->responseService->success($data); |
|||
} catch (Exception $e) { |
|||
$m_prefix = __('exception.exception_handler.resource'); |
|||
return $this->responseService->systemError( |
|||
$m_prefix . ':' . $e->getMessage() |
|||
); |
|||
} |
|||
} |
|||
|
|||
public function store(Request $request): JsonResponse |
|||
{ |
|||
try { |
|||
$data = $request->all(); |
|||
$rules = [ |
|||
'license_plate' => 'required', |
|||
]; |
|||
$messages = [ |
|||
'license_plate.required' => __( |
|||
'validation.license_plate.n_empty' |
|||
) |
|||
]; |
|||
$validator = Validator::make($data, $rules, $messages); |
|||
|
|||
if ($validator->fails()) { |
|||
throw new ValidationException($validator); |
|||
} |
|||
|
|||
$this->service->createModel($data); |
|||
|
|||
return $this->responseService->success( |
|||
null, |
|||
__('admin.save_succeeded') |
|||
); |
|||
} catch (ValidationException|CustomException $e) { |
|||
throw $e; |
|||
} catch (Exception $e) { |
|||
return $this->responseService->systemError( |
|||
__('exception.parking_information.create_failed') . ':' |
|||
. $e->getMessage() |
|||
); |
|||
} |
|||
|
|||
} |
|||
|
|||
/** |
|||
* @param string $id |
|||
* @return JsonResponse |
|||
* @throws CustomException |
|||
* @throws ValidationException |
|||
*/ |
|||
public function destroy(string $id): JsonResponse |
|||
{ |
|||
try { |
|||
$this->validateId($id, ParkingInformation::class); |
|||
$this->service->deleteModel($id); |
|||
return $this->responseService->success( |
|||
null, |
|||
__('admin.delete_succeeded') |
|||
); |
|||
} catch (ValidationException|CustomException $e) { |
|||
throw $e; |
|||
} catch (Exception $e) { |
|||
return $this->responseService->systemError( |
|||
__('exception.license_plate.destroy_failed') . ':' |
|||
. $e->getMessage() |
|||
); |
|||
} |
|||
} |
|||
|
|||
|
|||
/** |
|||
* @return JsonResponse |
|||
* @throws InvalidArgumentException |
|||
*/ |
|||
public function rule(): JsonResponse |
|||
{ |
|||
try { |
|||
return $this->responseService->success( |
|||
$this->methodShow('information') |
|||
); |
|||
} catch (Exception $e) { |
|||
return $this->responseService->systemError( |
|||
__('exception.get_data_failed') . ':' . $e->getMessage() |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
<?php |
|||
|
|||
namespace App\Models; |
|||
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|||
use Illuminate\Database\Eloquent\Model; |
|||
use Illuminate\Database\Eloquent\SoftDeletes; |
|||
|
|||
class ParkingInformation extends Model |
|||
{ |
|||
use HasFactory, SoftDeletes; |
|||
|
|||
protected $table = 'parking_information'; |
|||
|
|||
protected $fillable = [ |
|||
'floor_region_id', |
|||
'space_id', |
|||
'license_plate_id', |
|||
'entry_time', |
|||
'space_type_id' |
|||
]; |
|||
|
|||
protected $hidden = [ |
|||
'created_at', |
|||
'updated_at', |
|||
'deleted_at' |
|||
]; |
|||
|
|||
|
|||
public function getEntryTimeAttribute($value): string |
|||
{ |
|||
return get_datetime('datetime', strtotime($value)); |
|||
} |
|||
} |
|||
@ -0,0 +1,152 @@ |
|||
<?php |
|||
|
|||
namespace App\Services; |
|||
|
|||
use App\Models\AdminFloorRegion; |
|||
use App\Models\ParkingInformation; |
|||
use App\Models\ParkingLicensePlate; |
|||
use App\Models\ParkingSpace; |
|||
use App\Models\ParkingSpaceType; |
|||
use Exception; |
|||
use Illuminate\Database\Eloquent\Builder; |
|||
use Illuminate\Database\Eloquent\Model; |
|||
use Illuminate\Support\Facades\DB; |
|||
|
|||
class ParkingInformationService extends BaseService |
|||
{ |
|||
|
|||
public function __construct(OperationLogService $logService) |
|||
{ |
|||
parent::__construct($logService); |
|||
$logService->menuTitle = 'parking_information'; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* @param $data |
|||
* @return Builder|Model |
|||
* @throws Exception |
|||
*/ |
|||
public function createModel($data): Builder|Model |
|||
{ |
|||
try { |
|||
DB::beginTransaction(); |
|||
|
|||
$license_plate = $data['license_plate']; |
|||
$license_plate = ParkingLicensePlate::query()->where( |
|||
'number', |
|||
$license_plate |
|||
)->first(['id', 'space_type_id']); |
|||
$license_plate_id = 0; |
|||
if ($license_plate) { |
|||
$license_plate_id = $license_plate['id']; |
|||
$space_type_id = $license_plate['space_type_id']; |
|||
} else { |
|||
$space_type_data = ParkingSpaceType::getDefaultData(); |
|||
$space_type_id = $space_type_data['id'] ?? 0; |
|||
} |
|||
|
|||
$space_id = 0; |
|||
$floor_region_id = 0; |
|||
if ($space_type_id) { |
|||
$parkingSpaceWhere = [ |
|||
'space_type_id' => $space_type_id, |
|||
'status' => 0, |
|||
'license_plate_id' => 0 |
|||
]; |
|||
$res = ParkingSpace::query() |
|||
->where($parkingSpaceWhere) |
|||
->first(['id', 'floor_id']); |
|||
if ($res) { |
|||
$space_id = $res['id']; |
|||
if ($res['floor_id']) { |
|||
$floor_region_id = AdminFloorRegion::query()->where( |
|||
'floor_id', |
|||
$res['floor_id'] |
|||
)->value('id'); |
|||
} |
|||
} |
|||
} |
|||
|
|||
if ($space_id) { |
|||
$space = ParkingSpace::query()->where('status', 0) |
|||
->where('license_plate_id', 0)->first( |
|||
['id', 'floor_id', 'space_type_id'] |
|||
); |
|||
if ($space) { |
|||
$space_id = $space['id']; |
|||
if (!$space_type_id) { |
|||
$space_type_id = $space['space_type_id']; |
|||
} |
|||
if (!$floor_region_id && $space['floor_id']) { |
|||
$floor_region_id = AdminFloorRegion::query()->where( |
|||
'floor_id', |
|||
$space['floor_id'] |
|||
)->value('id'); |
|||
} |
|||
} |
|||
} |
|||
|
|||
if (!$space_type_id) { |
|||
$createData = [ |
|||
'number' => $license_plate, |
|||
'space_type_id' => $space_type_id |
|||
]; |
|||
(new ParkingLicensePlateService( |
|||
$this->logService |
|||
))->createModel($createData); |
|||
$license_plate_id = ParkingLicensePlate::query()->where( |
|||
$createData |
|||
)->value('id'); |
|||
} |
|||
if (!$license_plate_id) { |
|||
throw new Exception( |
|||
__('service.parking_information.not_default') |
|||
); |
|||
} |
|||
|
|||
if (ParkingInformation::query()->where('license_plate_id', $license_plate_id) |
|||
->exists() |
|||
) { |
|||
throw new Exception(__('service.license_plate.number_exists')); |
|||
} |
|||
|
|||
$model = ParkingInformation::query()->create([ |
|||
'floor_region_id' => $floor_region_id, |
|||
'space_id' => $space_id, |
|||
'license_plate_id' => $license_plate_id, |
|||
'entry_time' => get_datetime(), |
|||
'space_type_id' => $space_type_id, |
|||
'created_at' => get_datetime() |
|||
]); |
|||
|
|||
$this->logService->logCreated($model, 'parking_information.create'); |
|||
|
|||
DB::commit(); |
|||
return $model; |
|||
} catch (Exception $e) { |
|||
DB::rollBack(); |
|||
throw $e; |
|||
} |
|||
} |
|||
|
|||
public function deleteModel($id): bool |
|||
{ |
|||
try { |
|||
DB::beginTransaction(); |
|||
|
|||
$model = ParkingInformation::query()->findOrFail($id); |
|||
|
|||
$this->logService->logDeleted($model, 'parking_information.delete'); |
|||
|
|||
$model->delete(); |
|||
|
|||
DB::commit(); |
|||
return true; |
|||
} catch (Exception $e) { |
|||
DB::rollBack(); |
|||
throw $e; |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
<?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::create('parking_information', function (Blueprint $table) { |
|||
$table->id(); |
|||
$table->integer('floor_region_id')->comment('楼层区域id'); |
|||
$table->integer('space_id')->default('')->comment('车位id'); |
|||
$table->integer('license_plate_id')->default('')->comment('车牌id'); |
|||
$table->integer('space_type_id')->default('')->comment('车位类型id'); |
|||
$table->timestamp('entry_time')->comment('驶入时间'); |
|||
$table->timestamps(); |
|||
$table->softDeletes(); |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* Reverse the migrations. |
|||
*/ |
|||
public function down(): void |
|||
{ |
|||
Schema::dropIfExists('parking_electronic_map'); |
|||
} |
|||
}; |
|||
Loading…
Reference in new issue