diff --git a/app/console/controller/VehicleNewController.php b/app/console/controller/VehicleNewController.php index 203ab2f..938b68d 100644 --- a/app/console/controller/VehicleNewController.php +++ b/app/console/controller/VehicleNewController.php @@ -41,7 +41,48 @@ class VehicleNewController extends BaseController { if ($scene === 'default') { - + $result = [ + 'id' => $item['id'], + 'vin' => $item['vin'], + 'brand' => $item['brand'], + 'price' => $item['price'], + 'cover' => $item['cover'], + 'energy' => $item['energy'], + 'type' => $item['type'], + 'model' => $item['model'], + 'video' => $item['video'], + 'name' => isset($item['translation']) ? $item['translation']['name'] : '', + 'condition_desc' => isset($item['translation']) ? $item['translation']['condition_desc'] : '', + 'created_at' => date('Y-m-d H:i:s', $item['created_at']), + 'updated_at' => date('Y-m-d H:i:s', $item['updated_at']), + ]; + foreach ($item['asked'] as $asked) { + $result['asked']['id'] = $asked['id']; + $result['asked']['vehicle_id'] = $asked['vehicle_id']; + $result['asked']['status'] = $asked['status']; + $result['asked']['title'] = isset($asked['translation']) ? $asked['translation']['title']:''; + $result['asked']['content'] = isset($asked['translation']) ? $asked['translation']['content']:''; + } + foreach ($item['trait'] as $trait) { + $result['trait']['id'] = $trait['id']; + $result['trait']['vehicle_id'] = $trait['vehicle_id']; + $result['trait']['status'] = $trait['status']; + $result['trait']['title'] = isset($trait['translation']) ? $trait['translation']['title']:''; + $result['trait']['content'] = isset($trait['translation']) ? $trait['translation']['content']:''; + } + foreach ($item['album'] as $album) { + $result['album']['id'] = $album['id']; + $result['album']['vehicle_id'] = $album['vehicle_id']; + $result['album']['status'] = $album['status']; + $result['album']['alt'] = isset($album['translation']) ? $album['translation']['alt']:''; + } + foreach ($item['content'] as $content) { + $result['content']['id'] = $content['id']; + $result['content']['vehicle_id'] = $content['vehicle_id']; + $result['content']['status'] = $content['status']; + $result['content']['content'] = isset($content['translation']) ? $content['translation']['content']:''; + } + return $result; } return $item; @@ -75,6 +116,17 @@ class VehicleNewController extends BaseController } } + public function edit(Request $request, int $id): Response + { + try { + return $this->getService()->edit($request->all(), $id) + ? $this->successResponse() + : $this->errorResponse(); + } catch (\Throwable $th) { + return $this->errorResponse($th->getMessage()); + } + } + /** * 分页 * @@ -85,6 +137,16 @@ class VehicleNewController extends BaseController { try { return parent::index($request, $scene); + } catch (\Throwable $th) { + return $this->errorResponse($th->getMessage().$th->getLine().$th->getFile()); + } + } + + public function detail(Request $request, int $id, string $scene = 'show'): Response + { + try { + $request->setGet(['id'=>$id]); + return parent::one($request, $scene); } catch (\Throwable $th) { return $this->errorResponse($th->getMessage()); } diff --git a/app/console/model/VehicleNewAlbumModel.php b/app/console/model/VehicleNewAlbumModel.php index ef88fee..f22adaf 100644 --- a/app/console/model/VehicleNewAlbumModel.php +++ b/app/console/model/VehicleNewAlbumModel.php @@ -47,7 +47,7 @@ class VehicleNewAlbumModel extends Model /** * 允许自动填充模型字段 */ - protected $fillable = ['id', 'vehicle_id', 'status', 'sort', 'created_at', 'updated_at']; + protected $fillable = ['id', 'vehicle_id', 'pic', 'status', 'sort', 'created_at', 'updated_at']; /** * 不允许自动填充模型字段 @@ -69,4 +69,8 @@ class VehicleNewAlbumModel extends Model { return $this->hasMany(VehicleNewAlbumTranslationModel::class, 'album_id'); } + + const TRANS_FIELDS = [ + 'alt', + ]; } \ No newline at end of file diff --git a/app/console/model/VehicleNewModel.php b/app/console/model/VehicleNewModel.php index 6706a0f..e40c992 100644 --- a/app/console/model/VehicleNewModel.php +++ b/app/console/model/VehicleNewModel.php @@ -54,6 +54,9 @@ class VehicleNewModel extends Model */ protected $guarded = []; + const TRANS_FIELDS = [ + 'name', 'condition_desc' + ]; /** * 单条翻译 diff --git a/app/console/model/VehicleNewTranslationModel.php b/app/console/model/VehicleNewTranslationModel.php index 0767a83..352ef1e 100644 --- a/app/console/model/VehicleNewTranslationModel.php +++ b/app/console/model/VehicleNewTranslationModel.php @@ -47,7 +47,7 @@ class VehicleNewTranslationModel extends Model /** * 允许自动填充模型字段 */ - protected $fillable = ['id', 'vehicle_id', 'locale', 'condition_desc', 'location', 'created_at', 'updated_at']; + protected $fillable = ['id', 'vehicle_id', 'locale', 'name', 'condition_desc', 'location', 'created_at', 'updated_at']; /** * 不允许自动填充模型字段 diff --git a/app/console/service/VehicleNewService.php b/app/console/service/VehicleNewService.php index bc7813f..e14486b 100644 --- a/app/console/service/VehicleNewService.php +++ b/app/console/service/VehicleNewService.php @@ -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; + } + } } \ No newline at end of file diff --git a/config/route.php b/config/route.php index 73cede1..0d73447 100644 --- a/config/route.php +++ b/config/route.php @@ -171,10 +171,9 @@ Route::group('/console', function() { // 新车管理 Route::any('/vehicle/new/list', [\app\console\controller\VehicleNewController::class, 'index']); Route::any('/vehicle/new/add', [\app\console\controller\VehicleNewController::class, 'add']); -// Route::any('/vehicle/{id}/update', [\app\console\controller\VehicleNewController::class, 'edit']); -// Route::any('/vehicle/new/{id}/do', [\app\console\controller\VehicleNewController::class, 'doSaled']); -// Route::any('/vehicle/new/{id}/delete', [\app\console\controller\VehicleNewController::class, 'destroy']); -// Route::any('/vehicle/new/{id}/detail', [\app\console\controller\VehicleNewController::class, 'detail']); + Route::any('/vehicle/{id}/update', [\app\console\controller\VehicleNewController::class, 'edit']); + Route::any('/vehicle/new/{id}/delete', [\app\console\controller\VehicleNewController::class, 'destroy']); + Route::any('/vehicle/new/{id}/detail', [\app\console\controller\VehicleNewController::class, 'detail']); // 上传 Route::any('/upload/image', [\app\mzl\modules\upload\controller\UploadController::class, 'uploadImage']);