generated from liutk/owl-admin-base
197 lines
6.8 KiB
PHP
197 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers\System;
|
|
|
|
use App\Admin\Controllers\AdminController;
|
|
use App\Admin\Services\WorkFlowService;
|
|
use App\Enums\CheckType;
|
|
use App\Models\{Employee, WorkflowLog, WorkflowCheck};
|
|
use App\Models\Keyword;
|
|
use Slowlyo\OwlAdmin\Renderers\Form;
|
|
use Slowlyo\OwlAdmin\Renderers\Page;
|
|
use Illuminate\Http\Request;
|
|
use Slowlyo\OwlAdmin\Admin;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
|
|
|
/**
|
|
* 审核流程管理
|
|
*/
|
|
class WorkflowController extends AdminController
|
|
{
|
|
protected string $serviceName = WorkFlowService::class;
|
|
|
|
protected $jobOptions;
|
|
|
|
protected $employeeOptions;
|
|
|
|
public function list(): Page
|
|
{
|
|
$crud = $this->baseCRUD()
|
|
->tableLayout('fixed')
|
|
->headerToolbar([
|
|
$this->createButton(),
|
|
...$this->baseHeaderToolBar(),
|
|
])
|
|
->bulkActions([])
|
|
->filter($this->baseFilter()->body([
|
|
amis()->GroupControl()->mode('horizontal')->body([
|
|
amisMake()->TextControl()->name('search')->label(__('admin.keyword'))->placeholder(__('workflow.key').'/'.__('workflow.name'))->columnRatio(3)->clearable(),
|
|
]),
|
|
]))
|
|
->columns([
|
|
amisMake()->TableColumn()->name('id')->label(__('workflow.id')),
|
|
amisMake()->TableColumn()->name('key')->label(__('workflow.key')),
|
|
amisMake()->TableColumn()->name('name')->label(__('workflow.name')),
|
|
$this->rowActions([
|
|
$this->rowShowButton(),
|
|
$this->rowEditButton(),
|
|
$this->rowDeleteButton(),
|
|
]),
|
|
]);
|
|
|
|
return $this->baseList($crud);
|
|
}
|
|
|
|
public function form($edit): Form
|
|
{
|
|
return $this->baseForm()->title('')->body([
|
|
amisMake()->TextControl()->name('key')->label(__('workflow.key'))->required(),
|
|
amisMake()->TextControl()->name('name')->label(__('workflow.name'))->required(),
|
|
amisMake()->ArrayControl()->name('config')->label(__('workflow.config'))->items(amisMake()->ComboControl()->items([
|
|
amisMake()->SelectControl()->options(CheckType::options())->name('type')->label(__('workflow.type')),
|
|
amisMake()->SelectControl()
|
|
->options($this->getJobOptions())
|
|
->labelField('name')
|
|
->valueField('key')
|
|
->name('job')
|
|
// ->visibleOn('${config.type == '.CheckType::Job->value.'}')
|
|
->label(CheckType::Job->text()),
|
|
amisMake()->SelectControl()
|
|
->options($this->getEmployeeOptions())
|
|
->labelField('name')
|
|
->valueField('id')
|
|
->searchable()
|
|
->name('user')
|
|
// ->visibleOn('${config.type == '.CheckType::User->value.'}')
|
|
->label(CheckType::User->text()),
|
|
])),
|
|
]);
|
|
}
|
|
|
|
public function detail(): Form
|
|
{
|
|
$detail = amisMake()->Property()->items([
|
|
['label' => __('workflow.key'), 'content' => '${key}'],
|
|
['label' => __('workflow.name'), 'content' => '${name}'],
|
|
['label' => __('workflow.config'), 'content' => amisMake()->Steps()->labelPlacement('horizontal')->source('${config}'), 'span' => 3],
|
|
]);
|
|
|
|
return $this->baseDetail()->title('')->body($detail);
|
|
}
|
|
|
|
public function apply(Request $request)
|
|
{
|
|
$model = WorkflowCheck::findOrFail($request->input('id'));
|
|
$user = Employee::findOrFail($request->input('user'));
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
if (!$this->service->apply($model, $user)) {
|
|
return $this->response()->fail($this->service->getError());
|
|
}
|
|
DB::commit();
|
|
return $this->response()->success();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return $this->response()->fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function cancel(Request $request)
|
|
{
|
|
$model = WorkflowCheck::findOrFail($request->input('id'));
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
if (!$this->service->cancel($model)) {
|
|
return $this->response()->fail($this->service->getError());
|
|
}
|
|
DB::commit();
|
|
return $this->response()->success();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return $this->response()->fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function success(Request $request)
|
|
{
|
|
$user = Admin::user();
|
|
$employee = Employee::where('admin_user_id', $user->id)->first();
|
|
if (!$employee) {
|
|
return $this->response()->fail('当前登录账户未关联员工');
|
|
}
|
|
$log = WorkflowLog::findOrFail($request->input('id'));
|
|
try {
|
|
DB::beginTransaction();
|
|
if (!$this->service->check($employee, $log, true)) {
|
|
return $this->response()->fail($this->service->getError());
|
|
}
|
|
DB::commit();
|
|
return $this->response()->success();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return $this->response()->fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function fail(Request $request)
|
|
{
|
|
if (!$request->input('remarks')) {
|
|
return $this->response()->fail('请填写未通过原因');
|
|
}
|
|
$user = Admin::user();
|
|
$employee = Employee::where('admin_user_id', $user->id)->first();
|
|
if (!$employee) {
|
|
return $this->response()->fail('当前登录账户未关联员工');
|
|
}
|
|
$log = WorkflowLog::findOrFail($request->input('id'));
|
|
try {
|
|
DB::beginTransaction();
|
|
if (!$this->service->check($employee, $log, false, ['remarks' => $request->input('remarks')])) {
|
|
return $this->response()->fail($this->service->getError());
|
|
}
|
|
DB::commit();
|
|
return $this->response()->success();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return $this->response()->fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function logs(Request $request)
|
|
{
|
|
$list = WorkflowLog::with(['checkUser'])->where('check_id', $request->input('id'))->sort()->get();
|
|
return $this->response()->success($list);
|
|
}
|
|
|
|
public function getJobOptions()
|
|
{
|
|
if (! $this->jobOptions) {
|
|
$this->jobOptions = Keyword::where('parent_key', 'job')->get();
|
|
}
|
|
|
|
return $this->jobOptions;
|
|
}
|
|
|
|
public function getEmployeeOptions()
|
|
{
|
|
if (! $this->employeeOptions) {
|
|
$this->employeeOptions = Employee::enable()->get();
|
|
}
|
|
|
|
return $this->employeeOptions;
|
|
}
|
|
}
|