110 lines
4.9 KiB
PHP
110 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace app\apis\service;
|
|
|
|
use app\apis\model\EnergyModel;
|
|
use app\apis\model\EnergyTranslationModel;
|
|
use app\apis\model\VehicleNewModel;
|
|
use app\mzl\common\services\BaseService;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
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,
|
|
]
|
|
];
|
|
}
|
|
];
|
|
}
|
|
|
|
public static function enegry($params)
|
|
{
|
|
$locale = locale();
|
|
$energyId = VehicleNewModel::select('energy')->where(['brand'=>$params['brand']])->groupBy('energy')->get()->pluck('energy');
|
|
$energys = EnergyModel::whereIn('id',$energyId)->get()->toArray();
|
|
foreach ($energys as $key => $energy) {
|
|
$translation = EnergyTranslationModel::where(['locale'=>$locale,'energy_id'=>$energy['id']])->first()->toArray();
|
|
$energys[$key]['name'] = isset($translation['name'])?$translation['name']:'';
|
|
}
|
|
return $energys;
|
|
}
|
|
|
|
// 声明需要多语言翻译
|
|
protected array $translatable = [
|
|
'condition_desc','name',
|
|
];
|
|
} |