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

109 lines
3.5 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Hr;
use App\Admin\Services\{EmployeeSignRepairService, WorkFlowService};
use App\Enums\{CheckStatus};
use App\Exceptions\RuntimeException;
use App\Http\Controllers\Api\Controller;
use App\Http\Resources\{EmployeeSignRepairResource, WorkflowLogResource};
use App\Models\EmployeeSignRepair;
use App\Models\WorkflowCheck;
use Illuminate\Http\{Request, Response};
use Illuminate\Support\Facades\DB;
/**
* 补卡申请
*/
class SignRepairController extends Controller
{
public function index(Request $request)
{
$user = $this->guard()->user();
$list = EmployeeSignRepair::with(['workflow'])
->where('employee_id', $user->id)
->filter($request->all())
->orderByDesc(WorkflowCheck::checkStatusSortBuilder(new EmployeeSignRepair()))
->orderBy('id', 'desc')
->paginate($request->input('per_page'));
return EmployeeSignRepairResource::collection($list);
}
public function store(Request $request, EmployeeSignRepairService $service)
{
$user = $this->guard()->user();
$data = $request->all();
$data['employee_id'] = $user->id;
try {
DB::beginTransaction();
if (!$service->store($data)) {
throw new RuntimeException($service->getError());
}
$model = $service->getCurrentModel();
$workflow = WorkFlowService::make();
if (!$workflow->apply($model->workflow, $user)) {
throw new RuntimeException($workflow->getError());
}
DB::commit();
return response('', Response::HTTP_OK);
} catch (\Exception $e) {
DB::rollBack();
throw new RuntimeException($e->getMessage());
}
}
public function show($id)
{
$info = EmployeeSignRepair::with(['workflow', 'employee', 'store'])->findOrFail($id);
return EmployeeSignRepairResource::make($info);
}
public function update($id, Request $request, EmployeeSignRepairService $service)
{
$user = $this->guard()->user();
$model = EmployeeSignRepair::with(['workflow'])->where('employee_id', $user->id)->findOrFail($id);
if (!$model->canUpdate()) {
throw new RuntimeException('审核中, 无法修改');
}
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('', Response::HTTP_OK);
} catch (\Exception $e) {
DB::rollBack();
throw new RuntimeException($e->getMessage());
}
}
public function destroy($id, EmployeeSignRepairService $service)
{
$user = $this->guard()->user();
$model = EmployeeSignRepair::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('', Response::HTTP_OK);
} catch (\Exception $e) {
DB::rollBack();
throw new RuntimeException($e->getMessage());
}
}
}