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/CompanyController.php

156 lines
4.1 KiB

4 years ago
<?php
namespace App\Http\Controllers\Api;
use App\Http\Resources\CompanyCollection;
use App\Http\Resources\Company;
use App\Models\CompanyT;
use Illuminate\Http\Request;
/**
* @title 公司设置
* @description 公司设置
* @package App\Http\Controllers\Api
* @author zcstatham
* @time 2021/1/29
*/
class CompanyController extends BaseController
{
protected $rules = [
'store' => [
'rules' => [
'title' => 'bail|required|unique:App\Models\CompanyT|max:20',
'name' => 'bail|required|unique:App\Models\CompanyT|max:20',
'pid' => 'required',
]
],
'show' => [
'rules' => [
'id' => 'bail|inter|min:1',
],
'custom' => 'exists,App\Models\CompanyT,id'
],
'update' => [
'rules' => [
'id' => 'bail|inter|min:1',
'title' => 'required|max:20',
'name' => 'bail|required|max:20',
'pid' => 'required',
],
'custom' => [
'exists,App\Models\CompanyT,id',
'unique,App\Models\CompanyT,title,id',
'unique,App\Models\CompanyT,name,id'
]
],
'destroy' => [
'rules' => [
'id' => 'bail|inter|min:1',
],
'custom' => [
'exists,App\Models\CompanyT,id',
]
]
];
public function __construct(Request $request)
{
$this->model = new CompanyT();
parent::__construct($request);
}
/**
* @title 公司列表
* @description 公司列表
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
* @time 2021/1/28
*/
public function index()
{
$query = CompanyT::query();
if(isset($this->params['name']) && $this->params['name'] != ''){
$query->where('name', $this->params['name']);
}
if(isset($this->params['title']) && $this->params['title'] != ''){
$query->where('title', 'like', '%'. $this->params['title'] .'%');
}
$data = $query->paginate();
return $this->success(new CompanyCollection($data));
}
/**
* @title 公司新增
* @description 公司新增
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
* @time 2021/1/28
*/
public function store()
{
$company = new CompanyT();
$company->title = $this->params['title'];
$company->name = $this->params['name'];
$company->pid = $this->params['pid'];
if($company->save()){
return $this->success(new Company($company));
} else {
return $this->error(500, '配置信息保存失败');
}
}
/**
* @title 公司详情
* @description 公司详情
* @param $id
* @return mixed
* @author zcstatham
* @time 2021/1/28
*/
public function show($id)
{
return $this->success(new Company($this->model->where('id', $id)->first()));
}
/**
* @title 公司更新
* @description 公司更新
* @param $id
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
* @time 2021/1/28
*/
public function update($id)
{
$company = $this->model->where('id', $id)->first();
$company->title = $this->params['title'];
$company->name = $this->params['name'];
$company->pid = $this->params['pid'];
if($company->save()){
return $this->success(new Company($company));
} else {
return $this->error(500, '配置信息保存失败');
}
}
/**
* @title 公司删除
* @description 公司删除
* @param $id
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
* @time 2021/1/28
*/
public function destroy($id)
{
if(CompanyT::destroy($id)){
return $this->success();
} else {
return $this->error(500, '配置信息保存失败');
}
}
}