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/EmployeeRewardPunishmentCon...

158 lines
4.6 KiB

4 years ago
<?php
namespace App\Http\Controllers\Api;
use App\Http\Resources\EmployeeRewardPunishment;
use App\Http\Resources\EmployeeRewardPunishmentCollection;
use App\Models\EmployeeRewardPunishmentT;
use Illuminate\Http\Request;
/**
* @title 奖惩管理
* @description 考勤,奖励,惩罚等
* @package App\Http\Controllers\Api
* @author zcstatham
* @time 2021/2/3
*/
class EmployeeRewardPunishmentController extends BaseController
{
protected $rules = [
'store' => [
'rules' => [
'employee_id' => 'required',
'type' => 'required',
'date' => 'required|date',
'price' => 'required',
],
'custom' => [
'unique,App\Models\EmployeeRewardPunishmentT,date,type'
]
],
'show' => [
'rules' => [
'id' => 'bail|min:1',
],
'custom' => 'exists,App\Models\EmployeeRewardPunishmentT,id'
],
'update' => [
'rules' => [
'id' => 'bail|min:1',
'employee_id' => 'required',
'type' => 'required',
'date' => 'required|date',
'price' => 'required',
],
'custom' => [
'exists,App\Models\EmployeeRewardPunishmentT,id',
'unique,App\Models\EmployeeRewardPunishmentT,date,type,id'
]
],
'destroy' => [
'rules' => [
'id' => 'bail|min:1',
],
'custom' => [
'exists,App\Models\EmployeeRewardPunishmentT,id',
]
]
];
public function __construct(Request $request)
{
$this->model = new EmployeeRewardPunishmentT();
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']);
}
$data = $query->paginate();
return $this->success(new EmployeeRewardPunishmentCollection($data));
}
/**
* @title 奖惩保存
* @description 奖惩保存
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
*/
public function store()
{
$this->model->employee_id = $this->params['employee_id'];
$this->model->type = $this->params['type'];
$this->model->price = $this->params['type'] > 600 ? abs($this->params['price']) : -abs($this->params['price']);
$this->model->remark = $this->params['remark'] ?? '';
$this->model->date = $this->params['date'] ?? '';
$this->model->status = $this->params['status'] ?? 1;
if($this->model->save()){
return $this->success(new EmployeeRewardPunishment($this->model));
} else {
return $this->error(500, '奖惩信息保存失败');
}
}
/**
* @title 奖惩详情
* @description 奖惩详情
* @param $id
* @return mixed
* @author zcstatham
*/
public function show($id)
{
return $this->success(new EmployeeRewardPunishment($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->employee_id = $this->params['employee_id'];
$this->model->type = $this->params['type'];
$this->model->price = $this->params['type'] > 600 ? abs($this->params['price']) : -abs($this->params['price']);
$this->model->remark = $this->params['remark'] ?? $this->model->remark;
if($this->model->save()){
return $this->success(new EmployeeRewardPunishment($this->model));
} else {
return $this->error(500, '奖惩信息保存失败');
}
}
/**
* @title 奖惩删除
* @description 奖惩删除
* @param $id
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
*/
public function destroy($id)
{
$this->model = $this->model::find($id);
$this->model->status = 0;
if($this->model->save()){
return $this->success();
} else {
return $this->error(500, '奖惩信息保存失败');
}
}
}