store-manage/app/Admin/Controllers/System/WorkflowController.php

307 lines
11 KiB
PHP

<?php
namespace App\Admin\Controllers\System;
use App\Admin\Controllers\AdminController;
use App\Admin\Services\{WorkFlowService};
use App\Enums\CheckStatus;
use App\Enums\CheckType;
use App\Models\Employee;
use App\Models\Keyword;
use App\Models\WorkflowCheck;
use App\Models\WorkflowLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Slowlyo\OwlAdmin\Admin;
use Slowlyo\OwlAdmin\Renderers\Form;
use Slowlyo\OwlAdmin\Renderers\Page;
/**
* 审核流程管理
*
* @property WorkFlowService $service
*/
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(),
]),
]))
->filterDefaultVisible()
->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 create()
{
$this->isCreate = true;
$form = amis()->Card()
->className('base-form')
->header(['title' => __('admin.create')])
->toolbar([$this->backButton()])
->body($this->form(false)->api($this->getStorePath()));
$page = $this->basePage()->id('workflow-form-page')->body($form);
return $this->response()->success($page);
}
public function edit($id)
{
$this->isEdit = true;
if ($this->actionOfGetData()) {
return $this->response()->success($this->service->getEditData($id));
}
$form = amis()->Card()
->className('base-form')
->header(['title' => __('admin.edit')])
->toolbar([$this->backButton()])
->body(
$this->form(true)->api($this->getUpdatePath())->initApi($this->getEditGetDataPath())
);
$page = $this->basePage()->id('workflow-form-page')->body($form);
return $this->response()->success($page);
}
public function form($edit): Form
{
return $this->baseForm()->title('')->className('h-screen')->body([
amisMake()->TextControl()->name('key')->label(__('workflow.key'))->required(),
amisMake()->TextControl()->name('name')->label(__('workflow.name'))->required(),
amisMake()->TableControl()->name('config')->label(__('workflow.config'))->showIndex()->addable()->removable()->needConfirm(false)->columns([
amisMake()->SelectControl()->options(CheckType::options())->name('type')->label(__('workflow.type')),
// amisMake()->PickerControl()
// ->source(admin_url('api/keywords/tree-list?parent_key=job'))
// ->valueField('key')
// ->labelField('name')
// ->multiple(false)
// ->size('lg')
// ->pickerSchema(
// amis()->CRUDTable()->mode('table')->loadDataOnce(true)->syncLocation(false)->draggable(false)->columns([
// amis()->TableColumn()->name('key')->label(__('keyword.key')),
// amis()->TableColumn()->name('name')->label(__('keyword.name')),
// ])
// )
// ->name('job')
// ->label(__('workflow.job')),
// amis()->PickerControl()
// ->source(admin_url('api/employees?enable=1'))
// ->valueField('id')
// ->labelField('name')
// ->multiple(false)
// ->size('lg')
// ->pickerSchema(
// amis()->CRUDTable()->mode('table')->syncLocation(false)->draggable(false)
// ->filter($this->baseFilter()->body([
// amisMake()->TextControl()->name('name')->label(__('employee.name'))->size('md')->clearable(),
// amisMake()->TextControl()->name('phone')->label(__('employee.phone'))->size('md')->clearable(),
// ]))
// ->columns([
// amis()->TableColumn()->name('id')->label(__('employee.id')),
// amis()->TableColumn()->name('name')->label(__('employee.name')),
// amis()->TableColumn()->name('phone')->label(__('employee.phone')),
// ])
// )
// ->name('user')
// ->label(__('workflow.user')),
// amis()->PickerControl()
// ->source(admin_url('api/workflow/value-options?type=${type}'))
// ->multiple(false)
// ->size('lg')
// ->pickerSchema(
// amis()->CRUD2Table()->mode('table')->syncLocation(false)->draggable(false)
// ->filter($this->baseFilter()->body([
// amisMake()->TextControl()->name('search')->label('关键字')->size('md')->clearable(),
// ]))
// ->columns([
// amis()->TableColumn()->name('value'),
// amis()->TableColumn()->name('label'),
// ])
// )
// ->name('value')
// ->label(__('workflow.value')),
amisMake()->SelectControl()
->source(admin_url('api/workflow/value-options?type=${type}'))
->searchable()
->name('value')
->label(__('workflow.value')),
]),
]);
}
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 getValueOptions(Request $request)
{
$type = $request->input('type');
$options = [];
if ($type == CheckType::Job->value) {
$options = Keyword::where('parent_key', 'job')->select(['key as value', 'name as label'])->get();
}
if ($type == CheckType::User->value) {
$options = Employee::enable()->select(['id as value', 'name as label'])->get();
}
return $this->response()->success($options);
}
public function apply(Request $request)
{
$model = WorkflowCheck::findOrFail($request->input('id'));
$employee = $request->whenFilled('user', function ($id) {
if ($employee = Employee::find($id)) {
return $employee;
}
admin_abort('员工未找到');
}, function () {
if ($employee = Employee::where('admin_user_id', Admin::user()->id)->first()) {
return $employee;
}
admin_abort('当前登录账户未关联员工');
});
try {
DB::beginTransaction();
if (! $this->service->apply($model, $employee)) {
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::find($request->input('id'));
if (! $log) {
return $this->response()->fail('审核已取消');
}
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::find($request->input('id'));
if (! $log) {
return $this->response()->fail('审核已取消');
}
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();
// 判断当前用户是否有权限审核
$user = Employee::with(['jobs'])->where('admin_user_id', Admin::user()->id)->first();
if ($user) {
foreach ($list as &$item) {
$item->checkable = $item->check_status == CheckStatus::Processing && $this->service->authCheck($user, $item);
}
}
return $this->response()->success($list);
}
}