修改时不翻译
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace app\console\service;
|
||||
|
||||
use app\console\model\ModelModel;
|
||||
use app\console\model\ModelTranslationModel;
|
||||
use app\mzl\common\services\BaseService;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -122,6 +123,13 @@ class ModelService extends BaseService
|
||||
$sourceLocale,
|
||||
$sourceFields
|
||||
);
|
||||
ModelTranslationModel::updateOrCreate(
|
||||
[
|
||||
'model_id' => $id,
|
||||
'locale' => $sourceLocale,
|
||||
],
|
||||
$sourceFields
|
||||
);
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
|
||||
@@ -6,6 +6,7 @@ use app\console\model\DraftsModel;
|
||||
use app\console\model\VehicleApplyModel;
|
||||
use app\apis\model\VehicleAlbumModel;
|
||||
use app\console\model\VehicleDataModel;
|
||||
use app\console\model\VehicleDataTranslationModel;
|
||||
use app\mzl\common\org\XunfeiFanyi;
|
||||
use app\mzl\common\services\BaseService;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
@@ -243,10 +244,17 @@ class VehicleDataService extends BaseService
|
||||
}
|
||||
|
||||
// 异步翻译并写入 hs_vehicle_data_translation 表
|
||||
(new TranslationService())->queueTranslations(
|
||||
'vehicle',
|
||||
$id,
|
||||
$sourceLocale,
|
||||
// (new TranslationService())->queueTranslations(
|
||||
// 'vehicle',
|
||||
// $id,
|
||||
// $sourceLocale,
|
||||
// $sourceFields
|
||||
// );
|
||||
VehicleDataTranslationModel::updateOrCreate(
|
||||
[
|
||||
'vehicle_id' => $id,
|
||||
'locale' => $sourceLocale,
|
||||
],
|
||||
$sourceFields
|
||||
);
|
||||
|
||||
|
||||
@@ -3,10 +3,15 @@
|
||||
namespace app\console\service;
|
||||
|
||||
use app\console\model\VehicleNewAlbumModel;
|
||||
use app\console\model\VehicleNewAlbumTranslationModel;
|
||||
use app\console\model\VehicleNewAskedModel;
|
||||
use app\console\model\VehicleNewAskedTranslationModel;
|
||||
use app\console\model\VehicleNewContentModel;
|
||||
use app\console\model\VehicleNewContentTranslationModel;
|
||||
use app\console\model\VehicleNewModel;
|
||||
use app\console\model\VehicleNewTraitModel;
|
||||
use app\console\model\VehicleNewTraitTranslationModel;
|
||||
use app\console\model\VehicleNewTranslationModel;
|
||||
use app\mzl\common\services\BaseService;
|
||||
use app\mzl\modules\translation\service\TranslationService;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
@@ -265,10 +270,17 @@ class VehicleNewService extends BaseService
|
||||
}
|
||||
|
||||
// 异步翻译并写入 hs_vehicle_data_translation 表
|
||||
(new TranslationService())->queueTranslations(
|
||||
'vehicleNew',
|
||||
$id,
|
||||
$sourceLocale,
|
||||
// (new TranslationService())->queueTranslations(
|
||||
// 'vehicleNew',
|
||||
// $id,
|
||||
// $sourceLocale,
|
||||
// $sourceFields
|
||||
// );
|
||||
VehicleNewTranslationModel::updateOrCreate(
|
||||
[
|
||||
'vehicle_id' => $id,
|
||||
'locale' => $sourceLocale,
|
||||
],
|
||||
$sourceFields
|
||||
);
|
||||
|
||||
@@ -292,10 +304,17 @@ class VehicleNewService extends BaseService
|
||||
$askedId = $asked_res->id;
|
||||
$askedsId[] = $askedId;
|
||||
}
|
||||
(new TranslationService())->queueTranslations(
|
||||
'vehicleNewAsked',
|
||||
$askedId,
|
||||
$sourceLocale,
|
||||
// (new TranslationService())->queueTranslations(
|
||||
// 'vehicleNewAsked',
|
||||
// $askedId,
|
||||
// $sourceLocale,
|
||||
// $askedsourceFields
|
||||
// );
|
||||
VehicleNewAskedTranslationModel::updateOrCreate(
|
||||
[
|
||||
'asked_id' => $askedId,
|
||||
'locale' => $sourceLocale,
|
||||
],
|
||||
$askedsourceFields
|
||||
);
|
||||
}
|
||||
@@ -321,10 +340,17 @@ class VehicleNewService extends BaseService
|
||||
$traitId = $trait_res->id;
|
||||
$traitsId[] = $traitId;
|
||||
}
|
||||
(new TranslationService())->queueTranslations(
|
||||
'vehicleNewTrait',
|
||||
$traitId,
|
||||
$sourceLocale,
|
||||
// (new TranslationService())->queueTranslations(
|
||||
// 'vehicleNewTrait',
|
||||
// $traitId,
|
||||
// $sourceLocale,
|
||||
// $traitsourceFields
|
||||
// );
|
||||
VehicleNewTraitTranslationModel::updateOrCreate(
|
||||
[
|
||||
'trait_id' => $traitId,
|
||||
'locale' => $sourceLocale,
|
||||
],
|
||||
$traitsourceFields
|
||||
);
|
||||
}
|
||||
@@ -357,10 +383,17 @@ class VehicleNewService extends BaseService
|
||||
$albumsId[] = $albumId;
|
||||
}
|
||||
|
||||
(new TranslationService())->queueTranslations(
|
||||
'vehicleNewAlbum',
|
||||
$albumId,
|
||||
$sourceLocale,
|
||||
// (new TranslationService())->queueTranslations(
|
||||
// 'vehicleNewAlbum',
|
||||
// $albumId,
|
||||
// $sourceLocale,
|
||||
// $albumsourceFields
|
||||
// );
|
||||
VehicleNewAlbumTranslationModel::updateOrCreate(
|
||||
[
|
||||
'album_id' => $albumId,
|
||||
'locale' => $sourceLocale,
|
||||
],
|
||||
$albumsourceFields
|
||||
);
|
||||
}
|
||||
@@ -393,10 +426,17 @@ class VehicleNewService extends BaseService
|
||||
$contentsId[] = $contentId;
|
||||
}
|
||||
|
||||
(new TranslationService())->queueTranslations(
|
||||
'vehicleNewContent',
|
||||
$contentId,
|
||||
$sourceLocale,
|
||||
// (new TranslationService())->queueTranslations(
|
||||
// 'vehicleNewContent',
|
||||
// $contentId,
|
||||
// $sourceLocale,
|
||||
// $contentsourceFields
|
||||
// );
|
||||
VehicleNewContentTranslationModel::updateOrCreate(
|
||||
[
|
||||
'content_id' => $contentId,
|
||||
'locale' => $sourceLocale,
|
||||
],
|
||||
$contentsourceFields
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
<?php
|
||||
|
||||
namespace app\console\service;
|
||||
|
||||
use app\console\model\VehicleNewAttributeValuesModel;
|
||||
use app\console\model\VehicleNewSkuAttributesModel;
|
||||
use app\console\model\VehicleNewSkusModel;
|
||||
use app\console\model\VehicleNewSkusTranslationModel;
|
||||
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 VehicleNewSkusService extends BaseService
|
||||
{
|
||||
/**
|
||||
* 定义模型
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getModel(): string
|
||||
{
|
||||
return VehicleNewSkusModel::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* 多场景配置
|
||||
*/
|
||||
public function queryScenes(): array
|
||||
{
|
||||
return [
|
||||
'default' => function ($params) {
|
||||
return [
|
||||
'filters' => $params['filters'],
|
||||
'fields' => ['id','vehicle_id','status'],
|
||||
'relations' => [
|
||||
'translation' => ['sku_id', 'name', 'locale'],
|
||||
'skuAttributes' => ['id','sku_id','attribute_id','value_id','img',],
|
||||
'skuAttributes.attribute' => ['id', 'input_type', 'is_img', 'status'],
|
||||
'skuAttributes.value' => ['id','color_code'],
|
||||
],
|
||||
'sorts' => [
|
||||
['field' => 'created_at', 'direction' => 'desc']
|
||||
],
|
||||
'pagination' => [
|
||||
'page' => $params['page'] ?? 1,
|
||||
'per_page' => $params['per_page'] ?? 10,
|
||||
]
|
||||
];
|
||||
},
|
||||
'show' => function ($params) {
|
||||
return [
|
||||
'filters' => [
|
||||
['field'=>'id', 'operator'=>'=', 'value'=> $params['id']],
|
||||
],
|
||||
'fields' => ['id','vehicle_id', 'status'],
|
||||
'relations' => [
|
||||
'skuAttributes' => ['id','sku_id','attribute_id','value_id','img',],
|
||||
'skuAttributes.attribute' => ['id', 'input_type', 'is_img', 'status'],
|
||||
'skuAttributes.value' => ['id','color_code'],
|
||||
'translation' => ['sku_id', 'name', 'locale'],
|
||||
],
|
||||
];
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
// 声明需要多语言翻译
|
||||
protected array $translatable = [
|
||||
'name',
|
||||
];
|
||||
|
||||
public function add(array $params)
|
||||
{
|
||||
$sourceLocale = locale();
|
||||
$sourceFields = $this->extractSourceFields($params);
|
||||
|
||||
if (empty($sourceFields)) {
|
||||
throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段");
|
||||
};
|
||||
|
||||
$skuAttributes = $params['skuAttributes'];
|
||||
unset($params['skuAttributes']);
|
||||
$VehicleNewSkuAttributesService = new VehicleNewSkuAttributesService(new VehicleNewSkuAttributesModel());
|
||||
Db::beginTransaction();
|
||||
try {
|
||||
$sku_code = sprintf("%05d", $params['vehicle_id']);
|
||||
$params['sku_code'] = rand(100,999).$sku_code[0].' '.substr($sku_code,1).' '.sprintf("%04d", $skuAttributes[0]['attribute_id']).' '.sprintf("%04d", $skuAttributes[0]['value_id']);
|
||||
|
||||
$info = $this->create($params);
|
||||
if (!$info) {
|
||||
throw new BusinessException('提交失败');
|
||||
}
|
||||
|
||||
// 异步翻译并写入 hs_vehicle_new_skus_translation 表
|
||||
(new TranslationService())->queueTranslations(
|
||||
'vehicleNewSku',
|
||||
$info->id,
|
||||
$sourceLocale,
|
||||
$sourceFields
|
||||
);
|
||||
|
||||
foreach ($skuAttributes as $skuAttribute) {
|
||||
$skuAttribute['sku_id'] = $info->id;
|
||||
$skuAttribute_res = $VehicleNewSkuAttributesService->create($skuAttribute);
|
||||
if (!$skuAttribute_res) {
|
||||
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} 语言的翻译字段");
|
||||
};
|
||||
|
||||
$skuAttributes = $params['skuAttributes'];
|
||||
unset($params['skuAttributes']);
|
||||
$VehicleNewSkuAttributesService = new VehicleNewSkuAttributesService(new VehicleNewSkuAttributesModel());
|
||||
Db::beginTransaction();
|
||||
try {
|
||||
$info = $this->update($id, $params);
|
||||
if (!$info) {
|
||||
throw new BusinessException('提交失败');
|
||||
}
|
||||
|
||||
//异步翻译并写入 hs_vehicle_new_skus_translation 表
|
||||
// (new TranslationService())->queueTranslations(
|
||||
// 'vehicleNewSku',
|
||||
// $id,
|
||||
// $sourceLocale,
|
||||
// $sourceFields
|
||||
// );
|
||||
VehicleNewSkusTranslationModel::updateOrCreate(
|
||||
[
|
||||
'sku_id' => $id,
|
||||
'locale' => $sourceLocale,
|
||||
],
|
||||
$sourceFields
|
||||
);
|
||||
|
||||
|
||||
$skuAttributesId = [];
|
||||
foreach ($skuAttributes as $skuAttribute) {
|
||||
$skuAttribute['sku_id'] = $id;
|
||||
|
||||
$attribute_id = array_column($skuAttributes, 'attribute_id');
|
||||
if(count($attribute_id) !== count(array_unique($attribute_id))){
|
||||
Db::rollBack();
|
||||
throw new BusinessException('属性不能重复');
|
||||
}
|
||||
if(!empty($skuAttribute['id'])){
|
||||
$skuAttributeId = $skuAttribute['id'];
|
||||
$skuAttributesId[] = $skuAttributeId;
|
||||
$skuAttribute_res = $VehicleNewSkuAttributesService->update($skuAttributeId, $skuAttribute);
|
||||
if (!$skuAttribute_res) {
|
||||
throw new BusinessException('提交失败:属性值');
|
||||
}
|
||||
} else {
|
||||
unset($skuAttribute['id']);
|
||||
$skuAttribute_res = $VehicleNewSkuAttributesService->create($skuAttribute);
|
||||
if (!$skuAttribute_res) {
|
||||
throw new BusinessException('提交失败:属性值');
|
||||
}
|
||||
$skuAttributeId = $skuAttribute_res->id;
|
||||
$skuAttributesId[] = $skuAttributeId;
|
||||
}
|
||||
}
|
||||
VehicleNewSkuAttributesModel::whereNotIn('id', $skuAttributesId)->where('sku_id',$id)->delete();
|
||||
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Throwable $th) {
|
||||
Db::rollBack();
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user