| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace App\Module\Mex\AdminControllers\Helper;
- use Dcat\Admin\Grid;
- /**
- * Mex模块Grid助手类
- *
- * 提供通用的Grid列配置方法
- */
- class GridHelper
- {
- protected Grid $grid;
- protected $controller;
- public function __construct(Grid $grid, $controller = null)
- {
- $this->grid = $grid;
- $this->controller = $controller;
- }
- /**
- * 用户ID列,带链接
- */
- public function columnUserId(string $field = 'user_id', string $label = '用户ID'): Grid\Column
- {
- return $this->grid->column($field, $label)->link(function ($value) {
- return admin_url("users/{$value}");
- });
- }
- /**
- * 商品ID列,带链接
- */
- public function columnItemId(string $field = 'item_id', string $label = '商品ID'): Grid\Column
- {
- return $this->grid->column($field, $label)->link(function ($value) {
- return admin_url("game-items/{$value}");
- });
- }
- /**
- * 订单ID列,带链接
- */
- public function columnOrderId(string $field, string $label): Grid\Column
- {
- return $this->grid->column($field, $label)->link(function ($value) {
- return $value ? admin_url("mex-orders/{$value}") : '-';
- });
- }
- /**
- * 价格列,格式化显示
- */
- public function columnPrice(string $field = 'price', string $label = '价格', int $decimals = 5): Grid\Column
- {
- return $this->grid->column($field, $label)->display(function ($value) use ($decimals) {
- return number_format($value, $decimals);
- });
- }
- /**
- * 金额列,格式化显示
- */
- public function columnAmount(string $field, string $label, int $decimals = 5): Grid\Column
- {
- return $this->grid->column($field, $label)->display(function ($value) use ($decimals) {
- return $value ? number_format($value, $decimals) : '-';
- });
- }
- /**
- * 数量列,格式化显示
- */
- public function columnQuantity(string $field = 'quantity', string $label = '数量'): Grid\Column
- {
- return $this->grid->column($field, $label)->display(function ($value) {
- return number_format($value);
- });
- }
- /**
- * 百分比列
- */
- public function columnPercentage(string $field, string $label, int $decimals = 2): Grid\Column
- {
- return $this->grid->column($field, $label)->display(function ($value) use ($decimals) {
- return number_format($value * 100, $decimals) . '%';
- });
- }
- /**
- * 布尔值列,显示为是/否
- */
- public function columnBoolean(string $field, string $label): Grid\Column
- {
- return $this->grid->column($field, $label)->display(function ($value) {
- return $value ? '是' : '否';
- })->label([
- 1 => 'success',
- 0 => 'default',
- ]);
- }
- /**
- * 状态列,带颜色标签
- */
- public function columnStatus(string $field, string $label, array $statusMap, array $colorMap = []): Grid\Column
- {
- $column = $this->grid->column($field, $label)->using($statusMap);
-
- if (!empty($colorMap)) {
- $column->label($colorMap);
- }
-
- return $column;
- }
- /**
- * 时间戳列,格式化显示
- */
- public function columnTimestamp(string $field, string $label, string $format = 'Y-m-d H:i:s'): Grid\Column
- {
- return $this->grid->column($field, $label)->display(function ($value) use ($format) {
- return $value ? date($format, $value) : '-';
- });
- }
- /**
- * 日期时间列
- */
- public function columnDatetime(string $field, string $label): Grid\Column
- {
- return $this->grid->column($field, $label)->sortable();
- }
- /**
- * 文本截断列
- */
- public function columnText(string $field, string $label, int $limit = 50): Grid\Column
- {
- return $this->grid->column($field, $label)->limit($limit);
- }
- /**
- * JSON数据列
- */
- public function columnJson(string $field, string $label): Grid\Column
- {
- return $this->grid->column($field, $label)->display(function ($value) {
- if (is_string($value)) {
- $data = json_decode($value, true);
- return $data ? json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) : $value;
- }
- return json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
- })->limit(100);
- }
- /**
- * 平均价格计算列
- */
- public function columnAveragePrice(string $amountField, string $quantityField, string $label): Grid\Column
- {
- return $this->grid->column($label)->display(function () use ($amountField, $quantityField) {
- $amount = $this->{$amountField};
- $quantity = $this->{$quantityField};
-
- if ($quantity > 0) {
- $avgPrice = bcdiv($amount, $quantity, 5);
- return number_format($avgPrice, 5);
- }
- return '-';
- });
- }
- /**
- * 净值计算列
- */
- public function columnNetValue(string $positiveField, string $negativeField, string $label, int $decimals = 5): Grid\Column
- {
- return $this->grid->column($label)->display(function () use ($positiveField, $negativeField, $decimals) {
- $positive = $this->{$positiveField};
- $negative = $this->{$negativeField};
-
- if (is_numeric($positive) && is_numeric($negative)) {
- $net = bcsub($positive, $negative, $decimals);
- return number_format($net, $decimals);
- }
- return '-';
- });
- }
- }
|