GridHelperTrait.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. * 复用价值:高 - 统一处理余额的格式化显示,包括单位转换
  35. *
  36. * @param string $field 字段名
  37. * @param string $label 标签名
  38. * @return Column
  39. */
  40. public function columnBalance(string $field = 'balance', string $label = '余额'): Column
  41. {
  42. return $this->grid->column($field, $label)->display(function ($value) {
  43. return number_format($value / 1000, 3);
  44. })->sortable();
  45. }
  46. /**
  47. * 添加资金状态列
  48. *
  49. * 复用价值:高 - 统一处理资金状态的显示,包括状态名称和标签样式
  50. *
  51. * @param string $field 字段名
  52. * @param string $label 标签名
  53. * @return Column
  54. */
  55. public function columnStatus(string $field = 'status', string $label = '状态'): Column
  56. {
  57. return $this->grid->column($field, $label)
  58. ->using(FUND_STATUS::getNames())
  59. ->label([
  60. FUND_STATUS::NORMAL => 'success',
  61. FUND_STATUS::FROZEN => 'warning',
  62. FUND_STATUS::DELETED => 'danger',
  63. ]);
  64. }
  65. /**
  66. * 添加操作金额列
  67. *
  68. * 复用价值:高 - 统一处理操作金额的格式化显示,包括正负值的颜色区分
  69. *
  70. * @param string $field 字段名
  71. * @param string $label 标签名
  72. * @return Column
  73. */
  74. public function columnAmount(string $field = 'amount', string $label = '操作金额'): Column
  75. {
  76. return $this->grid->column($field, $label)->display(function ($value) {
  77. $formattedValue = number_format($value / 1000, 3);
  78. if ($value > 0) {
  79. return "<span class='text-success'>+{$formattedValue}</span>";
  80. } elseif ($value < 0) {
  81. return "<span class='text-danger'>{$formattedValue}</span>";
  82. } else {
  83. return "<span>{$formattedValue}</span>";
  84. }
  85. })->sortable();
  86. }
  87. /**
  88. * 添加操作类型列
  89. *
  90. * 复用价值:高 - 统一处理操作类型的显示,使用枚举类型
  91. *
  92. * @param string $field 字段名
  93. * @param string $label 标签名
  94. * @return Column
  95. */
  96. public function columnOperateType(string $field = 'operate_type', string $label = '操作类型'): Column
  97. {
  98. return $this->grid->column($field, $label)->using(LOG_TYPE::getNames());
  99. }
  100. /**
  101. * 添加时间戳格式化列
  102. *
  103. * 复用价值:高 - 统一处理时间戳的格式化显示
  104. *
  105. * @param string $field 字段名
  106. * @param string $label 标签名
  107. * @param string $format 日期格式
  108. * @return Column
  109. */
  110. public function columnTimestamp(string $field, string $label, string $format = 'Y-m-d H:i:s'): Column
  111. {
  112. return $this->grid->column($field, $label)->display(function ($value) use ($format) {
  113. return $value ? date($format, $value) : '';
  114. })->sortable();
  115. }
  116. /**
  117. * 添加金额格式化列(毫转元)
  118. *
  119. * 复用价值:高 - 统一处理金额的格式化显示,包括单位转换
  120. *
  121. * @param string $field 字段名
  122. * @param string $label 标签名
  123. * @param int $decimals 小数位数
  124. * @return Column
  125. */
  126. public function columnMoney(string $field, string $label, int $decimals = 3): Column
  127. {
  128. return $this->grid->column($field, $label)->display(function ($value) use ($decimals) {
  129. return number_format($value / 1000, $decimals);
  130. });
  131. }
  132. /**
  133. * 添加资金账户组合列
  134. *
  135. * 复用价值:高 - 将用户ID和资金账户组合显示,提高信息密度
  136. *
  137. * @param string $userIdField 用户ID字段名
  138. * @param string $fundIdField 资金账户字段名
  139. * @param string $label 标签名
  140. * @return Column
  141. */
  142. public function columnUserFund(string $userIdField = 'user_id', string $fundIdField = 'fund_id', string $label = '用户/账户'): Column
  143. {
  144. return $this->grid->column($userIdField, $label)->display(function ($userId) use ($fundIdField) {
  145. $fundId = $this->{$fundIdField};
  146. $fundsDesc = AccountService::getFundsDesc();
  147. if(is_int($fundId)){
  148. $fundName = $fundsDesc[$fundId] ?? $fundId;
  149. }else{
  150. $fundName = $fundsDesc[$fundId->value] ?? $fundId;
  151. }
  152. return "ID: {$userId}<br>账户: {$fundName}";
  153. });
  154. }
  155. /**
  156. * 添加转账信息组合列
  157. *
  158. * 复用价值:高 - 将转账的来源和目标信息组合显示,提高信息密度
  159. *
  160. * @param string $label 标签名
  161. * @return Column
  162. */
  163. public function columnTransferInfo(string $label = '转账信息'): Column
  164. {
  165. return $this->grid->column('user_id', $label)->display(function ($userId) {
  166. $fromFundId = $this->fund_id;
  167. $toUserId = $this->to_user_id;
  168. $toFundId = $this->to_fund_id;
  169. $fundsDesc = AccountService::getFundsDesc();
  170. $fromFundName = $fundsDesc[$fromFundId] ?? $fromFundId;
  171. $toFundName = $fundsDesc[$toFundId] ?? $toFundId;
  172. return "从: 用户{$userId}/{$fromFundName}<br>到: 用户{$toUserId}/{$toFundName}";
  173. });
  174. }
  175. }