store-manage/app/Http/Controllers/Api/Hr/HolidayController.php

109 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Hr;
use App\Admin\Services\{HolidayApplyService, WorkFlowService};
use App\Exceptions\RuntimeException;
use App\Http\Controllers\Api\Controller;
use App\Http\Resources\HolidayApplyResource;
use App\Models\HolidayApply;
use App\Models\WorkflowCheck;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/**
* 请假管理
*/
class HolidayController extends Controller
{
public function index(Request $request)
{
$user = $this->guard()->user();
$list = HolidayApply::with(['type', 'workflow'])
->filter($request->all())
->where('employee_id', $user->id)
->orderByDesc(WorkflowCheck::checkStatusSortBuilder(new HolidayApply()))
->orderBy('id', 'desc')
->paginate($request->input('per_page'));
return HolidayApplyResource::collection($list);
}
public function show($id)
{
$info = HolidayApply::with(['type', 'employee', 'store', 'workflow'])->findOrFail($id);
return HolidayApplyResource::make($info);
}
public function store(Request $request, HolidayApplyService $service)
{
$user = $this->guard()->user();
$data = $request->all();
$data['employee_id'] = $user->id;
try {
DB::beginTransaction();
$data = $service->resloveData($data);
$result = $service->validate($data);
if ($result !== true) {
throw new RuntimeException($result);
}
$model = HolidayApply::create($data);
$workflow = WorkFlowService::make();
if (!$workflow->apply($model->workflow, $user)) {
throw new RuntimeException($workflow->getError());
}
DB::commit();
return response()->noContent();
} catch (\Exception $e) {
DB::rollBack();
throw new RuntimeException($e->getMessage());
}
}
public function update($id, Request $request, HolidayApplyService $service)
{
$user = $this->guard()->user();
$model = HolidayApply::where('employee_id', $user->id)->findOrFail($id);
try {
DB::beginTransaction();
if (!$service->update($id, $request->all())) {
throw new RuntimeException($service->getError());
}
$workflow = WorkFlowService::make();
if (!$workflow->apply($model->workflow, $user)) {
throw new RuntimeException($workflow->getError());
}
DB::commit();
return response()->noContent();
} catch (\Exception $e) {
DB::rollBack();
throw new RuntimeException($e->getMessage());
}
}
public function destroy($id, HolidayApplyService $service)
{
$user = $this->guard()->user();
$model = HolidayApply::with(['workflow'])->where('employee_id', $user->id)->findOrFail($id);
try {
DB::beginTransaction();
if (!$service->delete($id)) {
throw new RuntimeException($service->getError());
}
DB::commit();
return response()->noContent();
} catch (\Exception $e) {
DB::rollBack();
throw new RuntimeException($e->getMessage());
}
}
}