function ($params) { return [ 'filters' => [ ], 'fields' => ['id','status'], 'relations' => [ 'translation' => ['attributes_id', 'locale', 'attribute_name'], 'value' => ['id', 'attribute_id', 'sort_order'], 'value.translation' => ['id', 'values_id', 'locale', 'value_name'], ], 'sorts' => [ ['field' => 'created_at', 'direction' => 'desc'] ], 'pagination' => [ 'page' => $params['page'] ?? 1, 'per_page' => $params['per_page'] ?? 10, ] ]; }, 'preset' => function ($params) { return [ 'filters' => [ ], 'fields' => ['id','status', 'input_type', 'category_id', 'is_img'], 'relations' => [ 'translation' => ['attributes_id', 'locale', 'attribute_name'], 'value' => ['id', 'attribute_id', 'color_code', 'sort_order'], 'value.translation' => ['id', 'values_id', 'locale', 'value_name'], ], 'sorts' => [ ['field' => 'created_at', 'direction' => 'desc'] ], ]; }, 'show' => function ($params) { return [ 'filters' => [ ['field'=>'id', 'operator'=>'=', 'value'=> $params['id']], ], 'fields' => ['id', 'status', 'input_type', 'category_id', 'is_img'], 'relations' => [ 'translation' => ['attributes_id', 'locale', 'attribute_name'], 'value' => ['id', 'attribute_id', 'color_code', 'sort_order'], 'value.translation' => ['id', 'values_id', 'locale', 'value_name'], ], ]; }, ]; } // 声明需要多语言翻译 protected array $translatable = [ 'attribute_name', ]; public function add(array $params) { $sourceLocale = locale(); $sourceFields = $this->extractSourceFields($params); if (empty($sourceFields)) { throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段"); }; $values = $params['value']; unset($params['value']); $VehicleNewAttributeValuesService = new VehicleNewAttributeValuesService(new VehicleNewAttributeValuesModel()); Db::beginTransaction(); try { $info = $this->create($params); if (!$info) { throw new BusinessException('提交失败'); } // 异步翻译并写入 hs_vehicle_data_translation 表 (new TranslationService())->queueTranslations( 'vehicleNewAttributes', $info->id, $sourceLocale, $sourceFields ); $valuesId = []; foreach ($values as $value) { $askedsourceFields = $VehicleNewAttributeValuesService->extractSourceFields($value); $value['attribute_id'] = $info->id; $value_res = $VehicleNewAttributeValuesService->create($value); if (!$value_res) { throw new BusinessException('提交失败'); } (new TranslationService())->queueTranslations( 'vehicleNewAttributeValues', $value_res->id, $sourceLocale, $askedsourceFields ); } 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} 语言的翻译字段"); }; $values = $params['value']; unset($params['value']); $VehicleNewAttributeValuesService = new VehicleNewAttributeValuesService(new VehicleNewAttributeValuesModel()); Db::beginTransaction(); try { $info = $this->update($id, $params); if (!$info) { throw new BusinessException('提交失败'); } // 异步翻译并写入 hs_vehicle_data_translation 表 (new TranslationService())->queueTranslations( 'vehicleNewAttributes', $id, $sourceLocale, $sourceFields ); $valuesId = []; foreach ($values as $value) { $valuesourceFields = $VehicleNewAttributeValuesService->extractSourceFields($value); $value['attribute_id'] = $id; if(!empty($value['id'])){ $valueId = $value['id']; $valuesId[] = $valueId; $value_res = $VehicleNewAttributeValuesService->update($valueId, $value); if (!$value_res) { throw new BusinessException('提交失败:属性值'); } } else { unset($value['id']); $value_res = $VehicleNewAttributeValuesService->create($value); if (!$value_res) { throw new BusinessException('提交失败:属性值'); } $valueId = $value_res->id; $valuesId[] = $valueId; } (new TranslationService())->queueTranslations( 'vehicleNewAttributeValues', $valueId, $sourceLocale, $valuesourceFields ); } VehicleNewAttributeValuesModel::whereNotIn('id', $valuesId)->where('attribute_id',$id)->delete(); Db::commit(); return true; } catch (\Throwable $th) { Db::rollBack(); throw $th; } } }