257 lines
8.9 KiB
PHP
257 lines
8.9 KiB
PHP
<?php
|
|
|
|
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;
|
|
use support\exception\BusinessException;
|
|
use support\Db;
|
|
use app\mzl\modules\translation\service\TranslationService;
|
|
use support\Log;
|
|
|
|
class BrandService extends BaseService
|
|
{
|
|
/**
|
|
* 定义模型
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getModel(): string
|
|
{
|
|
return BrandModel::class;
|
|
}
|
|
|
|
/**
|
|
* 多场景配置
|
|
*/
|
|
public function queryScenes(): array
|
|
{
|
|
return [
|
|
'default' => function ($params) {
|
|
return [
|
|
'filters' => $params['filters'],
|
|
'fields' => ['id', 'status', 'sort', 'toppic', 'descpic', 'logo', 'letter', 'is_hot', 'creater', 'created_at', 'updated_at'],
|
|
'relations' => [
|
|
'translation' => ['brand_id', 'name', 'desc','toppic_alt','descpic_alt','logo_alt'],
|
|
'banner' => ['id', 'pic', 'sort',],
|
|
'banner.translation' => ['id', 'alt',],
|
|
'creater' => ['id', 'nickname',],
|
|
'trait' => ['id', 'sort',],
|
|
'trait.translation' => ['id', 'title', 'content',],
|
|
],
|
|
'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' => ['brand_id', 'name']
|
|
],
|
|
'sorts' => [
|
|
['field' => 'id', 'direction' => 'desc']
|
|
]
|
|
];
|
|
}
|
|
];
|
|
}
|
|
|
|
|
|
protected array $translatable = [
|
|
'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'] ?? [];
|
|
unset($params['banners']);
|
|
$traits = $params['traits'] ?? [];
|
|
unset($params['traits']);
|
|
|
|
$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(
|
|
'brand',
|
|
$info->id,
|
|
$sourceLocale,
|
|
$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('提交失败');
|
|
}
|
|
// 异步翻译并写入 hs_brand_banner_translation 表
|
|
(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 $info->id;
|
|
} catch (\Throwable $th) {
|
|
Db::rollBack();
|
|
throw $th;
|
|
}
|
|
}
|
|
|
|
public function edit(array $params, int $id)
|
|
{
|
|
$sourceLocale = locale();
|
|
|
|
$BrandBannerService = new BrandBannerService(new BrandBannerModel());
|
|
$BrandTraitService = new BrandTraitService(new BrandTraitModel());
|
|
$banners = $params['banners'] ?? [];
|
|
unset($params['banners']);
|
|
$traits = $params['traits'] ?? [];
|
|
unset($params['traits']);
|
|
|
|
$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(
|
|
'brand',
|
|
$id,
|
|
$sourceLocale,
|
|
$sourceFields
|
|
);
|
|
|
|
//品牌轮播图
|
|
$bannersId = [];
|
|
foreach ($banners as $banner) {
|
|
$bannersourceFields = $BrandBannerService->extractSourceFields($banner);
|
|
$bannersourceFields['brand_id'] = $id;
|
|
$banner['brand_id'] = $id;
|
|
|
|
if(!empty($banner['id'])){
|
|
$bannerId = $banner['id'];
|
|
$bannersId[] = $bannerId;
|
|
$banner_res = $BrandBannerService->update($bannerId, $banner);
|
|
if (!$banner_res) {
|
|
throw new BusinessException('提交失败:品牌轮播图');
|
|
}
|
|
} else {
|
|
unset($banner['id']);
|
|
$banner_res = $BrandBannerService->create($banner);
|
|
if (!$banner_res) {
|
|
throw new BusinessException('提交失败:品牌轮播图.');
|
|
}
|
|
$bannerId = $banner_res->id;
|
|
$bannersId[] = $bannerId;
|
|
}
|
|
|
|
(new TranslationService())->queueTranslations(
|
|
'brandBanner',
|
|
$bannerId,
|
|
$sourceLocale,
|
|
$bannersourceFields
|
|
);
|
|
}
|
|
BrandBannerModel::whereNotIn('id', $bannersId)->where('brand_id',$id)->delete();
|
|
|
|
//品牌特性
|
|
$traitsId = [];
|
|
foreach ($traits as $trait) {
|
|
$traitsourceFields = $BrandTraitService->extractSourceFields($trait);
|
|
$traitsourceFields['brand_id'] = $id;
|
|
$trait['brand_id'] = $id;
|
|
|
|
if(!empty($trait['id'])){
|
|
$traitId = $trait['id'];
|
|
$traitsId[] = $traitId;
|
|
$trait_res = $BrandTraitService->update($traitId, $trait);
|
|
if (!$trait_res) {
|
|
throw new BusinessException('提交失败:品牌轮播图');
|
|
}
|
|
} else {
|
|
unset($trait['id']);
|
|
$trait_res = $BrandTraitService->create($trait);
|
|
if (!$trait_res) {
|
|
throw new BusinessException('提交失败:品牌轮播图.');
|
|
}
|
|
$traitId = $trait_res->id;
|
|
$traitsId[] = $traitId;
|
|
}
|
|
|
|
(new TranslationService())->queueTranslations(
|
|
'brandTrait',
|
|
$traitId,
|
|
$sourceLocale,
|
|
$traitsourceFields
|
|
);
|
|
}
|
|
BrandTraitModel::whereNotIn('id', $traitsId)->where('brand_id',$id)->delete();
|
|
|
|
|
|
Db::commit();
|
|
return true;
|
|
} catch (\Throwable $th) {
|
|
Db::rollBack();
|
|
throw $th;
|
|
}
|
|
}
|
|
} |