GridHelperTrait.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. return $progress . '%';
  93. })->progress();
  94. }
  95. /**
  96. * 显示计算的进度百分比
  97. *
  98. * @param string $currentField 当前值字段名
  99. * @param string $targetField 目标值字段名
  100. * @param string $label 标签
  101. * @return \Dcat\Admin\Grid\Column
  102. */
  103. public function columnCalculatedProgress(string $currentField = 'current_value', string $targetField = 'target_value', string $label = '进度')
  104. {
  105. return $this->grid->column('progress', $label)->display(function () use ($currentField, $targetField) {
  106. $current = $this->{$currentField};
  107. $target = $this->{$targetField};
  108. $progress = $target > 0 ? min(100, round(($current / $target) * 100)) : 0;
  109. return $progress . '%';
  110. })->progress();
  111. }
  112. /**
  113. * 显示父级分类
  114. *
  115. * @param string $field 字段名
  116. * @param string $label 标签
  117. * @return \Dcat\Admin\Grid\Column
  118. */
  119. public function columnParentCategory(string $field = 'parent_id', string $label = '父级分类')
  120. {
  121. return $this->grid->column($field, $label)->display(function ($parentId) {
  122. if ($parentId == 0) {
  123. return '无';
  124. }
  125. $parent = TaskCategory::find($parentId);
  126. return $parent ? $parent->name : '未知';
  127. });
  128. }
  129. /**
  130. * 显示任务选择器
  131. *
  132. * @param string $field 字段名
  133. * @param string $label 标签
  134. * @return \Dcat\Admin\Grid\Column
  135. */
  136. public function columnTaskSelector(string $field = 'task_id', string $label = '任务')
  137. {
  138. return $this->grid->column($field, $label)->display(function ($taskId) {
  139. $task = Task::find($taskId);
  140. return $task ? $task->name : "未知任务(#{$taskId})";
  141. });
  142. }
  143. /**
  144. * 显示条件选择器
  145. *
  146. * @param string $field 字段名
  147. * @param string $label 标签
  148. * @return \Dcat\Admin\Grid\Column
  149. */
  150. public function columnConditionSelector(string $field = 'condition_id', string $label = '条件')
  151. {
  152. return $this->grid->column($field, $label)->display(function ($conditionId) {
  153. $condition = TaskCondition::find($conditionId);
  154. return $condition ? $condition->name : "未知条件(#{$conditionId})";
  155. });
  156. }
  157. /**
  158. * 显示奖励内容
  159. *
  160. * @param string $field 字段名
  161. * @param string $label 标签
  162. * @return \Dcat\Admin\Grid\Column
  163. */
  164. public function columnRewards(string $field = 'rewards', string $label = '奖励内容')
  165. {
  166. return $this->grid->column($field, $label)->display(function ($rewards) {
  167. $result = [];
  168. foreach ($rewards as $reward) {
  169. $type = REWARD_TYPE::getDescription(REWARD_TYPE::tryFrom($reward['reward_type']) ?? REWARD_TYPE::ITEM);
  170. $result[] = "{$type}: {$reward['quantity']}";
  171. }
  172. return implode('<br>', $result);
  173. });
  174. }
  175. /**
  176. * 显示消耗内容
  177. *
  178. * @param string $field 字段名
  179. * @param string $label 标签
  180. * @return \Dcat\Admin\Grid\Column
  181. */
  182. public function columnCosts(string $field = 'costs', string $label = '消耗内容')
  183. {
  184. return $this->grid->column($field, $label)->display(function ($costs) {
  185. $result = [];
  186. foreach ($costs as $cost) {
  187. $result[] = "{$cost['cost_type']}: {$cost['quantity']}";
  188. }
  189. return implode('<br>', $result);
  190. });
  191. }
  192. }