diff --git a/app/Admin/Services/EmployeePromotionService.php b/app/Admin/Services/EmployeePromotionService.php index a08eadb..49dc737 100644 --- a/app/Admin/Services/EmployeePromotionService.php +++ b/app/Admin/Services/EmployeePromotionService.php @@ -13,6 +13,7 @@ use Illuminate\Support\Arr; use Slowlyo\OwlAdmin\Admin; use App\Enums\CheckStatus; use Slowlyo\OwlAdmin\Models\AdminUser; +use App\Exceptions\RuntimeException; class EmployeePromotionService extends BaseService { @@ -53,7 +54,7 @@ class EmployeePromotionService extends BaseService if ($user->can('admin.hr.promotion.update') && $model->canUpdate()) { array_push($actions, 'edit'); } - if ($user->can('admin.hr.promotion.delete') && $model->canUpdate()) { + if ($user->can('admin.hr.promotion.delete') ) { array_push($actions, 'delete'); } if (in_array($model->promotion_status, [PromotionStatus::Processing])) { @@ -82,8 +83,8 @@ class EmployeePromotionService extends BaseService */ public function apply($model, $data = []) { - if (!$model->canUpdate()) { - return $this->setError('审核中, 无法修改'); + if ($model->promotion_status != PromotionStatus::Employee) { + return $this->setError('无法修改'); } $validator = Validator::make($data, [ 'age' => ['required'], @@ -113,8 +114,8 @@ class EmployeePromotionService extends BaseService */ public function invitor($model, $data = []) { - if (!$model->canUpdate()) { - return $this->setError('审核中, 无法修改'); + if ($model->promotion_status != PromotionStatus::Invitor) { + return $this->setError('无法修改'); } $validator = Validator::make($data, [ 'reason' => ['required'], @@ -174,7 +175,7 @@ class EmployeePromotionService extends BaseService $list = $this->query()->with(['workflow'])->whereIn('id', explode(',', $ids))->get(); foreach ($list as $item) { if (!$item->canUpdate()) { - return $this->setError($item->promotion_status->text() . ', 无法删除'); + throw new RuntimeException($item->promotion_status->text() . ', 无法删除'); } $item->delete(); } diff --git a/app/Admin/Services/EmployeeService.php b/app/Admin/Services/EmployeeService.php index 1481800..3b1083b 100644 --- a/app/Admin/Services/EmployeeService.php +++ b/app/Admin/Services/EmployeeService.php @@ -44,6 +44,7 @@ class EmployeeService extends BaseService // 职位修改 if (isset($data['jobs'])) { + dd($data['jobs']); $jobs = is_array($data['jobs']) ? $data['jobs'] : explode(',', $data['jobs']); $model->jobs()->sync($jobs); } @@ -71,7 +72,7 @@ class EmployeeService extends BaseService // 职位修改 if (isset($data['jobs'])) { if (is_array($data['jobs'])) { - if (is_array($data['jobs'][0])) { + if (count($data['jobs']) > 0 && is_array($data['jobs'][0])) { $jobs = array_column($data['jobs'], 'key'); } else { $jobs = $data['jobs']; diff --git a/app/Enums/PromotionStatus.php b/app/Enums/PromotionStatus.php index e1bc457..7e3a37e 100644 --- a/app/Enums/PromotionStatus.php +++ b/app/Enums/PromotionStatus.php @@ -31,7 +31,7 @@ enum PromotionStatus: int public static function options() { return [ - self::Employee->value => '待提交', + self::Employee->value => '待补充', self::Invitor->value => '待推荐', self::Processing->value => '审核中', self::Success->value => '审核通过', diff --git a/app/Http/Controllers/Api/Hr/PromotionController.php b/app/Http/Controllers/Api/Hr/PromotionController.php index 2709a65..ea7f7d0 100644 --- a/app/Http/Controllers/Api/Hr/PromotionController.php +++ b/app/Http/Controllers/Api/Hr/PromotionController.php @@ -49,7 +49,6 @@ class PromotionController extends Controller public function show($id) { $info = EmployeePromotion::with(['workflow', 'employee', 'invitor', 'job'])->findOrFail($id); - return EmployeePromotionResource::make($info); } @@ -93,6 +92,40 @@ class PromotionController extends Controller 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) { diff --git a/routes/api.php b/routes/api.php index f57cd05..40cc011 100644 --- a/routes/api.php +++ b/routes/api.php @@ -101,6 +101,7 @@ Route::group([ Route::get('hr/promotion/apply', [\App\Http\Controllers\Api\Hr\PromotionController::class, 'applyList']); Route::get('hr/promotion/invite', [\App\Http\Controllers\Api\Hr\PromotionController::class, 'inviteList']); Route::get('hr/promotion/{id}', [\App\Http\Controllers\Api\Hr\PromotionController::class, 'show']); + Route::post('hr/promotion/{id}', [\App\Http\Controllers\Api\Hr\PromotionController::class, 'update']); Route::post('hr/promotion/{id}/apply', [\App\Http\Controllers\Api\Hr\PromotionController::class, 'applyUpdate']); Route::post('hr/promotion/{id}/invite', [\App\Http\Controllers\Api\Hr\PromotionController::class, 'inviteUpdate']); Route::delete('hr/promotion/{id}', [\App\Http\Controllers\Api\Hr\PromotionController::class, 'destroy']);