generated from liutk/owl-admin-base
147 lines
5.0 KiB
PHP
147 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\Enums\{CheckType, CheckStatus};
|
|
use App\Models\{Employee, Store, Keyword, Workflow, WorkflowLog};
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\Rule;
|
|
use App\Contracts\Checkable;
|
|
|
|
class WorkFlowService extends BaseService
|
|
{
|
|
protected array $withRelationships = [];
|
|
|
|
protected string $modelName = Workflow::class;
|
|
|
|
protected string $modelFilterName = '';
|
|
|
|
/**
|
|
* 发起审核申请
|
|
* 1. 待审核 Model 实现 Checkable
|
|
* 2. 生成全部的审核流程
|
|
*
|
|
* @param Checkable $subject 待审核记录
|
|
* @param Employee $user 申请人
|
|
*
|
|
* @return boolean true: 成功, false: 失败, $this->getError(): 错误消息
|
|
*/
|
|
public function apply(Checkable $subject, Employee $user)
|
|
{
|
|
if ($subject->check_status === CheckStatus::Success->value) {
|
|
return $this->setError('已经审核通过');
|
|
}
|
|
if ($subject->check_status === CheckStatus::Processing->value) {
|
|
return $this->setError('正在审核中');
|
|
}
|
|
$result = $subject->checkApplyPre();
|
|
if ($result !== true) {
|
|
return $this->setError($result);
|
|
}
|
|
|
|
$workflow = Workflow::where('key', $subject->getCheckKey())->first();
|
|
// 没有配置审核流程, 直接通过
|
|
if (!$workflow || !$workflow->config) {
|
|
$subject->checkSuccess();
|
|
return true;
|
|
}
|
|
$jobs = Keyword::where('parent_key', 'job')->get();
|
|
$config = collect($workflow->config)->sortBy('sort');
|
|
$batchId = WorkflowLog::max('id') + 1;
|
|
$subjectData = $subject->toArray();
|
|
foreach($config as $item) {
|
|
$checkValue = '';
|
|
$checkName = '';
|
|
// 职位审核
|
|
if ($item['type'] == CheckType::Job->value) {
|
|
// 没有门店, 则跳过
|
|
if (!$user->store_id) {
|
|
continue;
|
|
}
|
|
// 所属门店的职位审核
|
|
$store = Store::findOrFail($user->store_id);
|
|
$job = $jobs->firstWhere('key', $item['value']);
|
|
$checkValue = $store->id . '-' . $job->key;
|
|
$checkName = $store->title . '-' . $job->name;
|
|
}
|
|
// 指定用户审核
|
|
else if ($item['type'] == CheckType::User->value) {
|
|
$checkUser = Employee::findOrFail($item['value']);
|
|
$checkValue = $checkUser->id;
|
|
$checkName = $checkUser->name;
|
|
} else {
|
|
return $this->setError('未知的审核类型: ' . $item['type']);
|
|
break;
|
|
}
|
|
$subject->workflows()->create([
|
|
'batch_id' => $batchId,
|
|
'check_type' => $item['type'],
|
|
'check_value' => $checkValue,
|
|
'check_name' => $checkName,
|
|
'user_id' => $user->id,
|
|
'subject_data' => $subjectData,
|
|
'sort' => $item['sort']
|
|
]);
|
|
}
|
|
$subject->checkApply();
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 取消审核
|
|
*/
|
|
public function cancel(Checkable $subject)
|
|
{
|
|
$subject->workflows()->whereIn('check_status', [CheckStatus::None, CheckStatus::Processing])->delete();
|
|
$subject->checkCancel();
|
|
return true;
|
|
}
|
|
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
if (isset($data['config'])) {
|
|
foreach ($data['config'] as $key => &$item) {
|
|
if (!$item) {
|
|
$data['config'] = null;
|
|
break;
|
|
}
|
|
$item['title'] = match ($item['type']) {
|
|
CheckType::Job->value => CheckType::Job->text(),
|
|
CheckType::User->value => CheckType::User->text(),
|
|
};
|
|
$item['subTitle'] = match ($item['type']) {
|
|
CheckType::Job->value => Keyword::where('key', $item['job'])->value('name'),
|
|
CheckType::User->value => Employee::where('id', $item['user'])->value('name'),
|
|
};
|
|
$item['value'] = match ($item['type']) {
|
|
CheckType::Job->value => $item['job'],
|
|
CheckType::User->value => $item['user'],
|
|
};
|
|
$item['sort'] = $key + 1;
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function validate($data, $model = null)
|
|
{
|
|
$createRules = [
|
|
'key' => ['required', Rule::unique('workflows', 'key')],
|
|
'name' => ['required'],
|
|
'config' => ['required', 'array'],
|
|
];
|
|
$updateRules = [
|
|
'key' => [Rule::unique('workflows', 'key')->ignore($model?->id)],
|
|
];
|
|
$validator = Validator::make($data, $model ? $updateRules : $createRules, [
|
|
'key.unique' => ':input 已经存在',
|
|
]);
|
|
if ($validator->fails()) {
|
|
return $validator->errors()->first();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|