GridHelper.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace App\Module\Transfer\AdminControllers\Helper;
  3. use App\Module\Transfer\Enums\TransferStatus;
  4. use App\Module\Transfer\Enums\TransferType;
  5. use Dcat\Admin\Grid;
  6. /**
  7. * 表格辅助类
  8. */
  9. class GridHelper
  10. {
  11. /**
  12. * 配置订单表格列
  13. *
  14. * @param Grid $grid
  15. * @return void
  16. */
  17. public static function orderColumns(Grid $grid): void
  18. {
  19. // 基本信息列
  20. $grid->column('id', '订单ID')->sortable();
  21. $grid->column('out_order_id', '外部订单ID')->copyable();
  22. // 用户信息列
  23. $grid->column('user_id', '用户ID')->link(function ($value) {
  24. return admin_url("users/{$value}");
  25. });
  26. $grid->column('out_user_id', '外部用户ID');
  27. // 应用信息列
  28. $grid->column('transferApp.title', '划转应用')->link(function () {
  29. return admin_url("transfer/apps/{$this->transfer_app_id}");
  30. });
  31. // 类型和状态列
  32. $grid->column('type', '类型')->using(self::getTypeLabels())->label(self::getTypeColors());
  33. $grid->column('status', '状态')->using(self::getStatusLabels())->label(self::getStatusColors());
  34. // 金额信息列
  35. $grid->column('amount', '内部金额')->display(function ($value) {
  36. return number_format($value, 2);
  37. })->sortable();
  38. $grid->column('out_amount', '外部金额')->display(function ($value) {
  39. return number_format($value, 2);
  40. })->sortable();
  41. $grid->column('exchange_rate', '汇率')->display(function ($value) {
  42. return number_format($value, 4);
  43. });
  44. // 时间信息列
  45. $grid->column('created_at', '创建时间')->sortable();
  46. $grid->column('processed_at', '处理时间');
  47. $grid->column('completed_at', '完成时间');
  48. // 错误信息列
  49. $grid->column('error_message', '错误信息')->limit(50)->help('点击查看完整错误信息');
  50. // 备注列
  51. $grid->column('remark', '备注')->limit(30);
  52. }
  53. /**
  54. * 配置应用表格列
  55. *
  56. * @param Grid $grid
  57. * @return void
  58. */
  59. public static function appColumns(Grid $grid): void
  60. {
  61. // 基本信息列
  62. $grid->column('id', '应用ID')->sortable();
  63. $grid->column('keyname', '应用标识')->copyable();
  64. $grid->column('title', '应用名称')->link(function () {
  65. return admin_url("transfer/apps/{$this->id}");
  66. });
  67. $grid->column('description', '描述')->limit(50);
  68. // 外部应用信息列
  69. $grid->column('out_id', '外部应用ID');
  70. $grid->column('out_id2', '外部应用ID2');
  71. $grid->column('out_id3', '外部应用ID3');
  72. // 配置信息列
  73. $grid->column('currency_id', '货币类型');
  74. $grid->column('fund_id', '资金账户类型');
  75. $grid->column('exchange_rate', '汇率')->display(function ($value) {
  76. return number_format($value, 4);
  77. });
  78. // 状态列
  79. $grid->column('is_enabled', '状态')->switch([
  80. 'on' => ['value' => 1, 'text' => '启用', 'color' => 'success'],
  81. 'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
  82. ]);
  83. // API配置状态列
  84. $grid->column('api_status', 'API配置')->display(function () {
  85. $status = [];
  86. if ($this->order_callback_url) $status[] = '回调';
  87. if ($this->order_in_info_url) $status[] = '转入查询';
  88. if ($this->order_out_create_url) $status[] = '转出创建';
  89. if ($this->order_out_info_url) $status[] = '转出查询';
  90. if (empty($status)) {
  91. return '<span class="badge badge-info">内部模式</span>';
  92. }
  93. return '<span class="badge badge-success">' . implode('、', $status) . '</span>';
  94. });
  95. // 统计信息列
  96. $grid->column('orders_count', '订单数量')->display(function () {
  97. return $this->orders()->count();
  98. });
  99. // 时间列
  100. $grid->column('created_at', '创建时间')->sortable();
  101. $grid->column('updated_at', '更新时间');
  102. }
  103. /**
  104. * 配置表格工具栏
  105. *
  106. * @param Grid $grid
  107. * @return void
  108. */
  109. public static function configureTools(Grid $grid): void
  110. {
  111. // 批量操作
  112. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  113. $batch->disableDelete(); // 禁用批量删除
  114. });
  115. // 导出功能
  116. $grid->export()->rows(function (array $rows) {
  117. foreach ($rows as $index => &$row) {
  118. // 格式化导出数据
  119. if (isset($row['type'])) {
  120. $row['type'] = TransferType::from($row['type'])->getDescription();
  121. }
  122. if (isset($row['status'])) {
  123. $row['status'] = TransferStatus::from($row['status'])->getDescription();
  124. }
  125. }
  126. return $rows;
  127. });
  128. // 刷新按钮
  129. $grid->tools(function (Grid\Tools $tools) {
  130. $tools->append('<a class="btn btn-sm btn-outline-primary" href="javascript:void(0)" onclick="location.reload()">
  131. <i class="fa fa-refresh"></i> 刷新
  132. </a>');
  133. });
  134. }
  135. /**
  136. * 获取类型标签映射
  137. *
  138. * @return array
  139. */
  140. protected static function getTypeLabels(): array
  141. {
  142. $labels = [];
  143. foreach (TransferType::cases() as $type) {
  144. $labels[$type->value] = $type->getDescription();
  145. }
  146. return $labels;
  147. }
  148. /**
  149. * 获取类型颜色映射
  150. *
  151. * @return array
  152. */
  153. protected static function getTypeColors(): array
  154. {
  155. $colors = [];
  156. foreach (TransferType::cases() as $type) {
  157. $colors[$type->value] = $type->getColor();
  158. }
  159. return $colors;
  160. }
  161. /**
  162. * 获取状态标签映射
  163. *
  164. * @return array
  165. */
  166. protected static function getStatusLabels(): array
  167. {
  168. $labels = [];
  169. foreach (TransferStatus::cases() as $status) {
  170. $labels[$status->value] = $status->getDescription();
  171. }
  172. return $labels;
  173. }
  174. /**
  175. * 获取状态颜色映射
  176. *
  177. * @return array
  178. */
  179. protected static function getStatusColors(): array
  180. {
  181. $colors = [];
  182. foreach (TransferStatus::cases() as $status) {
  183. $colors[$status->value] = $status->getColor();
  184. }
  185. return $colors;
  186. }
  187. /**
  188. * 配置分页
  189. *
  190. * @param Grid $grid
  191. * @param int $perPage
  192. * @return void
  193. */
  194. public static function configurePagination(Grid $grid, int $perPage = 20): void
  195. {
  196. $grid->paginate($perPage);
  197. $grid->perPages([10, 20, 50, 100]);
  198. }
  199. /**
  200. * 配置排序
  201. *
  202. * @param Grid $grid
  203. * @param string $column
  204. * @param string $direction
  205. * @return void
  206. */
  207. public static function configureSort(Grid $grid, string $column = 'created_at', string $direction = 'desc'): void
  208. {
  209. $grid->model()->orderBy($column, $direction);
  210. }
  211. }