GridHelperTrait.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace App\Module\Task\AdminControllers\Helper;
  3. use App\Module\Task\Enums\RESET_TYPE;
  4. use App\Module\Task\Enums\REWARD_TYPE;
  5. use App\Module\Task\Enums\TASK_STATUS;
  6. use App\Module\Task\Enums\TASK_TYPE;
  7. use App\Module\Task\Models\Task;
  8. use App\Module\Task\Models\TaskCategory;
  9. use App\Module\Task\Models\TaskCondition;
  10. /**
  11. * 表格辅助特性
  12. *
  13. * 提供任务模块后台控制器的表格构建功能
  14. */
  15. trait GridHelperTrait
  16. {
  17. /**
  18. * 显示任务类型
  19. *
  20. * @param string $field 字段名
  21. * @param string $label 标签
  22. * @return \Dcat\Admin\Grid\Column
  23. */
  24. public function columnTaskType(string $field = 'type', string $label = '任务类型')
  25. {
  26. return $this->grid->column($field, $label)->display(function ($type) {
  27. return TASK_TYPE::getDescription(TASK_TYPE::tryFrom($type) ?? TASK_TYPE::DAILY);
  28. });
  29. }
  30. /**
  31. * 显示任务状态
  32. *
  33. * @param string $field 字段名
  34. * @param string $label 标签
  35. * @return \Dcat\Admin\Grid\Column
  36. */
  37. public function columnTaskStatus(string $field = 'status', string $label = '任务状态')
  38. {
  39. return $this->grid->column($field, $label)->display(function ($status) {
  40. return TASK_STATUS::getDescription(TASK_STATUS::from($status));
  41. });
  42. }
  43. /**
  44. * 显示重置类型
  45. *
  46. * @param string $field 字段名
  47. * @param string $label 标签
  48. * @return \Dcat\Admin\Grid\Column
  49. */
  50. public function columnResetType(string $field = 'reset_type', string $label = '重置类型')
  51. {
  52. return $this->grid->column($field, $label)->display(function ($resetType) {
  53. return RESET_TYPE::getDescription(RESET_TYPE::tryFrom($resetType) ?? RESET_TYPE::NONE);
  54. });
  55. }
  56. /**
  57. * 显示奖励类型
  58. *
  59. * @param string $field 字段名
  60. * @param string $label 标签
  61. * @return \Dcat\Admin\Grid\Column
  62. */
  63. public function columnRewardType(string $field = 'reward_type', string $label = '奖励类型')
  64. {
  65. return $this->grid->column($field, $label)->display(function ($rewardType) {
  66. return REWARD_TYPE::getDescription(REWARD_TYPE::tryFrom($rewardType) ?? REWARD_TYPE::ITEM);
  67. });
  68. }
  69. /**
  70. * 显示条件类型
  71. *
  72. * @param string $field 字段名
  73. * @param string $label 标签
  74. * @return \Dcat\Admin\Grid\Column
  75. */
  76. public function columnConditionType(string $field = 'condition_type', string $label = '条件类型')
  77. {
  78. return $this->grid->column($field, $label)->display(function ($type) {
  79. return $type == 'prerequisite' ? '前置条件' : '进度条件';
  80. });
  81. }
  82. /**
  83. * 显示进度百分比
  84. *
  85. * @param string $field 字段名
  86. * @param string $label 标签
  87. * @return \Dcat\Admin\Grid\Column
  88. */
  89. public function columnProgress(string $field = 'progress', string $label = '进度')
  90. {
  91. return $this->grid->column($field, $label)->display(function ($progress) {
  92. $percentage = $progress . '%';
  93. $color = $progress >= 100 ? 'success' : ($progress >= 50 ? 'warning' : 'info');
  94. return "<div class='progress' style='height: 20px;'>
  95. <div class='progress-bar bg-{$color}' role='progressbar' style='width: {$progress}%' aria-valuenow='{$progress}' aria-valuemin='0' aria-valuemax='100'>
  96. {$percentage}
  97. </div>
  98. </div>";
  99. });
  100. }
  101. /**
  102. * 显示计算的进度百分比
  103. *
  104. * @param string $currentField 当前值字段名
  105. * @param string $targetField 目标值字段名
  106. * @param string $label 标签
  107. * @return \Dcat\Admin\Grid\Column
  108. */
  109. public function columnCalculatedProgress(string $currentField = 'current_value', string $targetField = 'target_value', string $label = '进度')
  110. {
  111. return $this->grid->column('progress', $label)->display(function () use ($currentField, $targetField) {
  112. $current = $this->{$currentField};
  113. $target = $this->{$targetField};
  114. $progress = $target > 0 ? min(100, round(($current / $target) * 100)) : 0;
  115. return $progress;
  116. })->progressBar();
  117. }
  118. /**
  119. * 显示父级分类
  120. *
  121. * @param string $field 字段名
  122. * @param string $label 标签
  123. * @return \Dcat\Admin\Grid\Column
  124. */
  125. public function columnParentCategory(string $field = 'parent_id', string $label = '父级分类')
  126. {
  127. return $this->grid->column($field, $label)->display(function ($parentId) {
  128. if ($parentId == 0) {
  129. return '无';
  130. }
  131. $parent = TaskCategory::find($parentId);
  132. return $parent ? $parent->name : '未知';
  133. });
  134. }
  135. /**
  136. * 显示任务选择器
  137. *
  138. * @param string $field 字段名
  139. * @param string $label 标签
  140. * @return \Dcat\Admin\Grid\Column
  141. */
  142. public function columnTaskSelector(string $field = 'task_id', string $label = '任务')
  143. {
  144. return $this->grid->column($field, $label)->display(function ($taskId) {
  145. $task = Task::find($taskId);
  146. return $task ? $task->name : "未知任务(#{$taskId})";
  147. });
  148. }
  149. /**
  150. * 显示条件选择器
  151. *
  152. * @param string $field 字段名
  153. * @param string $label 标签
  154. * @return \Dcat\Admin\Grid\Column
  155. */
  156. public function columnConditionSelector(string $field = 'condition_id', string $label = '条件')
  157. {
  158. return $this->grid->column($field, $label)->display(function ($conditionId) {
  159. $condition = TaskCondition::find($conditionId);
  160. return $condition ? $condition->name : "未知条件(#{$conditionId})";
  161. });
  162. }
  163. /**
  164. * 显示奖励内容
  165. *
  166. * @param string $field 字段名
  167. * @param string $label 标签
  168. * @return \Dcat\Admin\Grid\Column
  169. */
  170. public function columnRewards(string $field = 'rewards', string $label = '奖励内容')
  171. {
  172. return $this->grid->column($field, $label)->display(function ($rewards) {
  173. $result = [];
  174. foreach ($rewards as $reward) {
  175. // 兼容不同的字段名格式:reward_type 或 rewardType
  176. $rewardType = $reward['reward_type'] ?? $reward['rewardType'] ?? null;
  177. $type = REWARD_TYPE::getDescription(REWARD_TYPE::tryFrom($rewardType) ?? REWARD_TYPE::ITEM);
  178. $result[] = "{$type}: {$reward['quantity']}";
  179. }
  180. return implode('<br>', $result);
  181. });
  182. }
  183. /**
  184. * 显示消耗内容
  185. *
  186. * @param string $field 字段名
  187. * @param string $label 标签
  188. * @return \Dcat\Admin\Grid\Column
  189. */
  190. public function columnCosts(string $field = 'costs', string $label = '消耗内容')
  191. {
  192. return $this->grid->column($field, $label)->display(function ($costs) {
  193. $result = [];
  194. foreach ($costs as $cost) {
  195. $result[] = "{$cost['cost_type']}: {$cost['quantity']}";
  196. }
  197. return implode('<br>', $result);
  198. });
  199. }
  200. /**
  201. * 显示自动化配置
  202. *
  203. * @param string $label 标签
  204. * @return \Dcat\Admin\Grid\Column
  205. */
  206. public function columnAutoConfig(string $label = '自动化配置')
  207. {
  208. return $this->grid->column('auto_config', $label)->display(function () {
  209. $configs = [];
  210. if ($this->auto_accept) $configs[] = '<span class="badge badge-primary">自动接取</span>';
  211. if ($this->auto_complete) $configs[] = '<span class="badge badge-success">自动完成</span>';
  212. if ($this->auto_reward) $configs[] = '<span class="badge badge-warning">自动奖励</span>';
  213. if ($this->auto_reset) $configs[] = '<span class="badge badge-info">自动重置</span>';
  214. return empty($configs) ? '<span class="text-muted">无</span>' : implode(' ', $configs);
  215. });
  216. }
  217. /**
  218. * 显示自动接取状态
  219. *
  220. * @param string $field 字段名
  221. * @param string $label 标签
  222. * @return \Dcat\Admin\Grid\Column
  223. */
  224. public function columnAutoAccept(string $field = 'auto_accept', string $label = '自动接取')
  225. {
  226. return $this->grid->column($field, $label)->switch();
  227. }
  228. /**
  229. * 显示自动完成状态
  230. *
  231. * @param string $field 字段名
  232. * @param string $label 标签
  233. * @return \Dcat\Admin\Grid\Column
  234. */
  235. public function columnAutoComplete(string $field = 'auto_complete', string $label = '自动完成')
  236. {
  237. return $this->grid->column($field, $label)->switch();
  238. }
  239. /**
  240. * 显示自动发放奖励状态
  241. *
  242. * @param string $field 字段名
  243. * @param string $label 标签
  244. * @return \Dcat\Admin\Grid\Column
  245. */
  246. public function columnAutoReward(string $field = 'auto_reward', string $label = '自动奖励')
  247. {
  248. return $this->grid->column($field, $label)->switch();
  249. }
  250. /**
  251. * 显示自动重置状态
  252. *
  253. * @param string $field 字段名
  254. * @param string $label 标签
  255. * @return \Dcat\Admin\Grid\Column
  256. */
  257. public function columnAutoReset(string $field = 'auto_reset', string $label = '自动重置')
  258. {
  259. return $this->grid->column($field, $label)->switch();
  260. }
  261. }