generated from liutk/owl-admin-base
136 lines
4.1 KiB
PHP
136 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\Admin\Filters\AgreementFilter;
|
|
use App\Enums\CheckStatus;
|
|
use App\Models\Agreement;
|
|
use App\Models\Employee;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Slowlyo\OwlAdmin\Admin;
|
|
use Slowlyo\OwlAdmin\Models\AdminUser;
|
|
|
|
class AgreementService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['employee', 'workflow', 'store'];
|
|
|
|
protected string $modelName = Agreement::class;
|
|
|
|
protected string $modelFilterName = AgreementFilter::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.agreement.view')) {
|
|
array_push($actions, 'view');
|
|
}
|
|
if ($user->can('admin.agreement.update') && ! in_array($model->workflow->check_status, [CheckStatus::Processing, CheckStatus::Success])) {
|
|
array_push($actions, 'edit');
|
|
}
|
|
if ($user->can('admin.agreement.delete') && ! in_array($model->workflow->check_status, [CheckStatus::Processing, CheckStatus::Success])) {
|
|
array_push($actions, 'delete');
|
|
}
|
|
if ($user->can('admin.agreement.download') && in_array($model->workflow->check_status, [CheckStatus::Success])) {
|
|
array_push($actions, 'download');
|
|
}
|
|
|
|
if (in_array($model->workflow->check_status, [CheckStatus::None, CheckStatus::Cancel, CheckStatus::Fail])) {
|
|
array_push($actions, 'apply');
|
|
}
|
|
|
|
if (in_array($model->workflow->check_status, [CheckStatus::Processing])) {
|
|
array_push($actions, 'cancel');
|
|
}
|
|
|
|
return $actions;
|
|
}
|
|
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
// 获取员工所在的门店
|
|
if (! isset($data['store_id']) && isset($data['employee_id'])) {
|
|
$data['store_id'] = Employee::where('id', $data['employee_id'])->value('store_id');
|
|
}
|
|
if (isset($data['images']) && $data['images']) {
|
|
$images = [];
|
|
foreach ($data['images'] as $value) {
|
|
if (is_array($value)) {
|
|
array_push($images, data_get($value, 'value'));
|
|
} else {
|
|
array_push($images, $value);
|
|
}
|
|
}
|
|
$data['images'] = $images;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function validate($data, $model = null)
|
|
{
|
|
$createRules = [
|
|
'employee_id' => ['required'],
|
|
'name' => ['required'],
|
|
];
|
|
$updateRules = [];
|
|
$validator = Validator::make($data, $model ? $updateRules : $createRules);
|
|
if ($validator->fails()) {
|
|
return $validator->errors()->first();
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|