From 04b20257bfaf888dd934367d2a6b94251dd7a69f Mon Sep 17 00:00:00 2001 From: Jing Li Date: Thu, 4 Apr 2024 12:13:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E4=BA=8B=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Controllers/AdminController.php | 72 ++++++++++++++++++- app/Admin/Services/BaseService.php | 14 +--- app/Admin/Services/EmployeeService.php | 6 +- .../Services/EmployeeSignRepairService.php | 5 +- app/Admin/Services/StoreService.php | 4 +- 5 files changed, 77 insertions(+), 24 deletions(-) diff --git a/app/Admin/Controllers/AdminController.php b/app/Admin/Controllers/AdminController.php index e1a01c6..88d5011 100644 --- a/app/Admin/Controllers/AdminController.php +++ b/app/Admin/Controllers/AdminController.php @@ -2,10 +2,12 @@ namespace App\Admin\Controllers; +use App\Traits\CustomActionTrait; use Illuminate\Http\Request; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\DB; use Slowlyo\OwlAdmin\Controllers\AdminController as Controller; -use App\Traits\CustomActionTrait; +use Throwable; /** * @property \App\Admin\Services\BaseService $service @@ -14,6 +16,38 @@ abstract class AdminController extends Controller { use CustomActionTrait; + /** + * {@inheritdoc} + */ + public function store(Request $request) + { + try { + DB::beginTransaction(); + + if ($this->actionOfQuickEdit()) { + $result = $this->service->quickEdit($request->all()); + } elseif ($this->actionOfQuickEditItem()) { + $result = $this->service->quickEditItem($request->all()); + } else { + $result = $this->service->store($request->all()); + } + + if (! $result) { + admin_abort($this->service->getError() ?: __('admin.save') . __('admin.failed')); + } + + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + throw $th; + } + + return $this->response()->successMessage(__('admin.save') . __('admin.successfully')); + } + + /** + * {@inheritdoc} + */ public function update(Request $request) { $input = Arr::except($request->all(), $this->service->primaryKey()); @@ -22,9 +56,41 @@ abstract class AdminController extends Controller $input = Arr::only($input, explode(',', $request->input('_fields'))); } - $result = $this->service->update($this->getPrimaryValue($request), $input); + try { + DB::beginTransaction(); - return $this->autoResponse($result, __('admin.save')); + $result = $this->service->update($this->getPrimaryValue($request), $input); + + if (! $result) { + admin_abort($this->service->getError() ?: __('admin.save') . __('admin.failed')); + } + + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + throw $th; + } + + return $this->response()->successMessage(__('admin.save') . __('admin.successfully')); + } + + /** + * {@inheritdoc} + */ + public function destroy($ids) + { + try { + DB::beginTransaction(); + + $this->service->delete($ids); + + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + throw $th; + } + + return $this->response()->successMessage(__('admin.delete') . __('admin.successfully')); } public function getQuickEditItemPath(array $fields = ['*']) diff --git a/app/Admin/Services/BaseService.php b/app/Admin/Services/BaseService.php index 912b586..8197a61 100644 --- a/app/Admin/Services/BaseService.php +++ b/app/Admin/Services/BaseService.php @@ -105,15 +105,9 @@ class BaseService extends AdminService public function delete(string $ids): mixed { - $id = explode(',', $ids); - $result = $this->preDelete($id); - if ($result !== true) { - $this->setError($result); + $this->preDelete(explode(',', $ids)); - return false; - } - - return $this->query()->whereIn($this->primaryKey(), $id)->delete(); + return parent::delete($ids); } /** @@ -157,10 +151,8 @@ class BaseService extends AdminService * 删除的前置方法 * * @param array $ids 主键id - * @return mixed true: 继续后续操作, string: 中断操作, 返回错误提示 */ - public function preDelete(array $ids) + public function preDelete(array $ids): void { - return true; } } diff --git a/app/Admin/Services/EmployeeService.php b/app/Admin/Services/EmployeeService.php index c94baa2..7c6f5d9 100644 --- a/app/Admin/Services/EmployeeService.php +++ b/app/Admin/Services/EmployeeService.php @@ -97,11 +97,11 @@ class EmployeeService extends BaseService $model->jobs()->sync($jobs); } - public function preDelete(array $ids) + public function preDelete(array $ids): void { // 店员关联 if (DB::table('store_employees')->whereIn('employee_id', $ids)->exists()) { - return '员工已关联门店, 请先从门店中删除'; + admin_abort('员工已关联门店, 请先从门店中删除'); } // 删除管理员 @@ -111,8 +111,6 @@ class EmployeeService extends BaseService // 删除职位关联 DB::table('employee_jobs')->whereIn('employee_id', $ids)->delete(); - - return true; } public function validate($data, $model = null) diff --git a/app/Admin/Services/EmployeeSignRepairService.php b/app/Admin/Services/EmployeeSignRepairService.php index ef57d20..cf58343 100644 --- a/app/Admin/Services/EmployeeSignRepairService.php +++ b/app/Admin/Services/EmployeeSignRepairService.php @@ -43,11 +43,10 @@ class EmployeeSignRepairService extends BaseService return $data; } - public function preDelete(array $ids) + public function preDelete(array $ids): void { // 删除审核流程记录 WorkflowCheck::where('subject_type', (new WorkflowLog)->getMorphClass())->whereIn('subject_id', $ids)->delete(); - return true; } public function validate($data, $model = null) @@ -81,4 +80,4 @@ class EmployeeSignRepairService extends BaseService } return true; } -} \ No newline at end of file +} diff --git a/app/Admin/Services/StoreService.php b/app/Admin/Services/StoreService.php index 013e41b..c566d70 100644 --- a/app/Admin/Services/StoreService.php +++ b/app/Admin/Services/StoreService.php @@ -91,11 +91,9 @@ class StoreService extends BaseService return true; } - public function preDelete(array $ids) + public function preDelete(array $ids): void { // 修改员工关联 Employee::whereIn('store_id', $ids)->update(['store_id' => 0]); - - return true; } }