diff --git a/app/console/controller/BrandController.php b/app/console/controller/BrandController.php index b8bce33..a34ff32 100644 --- a/app/console/controller/BrandController.php +++ b/app/console/controller/BrandController.php @@ -129,9 +129,6 @@ class BrandController extends BaseController */ public function index(Request $request, string $scene = 'default'): Response { -// $Brand = BrandModel::with('banner')->with('trait')->where('id',314)->first()->toArray(); -// var_dump($Brand); -// return json($Brand); try { return parent::index($request, $scene); } catch (\Throwable $th) { diff --git a/app/console/controller/VehicleNewAttributeCategoriesController.php b/app/console/controller/VehicleNewAttributeCategoriesController.php new file mode 100644 index 0000000..baa98a2 --- /dev/null +++ b/app/console/controller/VehicleNewAttributeCategoriesController.php @@ -0,0 +1,119 @@ + $item['id'], + 'sort_order' => $item['sort_order'], + 'category_name' => isset($item['translation']) ? $item['translation']['category_name'] : '', + ]; + + return $result; + } + + return $item; + } + + /** + * 参数预处理 (根据场景处理) + * @param Request $request + * @param string $scene + * @return array + */ + protected function prepareParams(Request $request, string $scene): array + { + $params = parent::prepareParams($request, $scene); + + if ($scene === 'default') { + + } + + return $params; + } + + /** + * 分页 + * + * @param Request $request 请求对象 + * @return Response 响应对象 + */ + public function index(Request $request, string $scene = 'default'): Response + { + try { + return parent::index($request, $scene); + } catch (\Throwable $th) { + return $this->errorResponse($th->getMessage()); + } + } + + + public function add(Request $request): Response + { + try { + return $this->getService()->add($request->all()) + ? $this->successResponse() + : $this->errorResponse(); + } catch (\Throwable $th) { + return $this->errorResponse($th->getMessage().$th->getLine().$th->getFile()); + } + } + + 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().$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().$th->getLine().$th->getFile()); + } + } +} \ No newline at end of file diff --git a/app/console/controller/VehicleNewAttributeCategoriesTranslationController.php b/app/console/controller/VehicleNewAttributeCategoriesTranslationController.php new file mode 100644 index 0000000..7f74fa5 --- /dev/null +++ b/app/console/controller/VehicleNewAttributeCategoriesTranslationController.php @@ -0,0 +1,81 @@ +errorResponse($th->getMessage()); + } + } +} \ No newline at end of file diff --git a/app/console/controller/VehicleNewAttributeValuesController.php b/app/console/controller/VehicleNewAttributeValuesController.php new file mode 100644 index 0000000..a1f4e76 --- /dev/null +++ b/app/console/controller/VehicleNewAttributeValuesController.php @@ -0,0 +1,81 @@ +errorResponse($th->getMessage()); + } + } +} \ No newline at end of file diff --git a/app/console/controller/VehicleNewAttributeValuesTranslationController.php b/app/console/controller/VehicleNewAttributeValuesTranslationController.php new file mode 100644 index 0000000..75177dc --- /dev/null +++ b/app/console/controller/VehicleNewAttributeValuesTranslationController.php @@ -0,0 +1,81 @@ +errorResponse($th->getMessage()); + } + } +} \ No newline at end of file diff --git a/app/console/controller/VehicleNewAttributesController.php b/app/console/controller/VehicleNewAttributesController.php new file mode 100644 index 0000000..d99ec14 --- /dev/null +++ b/app/console/controller/VehicleNewAttributesController.php @@ -0,0 +1,125 @@ + $item['id'], + 'status' => $item['status'], + 'attribute_name' => isset($item['translation']) ? $item['translation']['attribute_name'] : '', + ]; + + foreach ($item['value'] as $value) { + $valueRes['id'] = $value['id']; + $valueRes['attribute_id'] = $value['attribute_id']; + $valueRes['value_name'] = isset($value['translation']) ? $value['translation']['value_name']:''; + $result['value'][] = $valueRes; + } + return $result; + } + + return $item; + } + + /** + * 参数预处理 (根据场景处理) + * @param Request $request + * @param string $scene + * @return array + */ + protected function prepareParams(Request $request, string $scene): array + { + $params = parent::prepareParams($request, $scene); + + if ($scene === 'default') { + + } + + return $params; + } + + /** + * 分页 + * + * @param Request $request 请求对象 + * @return Response 响应对象 + */ + public function index(Request $request, string $scene = 'default'): Response + { + try { + return parent::index($request, $scene); + } catch (\Throwable $th) { + return $this->errorResponse($th->getMessage().$th->getFile().$th->getLine()); + } + } + + public function add(Request $request): Response + { + try { + return $this->getService()->add($request->all()) + ? $this->successResponse() + : $this->errorResponse(); + } catch (\Throwable $th) { + return $this->errorResponse($th->getMessage().$th->getLine().$th->getFile()); + } + } + + 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().$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().$th->getLine().$th->getFile()); + } + } +} \ No newline at end of file diff --git a/app/console/controller/VehicleNewAttributesTranslationController.php b/app/console/controller/VehicleNewAttributesTranslationController.php new file mode 100644 index 0000000..9636b0e --- /dev/null +++ b/app/console/controller/VehicleNewAttributesTranslationController.php @@ -0,0 +1,81 @@ +errorResponse($th->getMessage()); + } + } +} \ No newline at end of file diff --git a/app/console/controller/VehicleNewSkuAttributesController.php b/app/console/controller/VehicleNewSkuAttributesController.php new file mode 100644 index 0000000..c85d8ce --- /dev/null +++ b/app/console/controller/VehicleNewSkuAttributesController.php @@ -0,0 +1,87 @@ + $item['attribute']['id'],'locale'=>locale()])->first(); + $value = VehicleNewAttributeValuesTranslationModel::where(['values_id' => $item['value']['id'],'locale'=>locale()])->first(); + + $item['attribute']['attribute_name'] = $attribute['attribute_name'] ; + $item['value']['value_name'] = $value['value_name']; + } + + return $item; + } + + /** + * 参数预处理 (根据场景处理) + * @param Request $request + * @param string $scene + * @return array + */ + protected function prepareParams(Request $request, string $scene): array + { + $params = parent::prepareParams($request, $scene); + + if ($scene === 'default') { + + } + + return $params; + } + + /** + * 分页 + * + * @param Request $request 请求对象 + * @return Response 响应对象 + */ + public function index(Request $request, string $scene = 'default'): Response + { + try { + return parent::index($request, $scene); + } catch (\Throwable $th) { + return $this->errorResponse($th->getMessage().$th->getLine().$th->getFile()); + } + } +} \ No newline at end of file diff --git a/app/console/controller/VehicleNewSkusController.php b/app/console/controller/VehicleNewSkusController.php new file mode 100644 index 0000000..962aa43 --- /dev/null +++ b/app/console/controller/VehicleNewSkusController.php @@ -0,0 +1,125 @@ + $skuAttribute['attribute']['id'],'locale'=>locale()])->first(); + $value = VehicleNewAttributeValuesTranslationModel::where(['values_id' => $skuAttribute['value']['id'],'locale'=>locale()])->first(); + + $skuAttribute['attribute']['attribute_name'] = $attribute['attribute_name'] ; + $skuAttribute['value']['value_name'] = $value['value_name']; + } + + } + + return $item; + } + + /** + * 参数预处理 (根据场景处理) + * @param Request $request + * @param string $scene + * @return array + */ + protected function prepareParams(Request $request, string $scene): array + { + $params = parent::prepareParams($request, $scene); + + if ($scene === 'default') { + + } + + return $params; + } + + /** + * 分页 + * + * @param Request $request 请求对象 + * @return Response 响应对象 + */ + public function index(Request $request, string $scene = 'default'): Response + { + try { +// $res = VehicleNewSkusModel::with('skuAttributes')->find(9); +// return $this->successResponse($res); + return parent::index($request, $scene); + } catch (\Throwable $th) { + return $this->errorResponse($th->getMessage()); + } + } + + public function add(Request $request): Response + { + try { + return $this->getService()->add($request->all()) + ? $this->successResponse() + : $this->errorResponse(); + } catch (\Throwable $th) { + return $this->errorResponse($th->getMessage().$th->getLine().$th->getFile()); + } + } + + 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().$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()); + } + } +} \ No newline at end of file diff --git a/app/console/model/VehicleNewAttributeCategoriesModel.php b/app/console/model/VehicleNewAttributeCategoriesModel.php new file mode 100644 index 0000000..02b03d8 --- /dev/null +++ b/app/console/model/VehicleNewAttributeCategoriesModel.php @@ -0,0 +1,78 @@ +hasOne(VehicleNewAttributeCategoriesTranslationModel::class, 'categories_id')->where('locale', locale()); + } + + /** + * 多条翻译 + */ + public function translations() + { + return $this->hasMany(VehicleNewAttributeCategoriesTranslationModel::class, 'categories_id'); + } + + public function attribute() + { + return $this->hasMany(VehicleNewAttributesModel::class, 'category_id', 'id'); + } +} \ No newline at end of file diff --git a/app/console/model/VehicleNewAttributeCategoriesTranslationModel.php b/app/console/model/VehicleNewAttributeCategoriesTranslationModel.php new file mode 100644 index 0000000..d3fe95c --- /dev/null +++ b/app/console/model/VehicleNewAttributeCategoriesTranslationModel.php @@ -0,0 +1,56 @@ +hasOne(VehicleNewAttributeValuesTranslationModel::class, 'values_id')->where('locale', locale()); + } + + /** + * 多条翻译 + */ + public function translations() + { + return $this->hasMany(VehicleNewAttributeValuesTranslationModel::class, 'values_id'); + } +} \ No newline at end of file diff --git a/app/console/model/VehicleNewAttributeValuesTranslationModel.php b/app/console/model/VehicleNewAttributeValuesTranslationModel.php new file mode 100644 index 0000000..f821ab5 --- /dev/null +++ b/app/console/model/VehicleNewAttributeValuesTranslationModel.php @@ -0,0 +1,56 @@ +hasOne(VehicleNewAttributesTranslationModel::class, 'attributes_id','id')->where('locale', locale()); + } + + /** + * 多条翻译 + */ + public function translations() + { + return $this->hasMany(VehicleNewAttributesTranslationModel::class, 'attributes_id'); + } + + public function value() + { + return $this->hasMany(VehicleNewAttributeValuesModel::class, 'attribute_id', 'id'); + } + +} \ No newline at end of file diff --git a/app/console/model/VehicleNewAttributesTranslationModel.php b/app/console/model/VehicleNewAttributesTranslationModel.php new file mode 100644 index 0000000..75d83ec --- /dev/null +++ b/app/console/model/VehicleNewAttributesTranslationModel.php @@ -0,0 +1,56 @@ +belongsTo(VehicleNewAttributesModel::class, 'attribute_id', 'id'); + } + + public function value() + { + return $this->belongsTo(VehicleNewAttributeValuesModel::class, 'value_id', 'id'); + } + + public function skuAttributes() + { + return $this->belongsTo(VehicleNewSkusModel::class, 'sku_id', 'id'); + } + +} \ No newline at end of file diff --git a/app/console/model/VehicleNewSkusModel.php b/app/console/model/VehicleNewSkusModel.php new file mode 100644 index 0000000..a9c71f8 --- /dev/null +++ b/app/console/model/VehicleNewSkusModel.php @@ -0,0 +1,61 @@ +hasMany(VehicleNewSkuAttributesModel::class, 'sku_id', 'id'); + } +} \ No newline at end of file diff --git a/app/console/service/VehicleNewAttributeCategoriesService.php b/app/console/service/VehicleNewAttributeCategoriesService.php new file mode 100644 index 0000000..1c6a30a --- /dev/null +++ b/app/console/service/VehicleNewAttributeCategoriesService.php @@ -0,0 +1,135 @@ + function ($params) { + return [ + 'filters' => [ + ], + 'fields' => ['id','sort_order'], + 'relations' => [ + 'translation' => ['categories_id', 'locale', 'category_name'], + 'attribute' => ['id','status'], + 'attribute.translation' => ['attributes_id', 'locale', 'attribute_name'], + ], + 'sorts' => [ + ['field' => 'created_at', 'direction' => 'desc'] + ], + 'pagination' => [ + 'page' => $params['page'] ?? 1, + 'per_page' => $params['per_page'] ?? 10, + ] + ]; + }, + 'show' => function ($params) { + return [ + 'filters' => [ + ['field'=>'id', 'operator'=>'=', 'value'=> $params['id']], + ], + 'fields' => ['id', 'status'], + 'relations' => [ + 'translation' => ['categories_id', 'locale', 'category_name'], + 'attribute' => ['id','status'], + 'attribute.translation' => ['attributes_id', 'locale', 'attribute_name'], + ], + ]; + }, + ]; + } + + // 声明需要多语言翻译 + protected array $translatable = [ + 'category_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( + 'vehicleNewAttributeCategories', + $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( + 'vehicleNewAttributeCategories', + $id, + $sourceLocale, + $sourceFields + ); + + Db::commit(); + return true; + } catch (\Throwable $th) { + Db::rollBack(); + throw $th; + } + } +} \ No newline at end of file diff --git a/app/console/service/VehicleNewAttributeCategoriesTranslationService.php b/app/console/service/VehicleNewAttributeCategoriesTranslationService.php new file mode 100644 index 0000000..bc22128 --- /dev/null +++ b/app/console/service/VehicleNewAttributeCategoriesTranslationService.php @@ -0,0 +1,59 @@ + function ($params) { + // $keyword = $params['keyword'] ?? ''; + // return [ + // 'filters' => [ + // ['field' => '***', 'operator' => '=', 'value' => '***'] + // ], + // 'fields' => ['id'], + // 'relations' => [], + // 'custom' => function(Builder $q) use ($keyword) { + // // 示例 + // if ($keyword) { + // $q->where(function($q2) use($keyword) { + // $q2->whereHas('***', function($q3) use($keyword) { + // $q3->where('***', 'like', "%{$keyword}%"); + // })->orWhereHas('***', function($q4) use($keyword) { + // $q4->where('***', 'like', "%{$keyword}%"); + // }); + // }); + // } + // }, + // 'sorts' => [ + // ['field' => 'created_at', 'direction' => 'desc'] + // ], + // 'pagination' => [ + // 'page' => $params['page'] ?? 1, + // 'per_page' => $params['per_page'] ?? 10, + // ] + // ]; + // } + ]; + } +} \ No newline at end of file diff --git a/app/console/service/VehicleNewAttributeValuesService.php b/app/console/service/VehicleNewAttributeValuesService.php new file mode 100644 index 0000000..0db1152 --- /dev/null +++ b/app/console/service/VehicleNewAttributeValuesService.php @@ -0,0 +1,64 @@ + function ($params) { + // $keyword = $params['keyword'] ?? ''; + // return [ + // 'filters' => [ + // ['field' => '***', 'operator' => '=', 'value' => '***'] + // ], + // 'fields' => ['id'], + // 'relations' => [], + // 'custom' => function(Builder $q) use ($keyword) { + // // 示例 + // if ($keyword) { + // $q->where(function($q2) use($keyword) { + // $q2->whereHas('***', function($q3) use($keyword) { + // $q3->where('***', 'like', "%{$keyword}%"); + // })->orWhereHas('***', function($q4) use($keyword) { + // $q4->where('***', 'like', "%{$keyword}%"); + // }); + // }); + // } + // }, + // 'sorts' => [ + // ['field' => 'created_at', 'direction' => 'desc'] + // ], + // 'pagination' => [ + // 'page' => $params['page'] ?? 1, + // 'per_page' => $params['per_page'] ?? 10, + // ] + // ]; + // } + ]; + } + + // 声明需要多语言翻译 + protected array $translatable = [ + 'value_name', + ]; +} \ No newline at end of file diff --git a/app/console/service/VehicleNewAttributeValuesTranslationService.php b/app/console/service/VehicleNewAttributeValuesTranslationService.php new file mode 100644 index 0000000..f8d6bef --- /dev/null +++ b/app/console/service/VehicleNewAttributeValuesTranslationService.php @@ -0,0 +1,59 @@ + function ($params) { + // $keyword = $params['keyword'] ?? ''; + // return [ + // 'filters' => [ + // ['field' => '***', 'operator' => '=', 'value' => '***'] + // ], + // 'fields' => ['id'], + // 'relations' => [], + // 'custom' => function(Builder $q) use ($keyword) { + // // 示例 + // if ($keyword) { + // $q->where(function($q2) use($keyword) { + // $q2->whereHas('***', function($q3) use($keyword) { + // $q3->where('***', 'like', "%{$keyword}%"); + // })->orWhereHas('***', function($q4) use($keyword) { + // $q4->where('***', 'like', "%{$keyword}%"); + // }); + // }); + // } + // }, + // 'sorts' => [ + // ['field' => 'created_at', 'direction' => 'desc'] + // ], + // 'pagination' => [ + // 'page' => $params['page'] ?? 1, + // 'per_page' => $params['per_page'] ?? 10, + // ] + // ]; + // } + ]; + } +} \ No newline at end of file diff --git a/app/console/service/VehicleNewAttributesService.php b/app/console/service/VehicleNewAttributesService.php new file mode 100644 index 0000000..9f1a14d --- /dev/null +++ b/app/console/service/VehicleNewAttributesService.php @@ -0,0 +1,192 @@ + 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, + ] + ]; + }, + 'show' => function ($params) { + return [ + 'filters' => [ + ['field'=>'id', 'operator'=>'=', 'value'=> $params['id']], + ], + 'fields' => ['id', 'status'], + 'relations' => [ + 'translation' => ['attributes_id', 'locale', 'attribute_name'], + 'value' => ['id', 'attribute_id', '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; + } + } +} \ No newline at end of file diff --git a/app/console/service/VehicleNewAttributesTranslationService.php b/app/console/service/VehicleNewAttributesTranslationService.php new file mode 100644 index 0000000..bc67ad0 --- /dev/null +++ b/app/console/service/VehicleNewAttributesTranslationService.php @@ -0,0 +1,59 @@ + function ($params) { + // $keyword = $params['keyword'] ?? ''; + // return [ + // 'filters' => [ + // ['field' => '***', 'operator' => '=', 'value' => '***'] + // ], + // 'fields' => ['id'], + // 'relations' => [], + // 'custom' => function(Builder $q) use ($keyword) { + // // 示例 + // if ($keyword) { + // $q->where(function($q2) use($keyword) { + // $q2->whereHas('***', function($q3) use($keyword) { + // $q3->where('***', 'like', "%{$keyword}%"); + // })->orWhereHas('***', function($q4) use($keyword) { + // $q4->where('***', 'like', "%{$keyword}%"); + // }); + // }); + // } + // }, + // 'sorts' => [ + // ['field' => 'created_at', 'direction' => 'desc'] + // ], + // 'pagination' => [ + // 'page' => $params['page'] ?? 1, + // 'per_page' => $params['per_page'] ?? 10, + // ] + // ]; + // } + ]; + } +} \ No newline at end of file diff --git a/app/console/service/VehicleNewSkuAttributesService.php b/app/console/service/VehicleNewSkuAttributesService.php new file mode 100644 index 0000000..ca7e8c3 --- /dev/null +++ b/app/console/service/VehicleNewSkuAttributesService.php @@ -0,0 +1,80 @@ + function ($params) { + return [ + 'filters' => [ + ['field' => 'id', 'operator' => '=', 'value' => '7'] + ], + 'fields' => ['id','attribute_id','value_id','img'], + 'relations' => [ + 'attribute' => ['id', 'input_type', 'is_img', 'status'], + 'attribute.translation' => ['attribute_name'], + 'value' => ['id','color_code'], + 'value.translation' => ['value_name'], + ], + 'sorts' => [ + ['field' => 'created_at', 'direction' => 'desc'] + ], + 'pagination' => [ + 'page' => $params['page'] ?? 1, + 'per_page' => $params['per_page'] ?? 10, + ] + ]; + }, + // 'default' => function ($params) { + // $keyword = $params['keyword'] ?? ''; + // return [ + // 'filters' => [ + // ['field' => '***', 'operator' => '=', 'value' => '***'] + // ], + // 'fields' => ['id'], + // 'relations' => [], + // 'custom' => function(Builder $q) use ($keyword) { + // // 示例 + // if ($keyword) { + // $q->where(function($q2) use($keyword) { + // $q2->whereHas('***', function($q3) use($keyword) { + // $q3->where('***', 'like', "%{$keyword}%"); + // })->orWhereHas('***', function($q4) use($keyword) { + // $q4->where('***', 'like', "%{$keyword}%"); + // }); + // }); + // } + // }, + // 'sorts' => [ + // ['field' => 'created_at', 'direction' => 'desc'] + // ], + // 'pagination' => [ + // 'page' => $params['page'] ?? 1, + // 'per_page' => $params['per_page'] ?? 10, + // ] + // ]; + // } + ]; + } +} \ No newline at end of file diff --git a/app/console/service/VehicleNewSkusService.php b/app/console/service/VehicleNewSkusService.php new file mode 100644 index 0000000..11256a2 --- /dev/null +++ b/app/console/service/VehicleNewSkusService.php @@ -0,0 +1,177 @@ + function ($params) { + return [ + 'filters' => [ + ['field' => 'status', 'operator' => '=', 'value' => '1'], + ], + 'fields' => ['id','status'], + 'relations' => [ + 'skuAttributes' => [], + 'skuAttributes.attribute' => ['id', 'input_type', 'is_img', 'status'], +// 'skuAttributes.attribute.translation' => ['attribute_name'], + 'skuAttributes.value' => ['id','color_code'], + ], + 'sorts' => [ + ['field' => 'created_at', 'direction' => 'desc'] + ], + 'pagination' => [ + 'page' => $params['page'] ?? 1, + 'per_page' => $params['per_page'] ?? 10, + ] + ]; + }, + 'show' => function ($params) { + return [ + 'filters' => [ + ['field'=>'id', 'operator'=>'=', 'value'=> $params['id']], + ], + 'fields' => ['id', 'status'], + 'relations' => [ + 'skuAttributes' => [], + 'skuAttributes.attribute' => ['id', 'input_type', 'is_img', 'status'], +// 'skuAttributes.attribute.translation' => ['attribute_name'], + 'skuAttributes.value' => ['id','color_code'], + ], + ]; + }, + ]; + } + + + public function add(array $params) + { + $sourceLocale = locale(); +// $sourceFields = $this->extractSourceFields($params); +// +// if (empty($sourceFields)) { +// throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段"); +// }; + + $skuAttributes = $params['skuAttributes']; + unset($params['skuAttributes']); + $VehicleNewSkuAttributesService = new VehicleNewSkuAttributesService(new VehicleNewSkuAttributesModel()); + Db::beginTransaction(); + try { + $sku_code = sprintf("%05d", $params['vehicle_id']); + $params['sku_code'] = rand(100,999).$sku_code[0].' '.substr($sku_code,1).' '.sprintf("%04d", $skuAttributes[0]['attribute_id']).' '.sprintf("%04d", $skuAttributes[0]['value_id']); + + $info = $this->create($params); + if (!$info) { + throw new BusinessException('提交失败'); + } + + // 异步翻译并写入 hs_vehicle_data_translation 表 +// (new TranslationService())->queueTranslations( +// 'vehicleNewAttributes', +// $info->id, +// $sourceLocale, +// $sourceFields +// ); + + foreach ($skuAttributes as $skuAttribute) { + $skuAttribute['sku_id'] = $info->id; + $skuAttribute_res = $VehicleNewSkuAttributesService->create($skuAttribute); + if (!$skuAttribute_res) { + 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} 语言的翻译字段"); +// }; + + $skuAttributes = $params['skuAttributes']; + unset($params['skuAttributes']); + $VehicleNewSkuAttributesService = new VehicleNewSkuAttributesService(new VehicleNewSkuAttributesModel()); + Db::beginTransaction(); + try { + $info = $this->update($id, $params); + if (!$info) { + throw new BusinessException('提交失败'); + } + + // 异步翻译并写入 hs_vehicle_data_translation 表 +// (new TranslationService())->queueTranslations( +// 'vehicleNewAttributes', +// $id, +// $sourceLocale, +// $sourceFields +// ); + + + $skuAttributesId = []; + foreach ($skuAttributes as $skuAttribute) { + $skuAttribute['sku_id'] = $id; + + if(!empty($skuAttribute['id'])){ + $skuAttributeId = $skuAttribute['id']; + $skuAttributesId[] = $skuAttributeId; + $skuAttribute_res = $VehicleNewSkuAttributesService->update($skuAttributeId, $skuAttribute); + if (!$skuAttribute_res) { + throw new BusinessException('提交失败:属性值'); + } + } else { + unset($skuAttribute['id']); + $skuAttribute_res = $VehicleNewSkuAttributesService->create($skuAttribute); + if (!$skuAttribute_res) { + throw new BusinessException('提交失败:属性值'); + } + $skuAttributeId = $skuAttribute_res->id; + $skuAttributesId[] = $skuAttributeId; + } + } + VehicleNewSkuAttributesModel::whereNotIn('id', $skuAttributesId)->where('attribute_id',$id)->delete(); + + + Db::commit(); + return true; + } catch (\Throwable $th) { + Db::rollBack(); + throw $th; + } + } +} \ No newline at end of file diff --git a/app/console/validate/VehicleNewAttributeCategoriesTranslationValidator.php b/app/console/validate/VehicleNewAttributeCategoriesTranslationValidator.php new file mode 100644 index 0000000..801f884 --- /dev/null +++ b/app/console/validate/VehicleNewAttributeCategoriesTranslationValidator.php @@ -0,0 +1,29 @@ + [ + + ], + + 'update' => [ + + ] + ]; +} \ No newline at end of file diff --git a/app/console/validate/VehicleNewAttributeCategoriesValidator.php b/app/console/validate/VehicleNewAttributeCategoriesValidator.php new file mode 100644 index 0000000..7be8f44 --- /dev/null +++ b/app/console/validate/VehicleNewAttributeCategoriesValidator.php @@ -0,0 +1,29 @@ + [ + + ], + + 'update' => [ + + ] + ]; +} \ No newline at end of file diff --git a/app/console/validate/VehicleNewAttributeValuesTranslationValidator.php b/app/console/validate/VehicleNewAttributeValuesTranslationValidator.php new file mode 100644 index 0000000..4d543f8 --- /dev/null +++ b/app/console/validate/VehicleNewAttributeValuesTranslationValidator.php @@ -0,0 +1,29 @@ + [ + + ], + + 'update' => [ + + ] + ]; +} \ No newline at end of file diff --git a/app/console/validate/VehicleNewAttributeValuesValidator.php b/app/console/validate/VehicleNewAttributeValuesValidator.php new file mode 100644 index 0000000..602ef0a --- /dev/null +++ b/app/console/validate/VehicleNewAttributeValuesValidator.php @@ -0,0 +1,29 @@ + [ + + ], + + 'update' => [ + + ] + ]; +} \ No newline at end of file diff --git a/app/console/validate/VehicleNewAttributesTranslationValidator.php b/app/console/validate/VehicleNewAttributesTranslationValidator.php new file mode 100644 index 0000000..6b58eb9 --- /dev/null +++ b/app/console/validate/VehicleNewAttributesTranslationValidator.php @@ -0,0 +1,29 @@ + [ + + ], + + 'update' => [ + + ] + ]; +} \ No newline at end of file diff --git a/app/console/validate/VehicleNewAttributesValidator.php b/app/console/validate/VehicleNewAttributesValidator.php new file mode 100644 index 0000000..8503278 --- /dev/null +++ b/app/console/validate/VehicleNewAttributesValidator.php @@ -0,0 +1,29 @@ + [ + + ], + + 'update' => [ + + ] + ]; +} \ No newline at end of file diff --git a/app/console/validate/VehicleNewSkuAttributesValidator.php b/app/console/validate/VehicleNewSkuAttributesValidator.php new file mode 100644 index 0000000..a0eb932 --- /dev/null +++ b/app/console/validate/VehicleNewSkuAttributesValidator.php @@ -0,0 +1,29 @@ + [ + + ], + + 'update' => [ + + ] + ]; +} \ No newline at end of file diff --git a/app/console/validate/VehicleNewSkusValidator.php b/app/console/validate/VehicleNewSkusValidator.php new file mode 100644 index 0000000..5bb480a --- /dev/null +++ b/app/console/validate/VehicleNewSkusValidator.php @@ -0,0 +1,29 @@ + [ + + ], + + 'update' => [ + + ] + ]; +} \ No newline at end of file diff --git a/app/mzl/modules/translation/config/trans.php b/app/mzl/modules/translation/config/trans.php index 751ccbc..202fc91 100644 --- a/app/mzl/modules/translation/config/trans.php +++ b/app/mzl/modules/translation/config/trans.php @@ -67,6 +67,18 @@ return [ 'translation_model' => \app\console\model\VehicleNewContentTranslationModel::class, 'foreign_key' => 'content_id', ], + 'vehicleNewAttributes' => [ + 'translation_model' => \app\console\model\VehicleNewAttributesTranslationModel::class, + 'foreign_key' => 'attributes_id', + ], + 'vehicleNewAttributeValues' => [ + 'translation_model' => \app\console\model\VehicleNewAttributeValuesTranslationModel::class, + 'foreign_key' => 'values_id', + ], + 'vehicleNewAttributeCategories' => [ + 'translation_model' => \app\console\model\VehicleNewAttributeCategoriesTranslationModel::class, + 'foreign_key' => 'categories_id', + ], ], // 默认支持的目标语种列表 diff --git a/config/route.php b/config/route.php index d9244dd..174b500 100644 --- a/config/route.php +++ b/config/route.php @@ -184,6 +184,28 @@ Route::group('/console', function() { Route::any('/vehicle/new/{id}/delete', [\app\console\controller\VehicleNewController::class, 'destroy']); Route::any('/vehicle/new/{id}/detail', [\app\console\controller\VehicleNewController::class, 'detail']); + //新车sku + Route::any('/vehicle/new/sku/list', [\app\console\controller\VehicleNewSkusController::class, 'index']); + Route::any('/vehicle/new/sku/attributes/list', [\app\console\controller\VehicleNewSkuAttributesController::class, 'index']); + Route::any('/vehicle/new/sku/add', [\app\console\controller\VehicleNewSkusController::class, 'add']); + Route::any('/vehicle/new/sku/{id}/update', [\app\console\controller\VehicleNewSkusController::class, 'edit']); + Route::any('/vehicle/new/sku/{id}/delete', [\app\console\controller\VehicleNewSkusController::class, 'destroy']); + Route::any('/vehicle/new/sku/{id}/detail', [\app\console\controller\VehicleNewSkusController::class, 'detail']); + + //新车属性 + Route::any('/vehicle/new/attributes/list', [\app\console\controller\VehicleNewAttributesController::class, 'index']); + Route::any('/vehicle/new/attributes/add', [\app\console\controller\VehicleNewAttributesController::class, 'add']); + Route::any('/vehicle/new/attributes/{id}/update', [\app\console\controller\VehicleNewAttributesController::class, 'edit']); + Route::any('/vehicle/new/attributes/{id}/delete', [\app\console\controller\VehicleNewAttributesController::class, 'destroy']); + Route::any('/vehicle/new/attributes/{id}/detail', [\app\console\controller\VehicleNewAttributesController::class, 'detail']); + + //新车属性分类 + Route::any('/vehicle/new/categories/list', [\app\console\controller\VehicleNewAttributeCategoriesController::class, 'index']); + Route::any('/vehicle/new/categories/add', [\app\console\controller\VehicleNewAttributeCategoriesController::class, 'add']); + Route::any('/vehicle/new/categories/{id}/update', [\app\console\controller\VehicleNewAttributeCategoriesController::class, 'edit']); + Route::any('/vehicle/new/categories/{id}/delete', [\app\console\controller\VehicleNewAttributeCategoriesController::class, 'destroy']); + Route::any('/vehicle/new/categories/{id}/detail', [\app\console\controller\VehicleNewAttributeCategoriesController::class, 'detail']); + // 上传 Route::any('/upload/image', [\app\mzl\modules\upload\controller\UploadController::class, 'uploadImage']);