diff --git a/app/Admin/Controllers/AdminController.php b/app/Admin/Controllers/AdminController.php index ef2bc15..6a2daaf 100644 --- a/app/Admin/Controllers/AdminController.php +++ b/app/Admin/Controllers/AdminController.php @@ -41,7 +41,7 @@ abstract class AdminController extends Controller DB::commit(); } catch (Throwable $th) { DB::rollBack(); - throw $this->prepareException($th); + return $this->renderException($th); } return $this->response()->successMessage(__('admin.save').__('admin.successfully')); @@ -70,7 +70,7 @@ abstract class AdminController extends Controller DB::commit(); } catch (Throwable $th) { DB::rollBack(); - throw $this->prepareException($th); + return $this->renderException($th); } return $this->response()->successMessage(__('admin.save').__('admin.successfully')); @@ -89,7 +89,7 @@ abstract class AdminController extends Controller DB::commit(); } catch (Throwable $th) { DB::rollBack(); - throw $this->prepareException($th); + return $this->renderException($th); } return $this->response()->successMessage(__('admin.delete').__('admin.successfully')); @@ -106,16 +106,23 @@ abstract class AdminController extends Controller return $path; } - protected function prepareException(Throwable $e) + protected function renderException(Throwable $e) { + report($e); + + if ($e instanceof AdminException) { + return $e->render(); + } + + $message = $e->getMessage(); + if ($e instanceof ValidationException) { $message = Arr::first($e->errors()); if (is_array($message)) { - $message = Arr::first($message); + $message = Arr::first($message) ?: '参数错误'; } - $e = new AdminException($message ?: '参数错误'); } - return $e; + return $this->response()->fail($message); } } diff --git a/app/Admin/Controllers/Plan/PlanController.php b/app/Admin/Controllers/Plan/PlanController.php index 34cc2ec..24f245b 100644 --- a/app/Admin/Controllers/Plan/PlanController.php +++ b/app/Admin/Controllers/Plan/PlanController.php @@ -178,7 +178,7 @@ class PlanController extends AdminController amis()->TableColumn('name', __('plan.task.name')), amis()->TableColumn('taskable.date', __('plan.task_ledger.date')), amis()->TableColumn('taskable.store.title', __('plan.task_ledger.store')), - amis()->TableColumn('taskable.store.master.name', __('plan.task_ledger.store_master')), + amis()->TableColumn('taskable.store_master.name', __('plan.task_ledger.store_master')), amis()->TableColumn('task_status', __('plan.task.status'))->type('mapping')->map(TaskStatus::labelMap()), amis()->TableColumn('completed_at', __('plan.task.completed_at')), amis()->TableColumn('created_at', __('plan.task.created_at')), diff --git a/app/Admin/Controllers/Plan/TaskController.php b/app/Admin/Controllers/Plan/TaskController.php index 32298d6..f0a81f2 100644 --- a/app/Admin/Controllers/Plan/TaskController.php +++ b/app/Admin/Controllers/Plan/TaskController.php @@ -23,7 +23,7 @@ class TaskController extends AdminController ->with([ 'taskable' => function (MorphTo $morphTo) { $morphTo->morphWith([ - TaskLedger::class => ['store.master'], + TaskLedger::class => ['store', 'storeMaster'], ]); }, ]) diff --git a/app/Console/Commands/TaskLedgerGenerateCommand.php b/app/Console/Commands/TaskLedgerGenerateCommand.php index aa01cbb..84edba3 100644 --- a/app/Console/Commands/TaskLedgerGenerateCommand.php +++ b/app/Console/Commands/TaskLedgerGenerateCommand.php @@ -56,14 +56,14 @@ class TaskLedgerGenerateCommand extends Command $planable->save(); $plan = $planable->plan()->create([ - 'name' => "{$planable->date} 总账录入", + 'name' => "【{$planable->date}】总账录入", 'plan_status' => PlanStatus::Published, ]); return $planable->setRelation('plan', $plan); }); - $stores = Store::all(); + $stores = Store::with(['master'])->get(); /** @var \App\Models\Store */ foreach ($stores as $store) { @@ -71,6 +71,8 @@ class TaskLedgerGenerateCommand extends Command $taskable = TaskLedger::firstOrNew([ 'store_id' => $store->id, 'date' => $planable->date, + ], [ + 'store_master_id' => $store->master?->id, ]); if ($taskable->exists) { diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 5cc2140..6bfc524 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Throwable; use Illuminate\Http\{Request}; use Illuminate\Validation\ValidationException; +use Slowlyo\OwlAdmin\Exceptions\AdminException; class Handler extends ExceptionHandler { @@ -21,6 +22,7 @@ class Handler extends ExceptionHandler ]; protected $dontReport = [ + AdminException::class, RuntimeException::class, ]; diff --git a/app/Models/TaskLedger.php b/app/Models/TaskLedger.php index 3bed357..2be71f8 100644 --- a/app/Models/TaskLedger.php +++ b/app/Models/TaskLedger.php @@ -13,8 +13,9 @@ class TaskLedger extends Model use HasFactory, HasDateTimeFormatter; protected $fillable = [ - 'store_id', 'date', + 'store_id', + 'store_master_id', ]; public function task(): MorphOne @@ -26,4 +27,9 @@ class TaskLedger extends Model { return $this->belongsTo(Store::class); } + + public function storeMaster(): BelongsTo + { + return $this->belongsTo(Employee::class, 'store_master_id'); + } } diff --git a/database/migrations/2024_04_15_081617_create_task_ledgers_table.php b/database/migrations/2024_04_15_081617_create_task_ledgers_table.php index 07b128e..3d9baa5 100644 --- a/database/migrations/2024_04_15_081617_create_task_ledgers_table.php +++ b/database/migrations/2024_04_15_081617_create_task_ledgers_table.php @@ -13,8 +13,9 @@ return new class extends Migration { Schema::create('task_ledgers', function (Blueprint $table) { $table->id(); - $table->foreignId('store_id')->comment('门店ID'); $table->date('date')->comment('日期'); + $table->foreignId('store_id')->comment('门店ID'); + $table->foreignId('store_master_id')->comment('店长'); $table->timestamps(); }); }