getId(); $quantity = $data->getNumber(); $userId = $this->user_id; // 设置默认数量 if ($quantity <= 0) { $quantity = 1; } // 先进行验证,避免不必要的事务开销 $validation = new ItemDismantleValidation([ 'user_id' => $userId, 'item_id' => $itemId, 'quantity' => $quantity, 'instance_id' => 0 // 默认不指定实例 ]); // 验证数据 $validation->validated(); // 调用分解服务(服务内部会处理事务) $result = DismantleService::dismantleItem($userId, $itemId, null, $quantity, [ 'source_type' => 'app_dismantle', 'device_info' => request()->userAgent(), 'ip_address' => request()->ip(), ]); if (!$result) { throw new LogicException("分解失败,请检查物品是否存在或是否可分解"); } // 设置响应状态 $this->response->setCode(RESPONSE_CODE::OK); $this->response->setMsg('分解成功'); Log::info('用户物品分解成功', [ 'user_id' => $userId, 'item_id' => $itemId, 'quantity' => $quantity, 'result' => $result ]); } catch (\Exception $e) { Log::error('物品分解操作异常', [ 'user_id' => $userId ?? null, 'item_id' => $itemId ?? null, 'quantity' => $quantity ?? null, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); // 重新抛出异常,交由框架处理 throw $e; } return $response; } }