| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace App\Module\GameItems\AdminControllers\Actions;
- use App\Module\GameItems\Models\ItemChestConfig;
- use App\Module\GameItems\Enums\ITEM_TYPE;
- use Dcat\Admin\Grid\RowAction;
- use Illuminate\Http\Request;
- /**
- * 复制宝箱配置行操作
- */
- class DuplicateItemChestConfigAction extends RowAction
- {
- /**
- * 按钮标题
- *
- * @var string
- */
- protected $title = '<i class="fa fa-copy"></i> 复制';
- /**
- * 处理请求
- *
- * @param Request $request
- * @return \Dcat\Admin\Actions\Response
- */
- public function handle(Request $request)
- {
- try {
- // 获取当前行ID
- $id = $this->getKey();
-
- // 查找原宝箱配置
- $originalConfig = ItemChestConfig::with(['item'])->find($id);
- if (!$originalConfig) {
- return $this->response()->error('宝箱配置不存在');
- }
-
- // 查找一个未被使用的宝箱物品
- $usedItemIds = ItemChestConfig::pluck('item_id')->toArray();
- $availableChest = \App\Module\GameItems\Models\Item::where('type', ITEM_TYPE::CHEST)
- ->whereNotIn('id', $usedItemIds)
- ->first();
- if (!$availableChest) {
- return $this->response()->error('没有可用的宝箱物品进行复制,所有宝箱都已有配置');
- }
- // 复制配置,使用找到的可用宝箱
- $newConfig = $originalConfig->replicate();
- $newConfig->item_id = $availableChest->id; // 使用可用的宝箱ID
- $newConfig->is_active = false; // 复制的配置默认为未激活状态
- $newConfig->save();
- $chestName = $originalConfig->item ? $originalConfig->item->name : '未知宝箱';
- // 跳转到编辑页面,让用户修改配置
- return $this->response()
- ->success("已成功复制宝箱配置 [{$chestName}] 到 [{$availableChest->name}] (ID: {$newConfig->id})")
- ->redirect(admin_url("game-items-chest-configs/{$newConfig->id}/edit"));
- } catch (\Exception $e) {
- return $this->response()
- ->error('复制失败: ' . $e->getMessage());
- }
- }
- /**
- * 确认信息
- *
- * @return array|string|void
- */
- public function confirm()
- {
- return ['确定要复制此宝箱配置吗?', '复制操作将创建一个新的宝箱配置记录,并跳转到编辑页面'];
- }
- }
|