193 lines
5.9 KiB
PHP
193 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace app\applet\service;
|
|
|
|
use app\applet\model\DraftsAlbumModel;
|
|
use app\applet\model\DraftsDataModel;
|
|
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 DraftsDataService extends BaseService
|
|
{
|
|
/**
|
|
* 定义模型
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getModel(): string
|
|
{
|
|
return DraftsDataModel::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 = [
|
|
'name',
|
|
'condition_desc',
|
|
// 'location'
|
|
];
|
|
|
|
/**
|
|
* 从$data中提取出源文本字段,并从$data里删除它们
|
|
*/
|
|
protected function extractSourceFields(array &$data): array
|
|
{
|
|
$source = [];
|
|
foreach ($this->translatable as $field) {
|
|
if (array_key_exists($field, $data)) {
|
|
$source[$field] = $data[$field];
|
|
unset($data[$field]);
|
|
}
|
|
}
|
|
return $source;
|
|
}
|
|
|
|
public function add(array $params)
|
|
{
|
|
$sourceLocale = locale();
|
|
$sourceFields = $this->extractSourceFields($params);
|
|
if (empty($sourceFields)) {
|
|
throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段");
|
|
};
|
|
|
|
$albums = DraftsAlbumModel::VEHICLE_ALBUM_FIELDS;
|
|
foreach ($params as $ko => $vo) {
|
|
if (in_array($ko, $albums)) {
|
|
$album[$ko] = $vo ?? '';
|
|
} else {
|
|
if (in_array($ko, DraftsDataModel::TRANS_FIELDS)) {
|
|
$trans[$ko] = $vo ?? '';
|
|
} else {
|
|
$data[$ko] = $vo ?? '';
|
|
}
|
|
}
|
|
}
|
|
|
|
Db::beginTransaction();
|
|
try {
|
|
$info = $this->create($data);
|
|
if (!$info) {
|
|
throw new BusinessException('提交失败');
|
|
}
|
|
|
|
// 异步翻译并写入 hs_drafts_translation 表
|
|
(new TranslationService())->queueTranslations(
|
|
'drafts',
|
|
$info->id,
|
|
$sourceLocale,
|
|
$sourceFields
|
|
);
|
|
|
|
$album['drafts_data_id'] = $info->id;
|
|
$albumModel = new DraftsAlbumModel();
|
|
if (!$albumModel->create($album)) {
|
|
throw new BusinessException('保存失败');
|
|
}
|
|
|
|
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} 语言的翻译字段");
|
|
};
|
|
|
|
$album = [];
|
|
$trans = [];
|
|
$albums = DraftsAlbumModel::VEHICLE_ALBUM_FIELDS;
|
|
foreach ($params as $ko => $vo) {
|
|
if (in_array($ko, $albums)) {
|
|
$album[$ko] = $vo ?? '';
|
|
} else {
|
|
if (in_array($ko, DraftsAlbumModel::TRANS_FIELDS)) {
|
|
$trans[$ko] = $vo ?? '';
|
|
} else {
|
|
$data[$ko] = $vo ?? '';
|
|
}
|
|
}
|
|
}
|
|
|
|
Db::beginTransaction();
|
|
try {
|
|
$params['info_status'] = 0;
|
|
$info = $this->update($id, $params);
|
|
if (!$info) {
|
|
throw new BusinessException('提交失败');
|
|
}
|
|
|
|
// 异步翻译并写入 hs_vehicle_data_translation 表
|
|
(new TranslationService())->queueTranslations(
|
|
'vehicle',
|
|
$id,
|
|
$sourceLocale,
|
|
$sourceFields
|
|
);
|
|
|
|
if ($album) {
|
|
// $album['vehicle_data_id'] = $info->id;
|
|
$albumModel = new DraftsAlbumModel();
|
|
if (!$albumModel->updateOrCreate(
|
|
['vehicle_data_id' => $id],
|
|
$album
|
|
)) {
|
|
throw new BusinessException('提交失败');
|
|
}
|
|
}
|
|
|
|
Db::commit();
|
|
return true;
|
|
} catch (\Throwable $th) {
|
|
Db::rollBack();
|
|
throw $th;
|
|
}
|
|
}
|
|
} |