generated from liutk/owl-admin-base
181 lines
5.4 KiB
PHP
181 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\Admin\Filters\EmployeePromotionFilter;
|
|
use App\Enums\PromotionStatus;
|
|
use App\Models\Employee;
|
|
use App\Models\EmployeePromotion;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Slowlyo\OwlAdmin\Admin;
|
|
use Slowlyo\OwlAdmin\Models\AdminUser;
|
|
|
|
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') && ! in_array($model->promotion_status, [PromotionStatus::Processing, PromotionStatus::Success])) {
|
|
array_push($actions, 'edit');
|
|
}
|
|
if ($user->can('admin.hr.promotion.delete') && ! in_array($model->promotion_status, [PromotionStatus::Processing, PromotionStatus::Success])) {
|
|
array_push($actions, 'delete');
|
|
}
|
|
if (in_array($model->promotion_status, [PromotionStatus::Processing])) {
|
|
array_push($actions, 'cancel');
|
|
}
|
|
|
|
return $actions;
|
|
}
|
|
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
// 获取员工所在的门店
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 邀请人填写推荐理由
|
|
*
|
|
* @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 '已经拥有该职位了';
|
|
}
|
|
|
|
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) {
|
|
$item->delete();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|