You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
user_center/app/Http/Controllers/Api/CarModelController.php

238 lines
9.3 KiB

<?php
namespace App\Http\Controllers\Api;
use App\Http\Resources\CarModel;
use App\Http\Resources\CarModelCollection;
use App\Models\CarModelT;
use Illuminate\Http\Request;
/**
* @title 车型管理
* @description CarModelController
* @package App\Http\Controllers\Api
* @author zcstatham
*/
class CarModelController extends BaseController
{
protected $rules = [
'store' => [
'rules' => [
'car_series_id' => 'required|numeric',
'name' => 'required|string',
'year' => 'required|string',
'modelYears' => 'string',
'vehicle_type' => 'string',
'vehicle_size' => 'string',
'engine_model' => 'string',
'emission_standard' => 'string',
'displacement' => 'string',
'induction' => 'string',
'fuel_type' => 'string',
'cylinder_arrangement' => 'string',
'cylinders' => 'numeric',
'valves_per_cylinder' => 'numeric',
'transmission_type' => 'string',
'transmission_description' => 'string',
'gear_number' => 'string',
'front_tyre' => 'string',
'rear_tyre' => 'string',
'doors' => 'string',
'seats' => 'string',
'tuhu_code' => 'string',
'guidance_price' => 'string',
]
],
'show' => [
'rules' => [
'id' => 'bail|min:1',
],
'custom' => 'exists,App\Models\CarModelT,id'
],
'update' => [
'rules' => [
'id' => 'bail|min:1',
'car_series_id' => 'required|numeric',
'name' => 'required|string',
'year' => 'required|string',
'modelYears' => 'string',
'vehicle_type' => 'string',
'vehicle_size' => 'string',
'engine_model' => 'string',
'emission_standard' => 'string',
'displacement' => 'string',
'induction' => 'string',
'fuel_type' => 'string',
'cylinder_arrangement' => 'string',
'cylinders' => 'numeric',
'valves_per_cylinder' => 'numeric',
'transmission_type' => 'string',
'transmission_description' => 'string',
'gear_number' => 'string',
'front_tyre' => 'string',
'rear_tyre' => 'string',
'doors' => 'string',
'seats' => 'string',
'tuhu_code' => 'string',
'guidance_price' => 'string',
],
'custom' => [
'exists,App\Models\CarModelT,id',
]
],
'destroy' => [
'rules' => [
'id' => 'bail|min:1',
],
'custom' => [
'exists,App\Models\CarModelT,id',
]
]
];
public function __construct(Request $request)
{
$this->model = new CarModelT();
parent::__construct($request);
}
/**
* @title 车型列表
* @description 车型列表
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
*/
public function index()
{
$query = $this->model->query();
if(isset($this->params['name']) && $this->params['name'] != ''){
$query->where('name', $this->params['name']);
}
if(isset($this->params['year']) && $this->params['name'] != ''){
$query->where('name', $this->params['name']);
}
if(isset($this->params['modelYears']) && $this->params['modelYears'] != ''){
$query->whereRaw('FIND_IN_SET('. $this->params['modelYears'] .', modelYears)');
}
if(isset($this->params['displacement']) && $this->params['displacement'] != ''){
$query->where('displacement', $this->params['displacement']);
}
if(isset($this->params['transmission_type']) && $this->params['transmission_type'] != ''){
$query->where('transmission_type', $this->params['transmission_type']);
}
if(isset($this->params['transmission_description']) && $this->params['transmission_description'] != ''){
$query->where('transmission_description', $this->params['transmission_description']);
}
if(isset($this->params['car_series_id']) && $this->params['car_series_id'] > 0){
$query->where('car_series_id', $this->params['car_series_id']);
}
if(!isset($this->params['is_all']) || $this->params['is_all'] == 0){
$query->select(['id', 'car_series_id', 'name']);
}
if(isset($this->params['is_page']) && $this->params['is_page'] == 1){
return $this->success(new CarModelCollection($query->paginate()));
} else {
return $this->success($query->get());
}
}
/**
* @title 车型保存
* @description 车型保存
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
*/
public function store()
{
$this->model->car_series_id = $this->params['car_series_id'] ?? 0;
$this->model->name = $this->params['name'] ?? '';
$this->model->year = $this->params['year'] ?? '';
$this->model->modelYears = $this->params['modelYears'] ?? '';
$this->model->vehicle_type = $this->params['vehicle_type'] ?? '';
$this->model->vehicle_size = $this->params['vehicle_size'] ?? '';
$this->model->engine_model = $this->params['engine_model'] ?? '';
$this->model->emission_standard = $this->params['emission_standard'] ?? '';
$this->model->displacement = $this->params['displacement'] ?? '';
$this->model->induction = $this->params['induction'] ?? '';
$this->model->fuel_type = $this->params['fuel_type'] ?? '';
$this->model->cylinder_arrangement = $this->params['cylinder_arrangement'] ?? '';
$this->model->cylinders = $this->params['cylinders'] ?? 0;
$this->model->valves_per_cylinder = $this->params['valves_per_cylinder'] ?? 0;
$this->model->transmission_type = $this->params['transmission_type'] ?? '';
$this->model->transmission_description = $this->params['transmission_description'] ?? '';
$this->model->gear_number = $this->params['gear_number'] ?? '';
$this->model->front_tyre = $this->params['front_tyre'] ?? '';
$this->model->rear_tyre = $this->params['rear_tyre'] ?? '';
$this->model->doors = $this->params['doors'] ?? '';
$this->model->seats = $this->params['seats'] ?? '';
$this->model->tuhu_code = $this->params['tuhu_code'] ?? '';
$this->model->guidance_price = $this->params['guidance_price'] ?? '';
if($this->model->save()){
return $this->success(new CarModel($this->model));
} else {
return $this->error(500, '车型信息保存失败');
}
}
/**
* @title 车型详情
* @description 车型详情
* @param $id
* @return mixed
* @author zcstatham
*/
public function show($id)
{
return $this->success(new CarModel($this->model->where('id', $id)->first()));
}
/**
* @title 车型更新
* @description 车型更新
* @param $id
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
*/
public function update($id)
{
$this->model = $this->model->where('id', $id)->first();
$this->model->car_series_id = $this->params['car_series_id'] ?? 0;
$this->model->name = $this->params['name'] ?? '';
$this->model->year = $this->params['year'] ?? '';
$this->model->modelYears = $this->params['modelYears'] ?? '';
$this->model->vehicle_type = $this->params['vehicle_type'] ?? '';
$this->model->vehicle_size = $this->params['vehicle_size'] ?? '';
$this->model->engine_model = $this->params['engine_model'] ?? '';
$this->model->emission_standard = $this->params['emission_standard'] ?? '';
$this->model->displacement = $this->params['displacement'] ?? '';
$this->model->induction = $this->params['induction'] ?? '';
$this->model->fuel_type = $this->params['fuel_type'] ?? '';
$this->model->cylinder_arrangement = $this->params['cylinder_arrangement'] ?? '';
$this->model->cylinders = $this->params['cylinders'] ?? 0;
$this->model->valves_per_cylinder = $this->params['valves_per_cylinder'] ?? 0;
$this->model->transmission_type = $this->params['transmission_type'] ?? '';
$this->model->transmission_description = $this->params['transmission_description'] ?? '';
$this->model->gear_number = $this->params['gear_number'] ?? '';
$this->model->front_tyre = $this->params['front_tyre'] ?? '';
$this->model->rear_tyre = $this->params['rear_tyre'] ?? '';
$this->model->doors = $this->params['doors'] ?? '';
$this->model->seats = $this->params['seats'] ?? '';
$this->model->tuhu_code = $this->params['tuhu_code'] ?? '';
$this->model->guidance_price = $this->params['guidance_price'] ?? '';
if($this->model->save()){
return $this->success(new CarModel($this->model));
} else {
return $this->error(500, '车型信息保存失败');
}
}
}