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.
233 lines
6.3 KiB
233 lines
6.3 KiB
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\AdminTranslation;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class AdminTranslationService
|
|
{
|
|
/**
|
|
* @var OperationLogService
|
|
*/
|
|
private OperationLogService $logService;
|
|
|
|
/**
|
|
* @param OperationLogService $logService
|
|
*/
|
|
public function __construct(OperationLogService $logService)
|
|
{
|
|
$this->logService = $logService;
|
|
$this->logService->menuTitle = 'translation';
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @throws Exception
|
|
*/
|
|
public function createModel(array $data)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
DB::commit();
|
|
|
|
$save_data = [
|
|
'en' => $data['en'],
|
|
'zh_cn' => $data['zh_cn'],
|
|
'zh_tw' => $data['zh_tw']
|
|
];
|
|
if (AdminTranslation::query()->where($save_data)->exists()) {
|
|
throw new Exception(
|
|
__('service.admin_translation.data_exists')
|
|
);
|
|
}
|
|
|
|
$save_data['created_at'] = get_datetime();
|
|
$model = AdminTranslation::query()->create($save_data);
|
|
|
|
$this->logService->logCreated($model, 'translation.create');
|
|
|
|
return $model;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
// 添加,编辑翻译
|
|
public function saveData($cn, $en, $tw, $type_id, $type)
|
|
{
|
|
$save_data = [
|
|
'en' => $en,
|
|
'zh_cn' => $cn,
|
|
'zh_tw' => $tw,
|
|
'type_id' => $type_id,
|
|
'type' => $type
|
|
];
|
|
$where = [
|
|
'type_id' => $type_id,
|
|
'type' => $type
|
|
];
|
|
$res = AdminTranslation::query()->where($where)->first();
|
|
if ($res) {
|
|
if ($save_data['en'] != $res['en'] ||
|
|
$save_data['zh_cn'] != $res['zh_cn'] ||
|
|
$save_data['zh_tw'] != $res['zh_tw']
|
|
) {
|
|
$save_data['updated_at'] = get_datetime();
|
|
$model = AdminTranslation::query()->findOrFail($res['id']);
|
|
$oldValues = $model->toArray();
|
|
$model->update($save_data);
|
|
$this->logService->logUpdated(
|
|
$model,
|
|
$oldValues,
|
|
'translation.update'
|
|
);
|
|
};
|
|
return $res['id'];
|
|
} else {
|
|
$save_data['created_at'] = get_datetime();
|
|
$model = AdminTranslation::query()->create($save_data);
|
|
$this->logService->logCreated($model, 'translation.create');
|
|
return $model->id;
|
|
}
|
|
}
|
|
|
|
// 保存翻译
|
|
public static function saveTranslation($cn, $en, $tw, $type_id, $type)
|
|
{
|
|
// 创建翻译管理
|
|
return (new AdminTranslationService(
|
|
new OperationLogService()
|
|
))->saveData($cn, $en, $tw, $type_id, $type);
|
|
}
|
|
|
|
//获取翻译 type: 1 车位类型、2 车位属性
|
|
public static function getTranslation($type_id, $type)
|
|
{
|
|
$columns = ['id', 'en', 'zh_cn', 'zh_tw'];
|
|
$where = [
|
|
'type_id' => $type_id,
|
|
'type' => $type
|
|
];
|
|
return AdminTranslation::query()->where($where)->first( $columns);
|
|
}
|
|
|
|
// 返回当前翻译
|
|
public static function getTranslationName($type_id, $type, $is_cn = false)
|
|
{
|
|
$data = self::getTranslation($type_id, $type);
|
|
if ($data) {
|
|
$locale = App::getLocale();
|
|
if ($locale == 'zh-TW') {
|
|
return $data['zh_tw'];
|
|
} else if ($locale == 'en') {
|
|
return $data['en'];
|
|
}
|
|
return $is_cn ? $data['zh_cn'] : '';
|
|
}
|
|
return '';
|
|
}
|
|
|
|
// 同步删除翻译
|
|
public static function syncDelete($type_id, $type)
|
|
{
|
|
$data = self::getTranslation($type_id, $type);
|
|
if ($data) {
|
|
$model = AdminTranslation::query()->findOrFail($data['id']);
|
|
$model->delete();
|
|
(new AdminTranslationService(
|
|
new OperationLogService()
|
|
))->logService->logDeleted($model, 'translation.delete');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @param int $id
|
|
* @throws Exception
|
|
*/
|
|
public function updateModel(array $data, int $id)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
DB::commit();
|
|
|
|
$update_data = [
|
|
'en' => $data['en'],
|
|
'zh_cn' => $data['zh_cn'],
|
|
'zh_tw' => $data['zh_tw']
|
|
];
|
|
|
|
if (AdminTranslation::query()->where($update_data)->where(
|
|
'id',
|
|
'<>',
|
|
$id
|
|
)->exists()
|
|
) {
|
|
throw new Exception(
|
|
__('service.admin_translation.data_exists')
|
|
);
|
|
}
|
|
|
|
$model = AdminTranslation::query()->findOrFail($id);
|
|
$oldValues = $model->toArray();
|
|
$update_data['updated_at'] = get_datetime();
|
|
$model->update($update_data);
|
|
|
|
$this->logService->logUpdated($model, $oldValues, 'translation.update');
|
|
|
|
return $model;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param int $id
|
|
* @return bool
|
|
* @throws Exception
|
|
*/
|
|
public function deleteModel(int $id): bool
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$model = AdminTranslation::query()->findOrFail($id);
|
|
|
|
$this->logService->logDeleted($model, 'translation.delete');
|
|
|
|
$model->delete();
|
|
|
|
DB::commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* 返回翻译类型内容
|
|
翻译type 说明
|
|
1 车位类型
|
|
2 车位属性
|
|
3 停车场
|
|
4 楼层管理
|
|
5 区域管理
|
|
6 通道管理
|
|
7 岗亭管理
|
|
8 离场原因
|
|
9 设备管理
|
|
10 模式管理
|
|
100 - 199 告警信息
|
|
*/
|
|
public static function getTranslationTypeName($type_id, $type, $name)
|
|
{
|
|
$tr_name = self::getTranslationName($type_id, $type);
|
|
return $tr_name ?: $name;
|
|
}
|
|
}
|
|
|