品牌信息
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user