| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace App\Module\Task\AdminControllers\Helper;
- use App\Module\Task\Enums\RESET_TYPE;
- use App\Module\Task\Enums\REWARD_TYPE;
- use App\Module\Task\Enums\TASK_STATUS;
- use App\Module\Task\Enums\TASK_TYPE;
- use App\Module\Task\Models\Task;
- use App\Module\Task\Models\TaskCategory;
- use App\Module\Task\Models\TaskCondition;
- /**
- * 表格辅助特性
- *
- * 提供任务模块后台控制器的表格构建功能
- */
- trait GridHelperTrait
- {
- /**
- * 显示任务类型
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnTaskType(string $field = 'type', string $label = '任务类型')
- {
- return $this->grid->column($field, $label)->display(function ($type) {
- return TASK_TYPE::getDescription(TASK_TYPE::tryFrom($type) ?? TASK_TYPE::DAILY);
- });
- }
- /**
- * 显示任务状态
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnTaskStatus(string $field = 'status', string $label = '任务状态')
- {
- return $this->grid->column($field, $label)->display(function ($status) {
- return TASK_STATUS::getDescription(TASK_STATUS::from($status));
- });
- }
- /**
- * 显示重置类型
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnResetType(string $field = 'reset_type', string $label = '重置类型')
- {
- return $this->grid->column($field, $label)->display(function ($resetType) {
- return RESET_TYPE::getDescription(RESET_TYPE::tryFrom($resetType) ?? RESET_TYPE::NONE);
- });
- }
- /**
- * 显示奖励类型
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnRewardType(string $field = 'reward_type', string $label = '奖励类型')
- {
- return $this->grid->column($field, $label)->display(function ($rewardType) {
- return REWARD_TYPE::getDescription(REWARD_TYPE::tryFrom($rewardType) ?? REWARD_TYPE::ITEM);
- });
- }
- /**
- * 显示条件类型
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnConditionType(string $field = 'condition_type', string $label = '条件类型')
- {
- return $this->grid->column($field, $label)->display(function ($type) {
- return $type == 'prerequisite' ? '前置条件' : '进度条件';
- });
- }
- /**
- * 显示进度百分比
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnProgress(string $field = 'progress', string $label = '进度')
- {
- return $this->grid->column($field, $label)->display(function ($progress) {
- return $progress . '%';
- })->progress();
- }
- /**
- * 显示计算的进度百分比
- *
- * @param string $currentField 当前值字段名
- * @param string $targetField 目标值字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnCalculatedProgress(string $currentField = 'current_value', string $targetField = 'target_value', string $label = '进度')
- {
- return $this->grid->column('progress', $label)->display(function () use ($currentField, $targetField) {
- $current = $this->{$currentField};
- $target = $this->{$targetField};
- $progress = $target > 0 ? min(100, round(($current / $target) * 100)) : 0;
- return $progress . '%';
- })->progress();
- }
- /**
- * 显示父级分类
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnParentCategory(string $field = 'parent_id', string $label = '父级分类')
- {
- return $this->grid->column($field, $label)->display(function ($parentId) {
- if ($parentId == 0) {
- return '无';
- }
- $parent = TaskCategory::find($parentId);
- return $parent ? $parent->name : '未知';
- });
- }
- /**
- * 显示任务选择器
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnTaskSelector(string $field = 'task_id', string $label = '任务')
- {
- return $this->grid->column($field, $label)->display(function ($taskId) {
- $task = Task::find($taskId);
- return $task ? $task->name : "未知任务(#{$taskId})";
- });
- }
- /**
- * 显示条件选择器
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnConditionSelector(string $field = 'condition_id', string $label = '条件')
- {
- return $this->grid->column($field, $label)->display(function ($conditionId) {
- $condition = TaskCondition::find($conditionId);
- return $condition ? $condition->name : "未知条件(#{$conditionId})";
- });
- }
- /**
- * 显示奖励内容
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnRewards(string $field = 'rewards', string $label = '奖励内容')
- {
- return $this->grid->column($field, $label)->display(function ($rewards) {
- $result = [];
- foreach ($rewards as $reward) {
- $type = REWARD_TYPE::getDescription(REWARD_TYPE::tryFrom($reward['reward_type']) ?? REWARD_TYPE::ITEM);
- $result[] = "{$type}: {$reward['quantity']}";
- }
- return implode('<br>', $result);
- });
- }
- /**
- * 显示消耗内容
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnCosts(string $field = 'costs', string $label = '消耗内容')
- {
- return $this->grid->column($field, $label)->display(function ($costs) {
- $result = [];
- foreach ($costs as $cost) {
- $result[] = "{$cost['cost_type']}: {$cost['quantity']}";
- }
- return implode('<br>', $result);
- });
- }
- }
|