Merge branch 'feature/brand'

This commit is contained in:
2025-06-04 11:04:01 +08:00
116 changed files with 7676 additions and 27 deletions
+120
View File
@@ -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;
}
}
}
+131 -7
View File
@@ -2,13 +2,16 @@
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
{
@@ -30,12 +33,14 @@ class BrandService extends BaseService
return [
'default' => function ($params) {
return [
'filters' => [
['field' => 'status', 'operator' => '=', 'value' => '1']
],
'fields' => ['id', 'status', 'sort', 'created_at', 'updated_at'],
'filters' => $params['filters'],
'fields' => ['id', 'status', 'sort', 'toppic', 'descpic', 'logo', 'letter', 'is_hot', 'created_at', 'updated_at'],
'relations' => [
'translation' => ['brand_id', 'name']
'translation' => ['brand_id', 'name', 'desc','toppic_alt','descpic_alt','logo_alt'],
'banners' => ['id', 'pic', 'sort',],
'banners.translation' => ['id', 'alt',],
'traits' => ['id', 'sort',],
'traits.translation' => ['id', 'title', 'content',],
],
'sorts' => [
['field' => 'id', 'direction' => 'desc']
@@ -65,12 +70,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 +104,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('提交失败');
}
// 异步翻译并写入 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 true;
return $info->id;
} catch (\Throwable $th) {
Db::rollBack();
throw $th;
@@ -102,6 +151,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 +179,73 @@ class BrandService extends BaseService
$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) {
+120
View File
@@ -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;
}
}
}
@@ -0,0 +1,64 @@
<?php
namespace app\console\service;
use app\console\model\VehicleNewAlbumModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class VehicleNewAlbumService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return VehicleNewAlbumModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
// '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,
// ]
// ];
// }
];
}
// 声明需要多语言翻译
protected array $translatable = [
'alt',
];
}
@@ -0,0 +1,59 @@
<?php
namespace app\console\service;
use app\console\model\VehicleNewAlbumTranslationModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class VehicleNewAlbumTranslationService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return VehicleNewAlbumTranslationModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
// '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,
// ]
// ];
// }
];
}
}
@@ -0,0 +1,66 @@
<?php
namespace app\console\service;
use app\console\model\VehicleNewAskedModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class VehicleNewAskedService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return VehicleNewAskedModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
// '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,
// ]
// ];
// }
];
}
// 声明需要多语言翻译
protected array $translatable = [
'title',
'content',
];
}
@@ -0,0 +1,59 @@
<?php
namespace app\console\service;
use app\console\model\VehicleNewAskedTranslationModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class VehicleNewAskedTranslationService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return VehicleNewAskedTranslationModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
// '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,
// ]
// ];
// }
];
}
}
@@ -0,0 +1,64 @@
<?php
namespace app\console\service;
use app\console\model\VehicleNewContentModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class VehicleNewContentService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return VehicleNewContentModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
// '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,
// ]
// ];
// }
];
}
// 声明需要多语言翻译
protected array $translatable = [
'content',
];
}
@@ -0,0 +1,59 @@
<?php
namespace app\console\service;
use app\console\model\VehicleNewContentTranslationModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class VehicleNewContentTranslationService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return VehicleNewContentTranslationModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
// '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,
// ]
// ];
// }
];
}
}
+434
View File
@@ -0,0 +1,434 @@
<?php
namespace app\console\service;
use app\console\model\VehicleNewAlbumModel;
use app\console\model\VehicleNewAskedModel;
use app\console\model\VehicleNewContentModel;
use app\console\model\VehicleNewModel;
use app\console\model\VehicleNewTraitModel;
use app\mzl\common\services\BaseService;
use app\mzl\modules\translation\service\TranslationService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use support\Db;
use support\exception\BusinessException;
class VehicleNewService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return VehicleNewModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
'default' => function ($params) {
$keyword = $params['keyword'] ?? '';
return [
'filters' => $params['filters'],
'fields' => ['id', 'vin', 'brand', 'cover', 'price', 'type', 'energy', 'model', 'video', 'views', 'created_at', 'updated_at'],
'relations' => [
'translation' => ['vehicle_id', 'locale', 'name', 'condition_desc', 'location'],
'asked' => ['id', 'vehicle_id', 'status', 'sort'],
'asked.translation' => ['id', 'asked_id', 'locale', 'title', 'content'],
'trait' => ['id', 'vehicle_id', 'status', 'sort'],
'trait.translation' => ['id', 'trait_id', 'locale', 'title', 'content'],
'album' => ['id', 'vehicle_id', 'pic', 'status', 'sort'],
'album.translation' => ['id', 'album_id', 'locale', 'alt'],
'content' => ['id', 'vehicle_id', 'status', 'sort'],
'content.translation' => ['id', 'content_id', 'locale', 'content'],
'brand.translation' => ['brand_id', 'name'],
'types.translation' => ['type_id', 'name'],
'energy.translation' => ['energy_id', 'name'],
'models.translation' => ['model_id', 'name'],
],
'sorts' => [
['field' => 'id', 'direction' => 'desc']
],
'pagination' => [
'page' => $params['page'] ?? 1,
'per_page' => $params['per_page'] ?? 10,
]
];
},
'show' => function ($params) {
$keyword = $params['keyword'] ?? '';
return [
'filters' => [
['field'=>'id', 'operator'=>'=', 'value'=> $params['id']],
],
'fields' => ['id', 'vin', 'brand', 'cover', 'price', 'type', 'energy', 'model', 'video', 'views', 'created_at', 'updated_at'],
'relations' => [
'translation' => ['vehicle_id', 'locale', 'name', 'condition_desc', 'location'],
'asked' => ['id', 'vehicle_id', 'status', 'sort'],
'asked.translation' => ['id', 'asked_id', 'locale', 'title', 'content'],
'trait' => ['id', 'vehicle_id', 'status', 'sort'],
'trait.translation' => ['id', 'trait_id', 'locale', 'title', 'content'],
'album' => ['id', 'vehicle_id', 'pic', 'status', 'sort'],
'album.translation' => ['id', 'album_id', 'locale', 'alt'],
'content' => ['id', 'vehicle_id', 'status', 'sort'],
'content.translation' => ['id', 'content_id', 'locale', 'content'],
'brand.translation' => ['brand_id', 'name'],
'types.translation' => ['type_id', 'name'],
'energy.translation' => ['energy_id', 'name'],
'models.translation' => ['model_id', 'name'],
],
'sorts' => [
['field' => 'id', 'direction' => 'desc']
],
'pagination' => [
'page' => $params['page'] ?? 1,
'per_page' => $params['per_page'] ?? 10,
]
];
}
];
}
// 声明需要多语言翻译
protected array $translatable = [
'condition_desc','name',
];
public function add(array $params)
{
$sourceLocale = locale();
$sourceFields = $this->extractSourceFields($params);
if (empty($sourceFields)) {
throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段");
};
$askeds = $params['asked'];
unset($params['asked']);
$traits = $params['trait'];
unset($params['trait']);
$albums = $params['album'];
unset($params['album']);
$contents = $params['content'];
unset($params['content']);
$VehicleNewAskedService = new VehicleNewAskedService(new VehicleNewAskedModel());
$VehicleNewTraitService = new VehicleNewTraitService(new VehicleNewTraitModel());
$VehicleNewAlbumService = new VehicleNewAlbumService(new VehicleNewAlbumModel());
$VehicleNewContentService = new VehicleNewContentService(new VehicleNewContentModel());
Db::beginTransaction();
try {
if(empty($params['model'])){
$params['model'] = 0;
}
$info = $this->create($params);
if (!$info) {
throw new BusinessException('提交失败');
}
// 异步翻译并写入 hs_vehicle_data_translation 表
(new TranslationService())->queueTranslations(
'vehicleNew',
$info->id,
$sourceLocale,
$sourceFields
);
foreach ($askeds as $asked) {
$askedsourceFields = $VehicleNewAskedService->extractSourceFields($asked);
$asked['vehicle_id'] = $info->id;
$asked_res = $VehicleNewAskedService->create($asked);
if (!$asked_res) {
throw new BusinessException('提交失败');
}
// $askedsourceFields['asked_id'] = $asked_res->id;
(new TranslationService())->queueTranslations(
'vehicleNewAsked',
$asked_res->id,
$sourceLocale,
$askedsourceFields
);
}
foreach ($traits as $trait) {
$traitsourceFields = $VehicleNewTraitService->extractSourceFields($trait);
$trait['vehicle_id'] = $info->id;
$trait_res = $VehicleNewTraitService->create($trait);
if (!$trait_res) {
throw new BusinessException('提交失败');
}
(new TranslationService())->queueTranslations(
'vehicleNewTrait',
$trait_res->id,
$sourceLocale,
$traitsourceFields
);
}
foreach ($albums as $album) {
$albumsourceFields = $VehicleNewAlbumService->extractSourceFields($album);
$album['vehicle_id'] = $info->id;
$album_res = $VehicleNewAlbumService->create($album);
if (!$album_res) {
throw new BusinessException('提交失败');
}
(new TranslationService())->queueTranslations(
'vehicleNewAlbum',
$album_res->id,
$sourceLocale,
$albumsourceFields
);
}
foreach ($contents as $content) {
$contentsourceFields = $VehicleNewContentService->extractSourceFields($content);
$content['vehicle_id'] = $info->id;
$content_res = $VehicleNewContentService->create($content);
if (!$content_res) {
throw new BusinessException('提交失败');
}
(new TranslationService())->queueTranslations(
'vehicleNewContent',
$content_res->id,
$sourceLocale,
$contentsourceFields
);
}
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} 语言的翻译字段");
};
$askeds = $params['asked'];
unset($params['asked']);
$traits = $params['trait'];
unset($params['trait']);
$albums = $params['album'];
unset($params['album']);
$contents = $params['content'];
unset($params['content']);
$VehicleNewAskedService = new VehicleNewAskedService(new VehicleNewAskedModel());
$VehicleNewTraitService = new VehicleNewTraitService(new VehicleNewTraitModel());
$VehicleNewAlbumService = new VehicleNewAlbumService(new VehicleNewAlbumModel());
$VehicleNewContentService = new VehicleNewContentService(new VehicleNewContentModel());
// foreach ($params as $ko => $vo) {
// if (in_array($ko, $albums)) {
// $album[$ko] = $vo ?? '';
// } elseif (in_array($ko, $albums)) {
// $album[$ko] = $vo ?? '';
// } elseif (in_array($ko, $albums)) {
// $album[$ko] = $vo ?? '';
// } elseif (in_array($ko, $albums)) {
// $album[$ko] = $vo ?? '';
// } elseif (in_array($ko, $albums)) {
// $album[$ko] = $vo ?? '';
// } elseif (in_array($ko, VehicleNewModel::TRANS_FIELDS)) {
// $trans[$ko] = $vo ?? '';
// }else{
// $data[$ko] = $vo ?? '';
// }
// }
Db::beginTransaction();
try {
if(empty($params['model'])){
$params['model'] = 0;
}
$info = $this->update($id, $params);
if (!$info) {
throw new BusinessException('提交失败');
}
// 异步翻译并写入 hs_vehicle_data_translation 表
(new TranslationService())->queueTranslations(
'vehicleNew',
$id,
$sourceLocale,
$sourceFields
);
$askedsId = [];
foreach ($askeds as $asked) {
$askedsourceFields = $VehicleNewAskedService->extractSourceFields($asked);
$asked['vehicle_id'] = $id;
if(!empty($asked['id'])){
$askedId = $asked['id'];
$askedsId[] = $askedId;
$asked_res = $VehicleNewAskedService->update($askedId, $asked);
if (!$asked_res) {
throw new BusinessException('提交失败:常见问题2');
}
} else {
unset($asked['id']);
$asked_res = $VehicleNewAskedService->create($asked);
if (!$asked_res) {
throw new BusinessException('提交失败:常见问题1');
}
$askedId = $asked_res->id;
$askedsId[] = $askedId;
}
(new TranslationService())->queueTranslations(
'vehicleNewAsked',
$askedId,
$sourceLocale,
$askedsourceFields
);
}
VehicleNewAskedModel::whereNotIn('id', $askedsId)->where('vehicle_id',$id)->delete();
$traitsId = [];
foreach ($traits as $trait) {
$traitsourceFields = $VehicleNewTraitService->extractSourceFields($trait);
$trait['vehicle_id'] = $id;
if(!empty($trait['id'])){
$traitId = $trait['id'];
$traitsId[] = $traitId;
$trait_res = $VehicleNewTraitService->update($traitId, $trait);
if (!$trait_res) {
throw new BusinessException('提交失败:车辆特性');
}
} else {
unset($trait['id']);
$trait_res = $VehicleNewTraitService->create($trait);
if (!$trait_res) {
throw new BusinessException('提交失败:车辆特性');
}
$traitId = $trait_res->id;
$traitsId[] = $traitId;
}
(new TranslationService())->queueTranslations(
'vehicleNewTrait',
$traitId,
$sourceLocale,
$traitsourceFields
);
}
VehicleNewTraitModel::whereNotIn('id', $traitsId)->where('vehicle_id',$id)->delete();
$albumsId = [];
foreach ($albums as $album) {
$albumsourceFields = $VehicleNewAlbumService->extractSourceFields($album);
$album['vehicle_id'] = $id;
// $albumId = $album['id'];
// $album_res = $VehicleNewAskedService->update($albumId, $album);
// if (!$album_res) {
// throw new BusinessException('提交失败');
// }
if(!empty($album['id'])){
$albumId =$album['id'];
$albumsId[] =$albumId;
$album_res = $VehicleNewAlbumService->update($albumId,$album);
if (!$album_res) {
throw new BusinessException('提交失败:车辆图册');
}
} else {
unset($album['id']);
$album_res = $VehicleNewAlbumService->create($album);
if (!$album_res) {
throw new BusinessException('提交失败:车辆图册1');
}
$albumId = $album_res->id;
$albumsId[] = $albumId;
}
(new TranslationService())->queueTranslations(
'vehicleNewAlbum',
$albumId,
$sourceLocale,
$albumsourceFields
);
}
VehicleNewAlbumModel::whereNotIn('id', $albumsId)->where('vehicle_id',$id)->delete();
$contentsId = [];
foreach ($contents as $content) {
$contentsourceFields = $VehicleNewContentService->extractSourceFields($content);
$content['vehicle_id'] = $id;
// $contentId = $content['id'];
// $trait_res = $VehicleNewContentService->update($contentId, $content);
// if (!$trait_res) {
// throw new BusinessException('提交失败');
// }
if(!empty($content['id'])){
$contentId =$content['id'];
$contentsId[] =$contentId;
$content_res = $VehicleNewContentService->update($contentId,$content);
if (!$content_res) {
throw new BusinessException('提交失败:车辆介绍1');
}
} else {
unset($content['id']);
$content_res = $VehicleNewContentService->create($content);
if (!$content_res) {
throw new BusinessException('提交失败:车辆介绍');
}
$contentId = $content_res->id;
$contentsId[] = $contentId;
}
(new TranslationService())->queueTranslations(
'vehicleNewContent',
$contentId,
$sourceLocale,
$contentsourceFields
);
}
VehicleNewContentModel::whereNotIn('id', $contentsId)->where('vehicle_id',$id)->delete();
// if ($album) {
// // $album['vehicle_data_id'] = $info->id;
// $albumModel = new VehicleAlbumModel();
// if (!$albumModel->updateOrCreate(
// ['vehicle_data_id' => $id],
// $album
// )) {
// throw new BusinessException('提交失败');
// }
// }
// $applyModel = new VehicleApplyModel();
// if (!$applyModel->create([
// 'vehicle_data_id' => $info->id,
// 'desc' => '提交审核'
// ])) {
// throw new BusinessException('提交失败');
// }
// if (isset($params['draft_id']) && $params['draft_id']) {
// if (!DraftsModel::where('id', $params['draft_id'])->delete()) {
// throw new BusinessException('草稿移除失败');
// }
// }
Db::commit();
return true;
} catch (\Throwable $th) {
Db::rollBack();
throw $th;
}
}
}
@@ -0,0 +1,65 @@
<?php
namespace app\console\service;
use app\console\model\VehicleNewTraitModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class VehicleNewTraitService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return VehicleNewTraitModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
// '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,
// ]
// ];
// }
];
}
// 声明需要多语言翻译
protected array $translatable = [
'title',
'content',
];
}
@@ -0,0 +1,65 @@
<?php
namespace app\console\service;
use app\console\model\VehicleNewTraitTranslationModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class VehicleNewTraitTranslationService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return VehicleNewTraitTranslationModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
// '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,
// ]
// ];
// }
];
}
// 声明需要多语言翻译
protected array $translatable = [
'title',
'content',
];
}
@@ -0,0 +1,59 @@
<?php
namespace app\console\service;
use app\console\model\VehicleNewTranslationModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class VehicleNewTranslationService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return VehicleNewTranslationModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
// '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,
// ]
// ];
// }
];
}
}