You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
218 lines
6.1 KiB
218 lines
6.1 KiB
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\AdminFloor;
|
|
use App\Models\ParkingLicensePlate;
|
|
use App\Models\ParkingSpace;
|
|
use App\Models\ParkingSpaceAttributes;
|
|
use App\Models\ParkingSpaceType;
|
|
use Exception;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ParkingSpaceService extends BaseService
|
|
{
|
|
|
|
/**
|
|
* @var string[]
|
|
*/
|
|
protected array $statusArr = ['vacant', 'occupy'];
|
|
protected array $operationType
|
|
= [
|
|
'',
|
|
'no_cars',
|
|
'yes_cars',
|
|
'number_update'
|
|
];
|
|
|
|
public function __construct(OperationLogService $logService)
|
|
{
|
|
parent::__construct($logService);
|
|
$this->logService->menuTitle = 'cat_list';
|
|
}
|
|
|
|
/**
|
|
* 返回翻译后的状态
|
|
* @param $status
|
|
* @return string
|
|
*/
|
|
public function getStatusStr($status): string
|
|
{
|
|
$value = $this->statusArr[$status] ?? '';
|
|
if ($value) {
|
|
return __('service.parking_space.' . $value);
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
public function getRecognition($recognition): string
|
|
{
|
|
if ($recognition) {
|
|
return __('service.parking_space.' . $recognition);
|
|
}
|
|
return '-';
|
|
}
|
|
|
|
public function getOperationType($operation_type): string
|
|
{
|
|
$value = $this->operationType[$operation_type] ?? '';
|
|
if ($value) {
|
|
return __('service.parking_space.' . $value);
|
|
}
|
|
return '-';
|
|
}
|
|
|
|
public function getStatusArr(): array
|
|
{
|
|
$arr = [];
|
|
foreach ($this->statusArr as $key => $value) {
|
|
$arr[$key] = __('service.parking_space.' . $value);
|
|
}
|
|
return $arr;
|
|
}
|
|
|
|
public function optionItems($item)
|
|
{
|
|
$item['floor'] = AdminFloor::getName($item['floor_id']);
|
|
$item['space_attr'] = ParkingSpaceAttributes::getAttr(
|
|
$item['space_attr_id']
|
|
);
|
|
$item['space_type'] = ParkingSpaceType::getName(
|
|
$item['space_type_id']
|
|
);
|
|
$license_plate = ParkingLicensePlate::getNumber(
|
|
$item['license_plate_id']
|
|
);
|
|
$item['license_plate'] = $license_plate ?: '-';
|
|
$item['berthing_time'] = is_null($item['berthing_time'])
|
|
? '-' : $item['berthing_time'];
|
|
$item['recognition'] = $this->getRecognition(
|
|
$item['recognition']
|
|
);
|
|
$item['status'] = $this->getStatusStr(
|
|
$item['status']
|
|
);
|
|
$item['operation_type'] = $this->getOperationType(
|
|
$item['operation_type']
|
|
);
|
|
unset(
|
|
$item['floor_id'], $item['space_attr_id'],
|
|
$item['space_type_id'], $item['license_plate_id']
|
|
);
|
|
return $item;
|
|
}
|
|
|
|
/**
|
|
* 更新车位数据
|
|
* @param array $data
|
|
* @param string $key
|
|
* @return Builder
|
|
* @throws Exception
|
|
*/
|
|
public function updateData(
|
|
array $data,
|
|
string $key = 'space_type_id'
|
|
): Builder {
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$ids = $data['space_ids'];
|
|
$data_id = $data[$key];
|
|
|
|
$model = ParkingSpace::query()->whereIn('id', $ids)->select();
|
|
$oldValues = $model->get()->toArray();
|
|
|
|
$updateData = [
|
|
$key => $data_id,
|
|
'updated_at' => get_datetime()
|
|
];
|
|
$model->update($updateData);
|
|
|
|
$str = $key == 'space_type_id' ? 'space_type.update' : 'space_attributes.update';
|
|
|
|
$newValue = $oldValues;
|
|
$newValue[$key] = $data_id;
|
|
$newValue['updated_at'] = $updateData['updated_at'];
|
|
|
|
$this->logService->logUpdatedData(
|
|
new ParkingSpace(),
|
|
$oldValues,
|
|
$str,
|
|
$newValue
|
|
);
|
|
DB::commit();
|
|
return $model;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $data
|
|
* @return Builder|Model
|
|
* @throws Exception
|
|
*/
|
|
public function createModel($data): Builder|Model
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
if (ParkingSpace::query()->where('number', $data['number'])
|
|
->exists()
|
|
) {
|
|
throw new Exception(__('service.parking_space.number_exists'));
|
|
}
|
|
$model = $this->create($data);
|
|
DB::commit();
|
|
return $model;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function create($data): Builder|Model
|
|
{
|
|
$model = ParkingSpace::query()->create([
|
|
'floor_id' => $data['floor_id'],
|
|
'number' => $data['number'],
|
|
'space_attr_id' => $data['space_attr_id'],
|
|
'license_plate_id' => $data['license_plate_id'] ?? 0,
|
|
'status' => $data['status'] ?? 0,
|
|
'space_type_id' => $data['space_type_id'] ?? 0,
|
|
'operation_type' => $data['operation_type'] ?? 0,
|
|
'created_at' => get_datetime()
|
|
]);
|
|
$this->logService->logCreated($model, 'parking_space.create');
|
|
return $model;
|
|
}
|
|
|
|
// 更新车位属性
|
|
public function updateAttr($id, $space_attr_id)
|
|
{
|
|
$model = ParkingSpace::query()->findOrFail($id);
|
|
$oldValues = $model->toArray();
|
|
if ($space_attr_id != $oldValues['space_attr_id']) {
|
|
$model->update([
|
|
'space_attr_id' => $space_attr_id,
|
|
'updated_at' => get_datetime()
|
|
]);
|
|
$this->logService->logUpdated(
|
|
$model,
|
|
$oldValues,
|
|
'space_attributes.update'
|
|
);
|
|
}
|
|
}
|
|
|
|
// 获取车位列表
|
|
public static function getSelectList($floor_id): array
|
|
{
|
|
$columns = ['id as parking_space_id', 'number as parking_space_number'];
|
|
return ParkingSpace::query()->where('floor_id', $floor_id)->select(
|
|
$columns
|
|
)->get()->toArray();
|
|
}
|
|
}
|
|
|