GridHelperTrait.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace App\Module\Point\AdminControllers\Helper;
  3. use App\Module\Point\Enums\LOG_TYPE;
  4. use App\Module\Point\Enums\POINT_TYPE;
  5. use App\Module\Point\Services\AccountService;
  6. use Dcat\Admin\Grid\Column;
  7. /**
  8. * 列表页辅助特性
  9. *
  10. * 提供积分模块后台控制器的列表页构建功能的具体实现
  11. * 只保留具有复用价值的方法
  12. */
  13. trait GridHelperTrait
  14. {
  15. /**
  16. * 添加积分类型列
  17. *
  18. * 复用价值:高 - 统一处理积分类型的显示,使用枚举类型
  19. *
  20. * @param string $field 字段名
  21. * @param string $label 标签名
  22. * @return Column
  23. */
  24. public function columnPointId(string $field = 'point_id', string $label = '积分类型'): Column
  25. {
  26. return $this->grid->column($field, $label)->using([
  27. 1 => '经验积分',
  28. 2 => '成就积分',
  29. 3 => '活动积分',
  30. 4 => '签到积分',
  31. 5 => '推荐积分',
  32. ])->label([
  33. 1 => 'primary',
  34. 2 => 'success',
  35. 3 => 'warning',
  36. 4 => 'info',
  37. 5 => 'danger',
  38. ]);
  39. }
  40. /**
  41. * 添加积分余额列
  42. *
  43. * 整数积分显示,带千分位分隔符
  44. *
  45. * @param string $field 字段名
  46. * @param string $label 标签名
  47. * @return Column
  48. */
  49. public function columnBalance(string $field = 'balance', string $label = '积分余额'): Column
  50. {
  51. return $this->grid->column($field, $label)->display(function ($value) {
  52. return number_format($value);
  53. })->sortable();
  54. }
  55. /**
  56. * 添加操作积分数量列
  57. *
  58. * 整数积分显示,包括正负值的颜色区分
  59. *
  60. * @param string $field 字段名
  61. * @param string $label 标签名
  62. * @return Column
  63. */
  64. public function columnAmount(string $field = 'amount', string $label = '操作积分'): Column
  65. {
  66. return $this->grid->column($field, $label)->display(function ($value) {
  67. $formattedValue = number_format(abs($value));
  68. if ($value > 0) {
  69. return "<span class='text-success'>+{$formattedValue}</span>";
  70. } elseif ($value < 0) {
  71. return "<span class='text-danger'>-{$formattedValue}</span>";
  72. } else {
  73. return "<span>{$formattedValue}</span>";
  74. }
  75. })->sortable();
  76. }
  77. /**
  78. * 添加操作类型列
  79. *
  80. * 复用价值:高 - 统一处理操作类型的显示,使用枚举类型
  81. *
  82. * @param string $field 字段名
  83. * @param string $label 标签名
  84. * @return Column
  85. */
  86. public function columnOperateType(string $field = 'operate_type', string $label = '操作类型'): Column
  87. {
  88. return $this->grid->column($field, $label)->using(LOG_TYPE::getAllTypes())->label([
  89. 0 => 'default',
  90. 1 => 'success',
  91. 2 => 'primary',
  92. 3 => 'info',
  93. 4 => 'warning',
  94. 5 => 'success',
  95. 6 => 'success',
  96. 7 => 'danger',
  97. 8 => 'warning',
  98. 9 => 'info',
  99. 10 => 'info',
  100. 11 => 'primary',
  101. 12 => 'warning',
  102. 13 => 'success',
  103. 14 => 'success',
  104. 15 => 'danger',
  105. ]);
  106. }
  107. /**
  108. * 添加时间戳格式化列
  109. *
  110. * 复用价值:高 - 统一处理时间戳的格式化显示
  111. *
  112. * @param string $field 字段名
  113. * @param string $label 标签名
  114. * @param string $format 日期格式
  115. * @return Column
  116. */
  117. public function columnTimestamp(string $field, string $label, string $format = 'Y-m-d H:i:s'): Column
  118. {
  119. return $this->grid->column($field, $label)->display(function ($value) use ($format) {
  120. return $value ? date($format, $value) : '';
  121. })->sortable();
  122. }
  123. /**
  124. * 添加积分数量格式化
  125. *
  126. * 复用价值:高 - 统一处理积分数量的格式化显示
  127. *
  128. * @param string $field 字段名
  129. * @param string $label 标签名
  130. * @return Column
  131. */
  132. public function columnPoints(string $field, string $label): Column
  133. {
  134. return $this->grid->column($field, $label)->display(function ($value) {
  135. return number_format($value);
  136. })->sortable();
  137. }
  138. /**
  139. * 添加用户积分组合列
  140. *
  141. * 复用价值:高 - 将用户ID和积分类型组合显示,提高信息密度
  142. *
  143. * @param string $userIdField 用户ID字段名
  144. * @param string $pointIdField 积分类型字段名
  145. * @param string $label 标签名
  146. * @return Column
  147. */
  148. public function columnUserPoint(string $userIdField = 'user_id', string $pointIdField = 'point_id', string $label = '用户/积分'): Column
  149. {
  150. return $this->grid->column($userIdField, $label)->display(function ($userId) use ($pointIdField) {
  151. $pointId = $this->{$pointIdField};
  152. $pointTypes = [
  153. 1 => '经验积分',
  154. 2 => '成就积分',
  155. 3 => '活动积分',
  156. 4 => '签到积分',
  157. 5 => '推荐积分',
  158. ];
  159. if (is_object($pointId)) {
  160. $pointId = $pointId->value;
  161. }
  162. $pointName = $pointTypes[$pointId] ?? "积分{$pointId}";
  163. return "ID: {$userId}<br>类型: {$pointName}";
  164. });
  165. }
  166. /**
  167. * 添加积分流转信息组合列
  168. *
  169. * 复用价值:高 - 将积分流转的来源和目标信息组合显示
  170. *
  171. * @param string $label 标签名
  172. * @return Column
  173. */
  174. public function columnCirculationInfo(string $label = '流转信息'): Column
  175. {
  176. return $this->grid->column('user_id', $label)->display(function ($userId) {
  177. $fromPointId = $this->from_point_id;
  178. $toPointId = $this->to_point_id;
  179. $pointTypes = [
  180. 1 => '经验积分',
  181. 2 => '成就积分',
  182. 3 => '活动积分',
  183. 4 => '签到积分',
  184. 5 => '推荐积分',
  185. ];
  186. if (is_object($fromPointId)) {
  187. $fromPointId = $fromPointId->value;
  188. }
  189. if (is_object($toPointId)) {
  190. $toPointId = $toPointId->value;
  191. }
  192. $fromPointName = $pointTypes[$fromPointId] ?? "积分{$fromPointId}";
  193. $toPointName = $pointTypes[$toPointId] ?? "积分{$toPointId}";
  194. return "用户: {$userId}<br>从: {$fromPointName}<br>到: {$toPointName}";
  195. });
  196. }
  197. /**
  198. * 添加积分转账信息组合列
  199. *
  200. * 复用价值:高 - 将转账的来源和目标用户信息组合显示
  201. *
  202. * @param string $label 标签名
  203. * @return Column
  204. */
  205. public function columnTransferInfo(string $label = '转账信息'): Column
  206. {
  207. return $this->grid->column('from_user_id', $label)->display(function ($fromUserId) {
  208. $toUserId = $this->to_user_id;
  209. $pointId = $this->point_id;
  210. $pointTypes = [
  211. 1 => '经验积分',
  212. 2 => '成就积分',
  213. 3 => '活动积分',
  214. 4 => '签到积分',
  215. 5 => '推荐积分',
  216. ];
  217. if (is_object($pointId)) {
  218. $pointId = $pointId->value;
  219. }
  220. $pointName = $pointTypes[$pointId] ?? "积分{$pointId}";
  221. return "从: 用户{$fromUserId}<br>到: 用户{$toUserId}<br>类型: {$pointName}";
  222. });
  223. }
  224. /**
  225. * 添加状态列
  226. *
  227. * @param string $field 字段名
  228. * @param string $label 标签名
  229. * @return Column
  230. */
  231. public function columnStatus(string $field = 'status', string $label = '状态'): Column
  232. {
  233. return $this->grid->column($field, $label)->using([
  234. 0 => '待处理',
  235. 1 => '已完成',
  236. 2 => '已失败',
  237. ])->label([
  238. 0 => 'warning',
  239. 1 => 'success',
  240. 2 => 'danger',
  241. ]);
  242. }
  243. }