新车详情、删除
This commit is contained in:
@@ -40,12 +40,39 @@ class VehicleNewService extends BaseService
|
||||
],
|
||||
'fields' => ['id', 'vin', 'brand', 'cover', 'price', 'type', 'energy', 'model', 'video', 'views', 'created_at', 'updated_at'],
|
||||
'relations' => [
|
||||
'translation' => ['vehicle_id', 'locale', 'condition_desc', 'location'],
|
||||
'translation' => ['vehicle_id', 'locale', 'name', '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' => ['id', 'vehicle_id', 'pic', '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,
|
||||
]
|
||||
];
|
||||
},
|
||||
'show' => 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', 'name', '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', 'pic', 'status', 'sort'],
|
||||
'album.translation' => ['id', 'album_id', 'locale', 'alt'],
|
||||
'content' => ['id', 'vehicle_id', 'status', 'sort'],
|
||||
'content.translation' => ['id', 'content_id', 'locale', 'content'],
|
||||
@@ -65,7 +92,7 @@ class VehicleNewService extends BaseService
|
||||
|
||||
// 声明需要多语言翻译
|
||||
protected array $translatable = [
|
||||
'condition_desc',
|
||||
'condition_desc','name',
|
||||
];
|
||||
|
||||
public function add(array $params)
|
||||
@@ -141,14 +168,13 @@ class VehicleNewService extends BaseService
|
||||
foreach ($albums as $album) {
|
||||
$albumsourceFields = $VehicleNewAlbumService->extractSourceFields($album);
|
||||
$album['vehicle_id'] = $info->id;
|
||||
$trait_res = $VehicleNewAlbumService->create($trait);
|
||||
$album_res = $VehicleNewAlbumService->create($album);
|
||||
if (!$trait_res) {
|
||||
throw new BusinessException('提交失败');
|
||||
}
|
||||
|
||||
(new TranslationService())->queueTranslations(
|
||||
'vehicleNewTrait',
|
||||
$trait_res->id,
|
||||
'vehicleNewAlbum',
|
||||
$album_res->id,
|
||||
$sourceLocale,
|
||||
$albumsourceFields
|
||||
);
|
||||
@@ -177,4 +203,74 @@ class VehicleNewService extends BaseService
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function edit(array $params, int $id)
|
||||
{
|
||||
$sourceLocale = locale();
|
||||
$sourceFields = $this->extractSourceFields($params);
|
||||
if (empty($sourceFields)) {
|
||||
throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段");
|
||||
};
|
||||
|
||||
$album = [];
|
||||
$trans = [];
|
||||
$albums = VehicleNewAlbumModel::VEHICLE_New_ALBUM_FIELDS;
|
||||
foreach ($params as $ko => $vo) {
|
||||
if (in_array($ko, $albums)) {
|
||||
$album[$ko] = $vo ?? '';
|
||||
} elseif (in_array($ko, VehicleNewModel::TRANS_FIELDS)) {
|
||||
$trans[$ko] = $vo ?? '';
|
||||
}else{
|
||||
$data[$ko] = $vo ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
Db::beginTransaction();
|
||||
try {
|
||||
$info = $this->update($id, $params);
|
||||
if (!$info) {
|
||||
throw new BusinessException('提交失败');
|
||||
}
|
||||
|
||||
// 异步翻译并写入 hs_vehicle_data_translation 表
|
||||
(new TranslationService())->queueTranslations(
|
||||
'vehicle',
|
||||
$id,
|
||||
$sourceLocale,
|
||||
$sourceFields
|
||||
);
|
||||
|
||||
if ($album) {
|
||||
// $album['vehicle_data_id'] = $info->id;
|
||||
$albumModel = new VehicleAlbumModel();
|
||||
if (!$albumModel->updateOrCreate(
|
||||
['vehicle_data_id' => $id],
|
||||
$album
|
||||
)) {
|
||||
throw new BusinessException('提交失败');
|
||||
}
|
||||
}
|
||||
|
||||
// $applyModel = new VehicleApplyModel();
|
||||
// if (!$applyModel->create([
|
||||
// 'vehicle_data_id' => $info->id,
|
||||
// 'desc' => '提交审核'
|
||||
// ])) {
|
||||
// throw new BusinessException('提交失败');
|
||||
// }
|
||||
|
||||
// if (isset($params['draft_id']) && $params['draft_id']) {
|
||||
// if (!DraftsModel::where('id', $params['draft_id'])->delete()) {
|
||||
// throw new BusinessException('草稿移除失败');
|
||||
// }
|
||||
// }
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Throwable $th) {
|
||||
Db::rollBack();
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user