store-manage/app/Admin/Services/EmployeePromotionService.php

185 lines
5.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Admin\Services;
use App\Admin\Filters\EmployeePromotionFilter;
use App\Admin\WorkflowService;
use App\Enums\PromotionStatus;
use App\Models\Employee;
use App\Models\EmployeePromotion;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
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
{
protected array $withRelationships = ['employee', 'invitor', 'job', 'store', 'workflow'];
protected string $modelName = EmployeePromotion::class;
protected string $modelFilterName = EmployeePromotionFilter::class;
public function list()
{
$query = $this->listQuery();
if (request()->input('_all')) {
$list = (clone $query)->get();
$items = $list->all();
$total = $list->count();
} else {
$list = (clone $query)->paginate(request()->input('perPage', 20));
$items = $list->items();
$total = $list->total();
}
$user = Admin::user();
foreach($items as &$item) {
$item->row_actions = $this->rowActions($user, $item);
}
return compact('items', 'total');
}
public function rowActions(AdminUser $user, $model)
{
// view, edit, delete
// apply, cancel
$actions = [];
if ($user->can('admin.hr.promotion.view')) {
array_push($actions, 'view');
}
if ($user->can('admin.hr.promotion.update') && $model->canUpdate()) {
array_push($actions, 'edit');
}
if ($user->can('admin.hr.promotion.delete') ) {
array_push($actions, 'delete');
}
if (in_array($model->promotion_status, [PromotionStatus::Processing])) {
array_push($actions, 'cancel');
}
return $actions;
}
public function resloveData($data, $model = null)
{
// 获å<C2B7>å˜å·¥æ‰€åœ¨çš„门店
if (isset($data['employee_id'])) {
$data['store_id'] = Employee::where('id', $data['employee_id'])->value('store_id');
}
return $data;
}
/**
* 申请人完事资料
*
* @param EmployeePromotion $model
* @param array $data
* @return bool
*/
public function apply($model, $data = [])
{
if ($model->promotion_status != PromotionStatus::Employee) {
return $this->setError('无法修改');
}
$validator = Validator::make($data, [
'age' => ['required'],
'sex' => ['required'],
'education' => ['required'],
'first_work_time' => ['required'],
'work_years' => ['required'],
'work_years_in_company' => ['required'],
'comment_self' => ['required'],
'plans' => ['required'],
]);
if ($validator->fails()) {
return $this->setError($validator->errors()->first());
}
$model->update(['employee_data' => $data, 'promotion_status' => PromotionStatus::Invitor]);
return true;
}
/**
* é€è¯·äººå¡«å†™æŽ¨è<C2A8><C3A8>ç<EFBFBD>†ç”±
*
* @param EmployeePromotion $model
* @param array $data
* @return bool
*/
public function invitor($model, $data = [])
{
if ($model->promotion_status != PromotionStatus::Invitor) {
return $this->setError('无法修改');
}
$validator = Validator::make($data, [
'reason' => ['required'],
]);
if ($validator->fails()) {
return $this->setError($validator->errors()->first());
}
$attributes = array_merge($model->employee_data, $data);
$model->update(['employee_data' => $attributes, 'promotion_status' => PromotionStatus::Processing]);
return true;
}
public function validate($data, $model = null)
{
$createRules = [
'store_id' => ['required'],
'employee_id' => ['required'],
'invitor_id' => ['required'],
'job_id' => ['required'],
];
$updateRules = [];
$validator = Validator::make($data, $model ? $updateRules : $createRules);
if ($validator->fails()) {
return $validator->errors()->first();
}
if (DB::table('employee_jobs')->where(Arr::only($data, ['employee_id', 'job_id']))->exists()) {
return 'å·²ç»<C3A7>æ¥æœ‰è¯¥è<C2A5>Œä½<C3A4>了';
}
return true;
}
public function update($primaryKey, $data): bool
{
$model = $this->query()->whereKey($primaryKey)->firstOrFail();
if (!$model->canUpdate()) {
return $this->setError('审核中, 无法修改');
}
$data = $this->resloveData($data, $model);
$validate = $this->validate($data, $model);
if ($validate !== true) {
$this->setError($validate);
return false;
}
$model->update($data);
$this->currentModel = $model;
return true;
}
public function delete(string $ids): mixed
{
$list = $this->query()->with(['workflow'])->whereIn('id', explode(',', $ids))->get();
foreach ($list as $item) {
if (!$item->canUpdate()) {
throw new RuntimeException($item->promotion_status->text() . ', 无法删除');
}
$item->delete();
}
return true;
}
}