| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- <?php
- namespace App\Module\Transfer\Services;
- use App\Module\Transfer\Dtos\TransferAppDto;
- use App\Module\Transfer\Dtos\TransferOrderDto;
- use App\Module\Transfer\Logics\TransferLogic;
- use App\Module\Transfer\Models\TransferApp;
- use App\Module\Transfer\Models\TransferOrder;
- use App\Module\Transfer\Services\FeeService;
- /**
- * Transfer模块对外服务接口
- * 供其他模块调用的主要服务类
- */
- class TransferService
- {
- /**
- * 创建转出订单
- *
- * @param int $transferAppId 划转应用ID
- * @param int $userId 用户ID
- * @param string $amount 转出金额
- * @param string $password 安全密码
- * @param string|null $googleCode 谷歌验证码
- * @param string|null $outUserId 外部用户ID
- * @param string|null $remark 备注
- * @param array $callbackData 回调数据
- * @return TransferOrderDto|string 成功返回DTO,失败返回错误信息
- */
- public static function createTransferOut(
- int $transferAppId,
- int $userId,
- string $amount,
- string $password,
- ?string $googleCode = null,
- ?string $outUserId = null,
- ?string $remark = null,
- array $callbackData = []
- ): TransferOrderDto|string {
- try {
- $data = [
- 'transfer_app_id' => $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,
- 'business_id' => $businessId,
- 'amount' => $amount,
- 'out_user_id' => $outUserId,
- 'remark' => $remark,
- 'callback_data' => $callbackData,
- ];
- $order = TransferLogic::createTransferInFromArray($data);
- 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 array $data 订单数据
- * @return TransferOrderDto|string
- * @deprecated 建议使用 createTransferOut() 方法
- */
- public static function createTransferOutFromArray(array $data): TransferOrderDto|string
- {
- try {
- $order = TransferLogic::createTransferOutFromArray($data);
- return TransferOrderDto::fromModel($order);
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- }
- /**
- * 从数组创建转入订单(向后兼容方法)
- *
- * @param array $data 订单数据
- * @return TransferOrderDto|string
- * @deprecated 建议使用 createTransferIn() 方法
- */
- public static function createTransferInFromArray(array $data): TransferOrderDto|string
- {
- try {
- $order = TransferLogic::createTransferInFromArray($data);
- return TransferOrderDto::fromModel($order);
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- }
- /**
- * 计算转入手续费
- *
- * @param int $transferAppId 划转应用ID
- * @param string $amount 转入金额
- * @return array ['fee_rate' => 手续费率, 'fee_amount' => 手续费金额, 'actual_amount' => 实际到账金额]
- */
- public static function calculateInFee(int $transferAppId, string $amount): array
- {
- try {
- $app = TransferApp::findOrFail($transferAppId);
- return FeeService::calculateInFee($app, $amount);
- } catch (\Exception $e) {
- return [
- 'fee_rate' => 0.0000,
- 'fee_amount' => '0.0000',
- 'actual_amount' => $amount,
- 'error' => $e->getMessage(),
- ];
- }
- }
- /**
- * 计算转出手续费
- *
- * @param int $transferAppId 划转应用ID
- * @param string $amount 转出金额
- * @return array ['fee_rate' => 手续费率, 'fee_amount' => 手续费金额, 'actual_amount' => 实际到账金额]
- */
- public static function calculateOutFee(int $transferAppId, string $amount): array
- {
- try {
- $app = TransferApp::findOrFail($transferAppId);
- return FeeService::calculateOutFee($app, $amount);
- } catch (\Exception $e) {
- return [
- 'fee_rate' => 0.0000,
- 'fee_amount' => '0.0000',
- 'actual_amount' => $amount,
- 'error' => $e->getMessage(),
- ];
- }
- }
- /**
- * 获取应用的手续费配置
- *
- * @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' => [],
- ];
- }
- }
- }
|