GridHelperTrait.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Module\Fund\AdminControllers\Helper;
  3. use App\Module\Fund\Enums\FUND_STATUS;
  4. use App\Module\Fund\Enums\FUND_TYPE;
  5. use App\Module\Fund\Enums\LOG_TYPE;
  6. use App\Module\Fund\Enums\OPERATE_TYPE;
  7. use App\Module\Fund\Services\AccountService;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Grid\Column;
  10. /**
  11. * 列表页辅助特性
  12. *
  13. * 提供资金模块后台控制器的列表页构建功能的具体实现
  14. * 只保留具有复用价值的方法
  15. */
  16. trait GridHelperTrait
  17. {
  18. /**
  19. * 添加资金账户列
  20. *
  21. * 复用价值:高 - 统一处理资金账户的显示,使用AccountService获取账户描述
  22. *
  23. * @param string $field 字段名
  24. * @param string $label 标签名
  25. * @return Column
  26. */
  27. public function columnFundId(string $field = 'fund_id', string $label = '资金账户'): Column
  28. {
  29. return $this->grid->column($field, $label)->using(AccountService::getFundsDesc());
  30. }
  31. /**
  32. * 添加资金余额列
  33. *
  34. * @param string $field 字段名
  35. * @param string $label 标签名
  36. * @return Column
  37. */
  38. public function columnBalance(string $field = 'balance', string $label = '余额'): Column
  39. {
  40. return $this->grid->column($field, $label)->display(function ($value) {
  41. return number_format($value , 3,',');
  42. })->sortable();
  43. }
  44. public function columnBalance1000(string $field = 'balance', string $label = '余额'): Column
  45. {
  46. return $this->grid->column($field, $label)->display(function ($value) {
  47. return number_format($value / 1000, 3);
  48. })->sortable();
  49. }
  50. /**
  51. * 添加操作金额列
  52. *
  53. * 复用价值:高 - 统一处理操作金额的格式化显示,包括正负值的颜色区分
  54. *
  55. * @param string $field 字段名
  56. * @param string $label 标签名
  57. * @return Column
  58. */
  59. public function columnAmount(string $field = 'amount', string $label = '操作金额'): Column
  60. {
  61. return $this->grid->column($field, $label)->display(function ($value) {
  62. $formattedValue = number_format($value / 1000, 3);
  63. if ($value > 0) {
  64. return "<span class='text-success'>+{$formattedValue}</span>";
  65. } elseif ($value < 0) {
  66. return "<span class='text-danger'>{$formattedValue}</span>";
  67. } else {
  68. return "<span>{$formattedValue}</span>";
  69. }
  70. })->sortable();
  71. }
  72. /**
  73. * 添加操作类型列
  74. *
  75. * 复用价值:高 - 统一处理操作类型的显示,使用枚举类型
  76. *
  77. * @param string $field 字段名
  78. * @param string $label 标签名
  79. * @return Column
  80. */
  81. public function columnOperateType(string $field = 'operate_type', string $label = '操作类型'): Column
  82. {
  83. return $this->grid->column($field, $label)->using(LOG_TYPE::getNames());
  84. }
  85. /**
  86. * 添加时间戳格式化列
  87. *
  88. * 复用价值:高 - 统一处理时间戳的格式化显示
  89. *
  90. * @param string $field 字段名
  91. * @param string $label 标签名
  92. * @param string $format 日期格式
  93. * @return Column
  94. */
  95. public function columnTimestamp(string $field, string $label, string $format = 'Y-m-d H:i:s'): Column
  96. {
  97. return $this->grid->column($field, $label)->display(function ($value) use ($format) {
  98. return $value ? date($format, $value) : '';
  99. })->sortable();
  100. }
  101. /**
  102. * 添加金额格式化列(毫转元)
  103. *
  104. * 复用价值:高 - 统一处理金额的格式化显示,包括单位转换
  105. *
  106. * @param string $field 字段名
  107. * @param string $label 标签名
  108. * @param int $decimals 小数位数
  109. * @return Column
  110. */
  111. public function columnMoney(string $field, string $label, int $decimals = 3): Column
  112. {
  113. return $this->grid->column($field, $label)->display(function ($value) use ($decimals) {
  114. return number_format($value / 1000, $decimals);
  115. });
  116. }
  117. /**
  118. * 添加资金账户组合列
  119. *
  120. * 复用价值:高 - 将用户ID和资金账户组合显示,提高信息密度
  121. *
  122. * @param string $userIdField 用户ID字段名
  123. * @param string $fundIdField 资金账户字段名
  124. * @param string $label 标签名
  125. * @return Column
  126. */
  127. public function columnUserFund(string $userIdField = 'user_id', string $fundIdField = 'fund_id', string $label = '用户/账户'): Column
  128. {
  129. return $this->grid->column($userIdField, $label)->display(function ($userId) use ($fundIdField) {
  130. $fundId = $this->{$fundIdField};
  131. $fundsDesc = AccountService::getFundsDesc();
  132. if(is_int($fundId)){
  133. $fundName = $fundsDesc[$fundId] ?? $fundId;
  134. }else{
  135. $fundName = $fundsDesc[$fundId->value] ?? $fundId;
  136. }
  137. return "ID: {$userId}<br>账户: {$fundName}";
  138. });
  139. }
  140. /**
  141. * 添加转账信息组合列
  142. *
  143. * 复用价值:高 - 将转账的来源和目标信息组合显示,提高信息密度
  144. *
  145. * @param string $label 标签名
  146. * @return Column
  147. */
  148. public function columnTransferInfo(string $label = '转账信息'): Column
  149. {
  150. return $this->grid->column('user_id', $label)->display(function ($userId) {
  151. $fromFundId = $this->fund_id;
  152. $toUserId = $this->to_user_id;
  153. $toFundId = $this->to_fund_id;
  154. $fundsDesc = AccountService::getFundsDesc();
  155. $fromFundName = $fundsDesc[$fromFundId] ?? $fromFundId;
  156. $toFundName = $fundsDesc[$toFundId] ?? $toFundId;
  157. return "从: 用户{$userId}/{$fromFundName}<br>到: 用户{$toUserId}/{$toFundName}";
  158. });
  159. }
  160. }