Files
useds/app/common/service/UsedCarService.php
T

95 lines
2.5 KiB
PHP
Raw Normal View History

2026-01-05 15:28:19 +08:00
<?php
namespace app\common\service;
use app\common\validate\UsedCarValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\UsedCar;
2026-01-05 16:15:27 +08:00
use support\exception\BusinessException;
use support\Request;
2026-01-05 15:28:19 +08:00
class UsedCarService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = UsedCar::class;
/**
* 验证器
*/
protected $validator = UsedCarValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
2026-01-05 16:15:27 +08:00
2026-01-05 17:30:53 +08:00
/**
* 构造 CrudUtil 查询配置
*/
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
{
$p = $request->all();
$config = [];
$config['model'] = $this->modelClass;
switch ($scene) {
case 'default':
$config['where']['status'] = 1;
$config['order']['sort'] = 'desc';
$config['order']['id'] = 'desc';
break;
case 'index':
$config['where']['status'] = 1;
if (isset($p['scene']) && $p['scene'] == 'self') {
$config['where']['user_id'] = $request->user['id'];
}
// 关键字
if (isset($p['keyword']) && $p['keyword']) {
$k = '%' . $p['keyword'] . '%';
foreach (['product_name'] as $col) {
$config['orWhere'][] = [$col, 'like', $k];
}
}
$config['order']['sort'] = 'desc';
$config['order']['id'] = 'desc';
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;
}
2026-01-05 16:15:27 +08:00
public function beforeServiceStore($data, Request $request): array
{
if(empty($data['product_name'])){
throw new BusinessException('产品名称不能为空');
}
if(empty($data['images'])){
throw new BusinessException('产品图片不能为空');
}
2026-01-05 17:30:53 +08:00
$data['user_id'] = $request->user['id'];
2026-01-05 16:15:27 +08:00
return $data;
}
2026-01-05 15:28:19 +08:00
}