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

<?php
namespace App\Services;
use App\Models\AdminFloorRegion;
use Exception;
use Illuminate\Support\Facades\DB;
class AdminFloorRegionService extends BaseService
{
private static array $statusArr = ['disabled', 'enable'];
protected string $menuTitle = 'regional_management';
/**
* @return array|string[]
*/
public static function getStatus(): array
{
$statusArr = self::$statusArr;
foreach ($statusArr as $key => $value) {
$statusArr[$key] = __('admin.' . $value);
}
return $statusArr;
}
/**
* 创建角色
* @param array $data
* @throws Exception
*/
public function createModel(array $data)
{
try {
DB::beginTransaction();
$existsWhere = [
['name', '=', $data['name']],
['floor_id', '=', $data['floor_id']]
];
if (AdminFloorRegion::query()->where($existsWhere)->exists()) {
throw new Exception(__('service.admin_floor_region.name_exists'));
}
$model = AdminFloorRegion::query()->create([
'name' => $data['name'],
'floor_id' => $data['floor_id'],
'open_time' => $data['open_time'],
'close_time' => $data['close_time'],
'created_at' => get_datetime()
]);
$this->logService->logCreated($model, 'admin_floor.region_create');
AdminTranslationService::saveTranslation(
$data['name'],
$data['en_name'] ?? '',
$data['tw_name'] ?? '',
$model->id,
5
);
DB::commit();
return $model;
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
/**
* @param array $data
* @param int $id
* @throws Exception
*/
public function updateModel(array $data, int $id)
{
try {
DB::beginTransaction();
// 验证
$existsWhere = [
['name', '=', $data['name']],
['floor_id', '=', $data['floor_id']],
['id', '<>', $id]
];
if (AdminFloorRegion::query()->where($existsWhere)->exists()) {
throw new Exception(__('service.admin_floor_region.name_exists'));
}
// 更新
$model = AdminFloorRegion::query()->findOrFail($id);
$oldValues = $model->toArray();
$model->update([
'name' => $data['name'],
'floor_id' => $data['floor_id'],
'open_time' => $data['open_time'],
'close_time' => $data['close_time'],
'updated_at' => get_datetime()
]);
$this->logService->logUpdated(
$model,
$oldValues,
'admin_floor.region_update'
);
AdminTranslationService::saveTranslation(
$data['name'],
$data['en_name'] ?? '',
$data['tw_name'] ?? '',
$id,
5
);
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 = AdminFloorRegion::query()->findOrFail($id);
$this->logService->logDeleted($model, 'admin_floor.region_delete');
$model->delete();
AdminTranslationService::syncDelete($id, 5);
DB::commit();
return true;
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
}