120 lines
3.3 KiB
PHP
120 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace app\console\service;
|
|
|
|
use app\console\model\BrandBannerModel;
|
|
use app\mzl\common\services\BaseService;
|
|
use app\mzl\modules\translation\service\TranslationService;
|
|
use support\Db;
|
|
use support\exception\BusinessException;
|
|
|
|
class BrandBannerService extends BaseService
|
|
{
|
|
|
|
/**
|
|
* 定义模型
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getModel(): string
|
|
{
|
|
return BrandBannerModel::class;
|
|
}
|
|
|
|
/**
|
|
* 多场景配置
|
|
*/
|
|
public function queryScenes(): array
|
|
{
|
|
return [
|
|
'default' => function ($params) {
|
|
return [
|
|
'filters' => [
|
|
['field' => 'status', 'operator' => '=', 'value' => '1'],
|
|
['field' => 'brand_id', 'operator' => '=', 'value' => $params['brand_id']],
|
|
],
|
|
'fields' => ['id', 'status', 'sort', 'brand_id', 'pic', 'created_at', 'updated_at'],
|
|
'relations' => [
|
|
'translation' => ['brand_id','banner_id', 'title', 'content']
|
|
],
|
|
'sorts' => [
|
|
['field' => 'id', 'direction' => 'desc']
|
|
],
|
|
'pagination' => [
|
|
'page' => $params['page'] ?? 1,
|
|
'per_page' => $params['per_page'] ?? 10,
|
|
]
|
|
];
|
|
},
|
|
];
|
|
}
|
|
|
|
|
|
protected array $translatable = [
|
|
'alt'
|
|
];
|
|
|
|
public function add(array $params)
|
|
{
|
|
$sourceLocale = locale();
|
|
$sourceFields = $this->extractSourceFields($params);
|
|
$sourceFields['brand_id'] = $params['brand_id'];
|
|
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(
|
|
'brandBanner',
|
|
$info->id,
|
|
$sourceLocale,
|
|
$sourceFields
|
|
);
|
|
|
|
Db::commit();
|
|
return $info->id;
|
|
} 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(
|
|
'brandBanner',
|
|
$id,
|
|
$sourceLocale,
|
|
$sourceFields
|
|
);
|
|
|
|
Db::commit();
|
|
return $id;
|
|
} catch (\Throwable $th) {
|
|
Db::rollBack();
|
|
throw $th;
|
|
}
|
|
}
|
|
} |