|
@@ -126,6 +126,7 @@ class MexOrderLogic
|
|
|
$totalAmount = $price * $quantity;
|
|
$totalAmount = $price * $quantity;
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
|
|
+ // 1. 创建订单记录
|
|
|
$order = MexOrder::create([
|
|
$order = MexOrder::create([
|
|
|
'user_id' => $userId,
|
|
'user_id' => $userId,
|
|
|
'item_id' => $itemId,
|
|
'item_id' => $itemId,
|
|
@@ -138,6 +139,12 @@ class MexOrderLogic
|
|
|
'frozen_amount' => $totalAmount,
|
|
'frozen_amount' => $totalAmount,
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
|
|
+ // 2. 冻结用户资金
|
|
|
|
|
+ $freezeResult = self::freezeOrderFunds($order);
|
|
|
|
|
+ if (!$freezeResult['success']) {
|
|
|
|
|
+ return ['success' => false, 'message' => '冻结资金失败:' . $freezeResult['message']];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 触发订单创建事件
|
|
// 触发订单创建事件
|
|
|
event(new OrderCreatedEvent($order));
|
|
event(new OrderCreatedEvent($order));
|
|
|
|
|
|
|
@@ -279,6 +286,55 @@ class MexOrderLogic
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 冻结订单相关的资金(买入订单创建时使用)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param MexOrder $order 订单对象
|
|
|
|
|
+ * @return array 冻结结果
|
|
|
|
|
+ */
|
|
|
|
|
+ private static function freezeOrderFunds(MexOrder $order): array
|
|
|
|
|
+ {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 获取可用账户类型和冻结账户类型
|
|
|
|
|
+ $availableAccountType = FundLogic::getAvailableAccountType($order->currency_type);
|
|
|
|
|
+ $frozenAccountType = FundLogic::getFrozenAccountType($order->currency_type);
|
|
|
|
|
+
|
|
|
|
|
+ if (!$availableAccountType || !$frozenAccountType) {
|
|
|
|
|
+ return ['success' => false, 'message' => '不支持的币种类型'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 创建Fund服务实例(可用账户)
|
|
|
|
|
+ $fundService = new \App\Module\Fund\Services\FundService($order->user_id, $availableAccountType->value);
|
|
|
|
|
+
|
|
|
|
|
+ // 验证用户可用余额是否充足
|
|
|
|
|
+ if ($fundService->balance() < $order->frozen_amount) {
|
|
|
|
|
+ return ['success' => false, 'message' => '可用余额不足'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 使用circulation方法将资金从可用账户转移到冻结账户
|
|
|
|
|
+ $circulationResult = $fundService->circulation(
|
|
|
|
|
+ $frozenAccountType,
|
|
|
|
|
+ $order->frozen_amount,
|
|
|
|
|
+ $order->id,
|
|
|
|
|
+ 'mex_buy_order',
|
|
|
|
|
+ "农贸市场买入订单冻结,订单ID:{$order->id}"
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ if (is_string($circulationResult)) {
|
|
|
|
|
+ return ['success' => false, 'message' => '资金冻结失败:' . $circulationResult];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return [
|
|
|
|
|
+ 'success' => true,
|
|
|
|
|
+ 'message' => '资金冻结成功',
|
|
|
|
|
+ 'frozen_amount' => $order->frozen_amount,
|
|
|
|
|
+ 'circulation_id' => $circulationResult->id ?? null
|
|
|
|
|
+ ];
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ return ['success' => false, 'message' => '冻结资金异常:' . $e->getMessage()];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 解冻订单相关的资金(买入订单取消时使用)
|
|
* 解冻订单相关的资金(买入订单取消时使用)
|
|
|
*
|
|
*
|
|
@@ -288,20 +344,43 @@ class MexOrderLogic
|
|
|
private static function unfreezeOrderFunds(MexOrder $order): array
|
|
private static function unfreezeOrderFunds(MexOrder $order): array
|
|
|
{
|
|
{
|
|
|
try {
|
|
try {
|
|
|
- // 买入订单的资金冻结处理
|
|
|
|
|
- // 注意:当前买入订单创建时没有实际冻结资金,只是记录了frozen_amount
|
|
|
|
|
- // 这里主要是为了保持接口一致性,实际可能不需要特殊处理
|
|
|
|
|
|
|
+ // 获取可用账户类型和冻结账户类型
|
|
|
|
|
+ $availableAccountType = FundLogic::getAvailableAccountType($order->currency_type);
|
|
|
|
|
+ $frozenAccountType = FundLogic::getFrozenAccountType($order->currency_type);
|
|
|
|
|
|
|
|
- // 如果将来需要实际冻结资金,可以在这里添加解冻逻辑
|
|
|
|
|
- // 例如:调用Fund模块的解冻接口
|
|
|
|
|
|
|
+ if (!$availableAccountType || !$frozenAccountType) {
|
|
|
|
|
+ return ['success' => false, 'message' => '不支持的币种类型'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 创建Fund服务实例(冻结账户)
|
|
|
|
|
+ $fundService = new \App\Module\Fund\Services\FundService($order->user_id, $frozenAccountType->value);
|
|
|
|
|
+
|
|
|
|
|
+ // 验证冻结账户余额是否充足
|
|
|
|
|
+ if ($fundService->balance() < $order->frozen_amount) {
|
|
|
|
|
+ return ['success' => false, 'message' => '冻结账户余额不足,可能已被解冻'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 使用circulation方法将资金从冻结账户转移回可用账户
|
|
|
|
|
+ $circulationResult = $fundService->circulation(
|
|
|
|
|
+ $availableAccountType,
|
|
|
|
|
+ $order->frozen_amount,
|
|
|
|
|
+ $order->id,
|
|
|
|
|
+ 'mex_buy_order_cancel',
|
|
|
|
|
+ "农贸市场买入订单取消解冻,订单ID:{$order->id}"
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ if (is_string($circulationResult)) {
|
|
|
|
|
+ return ['success' => false, 'message' => '资金解冻失败:' . $circulationResult];
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
return [
|
|
return [
|
|
|
'success' => true,
|
|
'success' => true,
|
|
|
- 'message' => '买入订单资金解冻完成',
|
|
|
|
|
- 'note' => '当前买入订单未实际冻结资金,仅更新订单状态'
|
|
|
|
|
|
|
+ 'message' => '资金解冻成功',
|
|
|
|
|
+ 'unfrozen_amount' => $order->frozen_amount,
|
|
|
|
|
+ 'circulation_id' => $circulationResult->id ?? null
|
|
|
];
|
|
];
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
- return ['success' => false, 'message' => $e->getMessage()];
|
|
|
|
|
|
|
+ return ['success' => false, 'message' => '解冻资金异常:' . $e->getMessage()];
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|