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.
193 lines
5.0 KiB
193 lines
5.0 KiB
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\ParkingEquipment;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ParkingEquipmentService extends BaseService
|
|
{
|
|
|
|
public static array $typeArr
|
|
= [
|
|
1 => 'type1',
|
|
2 => 'type2',
|
|
3 => 'type3',
|
|
4 => 'type4',
|
|
5 => 'type5'
|
|
];
|
|
|
|
public static array $statusArr = ['offline', 'online'];
|
|
|
|
public static array $purposeArr = [1 => 'entrance', 2 => 'export'];
|
|
|
|
protected string $menuTitle = 'equipment_management';
|
|
|
|
/**
|
|
* @return array|string[]
|
|
*/
|
|
public static function getType(): array
|
|
{
|
|
$typeArr = self::$typeArr;
|
|
foreach ($typeArr as $key => $value) {
|
|
$typeArr[$key] = __('service.equipment_management.' . $value);
|
|
}
|
|
return $typeArr;
|
|
}
|
|
|
|
/**
|
|
* @return array|string[]
|
|
*/
|
|
public static function getStatus(): array
|
|
{
|
|
$statusArr = self::$statusArr;
|
|
foreach ($statusArr as $key => $value) {
|
|
$statusArr[$key] = __('admin.' . $value);
|
|
}
|
|
return $statusArr;
|
|
}
|
|
|
|
/**
|
|
* @return array|string[]
|
|
*/
|
|
public static function getPurpose(): array
|
|
{
|
|
$purposeArr = self::$purposeArr;
|
|
foreach ($purposeArr as $key => $value) {
|
|
$purposeArr[$key] = __('service.channel_management.' . $value);
|
|
}
|
|
return $purposeArr;
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @throws Exception
|
|
*/
|
|
public function createModel(array $data)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$existsWhere = [
|
|
['name', '=', $data['name']],
|
|
['type', '=', $data['type']]
|
|
];
|
|
if (ParkingEquipment::query()->where($existsWhere)->exists()) {
|
|
throw new Exception(
|
|
__service($this->menuTitle . '.name_exists')
|
|
);
|
|
}
|
|
|
|
$model = ParkingEquipment::query()->create([
|
|
'name' => $data['name'],
|
|
'type' => $data['type'],
|
|
'purpose' => $data['purpose'] ?? 0,
|
|
'channel_id' => $data['channel_id'],
|
|
'sn' => $data['sn'] ?? '',
|
|
'ip' => $data['ip'] ?? '',
|
|
'created_at' => get_datetime()
|
|
]);
|
|
|
|
$this->logService->logCreated($model, $this->menuTitle . '.create');
|
|
|
|
AdminTranslationService::saveTranslation(
|
|
$data['name'],
|
|
$data['en_name'] ?? '',
|
|
$data['tw_name'] ?? '',
|
|
$model->id,
|
|
9
|
|
);
|
|
|
|
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']],
|
|
['type', '=', $data['type']],
|
|
['id', '<>', $id]
|
|
];
|
|
if (ParkingEquipment::query()->where($existsWhere)->exists()) {
|
|
throw new Exception(
|
|
__service($this->menuTitle . '.name_exists')
|
|
);
|
|
}
|
|
|
|
// 更新
|
|
$model = ParkingEquipment::query()->findOrFail($id);
|
|
$oldValues = $model->toArray();
|
|
|
|
$model->update([
|
|
'name' => $data['name'],
|
|
'type' => $data['type'],
|
|
'purpose' => $data['purpose'] ?? 0,
|
|
'channel_id' => $data['channel_id'],
|
|
'sn' => $data['sn'] ?? '',
|
|
'ip' => $data['ip'] ?? '',
|
|
'updated_at' => get_datetime()
|
|
]);
|
|
|
|
$this->logService->logUpdated(
|
|
$model,
|
|
$oldValues,
|
|
$this->menuTitle . '.update'
|
|
);
|
|
|
|
AdminTranslationService::saveTranslation(
|
|
$data['name'],
|
|
$data['en_name'] ?? '',
|
|
$data['tw_name'] ?? '',
|
|
$id,
|
|
9
|
|
);
|
|
|
|
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 = ParkingEquipment::query()->findOrFail($id);
|
|
|
|
$this->logService->logDeleted($model, $this->menuTitle . '.delete');
|
|
|
|
$model->delete();
|
|
|
|
AdminTranslationService::syncDelete($id, 9);
|
|
|
|
DB::commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|
|
|