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

170 lines
5.2 KiB

<?php
namespace App\Http\Controllers\Api;
use App\Http\Resources\EmployeeCollection;
use App\Http\Resources\Employee;
use App\Models\EmployeeT;
use Illuminate\Http\Request;
/**
* @title 员工管理
* @description 员工管理
* @package App\Http\Controllers\Api
* @author zcstatham
* @time 2021/2/3
*/
class EmployeeController extends BaseController
{
protected $rules = [
'store' => [
'rules' => [
'username' => 'required',
'nickname' => 'required',
]
],
'show' => [
'rules' => [
'id' => 'bail|integer|min:1',
],
'custom' => 'exists,App\Models\EmployeeT,id'
],
'update' => [
'rules' => [
'id' => 'bail|integer|min:1',
'username' => 'required',
'nickname' => 'required',
],
'custom' => [
'exists,App\Models\EmployeeT,id',
]
],
'destroy' => [
'rules' => [
'id' => 'bail|integer|min:1',
],
'custom' => [
'exists,App\Models\EmployeeT,id',
]
]
];
public function __construct(Request $request)
{
$this->model = new EmployeeT();
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['status']) && $this->params['status'] >= 0){
$query->where('status', $this->params['status']);
} else {
$query->where('status', 3);
}
$data = $query->paginate();
return $this->success(new EmployeeCollection($data));
}
/**
* @title 员工保存
* @description 员工保存
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
*/
public function store()
{
$this->model->username = $this->params['username'];
$this->model->nickname = $this->params['nickname'];
$this->model->password = password_hash($this->params['password']??'123456', PASSWORD_DEFAULT );
$this->model->company_id = $this->params['company_id'];
$this->model->group_id = $this->params['group_id'];
$this->model->phone = $this->params['phone'];
$this->model->monthly_working_days = $this->params['monthly_working_days'];
$this->model->entry_date = $this->params['entry_date'];
$this->model->official_date = $this->params['official_date'];
$this->model->resign_date = $this->params['resign_date'];
$this->model->probation_salary = $this->params['probation_salary'];
$this->model->official_salary = $this->params['official_salary'];
$this->model->is_outer = $this->params['is_outer'];
$this->model->status = $this->params['status'];
if($this->model->save()){
return $this->success(new Employee($this->model));
} else {
return $this->error(500, '员工信息保存失败');
}
}
/**
* @title 员工详情
* @description 员工详情
* @param $id
* @return mixed
* @author zcstatham
*/
public function show($id)
{
return $this->success(new Employee($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->username = $this->params['username'];
$this->model->nickname = $this->params['nickname'];
$this->model->company_id = $this->params['company_id'];
$this->model->group_id = $this->params['group_id'];
$this->model->phone = $this->params['phone'];
$this->model->monthly_working_days = $this->params['monthly_working_days'];
$this->model->entry_date = $this->params['entry_date'];
$this->model->official_date = $this->params['official_date'];
$this->model->resign_date = $this->params['resign_date'];
$this->model->probation_salary = $this->params['probation_salary'];
$this->model->official_salary = $this->params['official_salary'];
$this->model->is_outer = $this->params['is_outer'];
$this->model->status = $this->params['status'];
if($this->model->save()){
return $this->success(new Employee($this->model));
} else {
return $this->error(500, '员工信息保存失败');
}
}
/**
* @title 员工删除
* @description 员工删除
* @param $id
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
*/
public function destroy($id)
{
if($this->model->destroy($id)){
return $this->success();
} else {
return $this->error(500, '员工信息保存失败');
}
}
}