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.
150 lines
3.9 KiB
150 lines
3.9 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\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);
|
|
}
|
|
|
|
/**
|
|
* 返回翻译后的状态
|
|
* @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' ? '类型' : '属性';
|
|
|
|
$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;
|
|
}
|
|
}
|
|
}
|
|
|