GridHelper.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace App\Module\Mex\AdminControllers\Helper;
  3. use Dcat\Admin\Grid;
  4. /**
  5. * Mex模块Grid助手类
  6. *
  7. * 提供通用的Grid列配置方法
  8. */
  9. class GridHelper
  10. {
  11. protected Grid $grid;
  12. protected $controller;
  13. public function __construct(Grid $grid, $controller = null)
  14. {
  15. $this->grid = $grid;
  16. $this->controller = $controller;
  17. }
  18. /**
  19. * 用户ID列,带链接
  20. */
  21. public function columnUserId(string $field = 'user_id', string $label = '用户ID'): Grid\Column
  22. {
  23. return $this->grid->column($field, $label)->link(function ($value) {
  24. return admin_url("users/{$value}");
  25. });
  26. }
  27. /**
  28. * 商品ID列,带链接
  29. */
  30. public function columnItemId(string $field = 'item_id', string $label = '商品ID'): Grid\Column
  31. {
  32. return $this->grid->column($field, $label)->link(function ($value) {
  33. return admin_url("game-items/{$value}");
  34. });
  35. }
  36. /**
  37. * 订单ID列,带链接
  38. */
  39. public function columnOrderId(string $field, string $label): Grid\Column
  40. {
  41. return $this->grid->column($field, $label)->link(function ($value) {
  42. return $value ? admin_url("mex-orders/{$value}") : '-';
  43. });
  44. }
  45. /**
  46. * 价格列,格式化显示
  47. */
  48. public function columnPrice(string $field = 'price', string $label = '价格', int $decimals = 5): Grid\Column
  49. {
  50. return $this->grid->column($field, $label)->display(function ($value) use ($decimals) {
  51. return number_format($value, $decimals);
  52. });
  53. }
  54. /**
  55. * 金额列,格式化显示
  56. */
  57. public function columnAmount(string $field, string $label, int $decimals = 5): Grid\Column
  58. {
  59. return $this->grid->column($field, $label)->display(function ($value) use ($decimals) {
  60. return $value ? number_format($value, $decimals) : '-';
  61. });
  62. }
  63. /**
  64. * 数量列,格式化显示
  65. */
  66. public function columnQuantity(string $field = 'quantity', string $label = '数量'): Grid\Column
  67. {
  68. return $this->grid->column($field, $label)->display(function ($value) {
  69. return number_format($value);
  70. });
  71. }
  72. /**
  73. * 百分比列
  74. */
  75. public function columnPercentage(string $field, string $label, int $decimals = 2): Grid\Column
  76. {
  77. return $this->grid->column($field, $label)->display(function ($value) use ($decimals) {
  78. return number_format($value * 100, $decimals) . '%';
  79. });
  80. }
  81. /**
  82. * 布尔值列,显示为是/否
  83. */
  84. public function columnBoolean(string $field, string $label): Grid\Column
  85. {
  86. return $this->grid->column($field, $label)->display(function ($value) {
  87. return $value ? '是' : '否';
  88. })->label([
  89. 1 => 'success',
  90. 0 => 'default',
  91. ]);
  92. }
  93. /**
  94. * 状态列,带颜色标签
  95. */
  96. public function columnStatus(string $field, string $label, array $statusMap, array $colorMap = []): Grid\Column
  97. {
  98. $column = $this->grid->column($field, $label)->using($statusMap);
  99. if (!empty($colorMap)) {
  100. $column->label($colorMap);
  101. }
  102. return $column;
  103. }
  104. /**
  105. * 时间戳列,格式化显示
  106. */
  107. public function columnTimestamp(string $field, string $label, string $format = 'Y-m-d H:i:s'): Grid\Column
  108. {
  109. return $this->grid->column($field, $label)->display(function ($value) use ($format) {
  110. return $value ? date($format, $value) : '-';
  111. });
  112. }
  113. /**
  114. * 日期时间列
  115. */
  116. public function columnDatetime(string $field, string $label): Grid\Column
  117. {
  118. return $this->grid->column($field, $label)->sortable();
  119. }
  120. /**
  121. * 文本截断列
  122. */
  123. public function columnText(string $field, string $label, int $limit = 50): Grid\Column
  124. {
  125. return $this->grid->column($field, $label)->limit($limit);
  126. }
  127. /**
  128. * JSON数据列
  129. */
  130. public function columnJson(string $field, string $label): Grid\Column
  131. {
  132. return $this->grid->column($field, $label)->display(function ($value) {
  133. if (is_string($value)) {
  134. $data = json_decode($value, true);
  135. return $data ? json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) : $value;
  136. }
  137. return json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  138. })->limit(100);
  139. }
  140. /**
  141. * 平均价格计算列
  142. */
  143. public function columnAveragePrice(string $amountField, string $quantityField, string $label): Grid\Column
  144. {
  145. return $this->grid->column($label)->display(function () use ($amountField, $quantityField) {
  146. $amount = $this->{$amountField};
  147. $quantity = $this->{$quantityField};
  148. if ($quantity > 0) {
  149. $avgPrice = bcdiv($amount, $quantity, 5);
  150. return number_format($avgPrice, 5);
  151. }
  152. return '-';
  153. });
  154. }
  155. /**
  156. * 净值计算列
  157. */
  158. public function columnNetValue(string $positiveField, string $negativeField, string $label, int $decimals = 5): Grid\Column
  159. {
  160. return $this->grid->column($label)->display(function () use ($positiveField, $negativeField, $decimals) {
  161. $positive = $this->{$positiveField};
  162. $negative = $this->{$negativeField};
  163. if (is_numeric($positive) && is_numeric($negative)) {
  164. $net = bcsub($positive, $negative, $decimals);
  165. return number_format($net, $decimals);
  166. }
  167. return '-';
  168. });
  169. }
  170. }