品牌信息
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace app\console\service;
|
||||
|
||||
use app\console\model\BrandBannerModel;
|
||||
use app\mzl\common\services\BaseService;
|
||||
use app\mzl\modules\translation\service\TranslationService;
|
||||
use support\Db;
|
||||
use support\exception\BusinessException;
|
||||
|
||||
class BrandBannerService extends BaseService
|
||||
{
|
||||
|
||||
/**
|
||||
* 定义模型
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getModel(): string
|
||||
{
|
||||
return BrandBannerModel::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* 多场景配置
|
||||
*/
|
||||
public function queryScenes(): array
|
||||
{
|
||||
return [
|
||||
'default' => function ($params) {
|
||||
return [
|
||||
'filters' => [
|
||||
['field' => 'status', 'operator' => '=', 'value' => '1'],
|
||||
['field' => 'brand_id', 'operator' => '=', 'value' => $params['brand_id']],
|
||||
],
|
||||
'fields' => ['id', 'status', 'sort', 'brand_id', 'pic', 'created_at', 'updated_at'],
|
||||
'relations' => [
|
||||
'translation' => ['brand_id','banner_id', 'title', 'content']
|
||||
],
|
||||
'sorts' => [
|
||||
['field' => 'id', 'direction' => 'desc']
|
||||
],
|
||||
'pagination' => [
|
||||
'page' => $params['page'] ?? 1,
|
||||
'per_page' => $params['per_page'] ?? 10,
|
||||
]
|
||||
];
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
protected array $translatable = [
|
||||
'alt'
|
||||
];
|
||||
|
||||
public function add(array $params)
|
||||
{
|
||||
$sourceLocale = locale();
|
||||
$sourceFields = $this->extractSourceFields($params);
|
||||
$sourceFields['brand_id'] = $params['brand_id'];
|
||||
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(
|
||||
'brandBanner',
|
||||
$info->id,
|
||||
$sourceLocale,
|
||||
$sourceFields
|
||||
);
|
||||
|
||||
Db::commit();
|
||||
return $info->id;
|
||||
} 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(
|
||||
'brandBanner',
|
||||
$id,
|
||||
$sourceLocale,
|
||||
$sourceFields
|
||||
);
|
||||
|
||||
Db::commit();
|
||||
return $id;
|
||||
} catch (\Throwable $th) {
|
||||
Db::rollBack();
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace app\console\service;
|
||||
|
||||
use app\console\model\BrandBannerModel;
|
||||
use app\console\model\BrandModel;
|
||||
use app\console\model\BrandTraitModel;
|
||||
use app\mzl\common\services\BaseService;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -33,9 +35,13 @@ class BrandService extends BaseService
|
||||
'filters' => [
|
||||
['field' => 'status', 'operator' => '=', 'value' => '1']
|
||||
],
|
||||
'fields' => ['id', 'status', 'sort', 'created_at', 'updated_at'],
|
||||
'fields' => ['id', 'status', 'sort', 'toppic', 'descpic', 'logo', 'letter', 'created_at', 'updated_at'],
|
||||
'relations' => [
|
||||
'translation' => ['brand_id', 'name']
|
||||
'translation' => ['brand_id', 'name', 'desc','toppic_alt','descpic_alt','logo_alt'],
|
||||
'banner' => ['id', 'pic', 'sort',],
|
||||
'banner.translation' => ['id', 'alt',],
|
||||
'trait' => ['id', 'sort',],
|
||||
'trait.translation' => ['id', 'title', 'content',],
|
||||
],
|
||||
'sorts' => [
|
||||
['field' => 'id', 'direction' => 'desc']
|
||||
@@ -65,12 +71,20 @@ class BrandService extends BaseService
|
||||
|
||||
|
||||
protected array $translatable = [
|
||||
'name'
|
||||
'name', 'desc','toppic_alt','descpic_alt','logo_alt'
|
||||
];
|
||||
|
||||
public function add(array $params)
|
||||
{
|
||||
$sourceLocale = locale();
|
||||
|
||||
$BrandBannerService = new BrandBannerService(new BrandBannerModel());
|
||||
$BrandTraitService = new BrandTraitService(new BrandTraitModel());
|
||||
$banners = $params['banners'] ?? null;
|
||||
unset($params['banners']);
|
||||
$traits = $params['traits'] ?? null;
|
||||
unset($params['traits']);
|
||||
|
||||
$sourceFields = $this->extractSourceFields($params);
|
||||
if (empty($sourceFields)) {
|
||||
throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段");
|
||||
@@ -91,8 +105,44 @@ class BrandService extends BaseService
|
||||
$sourceFields
|
||||
);
|
||||
|
||||
//品牌轮播图
|
||||
foreach ($banners as $banner) {
|
||||
$bannersourceFields = $BrandBannerService->extractSourceFields($banner);
|
||||
$bannersourceFields['brand_id'] = $info->id;
|
||||
$banner['brand_id'] = $info->id;
|
||||
$banner = $BrandBannerService->create($banner);
|
||||
if (!$banner) {
|
||||
throw new BusinessException('提交失败');
|
||||
}
|
||||
|
||||
(new TranslationService())->queueTranslations(
|
||||
'brandBanner',
|
||||
$banner->id,
|
||||
$sourceLocale,
|
||||
$bannersourceFields
|
||||
);
|
||||
}
|
||||
|
||||
//品牌特性
|
||||
foreach ($traits as $trait) {
|
||||
$traitsourceFields = $BrandTraitService->extractSourceFields($trait);
|
||||
$traitsourceFields['brand_id'] = $info->id;
|
||||
$trait['brand_id'] = $info->id;
|
||||
$trait = $BrandTraitService->create($trait);
|
||||
if (!$trait) {
|
||||
throw new BusinessException('提交失败');
|
||||
}
|
||||
|
||||
(new TranslationService())->queueTranslations(
|
||||
'brandTrait',
|
||||
$trait->id,
|
||||
$sourceLocale,
|
||||
$traitsourceFields
|
||||
);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
return $info->id;
|
||||
} catch (\Throwable $th) {
|
||||
Db::rollBack();
|
||||
throw $th;
|
||||
@@ -102,6 +152,14 @@ class BrandService extends BaseService
|
||||
public function edit(array $params, int $id)
|
||||
{
|
||||
$sourceLocale = locale();
|
||||
|
||||
$BrandBannerService = new BrandBannerService(new BrandBannerModel());
|
||||
$BrandTraitService = new BrandTraitService(new BrandTraitModel());
|
||||
$banners = $params['banners'] ?? null;
|
||||
unset($params['banners']);
|
||||
$traits = $params['traits'] ?? null;
|
||||
unset($params['traits']);
|
||||
|
||||
$sourceFields = $this->extractSourceFields($params);
|
||||
if (empty($sourceFields)) {
|
||||
throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段");
|
||||
@@ -122,6 +180,49 @@ class BrandService extends BaseService
|
||||
$sourceFields
|
||||
);
|
||||
|
||||
//品牌轮播图
|
||||
foreach ($banners as $banner) {
|
||||
$bannersourceFields = $BrandBannerService->extractSourceFields($banner);
|
||||
$bannersourceFields['brand_id'] = $id;
|
||||
$bannerId = $banner['id'];
|
||||
$banner['brand_id'] = $id;
|
||||
$banner = $BrandBannerService->update($bannerId,$banner);
|
||||
if (!$banner) {
|
||||
throw new BusinessException('提交失败');
|
||||
}
|
||||
|
||||
(new TranslationService())->queueTranslations(
|
||||
'brandBanner',
|
||||
$bannerId,
|
||||
$sourceLocale,
|
||||
$bannersourceFields
|
||||
);
|
||||
}
|
||||
$ids = array_column($banners,'id');
|
||||
BrandBannerModel::whereNotIn('id', $ids)->where('brand_id',$id)->delete();
|
||||
|
||||
//品牌特性
|
||||
foreach ($traits as $trait) {
|
||||
$traitsourceFields = $BrandTraitService->extractSourceFields($trait);
|
||||
$traitsourceFields['brand_id'] = $id;
|
||||
$traitId = $trait['id'];
|
||||
$trait['brand_id'] = $id;
|
||||
$trait = $BrandTraitService->update($traitId,$trait);
|
||||
if (!$trait) {
|
||||
throw new BusinessException('提交失败');
|
||||
}
|
||||
|
||||
(new TranslationService())->queueTranslations(
|
||||
'brandTrait',
|
||||
$traitId,
|
||||
$sourceLocale,
|
||||
$traitsourceFields
|
||||
);
|
||||
}
|
||||
$ids = array_column($traits,'id');
|
||||
BrandTraitModel::whereNotIn('id', $ids)->where('brand_id',$id)->delete();
|
||||
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Throwable $th) {
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace app\console\service;
|
||||
|
||||
use app\console\model\BrandTraitModel;
|
||||
use app\mzl\common\services\BaseService;
|
||||
use app\mzl\modules\translation\service\TranslationService;
|
||||
use support\Db;
|
||||
use support\exception\BusinessException;
|
||||
|
||||
class BrandTraitService extends BaseService
|
||||
{
|
||||
|
||||
/**
|
||||
* 定义模型
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getModel(): string
|
||||
{
|
||||
return BrandTraitModel::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* 多场景配置
|
||||
*/
|
||||
public function queryScenes(): array
|
||||
{
|
||||
return [
|
||||
'default' => function ($params) {
|
||||
return [
|
||||
'filters' => [
|
||||
['field' => 'status', 'operator' => '=', 'value' => '1'],
|
||||
['field' => 'brand_id', 'operator' => '=', 'value' => $params['brand_id']],
|
||||
],
|
||||
'fields' => ['id', 'status', 'sort', 'brand_id', 'created_at', 'updated_at'],
|
||||
'relations' => [
|
||||
'translation' => ['brand_id','trait_id', 'title', 'content']
|
||||
],
|
||||
'sorts' => [
|
||||
['field' => 'id', 'direction' => 'desc']
|
||||
],
|
||||
'pagination' => [
|
||||
'page' => $params['page'] ?? 1,
|
||||
'per_page' => $params['per_page'] ?? 10,
|
||||
]
|
||||
];
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
protected array $translatable = [
|
||||
'title', 'content'
|
||||
];
|
||||
|
||||
public function add(array $params)
|
||||
{
|
||||
$sourceLocale = locale();
|
||||
$sourceFields = $this->extractSourceFields($params);
|
||||
$sourceFields['brand_id'] = $params['brand_id'];
|
||||
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(
|
||||
'brandTrait',
|
||||
$info->id,
|
||||
$sourceLocale,
|
||||
$sourceFields
|
||||
);
|
||||
|
||||
Db::commit();
|
||||
return $info->id;
|
||||
} 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(
|
||||
'brandTrait',
|
||||
$id,
|
||||
$sourceLocale,
|
||||
$sourceFields
|
||||
);
|
||||
|
||||
Db::commit();
|
||||
return $id;
|
||||
} catch (\Throwable $th) {
|
||||
Db::rollBack();
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user