135 lines
4.0 KiB
PHP
135 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace app\console\service;
|
|
|
|
use app\console\model\VehicleNewAttributeCategoriesModel;
|
|
use app\mzl\common\services\BaseService;
|
|
use app\mzl\modules\translation\service\TranslationService;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use support\Db;
|
|
use support\exception\BusinessException;
|
|
|
|
class VehicleNewAttributeCategoriesService extends BaseService
|
|
{
|
|
/**
|
|
* 定义模型
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getModel(): string
|
|
{
|
|
return VehicleNewAttributeCategoriesModel::class;
|
|
}
|
|
|
|
/**
|
|
* 多场景配置
|
|
*/
|
|
public function queryScenes(): array
|
|
{
|
|
return [
|
|
'default' => function ($params) {
|
|
return [
|
|
'filters' => [
|
|
],
|
|
'fields' => ['id','sort_order'],
|
|
'relations' => [
|
|
'translation' => ['categories_id', 'locale', 'category_name'],
|
|
'attribute' => ['id','status'],
|
|
'attribute.translation' => ['attributes_id', 'locale', 'attribute_name'],
|
|
],
|
|
'sorts' => [
|
|
['field' => 'created_at', 'direction' => 'desc']
|
|
],
|
|
'pagination' => [
|
|
'page' => $params['page'] ?? 1,
|
|
'per_page' => $params['per_page'] ?? 10,
|
|
]
|
|
];
|
|
},
|
|
'show' => function ($params) {
|
|
return [
|
|
'filters' => [
|
|
['field'=>'id', 'operator'=>'=', 'value'=> $params['id']],
|
|
],
|
|
'fields' => ['id', 'status'],
|
|
'relations' => [
|
|
'translation' => ['categories_id', 'locale', 'category_name'],
|
|
'attribute' => ['id','status'],
|
|
'attribute.translation' => ['attributes_id', 'locale', 'attribute_name'],
|
|
],
|
|
];
|
|
},
|
|
];
|
|
}
|
|
|
|
// 声明需要多语言翻译
|
|
protected array $translatable = [
|
|
'category_name',
|
|
];
|
|
|
|
public function add(array $params)
|
|
{
|
|
$sourceLocale = locale();
|
|
$sourceFields = $this->extractSourceFields($params);
|
|
|
|
if (empty($sourceFields)) {
|
|
throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段");
|
|
};
|
|
|
|
Db::beginTransaction();
|
|
try {
|
|
$info = $this->create($params);
|
|
if (!$info) {
|
|
throw new BusinessException('提交失败');
|
|
}
|
|
|
|
// 异步翻译并写入 hs_vehicle_data_translation 表
|
|
(new TranslationService())->queueTranslations(
|
|
'vehicleNewAttributeCategories',
|
|
$info->id,
|
|
$sourceLocale,
|
|
$sourceFields
|
|
);
|
|
|
|
|
|
Db::commit();
|
|
return true;
|
|
} catch (\Throwable $th) {
|
|
Db::rollBack();
|
|
throw $th;
|
|
}
|
|
}
|
|
|
|
public function edit(array $params, int $id)
|
|
{
|
|
$sourceLocale = locale();
|
|
$sourceFields = $this->extractSourceFields($params);
|
|
|
|
if (empty($sourceFields)) {
|
|
throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段");
|
|
};
|
|
|
|
Db::beginTransaction();
|
|
try {
|
|
$info = $this->update($id, $params);
|
|
if (!$info) {
|
|
throw new BusinessException('提交失败');
|
|
}
|
|
|
|
// 异步翻译并写入 hs_vehicle_data_translation 表
|
|
(new TranslationService())->queueTranslations(
|
|
'vehicleNewAttributeCategories',
|
|
$id,
|
|
$sourceLocale,
|
|
$sourceFields
|
|
);
|
|
|
|
Db::commit();
|
|
return true;
|
|
} catch (\Throwable $th) {
|
|
Db::rollBack();
|
|
throw $th;
|
|
}
|
|
}
|
|
} |