GridHelper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Module\Mex\AdminControllers\Helper;
  3. use Dcat\Admin\Grid;
  4. use UCore\DcatAdmin\GridHelper as UGridHelper;
  5. /**
  6. * Mex模块Grid助手类
  7. *
  8. * 继承UCore的GridHelper,提供Mex模块特有的Grid列配置方法
  9. */
  10. class GridHelper extends UGridHelper
  11. {
  12. /**
  13. * 用户ID列,带链接
  14. */
  15. public function columnUserId($field = 'user_id', $label = '用户ID')
  16. {
  17. return $this->grid->column($field, $label)->link(function ($value) {
  18. return admin_url("users/{$value}");
  19. });
  20. }
  21. /**
  22. * 商品ID列,带链接
  23. */
  24. public function columnItemId($field = 'item_id', $label = '商品ID')
  25. {
  26. return $this->grid->column($field, $label)->link(function ($value) {
  27. return admin_url("game-items/{$value}");
  28. });
  29. }
  30. /**
  31. * 订单ID列,带链接
  32. */
  33. public function columnOrderId($field, $label)
  34. {
  35. return $this->grid->column($field, $label)->link(function ($value) {
  36. return $value ? admin_url("mex-orders/{$value}") : '-';
  37. });
  38. }
  39. /**
  40. * 价格列,格式化显示
  41. */
  42. public function columnPrice($field = 'price', $label = '价格', $decimals = 5)
  43. {
  44. return $this->grid->column($field, $label)->display(function ($value) use ($decimals) {
  45. return number_format($value, $decimals);
  46. });
  47. }
  48. /**
  49. * 金额列,格式化显示
  50. */
  51. public function columnAmount($field, $label, $decimals = 5)
  52. {
  53. return $this->grid->column($field, $label)->display(function ($value) use ($decimals) {
  54. return $value ? number_format($value, $decimals) : '-';
  55. });
  56. }
  57. /**
  58. * 数量列,格式化显示
  59. */
  60. public function columnQuantity($field = 'quantity', $label = '数量')
  61. {
  62. return $this->grid->column($field, $label)->display(function ($value) {
  63. return number_format($value);
  64. });
  65. }
  66. /**
  67. * 平均价格计算列(根据金额和数量字段计算)
  68. */
  69. public function columnAveragePrice($amountField, $quantityField, $label)
  70. {
  71. return $this->grid->column($label)->display(function () use ($amountField, $quantityField) {
  72. $amount = $this->{$amountField};
  73. $quantity = $this->{$quantityField};
  74. if ($quantity > 0) {
  75. $avgPrice = bcdiv($amount, $quantity, 5);
  76. return number_format($avgPrice, 5);
  77. }
  78. return '-';
  79. });
  80. }
  81. /**
  82. * 净值计算列(正值减去负值)
  83. */
  84. public function columnNetValue($positiveField, $negativeField, $label, $decimals = 5)
  85. {
  86. return $this->grid->column($label)->display(function () use ($positiveField, $negativeField, $decimals) {
  87. $positive = $this->{$positiveField};
  88. $negative = $this->{$negativeField};
  89. if (is_numeric($positive) && is_numeric($negative)) {
  90. $net = bcsub($positive, $negative, $decimals);
  91. return number_format($net, $decimals);
  92. }
  93. return '-';
  94. });
  95. }
  96. }