| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <?php
- namespace App\Module\Transfer\AdminControllers;
- use App\Module\Transfer\Models\TransferFeeDailyStats;
- use App\Module\Transfer\Services\FeeStatisticsService;
- use UCore\DcatAdmin\AdminController;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Http\Controllers\AdminController as BaseAdminController;
- use Illuminate\Http\Request;
- use Carbon\Carbon;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Widgets\Card;
- use Dcat\Admin\Widgets\Table;
- use Spatie\RouteAttributes\Attributes\Get;
- use Spatie\RouteAttributes\Attributes\Post;
- /**
- * 手续费统计后台控制器
- */
- class FeeStatisticsController extends AdminController
- {
- /**
- * 标题
- */
- protected $title = '手续费统计';
- /**
- * 模型
- */
- protected string $model = TransferFeeDailyStats::class;
- /**
- * 列表页面
- */
- #[Get('transfer/fee-statistics', name: 'admin.transfer.fee-statistics.index')]
- public function index(Content $content)
- {
- return $content
- ->title('手续费统计')
- ->description('查看Transfer模块手续费统计数据')
- ->body($this->grid());
- }
- /**
- * 详情页面
- */
- #[Get('transfer/fee-statistics/{id}', name: 'admin.transfer.fee-statistics.show')]
- public function show($id, Content $content)
- {
- return $content
- ->title('手续费统计详情')
- ->description('查看详细统计信息')
- ->body($this->detail($id));
- }
- /**
- * 统计概览页面
- */
- #[Get('transfer/fee-statistics/overview', name: 'admin.transfer.fee-statistics.overview')]
- public function overview(Content $content)
- {
- return $content
- ->title('手续费统计概览')
- ->description('查看手续费统计概览和趋势')
- ->body($this->overviewCards())
- ->body($this->trendChart());
- }
- /**
- * 手动执行统计
- */
- #[Post('transfer/fee-statistics/run', name: 'admin.transfer.fee-statistics.run')]
- public function runStatistics(Request $request)
- {
- try {
- $date = $request->input('date', Carbon::yesterday()->format('Y-m-d'));
- $rerun = $request->boolean('rerun', false);
- if ($rerun) {
- $result = FeeStatisticsService::restatistics($date);
- } else {
- $result = FeeStatisticsService::runDailyStatistics($date);
- }
- return response()->json([
- 'status' => true,
- 'message' => '统计执行成功',
- 'data' => $result
- ]);
- } catch (\Exception $e) {
- return response()->json([
- 'status' => false,
- 'message' => '统计执行失败: ' . $e->getMessage()
- ]);
- }
- }
- /**
- * 数据验证
- */
- #[Post('transfer/fee-statistics/validate', name: 'admin.transfer.fee-statistics.validate')]
- public function validateStatistics(Request $request)
- {
- try {
- $date = $request->input('date', Carbon::yesterday()->format('Y-m-d'));
- $result = FeeStatisticsService::validateStatistics($date);
- return response()->json([
- 'status' => true,
- 'message' => $result['valid'] ? '数据验证通过' : '数据验证失败',
- 'data' => $result
- ]);
- } catch (\Exception $e) {
- return response()->json([
- 'status' => false,
- 'message' => '数据验证失败: ' . $e->getMessage()
- ]);
- }
- }
- /**
- * 创建数据表格
- */
- protected function grid(): Grid
- {
- return Grid::make($this->model::with('transferApp'), function (Grid $grid) {
- $grid->column('id', 'ID')->sortable();
- $grid->column('stat_date', '统计日期')->display(function ($value) {
- return Carbon::parse($value)->format('Y-m-d');
- })->sortable();
-
- $grid->column('transferApp.title', '应用名称');
- $grid->column('total_order_count', '总订单数')->sortable();
- $grid->column('total_amount', '总交易金额')->display(function ($value) {
- return number_format($value, 4);
- })->sortable();
- $grid->column('total_fee_amount', '总手续费')->display(function ($value) {
- return number_format($value, 4);
- })->sortable();
- $grid->column('avg_fee_rate', '平均费率')->display(function ($value) {
- return number_format($value * 100, 2) . '%';
- });
-
- $grid->column('in_order_count', '转入订单数');
- $grid->column('in_fee_amount', '转入手续费')->display(function ($value) {
- return number_format($value, 4);
- });
-
- $grid->column('out_order_count', '转出订单数');
- $grid->column('out_fee_amount', '转出手续费')->display(function ($value) {
- return number_format($value, 4);
- });
-
- $grid->column('last_processed_order_id', '最后处理订单ID');
- $grid->column('created_at', '创建时间')->sortable();
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('transfer_app_id', '应用')->select(
- \App\Module\Transfer\Models\TransferApp::pluck('title', 'id')
- );
- $filter->date('stat_date', '统计日期');
- $filter->between('total_fee_amount', '手续费金额');
- });
- // 默认排序
- $grid->model()->orderBy('stat_date', 'desc');
-
- // 禁用创建、编辑、删除按钮
- $grid->disableCreateButton();
- $grid->disableActions();
-
- // 添加工具栏按钮
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append('<a href="' . route('admin.transfer.fee-statistics.overview') . '" class="btn btn-primary">统计概览</a>');
- });
- });
- }
- /**
- * 创建详情显示
- */
- protected function detail($id): Show
- {
- return Show::make($id, $this->model::with('transferApp'), function (Show $show) {
- $show->field('id', 'ID');
- $show->field('stat_date', '统计日期');
- $show->field('transferApp.title', '应用名称');
- $show->field('transferApp.keyname', '应用标识');
-
- $show->divider('订单统计');
- $show->field('total_order_count', '总订单数');
- $show->field('total_amount', '总交易金额')->as(function ($value) {
- return number_format($value, 4);
- });
- $show->field('total_fee_amount', '总手续费')->as(function ($value) {
- return number_format($value, 4);
- });
- $show->field('avg_fee_rate', '平均费率')->as(function ($value) {
- return number_format($value * 100, 2) . '%';
- });
-
- $show->divider('转入统计');
- $show->field('in_order_count', '转入订单数');
- $show->field('in_total_amount', '转入总金额')->as(function ($value) {
- return number_format($value, 4);
- });
- $show->field('in_fee_amount', '转入手续费')->as(function ($value) {
- return number_format($value, 4);
- });
- $show->field('in_avg_fee_rate', '转入平均费率')->as(function ($value) {
- return number_format($value * 100, 2) . '%';
- });
-
- $show->divider('转出统计');
- $show->field('out_order_count', '转出订单数');
- $show->field('out_total_amount', '转出总金额')->as(function ($value) {
- return number_format($value, 4);
- });
- $show->field('out_fee_amount', '转出手续费')->as(function ($value) {
- return number_format($value, 4);
- });
- $show->field('out_avg_fee_rate', '转出平均费率')->as(function ($value) {
- return number_format($value * 100, 2) . '%';
- });
-
- $show->divider('处理信息');
- $show->field('last_processed_order_id', '最后处理订单ID');
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- });
- }
- /**
- * 汇总卡片
- */
- protected function summaryCards()
- {
- // 获取最近7天的统计数据
- $recentStats = FeeStatisticsService::getRecentTrend(7);
- $totalOrders = $recentStats['summary']['total_orders'] ?? 0;
- $totalFee = $recentStats['summary']['total_fee'] ?? 0;
- $avgFeeRate = $recentStats['summary']['avg_fee_rate'] ?? 0;
- return new Card('最近7天统计汇总', [
- new Table(['指标', '数值'], [
- ['总订单数', number_format($totalOrders)],
- ['总手续费', number_format((float)$totalFee, 4)],
- ['平均费率', number_format((float)$avgFeeRate * 100, 2) . '%'],
- ['统计天数', count($recentStats['data'] ?? [])],
- ])
- ]);
- }
- /**
- * 概览卡片
- */
- protected function overviewCards()
- {
- // 获取应用汇总统计
- $appSummary = FeeStatisticsService::getAppSummary();
-
- $cards = [];
- foreach ($appSummary['data'] as $app) {
- $cards[] = new Card($app['transfer_app']['title'] ?? '未知应用', [
- new Table(['指标', '数值'], [
- ['统计天数', $app['stat_days']],
- ['总订单数', number_format($app['total_orders'])],
- ['总手续费', number_format($app['total_fee'], 4)],
- ['平均费率', number_format($app['avg_fee_rate'] * 100, 2) . '%'],
- ])
- ]);
- }
-
- return $cards;
- }
- /**
- * 趋势图表
- */
- protected function trendChart()
- {
- // 这里可以添加图表组件,显示手续费趋势
- return new Card('手续费趋势图', '图表功能待实现');
- }
- }
|