129 lines
3.4 KiB
PHP
129 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace app\admin\service;
|
|
|
|
use app\admin\validate\SysPortValidate;
|
|
use app\common\traits\crud\CrudServiceTrait;
|
|
use app\common\model\SysPort;
|
|
use app\common\utils\translation\service\TranslationService;
|
|
use app\queue\utils\Translat;
|
|
use support\Request;
|
|
|
|
class SysPortService
|
|
{
|
|
|
|
use CrudServiceTrait;
|
|
|
|
/**
|
|
* 指定当前要操作的模型
|
|
*/
|
|
protected string $modelClass = SysPort::class;
|
|
|
|
/**
|
|
* 验证器
|
|
*/
|
|
protected $validator = SysPortValidate::class;
|
|
|
|
public $translatFields = ['port_name','description'];
|
|
|
|
/**
|
|
* 获取验证器
|
|
*/
|
|
public function getValidator()
|
|
{
|
|
return new $this->validator;
|
|
}
|
|
|
|
/**
|
|
* 构建查询条件
|
|
*/
|
|
|
|
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
|
|
{
|
|
$p = $request->all();
|
|
|
|
$config = [];
|
|
$config['model'] = $this->modelClass;
|
|
switch ($scene) {
|
|
case 'index':
|
|
// ====== ====== 排序 ====== ======
|
|
$config['order']['sort'] = 'desc';
|
|
$config['order']['id'] = 'desc';
|
|
|
|
// ====== ====== 筛选 ====== ======
|
|
// 状态
|
|
if (isset($p['status']) && in_array($p['status'], [0,1])) {
|
|
$config['where']['status'] = $p['status'];
|
|
}
|
|
// 关键字
|
|
if (isset($p['keyword']) && $p['keyword']) {
|
|
$k = '%' . $p['keyword'] . '%';
|
|
foreach (['port_code','port_name'] as $col) {
|
|
$config['orWhere'][] = [$col, 'like', $k];
|
|
}
|
|
}
|
|
// 场景
|
|
if (isset($p['scene']) && $p['scene']) {
|
|
if($p['country_id'] == 'domestic'){
|
|
$config['where']['country_id'] = 45;
|
|
}else{
|
|
$config['where']['country_id'] = ['<>', 45];
|
|
}
|
|
}
|
|
//$config['where']['status'] = 1;
|
|
break;
|
|
case 'show':
|
|
$config['where']['id'] = $p['id'];
|
|
//$config['where']['status'] = 1;
|
|
break;
|
|
case 'store':
|
|
break;
|
|
case 'update':
|
|
$config['where']['id'] = $p['id'];
|
|
break;
|
|
case 'destroy':
|
|
$config['where']['id'] = $p['id'];
|
|
break;
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
|
|
/**
|
|
* 添加前置处理
|
|
*/
|
|
public function beforeServiceStore($data, Request $request)
|
|
{
|
|
if(empty($data['lat']))
|
|
unset($data['lat']);
|
|
if(empty($data['lng']))
|
|
unset($data['lng']);
|
|
return $data;
|
|
}
|
|
|
|
public function afterServiceStore($res)
|
|
{
|
|
$sourceLocale = locale();
|
|
$data = $res->toArray();
|
|
$traitsourceFields = Translat::extractSourceFields($data, $this->translatFields);
|
|
|
|
(new TranslationService())->queueTranslations(
|
|
'sys_port_translation',
|
|
$res['id'],
|
|
$sourceLocale,
|
|
$traitsourceFields
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 添加前置处理
|
|
*/
|
|
public function beforeServiceUpdate($data, Request $request)
|
|
{
|
|
if(empty($data['lat']))
|
|
unset($data['lat']);
|
|
if(empty($data['lng']))
|
|
unset($data['lng']);
|
|
return $data;
|
|
}
|
|
} |