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

151 lines
4.9 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Hr;
use App\Admin\Services\{EmployeePromotionService, WorkFlowService};
use App\Enums\PromotionStatus;
use App\Exceptions\RuntimeException;
use App\Http\Controllers\Api\Controller;
use App\Http\Resources\EmployeePromotionResource;
use App\Models\EmployeePromotion;
use App\Models\WorkflowCheck;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/**
* 升职申请
*/
class PromotionController extends Controller
{
/**
* 申请列表
*/
public function applyList(Request $request)
{
$user = $this->guard()->user();
$list = EmployeePromotion::with(['workflow', 'invitor', 'job'])
->where('employee_id', $user->id)
->filter($request->all())
->orderByDesc(WorkflowCheck::checkStatusSortBuilder(new EmployeePromotion()))
->orderBy('id', 'desc')
->paginate($request->input('per_page'));
return EmployeePromotionResource::collection($list);
}
/**
* 推荐列表
*/
public function inviteList(Request $request)
{
$user = $this->guard()->user();
$list = EmployeePromotion::with(['workflow', 'employee', 'job'])
->where('invitor_id', $user->id)
->filter($request->all())
->orderBy('id', 'desc')
->paginate($request->input('per_page'));
return EmployeePromotionResource::collection($list);
}
public function show($id)
{
$info = EmployeePromotion::with(['workflow', 'employee', 'invitor', 'job'])->findOrFail($id);
return EmployeePromotionResource::make($info);
}
/**
* 申请人完善资料
*/
public function applyUpdate($id, Request $request, EmployeePromotionService $service)
{
$user = $this->guard()->user();
$info = EmployeePromotion::where('employee_id', $user->id)->findOrFail($id);
if (!$service->apply($info, $request->all())) {
throw new RuntimeException($service->getError());
}
return response()->noContent();
}
/**
* 推荐人填写
*/
public function inviteUpdate($id, Request $request, EmployeePromotionService $service)
{
$user = $this->guard()->user();
$model = EmployeePromotion::with(['workflow', 'employee'])->where('invitor_id', $user->id)->findOrFail($id);
try {
DB::beginTransaction();
if (!$service->invitor($model, $request->all())) {
throw new RuntimeException($service->getError());
}
$workflow = WorkFlowService::make();
if (!$workflow->apply($model->workflow, $model->employee)) {
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, EmployeePromotionService $service)
{
$user = $this->guard()->user();
$info = EmployeePromotion::findOrFail($id);
try {
DB::beginTransaction();
// 申请人完善资料
if ($info->promotion_status == PromotionStatus::Employee) {
if (!$service->apply($info, $request->only(['age', 'sex', 'education', 'first_work_time', 'work_years', 'work_years_in_company', 'comment_self', 'plans', 'reason']))) {
throw new RuntimeException($service->getError());
}
}
// 推荐人填写
else if ($info->promotion_status == PromotionStatus::Invitor) {
if (!$service->invitor($info, $request->only(['reason']))) {
throw new RuntimeException($service->getError());
}
$workflow = WorkFlowService::make();
if (!$workflow->apply($info->workflow, $info->employee)) {
throw new RuntimeException($workflow->getError());
}
} else {
throw new RuntimeException('资料已填写, 不能修改');
}
DB::commit();
return response()->noContent();
} catch (\Exception $e) {
DB::rollBack();
throw new RuntimeException($e->getMessage());
}
}
public function destroy($id, EmployeePromotionService $service)
{
$user = $this->guard()->user();
$model = EmployeePromotion::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());
}
}
}