logService->menuTitle = 'cat_list'; } public function getRecognition($recognition): string { if ($recognition) { $str = $recognition >= $this->recognition ? 'high' : 'land'; return __('service.parking_space.' . $str); } return '-'; } public function getOperationType($operation_type): string { $value = $this->operationType[$operation_type] ?? ''; if ($value) { return __('service.parking_space.' . $value); } return '-'; } // 获取翻译后的状态数据列表 public function getStatusArr($is_child = false): array { $arr = []; foreach ($this->statusArr as $key => $value) { $arr[$key] = __('service.parking_space.' . $value); if ($value == 'occupy' && $is_child) { $arr['children'] = [ $key => [ 3 => __('service.parking_space.yes_license'), 4 => __('service.parking_space.no_license') ] ]; } } return $arr; } // 获取状态显示值 public function getParkingSpaceStatus($status, $license_plate_id = '') { $statusArr = $this->getStatusArr(true); // 是否有车 $yesStr = ''; $noStr = ''; if ($license_plate_id) { $yesStr = $statusArr['children'][1][3]; } else { $noStr = $statusArr['children'][1][4]; } if ($status == '1') { // 占用 是否有车 $str = $yesStr ?: $noStr; return $statusArr[1] . '/' . $str; } elseif ($status == '2') { return $statusArr[$status]; } else { $status_str = $statusArr[$status] ?? ''; if ($status_str) { // 空闲 是否有车 if ($status < 1) { return $yesStr ? $status_str . '/' . $yesStr : $status_str; } } return ''; } } // 获取查询状态条件 public function getStatusWhere($status) { $where = [['status', '=', 1]]; if ($status == 4) { // 无车牌 $where[] = ['license_plate_id', '=', 0]; } elseif ($status == 3) { // 有车牌 $where[] = ['license_plate_id', '>=', 1]; } else { $where = [['status', '=', $status]]; } return $where; } // 获取状态的值 public function getStatusArrKey(): array { return [0,1,2,3,4]; } 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['recognition'] = $this->getRecognition( $item['recognition'] ); $item['status'] = $this->getParkingSpaceStatus( $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 * @throws CustomException */ 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(); if (!$oldValues) { throw new CustomException(__service('parking_space.space_not')); } $updateData = [ $key => $data_id, 'updated_at' => get_datetime() ]; $model->update($updateData); // 同步更新模式 类型、属性、附属类型 if ($key == 'space_type_id') { foreach ($ids as $space_id) { $tar_mode_id = EventCalendarService::getTargetModeId(); $this->syncUpdateSpaceData($space_id, $data_id, $tar_mode_id, false); } } $newValue = $oldValues; $newValue[$key] = $data_id; $newValue['updated_at'] = $updateData['updated_at']; $this->logService->logUpdatedData( new ParkingSpace(), $oldValues, $this->getLogDescription($key), $newValue ); DB::commit(); return $model; } catch (Exception $e) { DB::rollBack(); throw $e; } } private function getLogDescription($key): string { $str = ''; if ($key == 'space_type_id') { $str = 'space_type.update'; } elseif ($key == 'space_attr_id') { $str = 'space_attributes.update'; } elseif ($key == 'auxiliary_space_type') { $str = 'parking_space.auxiliary_space_type'; } return $str; } /** * @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')); } if (empty($data['space_type_id'])) { $res = ParkingSpaceType::getDefaultData(); if ($res) { $data['space_type_id'] = $res['id']; } } $model = $this->createData($data); DB::commit(); return $model; } catch (Exception $e) { DB::rollBack(); throw $e; } } public function createData($data): Builder|Model { $model = ParkingSpace::query()->create([ 'floor_id' => $data['floor_id'], 'region_id' => $data['region_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(); } /** * 更新状态和车牌 * @param $id // 车位id * @param $status // 车位状态 * @param $license_plate // 车牌号码 * @param $start_at // 维修开始时间 * @param $end_at // 维修结束时间 * @throws Exception */ public function updateStatus( $id, $status, $license_plate, string $start_at = '', string $end_at = '' ) { try { DB::beginTransaction(); $model = ParkingSpace::query()->findOrFail($id); $oldValues = $model->toArray(); $update = [ 'status' => $status, 'updated_at' => get_datetime() ]; if (!empty($license_plate) && $status == 3) { $licensePlateService = new ParkingLicensePlateService( $this->logService ); $number_id = $licensePlateService->saveNumber( $license_plate, $oldValues['space_type_id'] ); if ($number_id) { $update['license_plate_id'] = $number_id; $update['berthing_time'] = get_datetime(); $update['recognition'] = '90'; } $update['status'] = 1; } else if ($status == 4) { $update['license_plate_id'] = 0; $update['berthing_time'] = null; $update['recognition'] = ''; $update['status'] = 1; } else if (!$status) { $update['license_plate_id'] = 0; $update['berthing_time'] = null; $update['recognition'] = ''; } if ($status == 2) { // 维修 $now_times = time(); $start_times = strtotime($start_at);// 维修开始时间 $end_times = strtotime($end_at);// 维修结束时间 if (!$start_times) { throw new CustomException(__validation('parking_repair_list.s_empty')); } if (!$end_times) { throw new CustomException(__validation('parking_repair_list.e_empty')); } if ($start_times > $end_times) { throw new CustomException(__validation('parking_repair_list.date_error')); } $start_time = get_datetime('datetime', $start_times); $end_time = get_datetime('datetime', $end_times); $ParkingSpaceRepair = new ParkingSpaceRepairService( $this->logService ); $ParkingSpaceRepair->createData( $id, $this->adminUserId, $start_time, $end_time ); if ($start_times <= $now_times && $end_times >= $now_times) { $this->updateRepairStatus($id, 2); } } $model->update($update); $this->logService->logUpdated( $model, $oldValues, 'parking_space.update_status' ); DB::commit(); return $model; } catch (Exception $e) { DB::rollBack(); throw $e; } } /** * 根据不同模式 更新车位类型 * @param $id * @param $type_id * @param $pattern_id * @param bool $is_target_mode // 是否判断是不是当前模式 * @return void */ public function syncUpdateSpaceData( $id, $type_id, $pattern_id, bool $is_target_mode = true ) { // 跳过判断 if ($is_target_mode) { // 非当前模式不更新 $targetPatternId = EventCalendarService::getTargetModeId(); if ($pattern_id != $targetPatternId) { return; } } $model = ParkingSpace::query()->findOrFail($id); $oldValues = $model->toArray(); // 相同不更新 if ($oldValues['space_type_id'] == $type_id) { return; } $updateData = [ 'space_type_id' => $type_id, 'updated_at' => date("Y-m-d H:i:s", time()) ]; $model->update($updateData); // 记录日志 $this->logService->logUpdated( $model, $oldValues, $this->getLogDescription('space_type_id') ); // 切换车位类型,同步切换车位灯颜色 if ($oldValues['status'] != 2) { $this->changeSpaceCameraColor($oldValues['number'], $type_id, $oldValues['space_attr_id']); } } // 切换模式更改车位类型 后 更改车位灯颜色 public function changeSpaceCameraColor($number, $space_type_id, $space_attr, $is_repair = false) { if ($number == '1-1') { $SpaceType = ParkingSpaceType::query()->find($space_type_id); $SpaceTypeAttr = ParkingSpaceTypeAttr::getSpaceTypeAttrInfo($space_type_id, $space_attr); $color_occupy = $SpaceType['default_color_occupy']; $color_vacant = $SpaceType['default_color_vacant']; $is_flicker = $SpaceType['default_is_warning']; if ($SpaceTypeAttr) { $color_occupy = $SpaceTypeAttr['color_occupy']; $color_vacant = $SpaceTypeAttr['color_vacant']; $is_flicker = $SpaceTypeAttr['is_warning']; } // 维修灯 if ($is_repair) { $color_occupy = $SpaceType['repair_light']; $color_vacant = $SpaceType['repair_light']; $is_flicker = false; } $HikParkingCameraService = new HikParkingCameraService($this->logService); $body = $HikParkingCameraService->getBody($color_occupy, $color_vacant, $is_flicker); $HikParkingCameraService->updatelight($body); } } /** * @param array $data * @param int $id * @throws Exception */ public function updateModel(array $data, int $id) { try { DB::beginTransaction(); // 验证 $existsWhere = [ ['number', '=', $data['number']], ['id', '<>', $id] ]; if (ParkingSpace::query()->where($existsWhere)->exists()) { throw new Exception(__service('parking_space.number_exists')); } $space_type_id = 0; $res = ParkingSpaceType::getDefaultData(); if ($res) { $space_type_id = $res['id']; } // 更新 $model = ParkingSpace::query()->findOrFail($id); $oldValues = $model->toArray(); $model->update([ 'floor_id' => $data['floor_id'], 'region_id' => $data['region_id'], 'number' => $data['number'], 'space_attr_id' => $data['space_attr_id'], 'space_type_id' => $space_type_id, 'updated_at' => get_datetime() ]); $this->logService->logUpdated( $model, $oldValues, 'parking_space.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 = ParkingSpace::query()->findOrFail($id); $oldValue = $model->toArray(); $this->logService->logDeleted($model, 'parking_space.delete'); $model->delete(); // 同步删除已添加的绘制地图 ParkingElectronicMapService::syncDeleteMapSpace( $id, $oldValue['floor_id'] ); 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(); foreach ($ids as $id) { $model = ParkingSpace::query()->findOrFail($id); $oldValue = $model->toArray(); $this->logService->logDeleted( $model, 'parking_space.delete' ); $model->delete(); // 同步删除已添加的绘制地图 ParkingElectronicMapService::syncDeleteMapSpace( $id, $oldValue['floor_id'] ); } DB::commit(); return true; } catch (Exception $e) { DB::rollBack(); throw $e; } } //修改为维修车位 public function updateRepairStatus($id, $status) { $model = ParkingSpace::query()->findOrFail($id); $oldValues = $model->toArray(); $update = [ 'status' => $status, 'updated_at' => date("Y-m-d H:i:s", time()) ]; $model->update($update); $this->logService->logUpdated( $model, $oldValues, 'parking_space.update_status' ); $number = $oldValues['number']; $space_attr_id = $oldValues['space_attr_id']; $space_type_id = $oldValues['space_type_id']; // 修改为类型的维修指定灯光 $this->changeSpaceCameraColor( $number, $space_attr_id, $space_type_id, $status == 2 ); } /** * 删除维修记录,则修改为空闲 * @param $space_id // 车位id * @param $start_at // 维修开始时间 * @param $end_at // 维修结束时间 * @return void */ public function syncUpdateParkingSpaceStatus($space_id, $start_at, $end_at) { // 判断是否维修记录在当前时间 $start_at_times = strtotime($start_at); $end_at_times = strtotime($end_at); $now_times = time(); $is_now = false; if ($start_at_times <= $now_times && $end_at_times >= $now_times) { $is_now = true; } // 判断车位状态是否在维修 $item = ParkingSpace::query()->find($space_id); if ($item['status'] == 2 && $is_now) { // 删除维修数据这撤销 $this->updateRepairStatus($space_id, 0); } } }