Browse Source

翻译管理优化 车位类型翻译

master
wanghongjun 3 weeks ago
parent
commit
9ede53a542
  1. 5
      app/Http/Controllers/Admin/ParkingSpaceTypeController.php
  2. 4
      app/Models/AdminTranslation.php
  3. 59
      app/Services/AdminTranslationService.php
  4. 35
      app/Services/ParkingSpaceTypeService.php
  5. 3
      database/migrations/2026_02_10_175744_create_admin_translation_table.php

5
app/Http/Controllers/Admin/ParkingSpaceTypeController.php

@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin;
use App\Exceptions\CustomException; use App\Exceptions\CustomException;
use App\Models\ParkingSpaceAttributes; use App\Models\ParkingSpaceAttributes;
use App\Models\ParkingSpaceType; use App\Models\ParkingSpaceType;
use App\Services\AdminTranslationService;
use App\Services\ApiResponseService; use App\Services\ApiResponseService;
use App\Services\ParkingSpaceTypeAttrService; use App\Services\ParkingSpaceTypeAttrService;
use App\Services\ParkingSpaceTypeService; use App\Services\ParkingSpaceTypeService;
@ -153,6 +154,10 @@ class ParkingSpaceTypeController extends BaseController
$this->validateId($id, ParkingSpaceType::class); $this->validateId($id, ParkingSpaceType::class);
$item = ParkingSpaceType::query()->findOrFail($id)->toArray(); $item = ParkingSpaceType::query()->findOrFail($id)->toArray();
$item['attributes'] = ParkingSpaceTypeAttrService::getTypeAttrData($item['id']); $item['attributes'] = ParkingSpaceTypeAttrService::getTypeAttrData($item['id']);
$Translation = AdminTranslationService::getTranslation($item['id'], 1);
$item['en_name'] = $Translation['en'] ?? '';
$item['tw_name'] = $Translation['zh_tw'] ?? '';
$data = [ $data = [
'color_list' => $this->SpaceTypeService->getColorList(), 'color_list' => $this->SpaceTypeService->getColorList(),
'attr_list' => ParkingSpaceAttributes::getTypeList(), 'attr_list' => ParkingSpaceAttributes::getTypeList(),

4
app/Models/AdminTranslation.php

@ -15,7 +15,9 @@ class AdminTranslation extends Model
protected $fillable = [ protected $fillable = [
'en', 'en',
'zh_cn', 'zh_cn',
'zh_tw' 'zh_tw',
'type_id',
'type'
]; ];
protected $hidden = [ protected $hidden = [

59
app/Services/AdminTranslationService.php

@ -57,6 +57,65 @@ class AdminTranslationService
} }
} }
// 添加,编辑翻译
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 = ['en', 'zh_cn', 'zh_tw'];
$where = [
'type_id' => $type_id,
'type' => $type
];
return AdminTranslation::query()->where($where)->first( $columns);
}
/** /**
* @param array $data * @param array $data
* @param int $id * @param int $id

35
app/Services/ParkingSpaceTypeService.php

@ -23,18 +23,20 @@ class ParkingSpaceTypeService
'blue', 'blue',
'purple', 'purple',
'cyan', 'cyan',
'white' 'white',
'magenta'
]; ];
public static array $colorArr public static array $colorArr
= [ = [
'red' => '#ff0000', 'red' => '#ff0000',
'green' => '#349c32', 'green' => '#349c32',
'yellow' => '#ffff00', 'yellow' => '#ffff00',
'blue' => '#0000ff', 'blue' => '#0000ff',
'purple' => '#800080', 'purple' => '#800080',
'cyan' => '#00ffff', 'cyan' => '#00ffff',
'white' => '#ffffff' 'white' => '#ffffff',
'magenta' => '#ca00c9'
]; ];
/** /**
@ -97,6 +99,14 @@ class ParkingSpaceTypeService
))->createBatchData($model->id, $attributes); ))->createBatchData($model->id, $attributes);
} }
AdminTranslationService::saveTranslation(
$data['name'],
$data['en_name'],
$data['tw_name'],
$model->id,
1
);
DB::commit(); DB::commit();
return $model; return $model;
} catch (Exception $e) { } catch (Exception $e) {
@ -162,6 +172,15 @@ class ParkingSpaceTypeService
$saveData['updated_at'] = get_datetime(); $saveData['updated_at'] = get_datetime();
$attributes = $saveData['attributes']; $attributes = $saveData['attributes'];
unset($saveData['attributes']); unset($saveData['attributes']);
// 保存翻译
$saveData['translation_id']
= AdminTranslationService::saveTranslation(
$data['name'],
$data['en_name'],
$data['tw_name'],
$id,
1
);
$model->update($saveData); $model->update($saveData);

3
database/migrations/2026_02_10_175744_create_admin_translation_table.php

@ -16,9 +16,12 @@ return new class extends Migration
$table->string('en')->comment('英文'); $table->string('en')->comment('英文');
$table->string('zh_cn')->comment('中文简体'); $table->string('zh_cn')->comment('中文简体');
$table->string('zh_tw')->comment('中文繁体'); $table->string('zh_tw')->comment('中文繁体');
$table->bigInteger('type_id')->default(0)->comment('关联类型id');
$table->tinyInteger('type')->default(0)->comment('关联类型');
$table->softDeletes(); $table->softDeletes();
$table->timestamps(); $table->timestamps();
$table->innoDb(); $table->innoDb();
$table->index(['type_id', 'type'], 'type_idx');
}); });
} }

Loading…
Cancel
Save