$transferAppId, 'user_id' => $userId, 'amount' => $amount, 'password' => $password, 'google_code' => $googleCode, 'out_user_id' => $outUserId, 'remark' => $remark, 'callback_data' => $callbackData, ]; $order = TransferLogic::createTransferOutFromArray($data); return TransferOrderDto::fromModel($order); } catch (\Exception $e) { return $e->getMessage(); } } /** * 创建转入订单 * * @param int $transferAppId 划转应用ID * @param int $userId 用户ID * @param string $businessId 业务订单ID * @param string $amount 转入金额 * @param string|null $outUserId 外部用户ID * @param string|null $remark 备注 * @param array $callbackData 回调数据 * @return TransferOrderDto|string 成功返回DTO,失败返回错误信息 */ public static function createTransferIn( int $transferAppId, int $userId, string $businessId, string $amount, ?string $outUserId = null, ?string $remark = null, array $callbackData = [] ): TransferOrderDto|string { try { $data = [ 'transfer_app_id' => $transferAppId, 'user_id' => $userId, 'out_order_id' => $businessId, 'amount' => $amount, 'out_user_id' => $outUserId, 'remark' => $remark, 'callback_data' => $callbackData, ]; $order = TransferLogic::createTransferInFromArray($data, $userId); return TransferOrderDto::fromModel($order); } catch (\Exception $e) { return $e->getMessage(); } } /** * 查询订单信息 * * @param int $orderId 订单ID * @param int|null $userId 用户ID(可选,用于权限验证) * @return TransferOrderDto|null */ public static function getOrderInfo(int $orderId, ?int $userId = null): ?TransferOrderDto { $query = TransferOrder::where('id', $orderId); if ($userId !== null) { $query->where('user_id', $userId); } $order = $query->first(); return $order ? TransferOrderDto::fromModel($order) : null; } /** * 根据外部订单ID查询订单信息 * * @param string $outOrderId 外部订单ID * @param int $outId 开放接口ID * @return TransferOrderDto|null */ public static function getOrderByOutId(string $outOrderId, int $outId): ?TransferOrderDto { $order = TransferOrder::where('out_order_id', $outOrderId) ->where('out_id', $outId) ->first(); return $order ? TransferOrderDto::fromModel($order) : null; } /** * 获取用户可用的划转应用列表 * * @param int $userId 用户ID * @return TransferAppDto[] */ public static function getAvailableApps(int $userId): array { $apps = TransferApp::where('is_enabled', true)->get(); return $apps->map(function ($app) { return TransferAppDto::fromModel($app); })->toArray(); } /** * 获取用户订单列表 * * @param int $userId 用户ID * @param array $filters 筛选条件 * @return array */ public static function getUserOrders(int $userId, array $filters = []): array { $query = TransferOrder::where('user_id', $userId); // 应用筛选条件 if (isset($filters['type'])) { $query->where('type', $filters['type']); } if (isset($filters['status'])) { $query->where('status', $filters['status']); } if (isset($filters['transfer_app_id'])) { $query->where('transfer_app_id', $filters['transfer_app_id']); } if (isset($filters['start_date'])) { $query->where('created_at', '>=', $filters['start_date']); } if (isset($filters['end_date'])) { $query->where('created_at', '<=', $filters['end_date']); } // 分页参数 $page = $filters['page'] ?? 1; $perPage = $filters['per_page'] ?? 20; $orders = $query->orderBy('created_at', 'desc') ->paginate($perPage, [ '*' ], 'page', $page); return [ 'data' => $orders->items(), 'current_page' => $orders->currentPage(), 'per_page' => $orders->perPage(), 'total' => $orders->total(), 'last_page' => $orders->lastPage(), ]; } /** * 获取应用配置信息 * * @param int $appId 应用ID * @return TransferAppDto|null */ public static function getAppConfig(int $appId): ?TransferAppDto { $app = TransferApp::find($appId); return $app ? TransferAppDto::fromModel($app) : null; } /** * 根据应用标识获取配置信息 * * @param string $keyname 应用标识 * @return TransferAppDto|null */ public static function getAppByKeyname(string $keyname): ?TransferAppDto { $app = TransferApp::where('keyname', $keyname) ->where('is_enabled', true) ->first(); return $app ? TransferAppDto::fromModel($app) : null; } /** * 处理回调结果 * * @param array $callbackData 回调数据 * @return bool */ public static function processCallback(array $callbackData): bool { try { return TransferLogic::processCallback($callbackData); } catch (\Exception $e) { \Log::error('Transfer callback processing failed', [ 'error' => $e->getMessage(), 'data' => $callbackData ]); return false; } } /** * 快速创建转出订单(简化版本,用于测试或内部调用) * * @param int $transferAppId 划转应用ID * @param int $userId 用户ID * @param string $amount 转出金额 * @param string $password 安全密码 * @return TransferOrderDto|string */ public static function quickCreateTransferOut( int $transferAppId, int $userId, string $amount, string $password ): TransferOrderDto|string { return self::createTransferOut( transferAppId: $transferAppId, userId: $userId, amount: $amount, password: $password ); } /** * 快速创建转入订单(简化版本,用于测试或内部调用) * * @param int $transferAppId 划转应用ID * @param int $userId 用户ID * @param string $businessId 业务订单ID * @param string $amount 转入金额 * @return TransferOrderDto|string */ public static function quickCreateTransferIn( int $transferAppId, int $userId, string $businessId, string $amount ): TransferOrderDto|string { return self::createTransferIn( transferAppId: $transferAppId, userId: $userId, businessId: $businessId, amount: $amount ); } /** * 获取应用的手续费配置 * * @param int $transferAppId 划转应用ID * @return array */ public static function getFeeConfig(int $transferAppId): array { try { $app = TransferApp::findOrFail($transferAppId); return FeeService::getFeeConfig($app); } catch (\Exception $e) { return [ 'error' => $e->getMessage(), 'in' => [ 'rate' => 0, 'min' => 0, 'max' => 0, 'enabled' => false ], 'out' => [ 'rate' => 0, 'min' => 0, 'max' => 0, 'enabled' => false ], 'account_uid' => 0, ]; } } /** * 获取手续费统计信息 * * @param int $appId 应用ID(0表示所有应用) * @param string $startDate 开始日期 * @param string $endDate 结束日期 * @return array */ public static function getFeeStatistics(int $appId = 0, string $startDate = '', string $endDate = ''): array { try { return FeeService::getFeeStatistics($appId, $startDate, $endDate); } catch (\Exception $e) { return [ 'error' => $e->getMessage(), 'total_orders' => 0, 'total_fee' => 0, 'avg_fee_rate' => 0, 'in_orders' => 0, 'in_fee' => 0, 'out_orders' => 0, 'out_fee' => 0, ]; } } /** * 获取应用的手续费收入统计 * * @param int $appId 应用ID * @param int $days 统计天数 * @return array */ public static function getAppFeeIncome(int $appId, int $days = 30): array { try { return FeeService::getAppFeeIncome($appId, $days); } catch (\Exception $e) { return [ 'error' => $e->getMessage(), 'app_id' => $appId, 'days' => $days, 'total_fee' => 0, 'total_orders' => 0, 'avg_daily_fee' => 0, 'daily_stats' => [], ]; } } }