自动事务

main
Jing Li 2024-04-04 12:13:17 +08:00
parent 0ec4e24c84
commit 04b20257bf
5 changed files with 77 additions and 24 deletions

View File

@ -2,10 +2,12 @@
namespace App\Admin\Controllers; namespace App\Admin\Controllers;
use App\Traits\CustomActionTrait;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Slowlyo\OwlAdmin\Controllers\AdminController as Controller; use Slowlyo\OwlAdmin\Controllers\AdminController as Controller;
use App\Traits\CustomActionTrait; use Throwable;
/** /**
* @property \App\Admin\Services\BaseService $service * @property \App\Admin\Services\BaseService $service
@ -14,6 +16,38 @@ abstract class AdminController extends Controller
{ {
use CustomActionTrait; 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) public function update(Request $request)
{ {
$input = Arr::except($request->all(), $this->service->primaryKey()); $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'))); $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 = ['*']) public function getQuickEditItemPath(array $fields = ['*'])

View File

@ -105,15 +105,9 @@ class BaseService extends AdminService
public function delete(string $ids): mixed public function delete(string $ids): mixed
{ {
$id = explode(',', $ids); $this->preDelete(explode(',', $ids));
$result = $this->preDelete($id);
if ($result !== true) {
$this->setError($result);
return false; return parent::delete($ids);
}
return $this->query()->whereIn($this->primaryKey(), $id)->delete();
} }
/** /**
@ -157,10 +151,8 @@ class BaseService extends AdminService
* 删除的前置方法 * 删除的前置方法
* *
* @param array $ids 主键id * @param array $ids 主键id
* @return mixed true: 继续后续操作, string: 中断操作, 返回错误提示
*/ */
public function preDelete(array $ids) public function preDelete(array $ids): void
{ {
return true;
} }
} }

View File

@ -97,11 +97,11 @@ class EmployeeService extends BaseService
$model->jobs()->sync($jobs); $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()) { 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(); DB::table('employee_jobs')->whereIn('employee_id', $ids)->delete();
return true;
} }
public function validate($data, $model = null) public function validate($data, $model = null)

View File

@ -43,11 +43,10 @@ class EmployeeSignRepairService extends BaseService
return $data; 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(); WorkflowCheck::where('subject_type', (new WorkflowLog)->getMorphClass())->whereIn('subject_id', $ids)->delete();
return true;
} }
public function validate($data, $model = null) public function validate($data, $model = null)

View File

@ -91,11 +91,9 @@ class StoreService extends BaseService
return true; return true;
} }
public function preDelete(array $ids) public function preDelete(array $ids): void
{ {
// 修改员工关联 // 修改员工关联
Employee::whereIn('store_id', $ids)->update(['store_id' => 0]); Employee::whereIn('store_id', $ids)->update(['store_id' => 0]);
return true;
} }
} }