TransferService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace App\Module\Transfer\Services;
  3. use App\Module\Transfer\Dtos\TransferAppDto;
  4. use App\Module\Transfer\Dtos\TransferOrderDto;
  5. use App\Module\Transfer\Logics\TransferLogic;
  6. use App\Module\Transfer\Models\TransferApp;
  7. use App\Module\Transfer\Models\TransferOrder;
  8. /**
  9. * Transfer模块对外服务接口
  10. * 供其他模块调用的主要服务类
  11. */
  12. class TransferService
  13. {
  14. /**
  15. * 创建转出订单
  16. *
  17. * @param array $data 订单数据
  18. * @return TransferOrderDto|string 成功返回DTO,失败返回错误信息
  19. */
  20. public static function createTransferOut(array $data): TransferOrderDto|string
  21. {
  22. try {
  23. $order = TransferLogic::createTransferOut($data);
  24. return TransferOrderDto::fromModel($order);
  25. } catch (\Exception $e) {
  26. return $e->getMessage();
  27. }
  28. }
  29. /**
  30. * 创建转入订单
  31. *
  32. * @param array $data 订单数据
  33. * @return TransferOrderDto|string 成功返回DTO,失败返回错误信息
  34. */
  35. public static function createTransferIn(array $data): TransferOrderDto|string
  36. {
  37. try {
  38. $order = TransferLogic::createTransferIn($data);
  39. return TransferOrderDto::fromModel($order);
  40. } catch (\Exception $e) {
  41. return $e->getMessage();
  42. }
  43. }
  44. /**
  45. * 查询订单信息
  46. *
  47. * @param int $orderId 订单ID
  48. * @param int|null $userId 用户ID(可选,用于权限验证)
  49. * @return TransferOrderDto|null
  50. */
  51. public static function getOrderInfo(int $orderId, ?int $userId = null): ?TransferOrderDto
  52. {
  53. $query = TransferOrder::where('id', $orderId);
  54. if ($userId !== null) {
  55. $query->where('user_id', $userId);
  56. }
  57. $order = $query->first();
  58. return $order ? TransferOrderDto::fromModel($order) : null;
  59. }
  60. /**
  61. * 根据外部订单ID查询订单信息
  62. *
  63. * @param string $outOrderId 外部订单ID
  64. * @param int $outId 开放接口ID
  65. * @return TransferOrderDto|null
  66. */
  67. public static function getOrderByOutId(string $outOrderId, int $outId): ?TransferOrderDto
  68. {
  69. $order = TransferOrder::where('out_order_id', $outOrderId)
  70. ->where('out_id', $outId)
  71. ->first();
  72. return $order ? TransferOrderDto::fromModel($order) : null;
  73. }
  74. /**
  75. * 获取用户可用的划转应用列表
  76. *
  77. * @param int $userId 用户ID
  78. * @return TransferAppDto[]
  79. */
  80. public static function getAvailableApps(int $userId): array
  81. {
  82. $apps = TransferApp::where('is_enabled', true)->get();
  83. return $apps->map(function ($app) {
  84. return TransferAppDto::fromModel($app);
  85. })->toArray();
  86. }
  87. /**
  88. * 获取用户订单列表
  89. *
  90. * @param int $userId 用户ID
  91. * @param array $filters 筛选条件
  92. * @return array
  93. */
  94. public static function getUserOrders(int $userId, array $filters = []): array
  95. {
  96. $query = TransferOrder::where('user_id', $userId);
  97. // 应用筛选条件
  98. if (isset($filters['type'])) {
  99. $query->where('type', $filters['type']);
  100. }
  101. if (isset($filters['status'])) {
  102. $query->where('status', $filters['status']);
  103. }
  104. if (isset($filters['transfer_app_id'])) {
  105. $query->where('transfer_app_id', $filters['transfer_app_id']);
  106. }
  107. if (isset($filters['start_date'])) {
  108. $query->where('created_at', '>=', $filters['start_date']);
  109. }
  110. if (isset($filters['end_date'])) {
  111. $query->where('created_at', '<=', $filters['end_date']);
  112. }
  113. // 分页参数
  114. $page = $filters['page'] ?? 1;
  115. $perPage = $filters['per_page'] ?? 20;
  116. $orders = $query->orderBy('created_at', 'desc')
  117. ->paginate($perPage, ['*'], 'page', $page);
  118. return [
  119. 'data' => $orders->items(),
  120. 'current_page' => $orders->currentPage(),
  121. 'per_page' => $orders->perPage(),
  122. 'total' => $orders->total(),
  123. 'last_page' => $orders->lastPage(),
  124. ];
  125. }
  126. /**
  127. * 获取应用配置信息
  128. *
  129. * @param int $appId 应用ID
  130. * @return TransferAppDto|null
  131. */
  132. public static function getAppConfig(int $appId): ?TransferAppDto
  133. {
  134. $app = TransferApp::find($appId);
  135. return $app ? TransferAppDto::fromModel($app) : null;
  136. }
  137. /**
  138. * 根据应用标识获取配置信息
  139. *
  140. * @param string $keyname 应用标识
  141. * @return TransferAppDto|null
  142. */
  143. public static function getAppByKeyname(string $keyname): ?TransferAppDto
  144. {
  145. $app = TransferApp::where('keyname', $keyname)
  146. ->where('is_enabled', true)
  147. ->first();
  148. return $app ? TransferAppDto::fromModel($app) : null;
  149. }
  150. /**
  151. * 处理回调结果
  152. *
  153. * @param array $callbackData 回调数据
  154. * @return bool
  155. */
  156. public static function processCallback(array $callbackData): bool
  157. {
  158. try {
  159. return TransferLogic::processCallback($callbackData);
  160. } catch (\Exception $e) {
  161. \Log::error('Transfer callback processing failed', [
  162. 'error' => $e->getMessage(),
  163. 'data' => $callbackData
  164. ]);
  165. return false;
  166. }
  167. }
  168. }