MexOrderLogic.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace App\Module\Mex\Logic;
  3. use App\Module\Mex\Models\MexOrder;
  4. use App\Module\Mex\Models\MexPriceConfig;
  5. use App\Module\Mex\Enums\OrderType;
  6. use App\Module\Mex\Enums\OrderStatus;
  7. use App\Module\Mex\Events\OrderCreatedEvent;
  8. use App\Module\Fund\Enums\FUND_CURRENCY_TYPE;
  9. use App\Module\Mex\Logic\FundLogic;
  10. /**
  11. * 农贸市场订单逻辑
  12. *
  13. * 处理订单相关的核心业务逻辑
  14. */
  15. class MexOrderLogic
  16. {
  17. /**
  18. * 创建卖出订单
  19. *
  20. * @param int $userId 用户ID
  21. * @param int $itemId 商品ID
  22. * @param int $quantity 数量
  23. * @param string $price 价格
  24. * @param FUND_CURRENCY_TYPE|null $currencyType 币种类型,默认使用钻石
  25. * @return array 操作结果
  26. */
  27. public static function createSellOrder(int $userId, int $itemId, int $quantity, string $price, ?FUND_CURRENCY_TYPE $currencyType = null): array
  28. {
  29. // 获取币种类型,默认使用钻石
  30. $currencyType = $currencyType ?? FundLogic::getDefaultCurrency();
  31. // 验证价格配置是否存在(不验证价格范围,挂单阶段无价格验证)
  32. $priceConfig = MexPriceConfig::where('item_id', $itemId)->where('is_enabled', true)->first();
  33. if (!$priceConfig) {
  34. return ['success' => false, 'message' => '商品未配置价格信息'];
  35. }
  36. $totalAmount = bcmul($price, $quantity, 5);
  37. try {
  38. $order = MexOrder::create([
  39. 'user_id' => $userId,
  40. 'item_id' => $itemId,
  41. 'currency_type' => $currencyType->value,
  42. 'order_type' => OrderType::SELL,
  43. 'quantity' => $quantity,
  44. 'price' => $price,
  45. 'total_amount' => $totalAmount,
  46. 'status' => OrderStatus::PENDING,
  47. ]);
  48. // 触发订单创建事件
  49. event(new OrderCreatedEvent($order));
  50. return [
  51. 'success' => true,
  52. 'order_id' => $order->id,
  53. 'message' => '卖出订单创建成功,等待撮合'
  54. ];
  55. } catch (\Exception $e) {
  56. return ['success' => false, 'message' => '创建订单失败:' . $e->getMessage()];
  57. }
  58. }
  59. /**
  60. * 创建买入订单
  61. *
  62. * @param int $userId 用户ID
  63. * @param int $itemId 商品ID
  64. * @param int $quantity 数量
  65. * @param string $price 价格
  66. * @param FUND_CURRENCY_TYPE|null $currencyType 币种类型,默认使用钻石
  67. * @return array 操作结果
  68. */
  69. public static function createBuyOrder(int $userId, int $itemId, int $quantity, string $price, ?FUND_CURRENCY_TYPE $currencyType = null): array
  70. {
  71. // 获取币种类型,默认使用钻石
  72. $currencyType = $currencyType ?? FundLogic::getDefaultCurrency();
  73. // 验证价格配置是否存在(不验证价格范围和保护阈值,挂单阶段无价格验证)
  74. $priceConfig = MexPriceConfig::where('item_id', $itemId)->where('is_enabled', true)->first();
  75. if (!$priceConfig) {
  76. return ['success' => false, 'message' => '商品未配置价格信息'];
  77. }
  78. $totalAmount = bcmul($price, $quantity, 5);
  79. try {
  80. $order = MexOrder::create([
  81. 'user_id' => $userId,
  82. 'item_id' => $itemId,
  83. 'currency_type' => $currencyType->value,
  84. 'order_type' => OrderType::BUY,
  85. 'quantity' => $quantity,
  86. 'price' => $price,
  87. 'total_amount' => $totalAmount,
  88. 'status' => OrderStatus::PENDING,
  89. 'frozen_amount' => $totalAmount,
  90. ]);
  91. // 触发订单创建事件
  92. event(new OrderCreatedEvent($order));
  93. return [
  94. 'success' => true,
  95. 'order_id' => $order->id,
  96. 'message' => '买入订单创建成功,等待撮合'
  97. ];
  98. } catch (\Exception $e) {
  99. return ['success' => false, 'message' => '创建订单失败:' . $e->getMessage()];
  100. }
  101. }
  102. /**
  103. * 取消订单
  104. *
  105. * @param int $userId 用户ID
  106. * @param int $orderId 订单ID
  107. * @return array 操作结果
  108. */
  109. public static function cancelOrder(int $userId, int $orderId): array
  110. {
  111. $order = MexOrder::where('id', $orderId)->where('user_id', $userId)->first();
  112. if (!$order) {
  113. return ['success' => false, 'message' => '订单不存在'];
  114. }
  115. if ($order->status !== OrderStatus::PENDING) {
  116. return ['success' => false, 'message' => '只能取消等待中的订单'];
  117. }
  118. try {
  119. $order->update(['status' => OrderStatus::CANCELLED]);
  120. return ['success' => true, 'message' => '订单已取消'];
  121. } catch (\Exception $e) {
  122. return ['success' => false, 'message' => '取消订单失败:' . $e->getMessage()];
  123. }
  124. }
  125. /**
  126. * 获取用户订单列表
  127. *
  128. * @param int $userId 用户ID
  129. * @param int $page 页码
  130. * @param int $pageSize 每页数量
  131. * @return array 订单列表
  132. */
  133. public static function getUserOrders(int $userId, int $page = 1, int $pageSize = 20): array
  134. {
  135. $orders = MexOrder::where('user_id', $userId)
  136. ->orderBy('created_at', 'desc')
  137. ->paginate($pageSize, ['*'], 'page', $page);
  138. return [
  139. 'orders' => $orders->items(),
  140. 'total' => $orders->total(),
  141. 'page' => $page,
  142. 'page_size' => $pageSize,
  143. ];
  144. }
  145. /**
  146. * 获取订单详情
  147. *
  148. * @param int $userId 用户ID
  149. * @param int $orderId 订单ID
  150. * @return array|null 订单详情
  151. */
  152. public static function getOrderDetail(int $userId, int $orderId): ?array
  153. {
  154. $order = MexOrder::where('id', $orderId)->where('user_id', $userId)->first();
  155. return $order ? $order->toArray() : null;
  156. }
  157. /**
  158. * 获取待撮合的买入订单
  159. * 根据文档要求:二级排序(价格DESC + 时间ASC),移除数量排序
  160. *
  161. * @param int $itemId 商品ID
  162. * @param int $limit 限制数量
  163. * @return array 订单列表
  164. */
  165. public static function getPendingBuyOrders(int $itemId, int $limit = 100): array
  166. {
  167. $orders = MexOrder::where('item_id', $itemId)
  168. ->where('order_type', OrderType::BUY)
  169. ->where('status', OrderStatus::PENDING)
  170. ->orderBy('price', 'desc') // 价格优先(高价优先)
  171. ->orderBy('created_at', 'asc') // 时间优先(早下单优先)
  172. ->limit($limit)
  173. ->get();
  174. return $orders->toArray();
  175. }
  176. }