GridHelperTrait.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace App\Module\Fund\AdminControllers\Helper;
  3. use App\Module\Fund\Enums\LOG_TYPE;
  4. use App\Module\Fund\Services\AccountService;
  5. use Dcat\Admin\Grid\Column;
  6. /**
  7. * 列表页辅助特性
  8. *
  9. * 提供资金模块后台控制器的列表页构建功能的具体实现
  10. * 只保留具有复用价值的方法
  11. */
  12. trait GridHelperTrait
  13. {
  14. /**
  15. * 添加资金账户列
  16. *
  17. * 复用价值:高 - 统一处理资金账户的显示,使用AccountService获取账户描述
  18. *
  19. * @param string $field 字段名
  20. * @param string $label 标签名
  21. * @return Column
  22. */
  23. public function columnFundId(string $field = 'fund_id', string $label = '资金账户'): Column
  24. {
  25. return $this->grid->column($field, $label)->using(AccountService::getFundsDesc());
  26. }
  27. /**
  28. * 添加资金余额列
  29. *
  30. * 使用基于币种精度的动态格式化显示
  31. *
  32. * @param string $field 字段名
  33. * @param string $label 标签名
  34. * @return Column
  35. */
  36. public function columnBalance(string $field = 'balance', string $label = '余额'): Column
  37. {
  38. return $this->grid->column($field, $label)->display(function ($value) {
  39. // 获取当前行的fund_id,然后获取对应的币种信息进行格式化
  40. $fundId = $this->fund_id;
  41. if (is_object($fundId)) {
  42. $fundId = $fundId->value;
  43. }
  44. // 获取币种信息并格式化金额
  45. $currency = \App\Module\Fund\Services\CurrencyService::getCurrencyByFundId($fundId);
  46. if ($currency) {
  47. return $currency->formatAmountName($value);
  48. }
  49. // 如果无法获取币种信息,使用默认格式
  50. return number_format($value, 0, '.', ',');
  51. })->sortable();
  52. }
  53. /**
  54. * 添加操作金额列
  55. *
  56. * 使用基于币种精度的动态格式化显示,包括正负值的颜色区分
  57. *
  58. * @param string $field 字段名
  59. * @param string $label 标签名
  60. * @return Column
  61. */
  62. public function columnAmount(string $field = 'amount', string $label = '操作金额'): Column
  63. {
  64. return $this->grid->column($field, $label)->display(function ($value) {
  65. // 获取当前行的fund_id,然后获取对应的币种信息进行格式化
  66. $fundId = $this->fund_id;
  67. if (is_object($fundId)) {
  68. $fundId = $fundId->value;
  69. }
  70. // 获取币种信息并格式化金额
  71. $currency = \App\Module\Fund\Services\CurrencyService::getCurrencyByFundId($fundId);
  72. if ($currency) {
  73. $formattedValue = $currency->formatAmount(abs($value));
  74. } else {
  75. // 如果无法获取币种信息,使用默认格式
  76. $formattedValue = number_format(abs($value), 0, '.', ',');
  77. }
  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::getValueDescription());
  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. * @return Column
  124. */
  125. public function columnMoney(string $field, string $label, ): Column
  126. {
  127. return $this->grid->column($field, $label)->display(function ($value) {
  128. return number_format($value , 0,',');
  129. });
  130. }
  131. /**
  132. * 添加资金账户组合列
  133. *
  134. * 复用价值:高 - 将用户ID和资金账户组合显示,提高信息密度
  135. *
  136. * @param string $userIdField 用户ID字段名
  137. * @param string $fundIdField 资金账户字段名
  138. * @param string $label 标签名
  139. * @return Column
  140. */
  141. public function columnUserFund(string $userIdField = 'user_id', string $fundIdField = 'fund_id', string $label = '用户/账户'): Column
  142. {
  143. return $this->grid->column($userIdField, $label)->display(function ($userId) use ($fundIdField) {
  144. $fundId = $this->{$fundIdField};
  145. $fundsDesc = AccountService::getFundsDesc();
  146. if(is_int($fundId)){
  147. $fundName = $fundsDesc[$fundId] ?? $fundId;
  148. }else{
  149. $fundName = $fundsDesc[$fundId->value] ?? $fundId;
  150. }
  151. return "ID: {$userId}<br>账户: {$fundName}";
  152. });
  153. }
  154. /**
  155. * 添加转账信息组合列
  156. *
  157. * 复用价值:高 - 将转账的来源和目标信息组合显示,提高信息密度
  158. *
  159. * @param string $label 标签名
  160. * @return Column
  161. */
  162. public function columnTransferInfo(string $label = '转账信息'): Column
  163. {
  164. return $this->grid->column('user_id', $label)->display(function ($userId) {
  165. $fromFundId = $this->fund_id;
  166. $toUserId = $this->to_user_id;
  167. $toFundId = $this->to_fund_id;
  168. $fundsDesc = AccountService::getFundsDesc();
  169. $fromFundName = $fundsDesc[$fromFundId] ?? $fromFundId;
  170. $toFundName = $fundsDesc[$toFundId] ?? $toFundId;
  171. return "从: 用户{$userId}/{$fromFundName}<br>到: 用户{$toUserId}/{$toFundName}";
  172. });
  173. }
  174. }