validated(); // 获取验证后的安全数据 $userId = $validation->getSafe('user_id'); $itemId = $validation->getSafe('itemId'); $price = $validation->getSafe('price'); $quantity = $validation->getSafe('num'); $direction = $validation->getSafe('direction'); // 创建响应对象 $response = new ResponseMatchexchangeAdd(); try { // 开启数据库事务 DB::beginTransaction(); // 获取默认币种(钻石) $currencyType = FundLogic::getDefaultCurrency(); // 根据交易方向调用不同的处理方法 if ($direction === MEX_DIRECTION::BUY) { $result = $this->processBuyOrder($userId, $itemId, $quantity, $price, $currencyType); } else { $result = $this->processSellOrder($userId, $itemId, $quantity, $price, $currencyType); } // 提交事务 DB::commit(); // 设置成功响应 $this->response->setCode(RESPONSE_CODE::OK); $this->response->setMsg($result['message'] ?? '挂单创建成功'); // 更新用户活动时间 $this->updateUserActivityTime(); } catch (\Exception $e) { // 回滚事务 DB::rollBack(); // 记录错误日志 Log::error('创建挂单失败', [ 'user_id' => $userId, 'item_id' => $itemId, 'price' => $price, 'quantity' => $quantity, 'direction' => $direction, 'direction_text' => $validation->getDirectionText(), 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); // 重新抛出异常,让框架处理 throw $e; } return $response; } /** * 处理买入订单 * * @param int $userId 用户ID * @param int $itemId 物品ID * @param int $quantity 数量 * @param float $price 价格 * @param FUND_CURRENCY_TYPE $currencyType 币种类型 * @return array 处理结果 */ private function processBuyOrder(int $userId, int $itemId, int $quantity, float $price, FUND_CURRENCY_TYPE $currencyType): array { // 调用服务层创建买入订单(直接传递原始价格,信任Fund模块的数据处理) $result = MexOrderService::createBuyOrder( $userId, $itemId, $quantity, $price, $currencyType ); if (!$result['success']) { throw new \Exception($result['message'] ?? '创建买入订单失败'); } Log::info('买入订单创建成功', [ 'user_id' => $userId, 'item_id' => $itemId, 'quantity' => $quantity, 'price' => $price, 'currency_type' => $currencyType->value, 'currency_name' => $currencyType->name, 'order_id' => $result['order_id'] ?? null ]); return $result; } /** * 处理卖出订单 * * @param int $userId 用户ID * @param int $itemId 物品ID * @param int $quantity 数量 * @param float $price 价格 * @param FUND_CURRENCY_TYPE $currencyType 币种类型 * @return array 处理结果 */ private function processSellOrder(int $userId, int $itemId, int $quantity, float $price, FUND_CURRENCY_TYPE $currencyType): array { // 调用服务层创建卖出订单(直接传递原始价格,信任Fund模块的数据处理) $result = MexOrderService::createSellOrder( $userId, $itemId, $quantity, $price, $currencyType ); if (!$result['success']) { throw new \Exception($result['message'] ?? '创建卖出订单失败'); } Log::info('卖出订单创建成功', [ 'user_id' => $userId, 'item_id' => $itemId, 'quantity' => $quantity, 'price' => $price, 'currency_type' => $currencyType->value, 'currency_name' => $currencyType->name, 'order_id' => $result['order_id'] ?? null ]); return $result; } }