|
|
@@ -32,6 +32,9 @@ class MexOrderLogic
|
|
|
*/
|
|
|
public static function createSellOrder(int $userId, int $itemId, int $quantity, float $price, ?FUND_CURRENCY_TYPE $currencyType = null): array
|
|
|
{
|
|
|
+ // 检查事务状态,确保调用者已开启事务
|
|
|
+ \UCore\Db\Helper::check_tr();
|
|
|
+
|
|
|
// 获取币种类型,默认使用钻石
|
|
|
$currencyType = $currencyType ?? FundLogic::getDefaultCurrency();
|
|
|
|
|
|
@@ -44,57 +47,55 @@ class MexOrderLogic
|
|
|
// 直接计算总金额,信任Fund模块的精度处理
|
|
|
$totalAmount = $price * $quantity;
|
|
|
|
|
|
- return DB::transaction(function () use ($userId, $itemId, $quantity, $price, $currencyType, $totalAmount) {
|
|
|
- try {
|
|
|
- // 1. 验证用户是否有足够的物品
|
|
|
- $checkResult = ItemService::checkItemQuantity($userId, $itemId, $quantity);
|
|
|
- if (!$checkResult->success) {
|
|
|
- return ['success' => false, 'message' => $checkResult->message];
|
|
|
- }
|
|
|
-
|
|
|
- // 2. 创建订单记录
|
|
|
- $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,
|
|
|
- ]);
|
|
|
-
|
|
|
- // 3. 冻结用户物品
|
|
|
- $freezeResult = ItemService::freezeItem(
|
|
|
- $userId,
|
|
|
- $itemId,
|
|
|
- null, // 统一属性物品,不需要实例ID
|
|
|
- $quantity,
|
|
|
- "农贸市场卖出订单冻结,订单ID:{$order->id}",
|
|
|
- [
|
|
|
- 'reason_type' => FREEZE_REASON_TYPE::TRADE_ORDER->value,
|
|
|
- 'source_id' => $order->id,
|
|
|
- 'source_type' => 'mex_sell_order',
|
|
|
- ]
|
|
|
- );
|
|
|
-
|
|
|
- if (!$freezeResult['success']) {
|
|
|
- throw new \Exception('冻结物品失败:' . $freezeResult['message']);
|
|
|
- }
|
|
|
-
|
|
|
- // 4. 触发订单创建事件
|
|
|
- event(new OrderCreatedEvent($order));
|
|
|
-
|
|
|
- return [
|
|
|
- 'success' => true,
|
|
|
- 'order_id' => $order->id,
|
|
|
- 'freeze_log_id' => $freezeResult['freeze_log_id'] ?? null,
|
|
|
- 'message' => '卖出订单创建成功,等待撮合'
|
|
|
- ];
|
|
|
- } catch (\Exception $e) {
|
|
|
- return ['success' => false, 'message' => '创建订单失败:' . $e->getMessage()];
|
|
|
+ try {
|
|
|
+ // 1. 验证用户是否有足够的物品
|
|
|
+ $checkResult = ItemService::checkItemQuantity($userId, $itemId, $quantity);
|
|
|
+ if (!$checkResult->success) {
|
|
|
+ return ['success' => false, 'message' => $checkResult->message];
|
|
|
}
|
|
|
- });
|
|
|
+
|
|
|
+ // 2. 创建订单记录
|
|
|
+ $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,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 3. 冻结用户物品
|
|
|
+ $freezeResult = ItemService::freezeItem(
|
|
|
+ $userId,
|
|
|
+ $itemId,
|
|
|
+ null, // 统一属性物品,不需要实例ID
|
|
|
+ $quantity,
|
|
|
+ "农贸市场卖出订单冻结,订单ID:{$order->id}",
|
|
|
+ [
|
|
|
+ 'reason_type' => FREEZE_REASON_TYPE::TRADE_ORDER->value,
|
|
|
+ 'source_id' => $order->id,
|
|
|
+ 'source_type' => 'mex_sell_order',
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ if (!$freezeResult['success']) {
|
|
|
+ throw new \Exception('冻结物品失败:' . $freezeResult['message']);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 触发订单创建事件
|
|
|
+ event(new OrderCreatedEvent($order));
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'success' => true,
|
|
|
+ 'order_id' => $order->id,
|
|
|
+ 'freeze_log_id' => $freezeResult['freeze_log_id'] ?? null,
|
|
|
+ 'message' => '卖出订单创建成功,等待撮合'
|
|
|
+ ];
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ return ['success' => false, 'message' => '创建订单失败:' . $e->getMessage()];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -109,6 +110,9 @@ class MexOrderLogic
|
|
|
*/
|
|
|
public static function createBuyOrder(int $userId, int $itemId, int $quantity, float $price, ?FUND_CURRENCY_TYPE $currencyType = null): array
|
|
|
{
|
|
|
+ // 检查事务状态,确保调用者已开启事务
|
|
|
+ \UCore\Db\Helper::check_tr();
|
|
|
+
|
|
|
// 获取币种类型,默认使用钻石
|
|
|
$currencyType = $currencyType ?? FundLogic::getDefaultCurrency();
|
|
|
|