function ($params) { return [ 'filters' => [ ['field' => 'status', 'operator' => '=', 'value' => '1'] ], 'fields' => ['id', 'status', 'sort', 'created_at', 'updated_at'], 'relations' => [ 'translation' => ['energy_id', 'name'] ], 'sorts' => [ ['field' => 'id', 'direction' => 'desc'] ], 'pagination' => [ 'page' => $params['page'] ?? 1, 'per_page' => $params['per_page'] ?? 10, ] ]; }, 'preset' => function ($params) { return [ 'filters' => [ ['field' => 'status', 'operator' => '=', 'value' => '1'] ], 'fields' => ['id'], 'relations' => [ 'translation' => ['energy_id', 'name'] ], 'sorts' => [ ['field' => 'id', 'direction' => 'desc'] ] ]; } ]; } protected array $translatable = [ 'name' ]; public function add(array $params) { $sourceLocale = locale(); $sourceFields = $this->extractSourceFields($params); 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( 'energy', $info->id, $sourceLocale, $sourceFields ); 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} 语言的翻译字段"); }; Db::beginTransaction(); try { $info = $this->update($id, $params); if (!$info) { throw new BusinessException('提交失败'); } // 异步翻译并写入 hs_vehicle_data_translation 表 (new TranslationService())->queueTranslations( 'energy', $id, $sourceLocale, $sourceFields ); Db::commit(); return true; } catch (\Throwable $th) { Db::rollBack(); throw $th; } } }