2025-07-08 09:59:28 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\console\service;
|
|
|
|
|
|
|
|
|
|
use app\console\model\AdModel;
|
|
|
|
|
use app\console\model\AdTranslationModel;
|
|
|
|
|
use app\console\model\ModelTranslationModel;
|
|
|
|
|
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 AdService extends BaseService
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 定义模型
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getModel(): string
|
|
|
|
|
{
|
|
|
|
|
return AdModel::class;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 多场景配置
|
|
|
|
|
*/
|
|
|
|
|
public function queryScenes(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'default' => function ($params) {
|
|
|
|
|
return [
|
|
|
|
|
'filters' => $params['filters'],
|
2025-07-09 16:53:45 +08:00
|
|
|
'fields' => ['id', 'adp_id', 'type', 'img', 'price', 'status', 'sort', 'creater', 'created_at', 'updated_at'],
|
2025-07-08 09:59:28 +08:00
|
|
|
'relations' => [
|
|
|
|
|
'translation' => ['ad_id', 'alt', 'title'],
|
|
|
|
|
],
|
|
|
|
|
'sorts' => [
|
|
|
|
|
['field' => 'sort', 'direction' => 'desc'],
|
|
|
|
|
['field' => 'created_at', 'direction' => 'desc'],
|
|
|
|
|
],
|
|
|
|
|
'pagination' => [
|
|
|
|
|
'page' => $params['page'] ?? 1,
|
|
|
|
|
'per_page' => $params['per_page'] ?? 8,
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
},
|
2025-07-08 15:38:43 +08:00
|
|
|
'show' => function ($params) {
|
|
|
|
|
return [
|
|
|
|
|
'filters' => $params['filters'],
|
2025-07-09 16:53:45 +08:00
|
|
|
'fields' => ['id', 'adp_id', 'type', 'img', 'price', 'status', 'sort', 'creater', 'created_at', 'updated_at'],
|
2025-07-08 15:38:43 +08:00
|
|
|
'relations' => [
|
|
|
|
|
'translation' => ['ad_id', 'alt', 'title'],
|
|
|
|
|
],
|
|
|
|
|
'sorts' => [
|
|
|
|
|
['field' => 'sort', 'direction' => 'desc'],
|
|
|
|
|
['field' => 'created_at', 'direction' => 'desc'],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
},
|
2025-07-08 09:59:28 +08:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected array $translatable = [
|
|
|
|
|
'alt', 'title'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
public function add(array $params)
|
|
|
|
|
{
|
|
|
|
|
$sourceLocale = locale();
|
|
|
|
|
$sourceFields = $this->extractSourceFields($params);
|
|
|
|
|
if (empty($sourceFields)) {
|
|
|
|
|
throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Db::beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
$info = $this->create($params);
|
|
|
|
|
if (!$info) {
|
|
|
|
|
throw new BusinessException('提交失败');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 异步翻译并写入 hs_ad_translation 表
|
|
|
|
|
(new TranslationService())->queueTranslations(
|
|
|
|
|
'ad',
|
|
|
|
|
$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_ad_translation 表
|
|
|
|
|
AdTranslationModel::updateOrCreate(
|
|
|
|
|
[
|
|
|
|
|
'ad_id' => $id,
|
|
|
|
|
'locale' => $sourceLocale,
|
|
|
|
|
],
|
|
|
|
|
$sourceFields
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Db::commit();
|
|
|
|
|
return true;
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
Db::rollBack();
|
|
|
|
|
throw $th;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|