停车场管理系统
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.
 
 

287 lines
8.6 KiB

<?php
namespace App\Services;
use App\Exceptions\CustomException;
use App\Models\AdminFloor;
use App\Models\AdminUsers;
use App\Models\Parking;
use App\Models\ParkingSpace;
use App\Models\ParkingSpaceRepair;
use Exception;
use Illuminate\Support\Facades\DB;
class ParkingSpaceRepairService extends BaseService
{
public array $syncStatusArr
= [
'not_synced',
'synced'
];
protected string $menuTitle = 'parking_repair_list';
public function getItem($item)
{
$ParkingSpace = ParkingSpace::query()->find($item['space_id']);
$item['parking_space_number'] = '';
$item['floor'] = '';
$item['parking'] = '';
if ($ParkingSpace) {
$item['parking_space_number'] = $ParkingSpace['number'];
$Floor = AdminFloor::getFirst($ParkingSpace['floor_id']);
$item['floor'] = $Floor['name'];
$item['parking'] = Parking::getName($Floor['building_floor']);
}
$item['admin_user'] = AdminUsers::getUsername($item['admin_user_id']);
$syncStatusArr = $this->getSyncStatus();
$item['sync_status_str'] = $syncStatusArr[$item['sync_status']] ?? '';
unset($item['admin_user_id'], $item['space_id']);
return $item;
}
/**
* @return array|string[]
*/
public function getSyncStatus(): array
{
$syncStatusArr = $this->syncStatusArr;
foreach ($syncStatusArr as $key => $value) {
$syncStatusArr[$key] = __service($this->menuTitle . '.' . $value);
}
return $syncStatusArr;
}
/**
* @param array $data
* @param bool $is_import
* @return string
* @throws CustomException
*/
public function createModel(array $data, bool $is_import = false): string
{
try {
DB::beginTransaction();
$spaceNameArr = explode(',', $data['parking_space_name']);
$count = count($spaceNameArr);
foreach ($spaceNameArr as $parking_space_name) {
$space = ParkingSpace::query()->where('number', $parking_space_name)->first();
$str = $count > 1 ? ':' . $parking_space_name : '';
if (!$space) {
if ($is_import) {
DB::rollBack();
return __service($this->menuTitle . '.space_not_exists')
. $str;
}
throw new CustomException(
__service($this->menuTitle . '.space_not_exists')
. $str
);
}
// 维修状态不可添加
$space = $space->toArray();
$space_id = $space['id'];
if ($space['status'] == 2) {
if ($is_import) {
DB::rollBack();
return __service($this->menuTitle . '.space_exists')
. $str;
}
throw new CustomException(
__service($this->menuTitle . '.space_exists') . $str
);
}
$this->createData(
$space_id,
$data['admin_user_id'],
$data['start_at'],
$data['end_at']
);
}
DB::commit();
return '';
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
// 创建维修数据
public function createData($space_id,$admin_user_id, $start_at, $end_at)
{
$model = ParkingSpaceRepair::query()->create([
'space_id' => $space_id,
'admin_user_id' => $admin_user_id,
'start_at' => $start_at,
'end_at' => $end_at,
'created_at' => get_datetime()
]);
$this->logService->logCreated(
$model,
$this->menuTitle . '.create'
);
return $model;
}
/**
* @param array $data
* @param int $id
* @throws Exception
*/
public function updateModel(array $data, int $id)
{
try {
DB::beginTransaction();
$space_id = ParkingSpace::getValueId($data['parking_space_name']);
// 更新
$model = ParkingSpaceRepair::query()->findOrFail($id);
$oldValues = $model->toArray();
$model->update([
'space_id' => $space_id,
'admin_user_id' => $data['admin_user_id'],
'start_at' => $data['start_at'],
'end_at' => $data['end_at'],
'updated_at' => get_datetime()
]);
$this->logService->logUpdated(
$model,
$oldValues,
$this->menuTitle . '.update'
);
DB::commit();
return $model;
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
/**
* @param $id
* @return bool
* @throws Exception
*/
public function deleteModel($id): bool
{
try {
DB::beginTransaction();
$model = ParkingSpaceRepair::query()->findOrFail($id);
$item = $model->toArray();
$this->logService->logDeleted($model, $this->menuTitle . '.delete');
$model->delete();
// 判断 并修改车位状态为空闲
(new ParkingSpaceService(
$this->logService
))->syncUpdateParkingSpaceStatus(
$item['space_id'],
$item['start_at'],
$item['end_at']
);
DB::commit();
return true;
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
/**
* @param $ids
* @return bool
* @throws Exception
*/
public function batchDeleteModel($ids): bool
{
try {
DB::beginTransaction();
$ParkingSpaceService = new ParkingSpaceService($this->logService);
foreach ($ids as $id) {
$model = ParkingSpaceRepair::query()->findOrFail($id);
$item = $model->toArray();
$this->logService->logDeleted(
$model,
$this->menuTitle . '.delete'
);
$model->delete();
$ParkingSpaceService->syncUpdateParkingSpaceStatus(
$item['space_id'],
$item['start_at'],
$item['end_at']
);
}
DB::commit();
return true;
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
/**
* @param $data
* @return bool
* @throws CustomException
*/
public function synchronize($data): bool
{
try {
DB::beginTransaction();
$start_at_times = strtotime($data['start_at']);
$start_at = get_datetime('datetime', $start_at_times);
$end_at_times = strtotime($data['end_at']);
$end_at = get_datetime('datetime', $end_at_times);
$newValues = $oldValues = ParkingSpaceRepair::query()->where(
function ($query) use ($start_at, $end_at) {
$query->orWhereRaw("'{$start_at}' BETWEEN start_at AND end_at");
$query->orWhereRaw("'{$end_at}' BETWEEN start_at AND end_at");
}
)->where('sync_status', 0)->select()->get()->toArray();
if (!$oldValues) {
throw new CustomException(__service($this->menuTitle . '.not_send'));//目前暂无可发送的维修车位信息
}
$ids = array_column($oldValues, 'id');
ParkingSpaceRepair::query()->whereIn('id', $ids)->update([
'start_at' => $start_at,
'end_at' => $end_at,
'sync_status' => 1,
'sync_at' => $start_at,
'updated_at' => get_datetime()
]);
$newValues['start_at'] = $start_at;
$newValues['end_at'] = $end_at;
$newValues['sync_status'] = 1;
$newValues['sync_at'] = $start_at;
$this->logService->logUpdatedData(
new ParkingSpaceRepair(),
$oldValues,
$this->menuTitle . '.update',
$newValues
);
DB::commit();
return true;
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
}