generated from liutk/owl-admin-base
自动事务
parent
0ec4e24c84
commit
04b20257bf
|
|
@ -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 = ['*'])
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue