| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- namespace App\Module\AppGame\Handler\Matchexchange;
- use App\Module\AppGame\Handler\BaseHandler;
- use App\Module\AppGame\Validations\MatchexchangeAddValidation;
- use App\Module\Mex\Services\MexOrderService;
- use App\Module\Fund\Enums\FUND_CURRENCY_TYPE;
- use App\Module\Mex\Logic\FundLogic;
- use Google\Protobuf\Internal\Message;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Uraus\Kku\Request\RequestMatchexchangeAdd;
- use Uraus\Kku\Response\ResponseMatchexchangeAdd;
- use Uraus\Kku\Common\MEX_DIRECTION;
- use Uraus\Kku\Common\RESPONSE_CODE;
- /**
- * 处理添加挂单请求
- */
- class AddHandler extends BaseHandler
- {
- /**
- * 是否需要登录
- *
- * @var bool
- */
- protected bool $need_login = true;
- /**
- * 处理添加挂单请求
- *
- * @param RequestMatchexchangeAdd $data 添加挂单请求数据
- * @return ResponseMatchexchangeAdd 添加挂单响应
- */
- public function handle(Message $data): Message
- {
- // 创建验证对象
- $validation = MatchexchangeAddValidation::makeByProrobufUser($data);
- // 验证请求数据
- $validation->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;
- }
- }
|