|
|
@@ -6,9 +6,7 @@ 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\Logic\MexAccountLogic;
|
|
|
use App\Module\Mex\Events\OrderCreatedEvent;
|
|
|
-use App\Module\Mex\Events\OrderCompletedEvent;
|
|
|
|
|
|
/**
|
|
|
* 农贸市场订单逻辑
|
|
|
@@ -19,7 +17,7 @@ class MexOrderLogic
|
|
|
{
|
|
|
/**
|
|
|
* 创建卖出订单
|
|
|
- *
|
|
|
+ *
|
|
|
* @param int $userId 用户ID
|
|
|
* @param int $itemId 商品ID
|
|
|
* @param int $quantity 数量
|
|
|
@@ -28,17 +26,12 @@ class MexOrderLogic
|
|
|
*/
|
|
|
public static function createSellOrder(int $userId, int $itemId, int $quantity, string $price): array
|
|
|
{
|
|
|
- // 验证价格配置
|
|
|
+ // 验证价格配置是否存在(不验证价格范围,挂单阶段无价格验证)
|
|
|
$priceConfig = MexPriceConfig::where('item_id', $itemId)->where('is_enabled', true)->first();
|
|
|
if (!$priceConfig) {
|
|
|
return ['success' => false, 'message' => '商品未配置价格信息'];
|
|
|
}
|
|
|
|
|
|
- // 验证卖出价格
|
|
|
- if (bccomp($price, $priceConfig->min_price, 5) > 0) {
|
|
|
- return ['success' => false, 'message' => "卖出价格不能高于最低价 {$priceConfig->min_price}"];
|
|
|
- }
|
|
|
-
|
|
|
$totalAmount = bcmul($price, $quantity, 5);
|
|
|
|
|
|
try {
|
|
|
@@ -55,10 +48,11 @@ class MexOrderLogic
|
|
|
// 触发订单创建事件
|
|
|
event(new OrderCreatedEvent($order));
|
|
|
|
|
|
- // 卖出订单立即处理
|
|
|
- $result = self::processSellOrderImmediately($order);
|
|
|
-
|
|
|
- return ['success' => true, 'order_id' => $order->id, 'result' => $result];
|
|
|
+ return [
|
|
|
+ 'success' => true,
|
|
|
+ 'order_id' => $order->id,
|
|
|
+ 'message' => '卖出订单创建成功,等待撮合'
|
|
|
+ ];
|
|
|
} catch (\Exception $e) {
|
|
|
return ['success' => false, 'message' => '创建订单失败:' . $e->getMessage()];
|
|
|
}
|
|
|
@@ -66,7 +60,7 @@ class MexOrderLogic
|
|
|
|
|
|
/**
|
|
|
* 创建买入订单
|
|
|
- *
|
|
|
+ *
|
|
|
* @param int $userId 用户ID
|
|
|
* @param int $itemId 商品ID
|
|
|
* @param int $quantity 数量
|
|
|
@@ -75,22 +69,12 @@ class MexOrderLogic
|
|
|
*/
|
|
|
public static function createBuyOrder(int $userId, int $itemId, int $quantity, string $price): array
|
|
|
{
|
|
|
- // 验证价格配置
|
|
|
+ // 验证价格配置是否存在(不验证价格范围和保护阈值,挂单阶段无价格验证)
|
|
|
$priceConfig = MexPriceConfig::where('item_id', $itemId)->where('is_enabled', true)->first();
|
|
|
if (!$priceConfig) {
|
|
|
return ['success' => false, 'message' => '商品未配置价格信息'];
|
|
|
}
|
|
|
|
|
|
- // 验证买入价格
|
|
|
- if (bccomp($price, $priceConfig->max_price, 5) < 0) {
|
|
|
- return ['success' => false, 'message' => "买入价格不能低于最高价 {$priceConfig->max_price}"];
|
|
|
- }
|
|
|
-
|
|
|
- // 验证数量保护阈值
|
|
|
- if ($quantity > $priceConfig->protection_threshold) {
|
|
|
- return ['success' => false, 'message' => "订单数量不能超过保护阈值 {$priceConfig->protection_threshold}"];
|
|
|
- }
|
|
|
-
|
|
|
$totalAmount = bcmul($price, $quantity, 5);
|
|
|
|
|
|
try {
|
|
|
@@ -108,108 +92,17 @@ class MexOrderLogic
|
|
|
// 触发订单创建事件
|
|
|
event(new OrderCreatedEvent($order));
|
|
|
|
|
|
- // 买入订单立即处理
|
|
|
- $result = self::processBuyOrderImmediately($order);
|
|
|
-
|
|
|
- return ['success' => true, 'order_id' => $order->id, 'result' => $result];
|
|
|
- } catch (\Exception $e) {
|
|
|
- return ['success' => false, 'message' => '创建订单失败:' . $e->getMessage()];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 立即处理卖出订单
|
|
|
- *
|
|
|
- * @param MexOrder $order 订单
|
|
|
- * @return array 处理结果
|
|
|
- */
|
|
|
- private static function processSellOrderImmediately(MexOrder $order): array
|
|
|
- {
|
|
|
- // 调用账户流转逻辑处理卖出订单
|
|
|
- $result = MexAccountLogic::processSellOrder(
|
|
|
- $order->user_id,
|
|
|
- $order->item_id,
|
|
|
- $order->quantity,
|
|
|
- $order->price,
|
|
|
- $order->total_amount,
|
|
|
- $order->id
|
|
|
- );
|
|
|
-
|
|
|
- if ($result['success']) {
|
|
|
- // 更新订单状态为已完成
|
|
|
- $order->update([
|
|
|
- 'status' => OrderStatus::COMPLETED,
|
|
|
- 'completed_quantity' => $order->quantity,
|
|
|
- 'completed_amount' => $order->total_amount,
|
|
|
- 'completed_at' => now(),
|
|
|
- ]);
|
|
|
-
|
|
|
- // 触发订单完成事件
|
|
|
- event(new OrderCompletedEvent($order));
|
|
|
-
|
|
|
return [
|
|
|
'success' => true,
|
|
|
- 'message' => '卖出订单已完成',
|
|
|
- 'transaction_id' => $result['transaction_id'] ?? null
|
|
|
+ 'order_id' => $order->id,
|
|
|
+ 'message' => '买入订单创建成功,等待撮合'
|
|
|
];
|
|
|
- } else {
|
|
|
- // 处理失败,更新订单状态为失败
|
|
|
- $order->update([
|
|
|
- 'status' => OrderStatus::FAILED,
|
|
|
- 'failed_reason' => $result['message'],
|
|
|
- 'completed_at' => now(),
|
|
|
- ]);
|
|
|
-
|
|
|
- return $result;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ return ['success' => false, 'message' => '创建订单失败:' . $e->getMessage()];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 立即处理买入订单
|
|
|
- *
|
|
|
- * @param MexOrder $order 订单
|
|
|
- * @return array 处理结果
|
|
|
- */
|
|
|
- private static function processBuyOrderImmediately(MexOrder $order): array
|
|
|
- {
|
|
|
- // 调用账户流转逻辑处理买入订单
|
|
|
- $result = MexAccountLogic::processBuyOrder(
|
|
|
- $order->user_id,
|
|
|
- $order->item_id,
|
|
|
- $order->quantity,
|
|
|
- $order->price,
|
|
|
- $order->total_amount,
|
|
|
- $order->id
|
|
|
- );
|
|
|
-
|
|
|
- if ($result['success']) {
|
|
|
- // 更新订单状态为已完成
|
|
|
- $order->update([
|
|
|
- 'status' => OrderStatus::COMPLETED,
|
|
|
- 'completed_quantity' => $order->quantity,
|
|
|
- 'completed_amount' => $order->total_amount,
|
|
|
- 'completed_at' => now(),
|
|
|
- ]);
|
|
|
|
|
|
- // 触发订单完成事件
|
|
|
- event(new OrderCompletedEvent($order));
|
|
|
-
|
|
|
- return [
|
|
|
- 'success' => true,
|
|
|
- 'message' => '买入订单已完成',
|
|
|
- 'transaction_id' => $result['transaction_id'] ?? null
|
|
|
- ];
|
|
|
- } else {
|
|
|
- // 处理失败,更新订单状态为失败
|
|
|
- $order->update([
|
|
|
- 'status' => OrderStatus::FAILED,
|
|
|
- 'failed_reason' => $result['message'],
|
|
|
- 'completed_at' => now(),
|
|
|
- ]);
|
|
|
-
|
|
|
- return $result;
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 取消订单
|