MexOrderLogic.php 6.8 KB

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