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

140 lines
4.0 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;
class EmployeePromotionService extends BaseService
{
protected array $withRelationships = ['employee', 'invitor', 'job', 'store', 'workflow'];
protected string $modelName = EmployeePromotion::class;
protected string $modelFilterName = EmployeePromotionFilter::class;
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->canUpdate()) {
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->canUpdate()) {
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) {
if (!$item->canUpdate()) {
return $this->setError($item->promotion_status->text() . ', 无法删除');
}
$item->delete();
}
return true;
}
}