| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?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;
- /**
- * Transfer模块对外服务接口
- * 供其他模块调用的主要服务类
- */
- class TransferService
- {
- /**
- * 创建转出订单
- *
- * @param array $data 订单数据
- * @return TransferOrderDto|string 成功返回DTO,失败返回错误信息
- */
- public static function createTransferOut(array $data): TransferOrderDto|string
- {
- try {
- $order = TransferLogic::createTransferOut($data);
- return TransferOrderDto::fromModel($order);
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- }
- /**
- * 创建转入订单
- *
- * @param array $data 订单数据
- * @return TransferOrderDto|string 成功返回DTO,失败返回错误信息
- */
- public static function createTransferIn(array $data): TransferOrderDto|string
- {
- try {
- $order = TransferLogic::createTransferIn($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;
- }
- }
- }
|