180 lines
6.5 KiB
PHP
180 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace app\console\service;
|
|
|
|
use app\console\model\VehicleNewAlbumModel;
|
|
use app\console\model\VehicleNewAskedModel;
|
|
use app\console\model\VehicleNewContentModel;
|
|
use app\console\model\VehicleNewModel;
|
|
use app\console\model\VehicleNewTraitModel;
|
|
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 VehicleNewService extends BaseService
|
|
{
|
|
/**
|
|
* 定义模型
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getModel(): string
|
|
{
|
|
return VehicleNewModel::class;
|
|
}
|
|
|
|
/**
|
|
* 多场景配置
|
|
*/
|
|
public function queryScenes(): array
|
|
{
|
|
return [
|
|
'default' => function ($params) {
|
|
$keyword = $params['keyword'] ?? '';
|
|
return [
|
|
'filters' => [
|
|
// ['field' => '***', 'operator' => '=', 'value' => '***']
|
|
],
|
|
'fields' => ['id', 'vin', 'brand', 'cover', 'price', 'type', 'energy', 'model', 'video', 'views', 'created_at', 'updated_at'],
|
|
'relations' => [
|
|
'translation' => ['vehicle_id', 'locale', 'condition_desc', 'location'],
|
|
'asked' => ['id', 'vehicle_id', 'status', 'sort'],
|
|
'asked.translation' => ['id', 'asked_id', 'locale', 'title', 'content'],
|
|
'trait' => ['id', 'vehicle_id', 'status', 'sort'],
|
|
'trait.translation' => ['id', 'trait_id', 'locale', 'title', 'content'],
|
|
'album' => ['id', 'vehicle_id', 'status', 'sort'],
|
|
'album.translation' => ['id', 'album_id', 'locale', 'alt'],
|
|
'content' => ['id', 'vehicle_id', 'status', 'sort'],
|
|
'content.translation' => ['id', 'content_id', 'locale', 'content'],
|
|
],
|
|
'sorts' => [
|
|
['field' => 'id', 'direction' => 'desc']
|
|
],
|
|
'pagination' => [
|
|
'page' => $params['page'] ?? 1,
|
|
'per_page' => $params['per_page'] ?? 10,
|
|
]
|
|
];
|
|
}
|
|
];
|
|
}
|
|
|
|
|
|
// 声明需要多语言翻译
|
|
protected array $translatable = [
|
|
'condition_desc',
|
|
];
|
|
|
|
public function add(array $params)
|
|
{
|
|
$sourceLocale = locale();
|
|
$sourceFields = $this->extractSourceFields($params);
|
|
|
|
if (empty($sourceFields)) {
|
|
throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段");
|
|
};
|
|
|
|
$askeds = $params['asked'];
|
|
unset($params['asked']);
|
|
$traits = $params['trait'];
|
|
unset($params['trait']);
|
|
$albums = $params['album'];
|
|
unset($params['album']);
|
|
$contents = $params['content'];
|
|
unset($params['content']);
|
|
|
|
$VehicleNewAskedService = new VehicleNewAskedService(new VehicleNewAskedModel());
|
|
$VehicleNewTraitService = new VehicleNewTraitService(new VehicleNewTraitModel());
|
|
$VehicleNewAlbumService = new VehicleNewAlbumService(new VehicleNewAlbumModel());
|
|
$VehicleNewContentService = new VehicleNewContentService(new VehicleNewContentModel());
|
|
|
|
Db::beginTransaction();
|
|
try {
|
|
$info = $this->create($params);
|
|
if (!$info) {
|
|
throw new BusinessException('提交失败');
|
|
}
|
|
|
|
// 异步翻译并写入 hs_vehicle_data_translation 表
|
|
(new TranslationService())->queueTranslations(
|
|
'vehicleNew',
|
|
$info->id,
|
|
$sourceLocale,
|
|
$sourceFields
|
|
);
|
|
|
|
foreach ($askeds as $asked) {
|
|
$askedsourceFields = $VehicleNewAskedService->extractSourceFields($asked);
|
|
$asked['vehicle_id'] = $info->id;
|
|
$asked_res = $VehicleNewAskedService->create($asked);
|
|
if (!$asked_res) {
|
|
throw new BusinessException('提交失败');
|
|
}
|
|
// $askedsourceFields['asked_id'] = $asked_res->id;
|
|
(new TranslationService())->queueTranslations(
|
|
'vehicleNewAsked',
|
|
$asked_res->id,
|
|
$sourceLocale,
|
|
$askedsourceFields
|
|
);
|
|
}
|
|
|
|
foreach ($traits as $trait) {
|
|
$traitsourceFields = $VehicleNewTraitService->extractSourceFields($trait);
|
|
$trait['vehicle_id'] = $info->id;
|
|
$trait_res = $VehicleNewTraitService->create($trait);
|
|
if (!$trait_res) {
|
|
throw new BusinessException('提交失败');
|
|
}
|
|
|
|
(new TranslationService())->queueTranslations(
|
|
'vehicleNewTrait',
|
|
$trait_res->id,
|
|
$sourceLocale,
|
|
$traitsourceFields
|
|
);
|
|
}
|
|
|
|
foreach ($albums as $album) {
|
|
$albumsourceFields = $VehicleNewAlbumService->extractSourceFields($album);
|
|
$album['vehicle_id'] = $info->id;
|
|
$trait_res = $VehicleNewAlbumService->create($trait);
|
|
if (!$trait_res) {
|
|
throw new BusinessException('提交失败');
|
|
}
|
|
|
|
(new TranslationService())->queueTranslations(
|
|
'vehicleNewTrait',
|
|
$trait_res->id,
|
|
$sourceLocale,
|
|
$albumsourceFields
|
|
);
|
|
}
|
|
|
|
foreach ($contents as $content) {
|
|
$contentsourceFields = $VehicleNewContentService->extractSourceFields($content);
|
|
$content['vehicle_id'] = $info->id;
|
|
$trait_res = $VehicleNewContentService->create($content);
|
|
if (!$trait_res) {
|
|
throw new BusinessException('提交失败');
|
|
}
|
|
|
|
(new TranslationService())->queueTranslations(
|
|
'vehicleNewContent',
|
|
$trait_res->id,
|
|
$sourceLocale,
|
|
$contentsourceFields
|
|
);
|
|
}
|
|
|
|
Db::commit();
|
|
return true;
|
|
} catch (\Throwable $th) {
|
|
Db::rollBack();
|
|
throw $th;
|
|
}
|
|
}
|
|
} |