Files
usedcar/app/console/service/BrandService.php
T

257 lines
8.9 KiB
PHP
Raw Normal View History

2025-05-14 13:19:23 +08:00
<?php
namespace app\console\service;
2025-05-21 11:28:30 +08:00
use app\console\model\BrandBannerModel;
2025-05-14 13:19:23 +08:00
use app\console\model\BrandModel;
2025-05-21 11:28:30 +08:00
use app\console\model\BrandTraitModel;
2025-05-14 13:19:23 +08:00
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;
2025-05-30 16:47:06 +08:00
use support\Log;
2025-05-14 13:19:23 +08:00
class BrandService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return BrandModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
'default' => function ($params) {
return [
2025-05-30 15:35:28 +08:00
'filters' => $params['filters'],
2025-06-06 14:08:10 +08:00
'fields' => ['id', 'status', 'sort', 'toppic', 'descpic', 'logo', 'letter', 'is_hot', 'creater', 'created_at', 'updated_at'],
2025-05-14 13:19:23 +08:00
'relations' => [
2025-05-21 11:28:30 +08:00
'translation' => ['brand_id', 'name', 'desc','toppic_alt','descpic_alt','logo_alt'],
2025-06-04 11:51:37 +08:00
'banner' => ['id', 'pic', 'sort',],
'banner.translation' => ['id', 'alt',],
2025-06-06 14:08:10 +08:00
'creater' => ['id', 'nickname',],
2025-06-04 11:51:37 +08:00
'trait' => ['id', 'sort',],
'trait.translation' => ['id', 'title', 'content',],
2025-05-14 13:19:23 +08:00
],
'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 = [
2025-05-21 11:28:30 +08:00
'name', 'desc','toppic_alt','descpic_alt','logo_alt'
2025-05-14 13:19:23 +08:00
];
public function add(array $params)
{
$sourceLocale = locale();
2025-05-21 11:28:30 +08:00
$BrandBannerService = new BrandBannerService(new BrandBannerModel());
$BrandTraitService = new BrandTraitService(new BrandTraitModel());
2025-06-04 14:27:31 +08:00
$banners = $params['banners'] ?? [];
2025-05-21 11:28:30 +08:00
unset($params['banners']);
2025-06-04 14:27:31 +08:00
$traits = $params['traits'] ?? [];
2025-05-21 11:28:30 +08:00
unset($params['traits']);
2025-05-14 13:19:23 +08:00
$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
);
2025-05-21 11:28:30 +08:00
//品牌轮播图
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('提交失败');
}
2025-05-23 14:24:04 +08:00
// 异步翻译并写入 hs_brand_banner_translation 表
2025-05-21 11:28:30 +08:00
(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
);
}
2025-05-14 13:19:23 +08:00
Db::commit();
2025-05-21 11:28:30 +08:00
return $info->id;
2025-05-14 13:19:23 +08:00
} catch (\Throwable $th) {
Db::rollBack();
throw $th;
}
}
public function edit(array $params, int $id)
{
$sourceLocale = locale();
2025-05-21 11:28:30 +08:00
$BrandBannerService = new BrandBannerService(new BrandBannerModel());
$BrandTraitService = new BrandTraitService(new BrandTraitModel());
2025-06-04 14:27:31 +08:00
$banners = $params['banners'] ?? [];
2025-05-21 11:28:30 +08:00
unset($params['banners']);
2025-06-04 14:27:31 +08:00
$traits = $params['traits'] ?? [];
2025-05-21 11:28:30 +08:00
unset($params['traits']);
2025-05-14 13:19:23 +08:00
$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
);
2025-05-21 11:28:30 +08:00
//品牌轮播图
2025-05-23 13:29:56 +08:00
$bannersId = [];
2025-05-21 11:28:30 +08:00
foreach ($banners as $banner) {
$bannersourceFields = $BrandBannerService->extractSourceFields($banner);
$bannersourceFields['brand_id'] = $id;
2025-05-23 14:24:04 +08:00
$banner['brand_id'] = $id;
2025-05-23 13:29:56 +08:00
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;
2025-05-21 11:28:30 +08:00
}
(new TranslationService())->queueTranslations(
'brandBanner',
$bannerId,
$sourceLocale,
$bannersourceFields
);
}
2025-05-23 13:29:56 +08:00
BrandBannerModel::whereNotIn('id', $bannersId)->where('brand_id',$id)->delete();
2025-05-21 11:28:30 +08:00
//品牌特性
2025-05-23 13:29:56 +08:00
$traitsId = [];
2025-05-21 11:28:30 +08:00
foreach ($traits as $trait) {
$traitsourceFields = $BrandTraitService->extractSourceFields($trait);
$traitsourceFields['brand_id'] = $id;
2025-05-23 14:24:04 +08:00
$trait['brand_id'] = $id;
2025-05-23 13:29:56 +08:00
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;
2025-05-21 11:28:30 +08:00
}
(new TranslationService())->queueTranslations(
'brandTrait',
$traitId,
$sourceLocale,
$traitsourceFields
);
}
2025-05-23 13:29:56 +08:00
BrandTraitModel::whereNotIn('id', $traitsId)->where('brand_id',$id)->delete();
2025-05-21 11:28:30 +08:00
2025-05-14 13:19:23 +08:00
Db::commit();
return true;
} catch (\Throwable $th) {
Db::rollBack();
throw $th;
}
}
}