generated from liutk/owl-admin-base
185 lines
5.4 KiB
PHP
185 lines
5.4 KiB
PHP
<?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;
|
||
}
|
||
}
|