DuplicateItemChestConfigAction.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers\Actions;
  3. use App\Module\GameItems\Models\ItemChestConfig;
  4. use App\Module\GameItems\Enums\ITEM_TYPE;
  5. use Dcat\Admin\Grid\RowAction;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 复制宝箱配置行操作
  9. */
  10. class DuplicateItemChestConfigAction extends RowAction
  11. {
  12. /**
  13. * 按钮标题
  14. *
  15. * @var string
  16. */
  17. protected $title = '<i class="fa fa-copy"></i> 复制';
  18. /**
  19. * 处理请求
  20. *
  21. * @param Request $request
  22. * @return \Dcat\Admin\Actions\Response
  23. */
  24. public function handle(Request $request)
  25. {
  26. try {
  27. // 获取当前行ID
  28. $id = $this->getKey();
  29. // 查找原宝箱配置
  30. $originalConfig = ItemChestConfig::with(['item'])->find($id);
  31. if (!$originalConfig) {
  32. return $this->response()->error('宝箱配置不存在');
  33. }
  34. // 查找一个未被使用的宝箱物品
  35. $usedItemIds = ItemChestConfig::pluck('item_id')->toArray();
  36. $availableChest = \App\Module\GameItems\Models\Item::where('type', ITEM_TYPE::CHEST)
  37. ->whereNotIn('id', $usedItemIds)
  38. ->first();
  39. if (!$availableChest) {
  40. return $this->response()->error('没有可用的宝箱物品进行复制,所有宝箱都已有配置');
  41. }
  42. // 复制配置,使用找到的可用宝箱
  43. $newConfig = $originalConfig->replicate();
  44. $newConfig->item_id = $availableChest->id; // 使用可用的宝箱ID
  45. $newConfig->is_active = false; // 复制的配置默认为未激活状态
  46. $newConfig->save();
  47. $chestName = $originalConfig->item ? $originalConfig->item->name : '未知宝箱';
  48. // 跳转到编辑页面,让用户修改配置
  49. return $this->response()
  50. ->success("已成功复制宝箱配置 [{$chestName}] 到 [{$availableChest->name}] (ID: {$newConfig->id})")
  51. ->redirect(admin_url("game-items-chest-configs/{$newConfig->id}/edit"));
  52. } catch (\Exception $e) {
  53. return $this->response()
  54. ->error('复制失败: ' . $e->getMessage());
  55. }
  56. }
  57. /**
  58. * 确认信息
  59. *
  60. * @return array|string|void
  61. */
  62. public function confirm()
  63. {
  64. return ['确定要复制此宝箱配置吗?', '复制操作将创建一个新的宝箱配置记录,并跳转到编辑页面'];
  65. }
  66. }