AddHandler.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace App\Module\AppGame\Handler\Matchexchange;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\AppGame\Validations\MatchexchangeAddValidation;
  5. use App\Module\Mex\Services\MexOrderService;
  6. use App\Module\Fund\Enums\FUND_CURRENCY_TYPE;
  7. use App\Module\Mex\Logic\FundLogic;
  8. use Google\Protobuf\Internal\Message;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. use Uraus\Kku\Request\RequestMatchexchangeAdd;
  12. use Uraus\Kku\Response\ResponseMatchexchangeAdd;
  13. use Uraus\Kku\Common\MEX_DIRECTION;
  14. use Uraus\Kku\Common\RESPONSE_CODE;
  15. /**
  16. * 处理添加挂单请求
  17. */
  18. class AddHandler extends BaseHandler
  19. {
  20. /**
  21. * 是否需要登录
  22. *
  23. * @var bool
  24. */
  25. protected bool $need_login = true;
  26. /**
  27. * 处理添加挂单请求
  28. *
  29. * @param RequestMatchexchangeAdd $data 添加挂单请求数据
  30. * @return ResponseMatchexchangeAdd 添加挂单响应
  31. */
  32. public function handle(Message $data): Message
  33. {
  34. // 创建验证对象
  35. $validation = MatchexchangeAddValidation::makeByProrobufUser($data);
  36. // 验证请求数据
  37. $validation->validated();
  38. // 获取验证后的安全数据
  39. $userId = $validation->getSafe('user_id');
  40. $itemId = $validation->getSafe('itemId');
  41. $price = $validation->getSafe('price');
  42. $quantity = $validation->getSafe('num');
  43. $direction = $validation->getSafe('direction');
  44. // 创建响应对象
  45. $response = new ResponseMatchexchangeAdd();
  46. try {
  47. // 开启数据库事务
  48. DB::beginTransaction();
  49. // 获取默认币种(钻石)
  50. $currencyType = FundLogic::getDefaultCurrency();
  51. // 根据交易方向调用不同的处理方法
  52. if ($direction === MEX_DIRECTION::BUY) {
  53. $result = $this->processBuyOrder($userId, $itemId, $quantity, $price, $currencyType);
  54. } else {
  55. $result = $this->processSellOrder($userId, $itemId, $quantity, $price, $currencyType);
  56. }
  57. // 提交事务
  58. DB::commit();
  59. // 设置成功响应
  60. $this->response->setCode(RESPONSE_CODE::OK);
  61. $this->response->setMsg($result['message'] ?? '挂单创建成功');
  62. // 更新用户活动时间
  63. $this->updateUserActivityTime();
  64. } catch (\Exception $e) {
  65. // 回滚事务
  66. DB::rollBack();
  67. // 记录错误日志
  68. Log::error('创建挂单失败', [
  69. 'user_id' => $userId,
  70. 'item_id' => $itemId,
  71. 'price' => $price,
  72. 'quantity' => $quantity,
  73. 'direction' => $direction,
  74. 'direction_text' => $validation->getDirectionText(),
  75. 'error' => $e->getMessage(),
  76. 'trace' => $e->getTraceAsString()
  77. ]);
  78. // 重新抛出异常,让框架处理
  79. throw $e;
  80. }
  81. return $response;
  82. }
  83. /**
  84. * 处理买入订单
  85. *
  86. * @param int $userId 用户ID
  87. * @param int $itemId 物品ID
  88. * @param int $quantity 数量
  89. * @param float $price 价格
  90. * @param FUND_CURRENCY_TYPE $currencyType 币种类型
  91. * @return array 处理结果
  92. */
  93. private function processBuyOrder(int $userId, int $itemId, int $quantity, float $price, FUND_CURRENCY_TYPE $currencyType): array
  94. {
  95. // 调用服务层创建买入订单(直接传递原始价格,信任Fund模块的数据处理)
  96. $result = MexOrderService::createBuyOrder(
  97. $userId,
  98. $itemId,
  99. $quantity,
  100. $price,
  101. $currencyType
  102. );
  103. if (!$result['success']) {
  104. throw new \Exception($result['message'] ?? '创建买入订单失败');
  105. }
  106. Log::info('买入订单创建成功', [
  107. 'user_id' => $userId,
  108. 'item_id' => $itemId,
  109. 'quantity' => $quantity,
  110. 'price' => $price,
  111. 'currency_type' => $currencyType->value,
  112. 'currency_name' => $currencyType->name,
  113. 'order_id' => $result['order_id'] ?? null
  114. ]);
  115. return $result;
  116. }
  117. /**
  118. * 处理卖出订单
  119. *
  120. * @param int $userId 用户ID
  121. * @param int $itemId 物品ID
  122. * @param int $quantity 数量
  123. * @param float $price 价格
  124. * @param FUND_CURRENCY_TYPE $currencyType 币种类型
  125. * @return array 处理结果
  126. */
  127. private function processSellOrder(int $userId, int $itemId, int $quantity, float $price, FUND_CURRENCY_TYPE $currencyType): array
  128. {
  129. // 调用服务层创建卖出订单(直接传递原始价格,信任Fund模块的数据处理)
  130. $result = MexOrderService::createSellOrder(
  131. $userId,
  132. $itemId,
  133. $quantity,
  134. $price,
  135. $currencyType
  136. );
  137. if (!$result['success']) {
  138. throw new \Exception($result['message'] ?? '创建卖出订单失败');
  139. }
  140. Log::info('卖出订单创建成功', [
  141. 'user_id' => $userId,
  142. 'item_id' => $itemId,
  143. 'quantity' => $quantity,
  144. 'price' => $price,
  145. 'currency_type' => $currencyType->value,
  146. 'currency_name' => $currencyType->name,
  147. 'order_id' => $result['order_id'] ?? null
  148. ]);
  149. return $result;
  150. }
  151. }