function(array $params) { return [ 'filters' => $params['filters'], 'fields' => [ 'id','vin','price','cover', 'type','energy','model','mileage','vehicle_age', 'views','is_hot','created_at','updated_at', 'brand', 'model', 'user_id', 'location' ], 'relations' => [ 'translation' => ['vehicle_id', 'name','condition_desc'], 'brand.translation' => ['brand_id', 'name'], 'album' => [ 'id', 'vehicle_data_id', 'front_45_degree', 'front_left_45_degree', 'front_left_headlight', 'front_view', 'lower_front', 'wheel_hub', 'front_right_seat', 'right_lower_side', 'rear_right_taillight', 'rear_right_45_degree', 'lower_rear', 'trunk', 'left_lower_side', 'rear_seats', 'center_console', 'control_panel', 'gear_shift', 'car_key', 'steering_wheel', 'dashboard', 'odometer_reading', 'front_left_door_panel', 'front_left_door_function_area', 'front_left_seat', 'interior_ceiling' ], 'user' => ['realname', 'nickname'], 'types.translation' => ['type_id', 'name'], 'energy.translation' => ['energy_id', 'name'], 'models.translation' => ['model_id', 'name'], 'user' => ['nickname'], 'location.translation' => ['location_id', 'name'] ], 'sorts' => [ ['field'=>'id','direction'=>'desc'], ], 'pagination' => [ 'page' => $params['page'] ?? 1, 'per_page' => $params['per_page'] ?? 10, ], ]; }, 'show' => function(array $params) { return [ 'filters' => [ ['field'=>'id', 'operator'=>'=', 'value'=> $params['id']], ], 'fields' => [ 'id','vin','price','cover', 'type','energy','model','mileage','vehicle_age', 'views','is_hot','created_at','updated_at', 'brand', 'model', 'user_id', 'location' ], 'relations' => [ 'translation' => ['vehicle_id', 'name','condition_desc'], 'brand.translation' => ['brand_id', 'name'], 'album' => [ 'id', 'vehicle_data_id', 'front_45_degree', 'front_left_45_degree', 'front_left_headlight', 'front_view', 'lower_front', 'wheel_hub', 'front_right_seat', 'right_lower_side', 'rear_right_taillight', 'rear_right_45_degree', 'lower_rear', 'trunk', 'left_lower_side', 'rear_seats', 'center_console', 'control_panel', 'gear_shift', 'car_key', 'steering_wheel', 'dashboard', 'odometer_reading', 'front_left_door_panel', 'front_left_door_function_area', 'front_left_seat', 'interior_ceiling' ], 'user' => ['realname', 'nickname'], 'types.translation' => ['type_id', 'name'], 'energy.translation' => ['energy_id', 'name'], 'models.translation' => ['model_id', 'name'], 'user' => ['nickname'], 'location.translation' => ['location_id', 'name'] ], 'sorts' => [ ['field'=>'id','direction'=>'desc'], ], 'pagination' => [ 'page' => $params['page'] ?? 1, 'per_page' => $params['per_page'] ?? 10, ], ]; }, ]; } /** * 操作销售状态 */ public function doSaled(array $data, int $id): bool { $info = $this->model->where('id', $id)->first(); if (!$info) { throw new BusinessException('未找到车辆信息', 404); } $info->is_saled = $data['is_saled']; if (!$info->save()) { throw new BusinessException('设置失败,请重试', 500); } return true; } // 声明需要多语言翻译 protected array $translatable = [ 'name', 'condition_desc', // 'location' ]; public function add(array $params) { $sourceLocale = locale(); $sourceFields = $this->extractSourceFields($params); if (empty($sourceFields)) { throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段"); }; $albums = VehicleAlbumModel::VEHICLE_ALBUM_FIELDS; foreach ($params as $ko => $vo) { if (in_array($ko, $albums)) { $album[$ko] = $vo ?? ''; } else { if (in_array($ko, VehicleDataModel::TRANS_FIELDS)) { $trans[$ko] = $vo ?? ''; } else { $data[$ko] = $vo ?? ''; } } } Db::beginTransaction(); try { $data['info_status'] = 1; $info = $this->create($data); if (!$info) { throw new BusinessException('提交失败'); } // 异步翻译并写入 hs_vehicle_data_translation 表 (new TranslationService())->queueTranslations( 'vehicle', $info->id, $sourceLocale, $sourceFields ); $album['vehicle_data_id'] = $info->id; $albumModel = new VehicleAlbumModel(); if (!$albumModel->create($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; } } public function edit(array $params, int $id) { $sourceLocale = locale(); $sourceFields = $this->extractSourceFields($params); if (empty($sourceFields)) { throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段"); }; $album = []; $trans = []; $albums = VehicleAlbumModel::VEHICLE_ALBUM_FIELDS; foreach ($params as $ko => $vo) { if (in_array($ko, $albums)) { $album[$ko] = $vo ?? ''; } else { if (in_array($ko, VehicleDataModel::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; } } }