| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace App\Module\Mex\Logic;
- use App\Module\Mex\Models\MexOrder;
- use App\Module\Mex\Models\MexPriceConfig;
- use App\Module\Mex\Enums\OrderType;
- use App\Module\Mex\Enums\OrderStatus;
- use App\Module\Mex\Events\OrderCreatedEvent;
- use App\Module\Fund\Enums\FUND_CURRENCY_TYPE;
- use App\Module\Mex\Logic\FundLogic;
- /**
- * 农贸市场订单逻辑
- *
- * 处理订单相关的核心业务逻辑
- */
- class MexOrderLogic
- {
- /**
- * 创建卖出订单
- *
- * @param int $userId 用户ID
- * @param int $itemId 商品ID
- * @param int $quantity 数量
- * @param string $price 价格
- * @param FUND_CURRENCY_TYPE|null $currencyType 币种类型,默认使用钻石
- * @return array 操作结果
- */
- public static function createSellOrder(int $userId, int $itemId, int $quantity, string $price, ?FUND_CURRENCY_TYPE $currencyType = null): array
- {
- // 获取币种类型,默认使用钻石
- $currencyType = $currencyType ?? FundLogic::getDefaultCurrency();
- // 验证价格配置是否存在(不验证价格范围,挂单阶段无价格验证)
- $priceConfig = MexPriceConfig::where('item_id', $itemId)->where('is_enabled', true)->first();
- if (!$priceConfig) {
- return ['success' => false, 'message' => '商品未配置价格信息'];
- }
- $totalAmount = bcmul($price, $quantity, 5);
- try {
- $order = MexOrder::create([
- 'user_id' => $userId,
- 'item_id' => $itemId,
- 'currency_type' => $currencyType->value,
- 'order_type' => OrderType::SELL,
- 'quantity' => $quantity,
- 'price' => $price,
- 'total_amount' => $totalAmount,
- 'status' => OrderStatus::PENDING,
- ]);
- // 触发订单创建事件
- event(new OrderCreatedEvent($order));
- return [
- 'success' => true,
- 'order_id' => $order->id,
- 'message' => '卖出订单创建成功,等待撮合'
- ];
- } catch (\Exception $e) {
- return ['success' => false, 'message' => '创建订单失败:' . $e->getMessage()];
- }
- }
- /**
- * 创建买入订单
- *
- * @param int $userId 用户ID
- * @param int $itemId 商品ID
- * @param int $quantity 数量
- * @param string $price 价格
- * @param FUND_CURRENCY_TYPE|null $currencyType 币种类型,默认使用钻石
- * @return array 操作结果
- */
- public static function createBuyOrder(int $userId, int $itemId, int $quantity, string $price, ?FUND_CURRENCY_TYPE $currencyType = null): array
- {
- // 获取币种类型,默认使用钻石
- $currencyType = $currencyType ?? FundLogic::getDefaultCurrency();
- // 验证价格配置是否存在(不验证价格范围和保护阈值,挂单阶段无价格验证)
- $priceConfig = MexPriceConfig::where('item_id', $itemId)->where('is_enabled', true)->first();
- if (!$priceConfig) {
- return ['success' => false, 'message' => '商品未配置价格信息'];
- }
- $totalAmount = bcmul($price, $quantity, 5);
- try {
- $order = MexOrder::create([
- 'user_id' => $userId,
- 'item_id' => $itemId,
- 'currency_type' => $currencyType->value,
- 'order_type' => OrderType::BUY,
- 'quantity' => $quantity,
- 'price' => $price,
- 'total_amount' => $totalAmount,
- 'status' => OrderStatus::PENDING,
- 'frozen_amount' => $totalAmount,
- ]);
- // 触发订单创建事件
- event(new OrderCreatedEvent($order));
- return [
- 'success' => true,
- 'order_id' => $order->id,
- 'message' => '买入订单创建成功,等待撮合'
- ];
- } catch (\Exception $e) {
- return ['success' => false, 'message' => '创建订单失败:' . $e->getMessage()];
- }
- }
- /**
- * 取消订单
- *
- * @param int $userId 用户ID
- * @param int $orderId 订单ID
- * @return array 操作结果
- */
- public static function cancelOrder(int $userId, int $orderId): array
- {
- $order = MexOrder::where('id', $orderId)->where('user_id', $userId)->first();
- if (!$order) {
- return ['success' => false, 'message' => '订单不存在'];
- }
- if ($order->status !== OrderStatus::PENDING) {
- return ['success' => false, 'message' => '只能取消等待中的订单'];
- }
- try {
- $order->update(['status' => OrderStatus::CANCELLED]);
- return ['success' => true, 'message' => '订单已取消'];
- } catch (\Exception $e) {
- return ['success' => false, 'message' => '取消订单失败:' . $e->getMessage()];
- }
- }
- /**
- * 获取用户订单列表
- *
- * @param int $userId 用户ID
- * @param int $page 页码
- * @param int $pageSize 每页数量
- * @return array 订单列表
- */
- public static function getUserOrders(int $userId, int $page = 1, int $pageSize = 20): array
- {
- $orders = MexOrder::where('user_id', $userId)
- ->orderBy('created_at', 'desc')
- ->paginate($pageSize, ['*'], 'page', $page);
- return [
- 'orders' => $orders->items(),
- 'total' => $orders->total(),
- 'page' => $page,
- 'page_size' => $pageSize,
- ];
- }
- /**
- * 获取订单详情
- *
- * @param int $userId 用户ID
- * @param int $orderId 订单ID
- * @return array|null 订单详情
- */
- public static function getOrderDetail(int $userId, int $orderId): ?array
- {
- $order = MexOrder::where('id', $orderId)->where('user_id', $userId)->first();
- return $order ? $order->toArray() : null;
- }
- /**
- * 获取待撮合的买入订单
- * 根据文档要求:二级排序(价格DESC + 时间ASC),移除数量排序
- *
- * @param int $itemId 商品ID
- * @param int $limit 限制数量
- * @return array 订单列表
- */
- public static function getPendingBuyOrders(int $itemId, int $limit = 100): array
- {
- $orders = MexOrder::where('item_id', $itemId)
- ->where('order_type', OrderType::BUY)
- ->where('status', OrderStatus::PENDING)
- ->orderBy('price', 'desc') // 价格优先(高价优先)
- ->orderBy('created_at', 'asc') // 时间优先(早下单优先)
- ->limit($limit)
- ->get();
- return $orders->toArray();
- }
- }
|