TransferAppController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace App\Module\Transfer\AdminControllers;
  3. use App\Module\Transfer\AdminControllers\Helper\TransferAppHelper;
  4. use App\Module\Transfer\Models\TransferApp;
  5. use App\Module\Transfer\Repositories\TransferAppRepository;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use Illuminate\Support\Facades\Http;
  10. use Spatie\RouteAttributes\Attributes\Resource;
  11. use Spatie\RouteAttributes\Attributes\Post;
  12. use Spatie\RouteAttributes\Attributes\Get;
  13. use UCore\DcatAdmin\AdminController;
  14. /**
  15. * 划转应用管理控制器
  16. *
  17. * 路由注解:
  18. * - Resource: transfer/apps (应用管理CRUD)
  19. * - 额外路由: 测试连接、切换状态、统计信息
  20. */
  21. #[Resource('transfer/apps', names: 'admin.transfer.apps')]
  22. class TransferAppController extends AdminController
  23. {
  24. /**
  25. * 页面标题
  26. */
  27. protected $title = '划转应用管理';
  28. /**
  29. * 模型类
  30. */
  31. protected $model = TransferApp::class;
  32. /**
  33. * 仓库类
  34. */
  35. protected $repository = TransferAppRepository::class;
  36. /**
  37. * 配置数据表格
  38. */
  39. protected function grid(): Grid
  40. {
  41. $grid = Grid::make(new TransferApp(), function (Grid $grid) {
  42. // 使用辅助类配置表格
  43. TransferAppHelper::grid($grid);
  44. // 设置每页显示数量
  45. $grid->paginate(20);
  46. // 禁用创建按钮(如果需要)
  47. // $grid->disableCreateButton();
  48. // 设置表格标题
  49. // $grid->header(function () {
  50. // return view('transfer::admin.app.header');
  51. // });
  52. });
  53. return $grid;
  54. }
  55. /**
  56. * 配置数据详情
  57. */
  58. protected function detail($id): Show
  59. {
  60. $show = Show::make($id, new TransferApp(), function (Show $show) {
  61. // 使用辅助类配置详情
  62. TransferAppHelper::show($show);
  63. // 添加自定义面板
  64. $show->panel()
  65. ->title('划转应用详情')
  66. ->tools(function ($tools) {
  67. // 添加测试连接按钮
  68. $appId = request()->route('app'); // 从路由参数获取应用ID
  69. $tools->append('<a class="btn btn-sm btn-outline-primary" href="javascript:void(0)" onclick="testConnection('.$appId.')">测试连接</a>');
  70. });
  71. });
  72. return $show;
  73. }
  74. /**
  75. * 配置创建和编辑表单
  76. */
  77. protected function form(): Form
  78. {
  79. $form = Form::make(new TransferApp(), function (Form $form) {
  80. // 使用辅助类配置表单
  81. TransferAppHelper::form($form);
  82. // 设置表单标题
  83. $form->title('划转应用配置');
  84. // 表单工具栏
  85. $form->tools(function (Form\Tools $tools) {
  86. // 添加测试按钮
  87. $tools->append('<a class="btn btn-outline-info" href="javascript:void(0)" onclick="testForm()">测试配置</a>');
  88. });
  89. });
  90. return $form;
  91. }
  92. /**
  93. * 测试应用连接
  94. */
  95. #[Post('transfer/apps/{id}/test-connection', name: 'admin.transfer.apps.test-connection')]
  96. public function testConnection($id)
  97. {
  98. try {
  99. $app = TransferApp::findOrFail($id);
  100. if ($app->isInternalMode()) {
  101. return response()->json([
  102. 'status' => true,
  103. 'message' => '农场内部模式,无需测试连接'
  104. ]);
  105. }
  106. // 测试各个API端点
  107. $results = [];
  108. $urls = [
  109. 'callback' => $app->order_callback_url,
  110. 'in_info' => $app->order_in_info_url,
  111. 'out_create' => $app->order_out_create_url,
  112. 'out_info' => $app->order_out_info_url,
  113. ];
  114. foreach ($urls as $type => $url) {
  115. if (empty($url)) {
  116. $results[$type] = ['status' => 'skip', 'message' => '未配置'];
  117. continue;
  118. }
  119. try {
  120. $response = Http::timeout(10)->get($url);
  121. $results[$type] = [
  122. 'status' => $response->successful() ? 'success' : 'error',
  123. 'message' => $response->successful() ? '连接成功' : "HTTP {$response->status()}",
  124. 'response_time' => $response->transferStats->getTransferTime() ?? 0
  125. ];
  126. } catch (\Exception $e) {
  127. $results[$type] = [
  128. 'status' => 'error',
  129. 'message' => $e->getMessage()
  130. ];
  131. }
  132. }
  133. return response()->json([
  134. 'status' => true,
  135. 'message' => '连接测试完成',
  136. 'data' => $results
  137. ]);
  138. } catch (\Exception $e) {
  139. return response()->json([
  140. 'status' => false,
  141. 'message' => '测试失败: ' . $e->getMessage()
  142. ]);
  143. }
  144. }
  145. /**
  146. * 切换应用状态
  147. */
  148. #[Post('transfer/apps/{id}/toggle-status', name: 'admin.transfer.apps.toggle-status')]
  149. public function toggleStatus($id)
  150. {
  151. try {
  152. $app = TransferApp::findOrFail($id);
  153. $app->is_enabled = !$app->is_enabled;
  154. $app->save();
  155. $status = $app->is_enabled ? '启用' : '禁用';
  156. return response()->json([
  157. 'status' => true,
  158. 'message' => "应用已{$status}"
  159. ]);
  160. } catch (\Exception $e) {
  161. return response()->json([
  162. 'status' => false,
  163. 'message' => '操作失败: ' . $e->getMessage()
  164. ]);
  165. }
  166. }
  167. /**
  168. * 获取应用统计信息
  169. */
  170. #[Get('transfer/apps/{id}/statistics', name: 'admin.transfer.apps.statistics')]
  171. public function statistics($id = null)
  172. {
  173. try {
  174. $query = \App\Module\Transfer\Models\TransferOrder::query();
  175. if ($id) {
  176. $query->where('transfer_app_id', $id);
  177. }
  178. $stats = [
  179. 'total_orders' => $query->count(),
  180. 'completed_orders' => (clone $query)->where('status', \App\Module\Transfer\Enums\TransferStatus::COMPLETED)->count(),
  181. 'failed_orders' => (clone $query)->where('status', \App\Module\Transfer\Enums\TransferStatus::FAILED)->count(),
  182. 'total_amount' => $query->sum('amount'),
  183. 'total_fee' => $query->sum('fee_amount'),
  184. 'today_orders' => (clone $query)->whereDate('created_at', today())->count(),
  185. 'today_amount' => (clone $query)->whereDate('created_at', today())->sum('amount'),
  186. 'today_fee' => (clone $query)->whereDate('created_at', today())->sum('fee_amount'),
  187. ];
  188. $stats['success_rate'] = $stats['total_orders'] > 0
  189. ? round($stats['completed_orders'] / $stats['total_orders'] * 100, 2)
  190. : 0;
  191. return response()->json([
  192. 'status' => true,
  193. 'data' => $stats
  194. ]);
  195. } catch (\Exception $e) {
  196. return response()->json([
  197. 'status' => false,
  198. 'message' => '获取统计信息失败: ' . $e->getMessage()
  199. ]);
  200. }
  201. }
  202. /**
  203. * 获取手续费统计信息
  204. */
  205. #[Get('transfer/apps/{id}/fee-statistics', name: 'admin.transfer.apps.fee-statistics')]
  206. public function feeStatistics($id = null)
  207. {
  208. try {
  209. $query = \App\Module\Transfer\Models\TransferOrder::query()
  210. ->where('status', \App\Module\Transfer\Enums\TransferStatus::COMPLETED)
  211. ->where('fee_amount', '>', 0);
  212. if ($id) {
  213. $query->where('transfer_app_id', $id);
  214. }
  215. $stats = $query->selectRaw('
  216. COUNT(*) as total_orders,
  217. SUM(fee_amount) as total_fee,
  218. AVG(fee_rate) as avg_fee_rate,
  219. SUM(CASE WHEN type = 1 THEN fee_amount ELSE 0 END) as in_fee,
  220. SUM(CASE WHEN type = 2 THEN fee_amount ELSE 0 END) as out_fee,
  221. COUNT(CASE WHEN type = 1 THEN 1 END) as in_orders,
  222. COUNT(CASE WHEN type = 2 THEN 1 END) as out_orders
  223. ')
  224. ->first();
  225. // 今日手续费统计
  226. $todayStats = (clone $query)->whereDate('created_at', today())
  227. ->selectRaw('COUNT(*) as today_orders, SUM(fee_amount) as today_fee')
  228. ->first();
  229. $result = [
  230. 'total_orders' => $stats->total_orders ?? 0,
  231. 'total_fee' => number_format($stats->total_fee ?? 0, 4),
  232. 'avg_fee_rate' => number_format(($stats->avg_fee_rate ?? 0) * 100, 2) . '%',
  233. 'in_fee' => number_format($stats->in_fee ?? 0, 4),
  234. 'out_fee' => number_format($stats->out_fee ?? 0, 4),
  235. 'in_orders' => $stats->in_orders ?? 0,
  236. 'out_orders' => $stats->out_orders ?? 0,
  237. 'today_orders' => $todayStats->today_orders ?? 0,
  238. 'today_fee' => number_format($todayStats->today_fee ?? 0, 4),
  239. ];
  240. return response()->json([
  241. 'status' => true,
  242. 'data' => $result
  243. ]);
  244. } catch (\Exception $e) {
  245. return response()->json([
  246. 'status' => false,
  247. 'message' => '获取手续费统计失败: ' . $e->getMessage()
  248. ]);
  249. }
  250. }
  251. }