| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- namespace App\Module\Transfer\AdminControllers\Helper;
- use App\Module\Transfer\Enums\TransferStatus;
- use App\Module\Transfer\Enums\TransferType;
- use Dcat\Admin\Grid;
- /**
- * 表格辅助类
- */
- class GridHelper
- {
- /**
- * 配置订单表格列
- *
- * @param Grid $grid
- * @return void
- */
- public static function orderColumns(Grid $grid): void
- {
- // 基本信息列
- $grid->column('id', '订单ID')->sortable();
- $grid->column('out_order_id', '外部订单ID')->copyable();
-
- // 用户信息列
- $grid->column('user_id', '用户ID')->link(function ($value) {
- return admin_url("users/{$value}");
- });
- $grid->column('out_user_id', '外部用户ID');
- // 应用信息列
- $grid->column('transferApp.title', '划转应用')->link(function () {
- return admin_url("transfer/apps/{$this->transfer_app_id}");
- });
- // 类型和状态列
- $grid->column('type', '类型')->using(self::getTypeLabels())->label(self::getTypeColors());
- $grid->column('status', '状态')->using(self::getStatusLabels())->label(self::getStatusColors());
- // 金额信息列
- $grid->column('amount', '内部金额')->display(function ($value) {
- return number_format($value, 2);
- })->sortable();
- $grid->column('out_amount', '外部金额')->display(function ($value) {
- return number_format($value, 2);
- })->sortable();
- $grid->column('exchange_rate', '汇率')->display(function ($value) {
- return number_format($value, 4);
- });
- // 时间信息列
- $grid->column('created_at', '创建时间')->sortable();
- $grid->column('processed_at', '处理时间');
- $grid->column('completed_at', '完成时间');
- // 错误信息列
- $grid->column('error_message', '错误信息')->limit(50)->help('点击查看完整错误信息');
- // 备注列
- $grid->column('remark', '备注')->limit(30);
- }
- /**
- * 配置应用表格列
- *
- * @param Grid $grid
- * @return void
- */
- public static function appColumns(Grid $grid): void
- {
- // 基本信息列
- $grid->column('id', '应用ID')->sortable();
- $grid->column('keyname', '应用标识')->copyable();
- $grid->column('title', '应用名称')->link(function () {
- return admin_url("transfer/apps/{$this->id}");
- });
- $grid->column('description', '描述')->limit(50);
- // 外部应用信息列
- $grid->column('out_id', '外部应用ID');
- $grid->column('out_id2', '外部应用ID2');
- $grid->column('out_id3', '外部应用ID3');
- // 配置信息列
- $grid->column('currency_id', '货币类型');
- $grid->column('fund_id', '资金账户类型');
- $grid->column('exchange_rate', '汇率')->display(function ($value) {
- return number_format($value, 4);
- });
- // 状态列
- $grid->column('is_enabled', '状态')->switch([
- 'on' => ['value' => 1, 'text' => '启用', 'color' => 'success'],
- 'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
- ]);
- // API配置状态列
- $grid->column('api_status', 'API配置')->display(function () {
- $status = [];
- if ($this->order_callback_url) $status[] = '回调';
- if ($this->order_in_info_url) $status[] = '转入查询';
- if ($this->order_out_create_url) $status[] = '转出创建';
- if ($this->order_out_info_url) $status[] = '转出查询';
-
- if (empty($status)) {
- return '<span class="badge badge-info">内部模式</span>';
- }
-
- return '<span class="badge badge-success">' . implode('、', $status) . '</span>';
- });
- // 统计信息列
- $grid->column('orders_count', '订单数量')->display(function () {
- return $this->orders()->count();
- });
- // 时间列
- $grid->column('created_at', '创建时间')->sortable();
- $grid->column('updated_at', '更新时间');
- }
- /**
- * 配置表格工具栏
- *
- * @param Grid $grid
- * @return void
- */
- public static function configureTools(Grid $grid): void
- {
- // 批量操作
- $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
- $batch->disableDelete(); // 禁用批量删除
- });
- // 导出功能
- $grid->export()->rows(function (array $rows) {
- foreach ($rows as $index => &$row) {
- // 格式化导出数据
- if (isset($row['type'])) {
- $row['type'] = TransferType::from($row['type'])->getDescription();
- }
- if (isset($row['status'])) {
- $row['status'] = TransferStatus::from($row['status'])->getDescription();
- }
- }
- return $rows;
- });
- // 刷新按钮
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append('<a class="btn btn-sm btn-outline-primary" href="javascript:void(0)" onclick="location.reload()">
- <i class="fa fa-refresh"></i> 刷新
- </a>');
- });
- }
- /**
- * 获取类型标签映射
- *
- * @return array
- */
- protected static function getTypeLabels(): array
- {
- $labels = [];
- foreach (TransferType::cases() as $type) {
- $labels[$type->value] = $type->getDescription();
- }
- return $labels;
- }
- /**
- * 获取类型颜色映射
- *
- * @return array
- */
- protected static function getTypeColors(): array
- {
- $colors = [];
- foreach (TransferType::cases() as $type) {
- $colors[$type->value] = $type->getColor();
- }
- return $colors;
- }
- /**
- * 获取状态标签映射
- *
- * @return array
- */
- protected static function getStatusLabels(): array
- {
- $labels = [];
- foreach (TransferStatus::cases() as $status) {
- $labels[$status->value] = $status->getDescription();
- }
- return $labels;
- }
- /**
- * 获取状态颜色映射
- *
- * @return array
- */
- protected static function getStatusColors(): array
- {
- $colors = [];
- foreach (TransferStatus::cases() as $status) {
- $colors[$status->value] = $status->getColor();
- }
- return $colors;
- }
- /**
- * 配置分页
- *
- * @param Grid $grid
- * @param int $perPage
- * @return void
- */
- public static function configurePagination(Grid $grid, int $perPage = 20): void
- {
- $grid->paginate($perPage);
- $grid->perPages([10, 20, 50, 100]);
- }
- /**
- * 配置排序
- *
- * @param Grid $grid
- * @param string $column
- * @param string $direction
- * @return void
- */
- public static function configureSort(Grid $grid, string $column = 'created_at', string $direction = 'desc'): void
- {
- $grid->model()->orderBy($column, $direction);
- }
- }
|