store-manage/app/Http/Controllers/Api/AgreementController.php

105 lines
3.1 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Admin\Services\{AgreementService, WorkFlowService};
use App\Exceptions\RuntimeException;
use App\Http\Resources\AgreementResource;
use App\Models\Agreement;
use App\Models\WorkflowCheck;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/**
* 合同管理
*/
class AgreementController extends Controller
{
public function index(Request $request)
{
$user = $this->guard()->user();
$list = Agreement::with(['workflow'])
->where('employee_id', $user->id)
->filter($request->all())
->orderByDesc(WorkflowCheck::checkStatusSortBuilder(new Agreement()))
->orderBy('id', 'desc')
->paginate($request->input('per_page'));
return AgreementResource::collection($list);
}
public function show($id)
{
$info = Agreement::with(['employee', 'workflow'])->findOrFail($id);
return AgreementResource::make($info);
}
public function store(Request $request, AgreementService $service)
{
$user = $this->guard()->user();
$data = $request->all();
$data['employee_id'] = $user->id;
try {
DB::beginTransaction();
if (!$service->store($data)) {
throw new RuntimeException($result);
}
$model = $service->getCurrentModel();
$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, AgreementService $service)
{
$user = $this->guard()->user();
$model = Agreement::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, AgreementService $service)
{
$user = $this->guard()->user();
$model = Agreement::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());
}
}
}