| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?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) {
- $percentage = $progress . '%';
- $color = $progress >= 100 ? 'success' : ($progress >= 50 ? 'warning' : 'info');
- return "<div class='progress' style='height: 20px;'>
- <div class='progress-bar bg-{$color}' role='progressbar' style='width: {$progress}%' aria-valuenow='{$progress}' aria-valuemin='0' aria-valuemax='100'>
- {$percentage}
- </div>
- </div>";
- });
- }
- /**
- * 显示计算的进度百分比
- *
- * @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;
- })->progressBar();
- }
- /**
- * 显示父级分类
- *
- * @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) {
- // 兼容不同的字段名格式:reward_type 或 rewardType
- $rewardType = $reward['reward_type'] ?? $reward['rewardType'] ?? null;
- $type = REWARD_TYPE::getDescription(REWARD_TYPE::tryFrom($rewardType) ?? 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);
- });
- }
- /**
- * 显示自动化配置
- *
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnAutoConfig(string $label = '自动化配置')
- {
- return $this->grid->column('auto_config', $label)->display(function () {
- $configs = [];
- if ($this->auto_accept) $configs[] = '<span class="badge badge-primary">自动接取</span>';
- if ($this->auto_complete) $configs[] = '<span class="badge badge-success">自动完成</span>';
- if ($this->auto_reward) $configs[] = '<span class="badge badge-warning">自动奖励</span>';
- if ($this->auto_reset) $configs[] = '<span class="badge badge-info">自动重置</span>';
- return empty($configs) ? '<span class="text-muted">无</span>' : implode(' ', $configs);
- });
- }
- /**
- * 显示自动接取状态
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnAutoAccept(string $field = 'auto_accept', string $label = '自动接取')
- {
- return $this->grid->column($field, $label)->switch();
- }
- /**
- * 显示自动完成状态
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnAutoComplete(string $field = 'auto_complete', string $label = '自动完成')
- {
- return $this->grid->column($field, $label)->switch();
- }
- /**
- * 显示自动发放奖励状态
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnAutoReward(string $field = 'auto_reward', string $label = '自动奖励')
- {
- return $this->grid->column($field, $label)->switch();
- }
- /**
- * 显示自动重置状态
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return \Dcat\Admin\Grid\Column
- */
- public function columnAutoReset(string $field = 'auto_reset', string $label = '自动重置')
- {
- return $this->grid->column($field, $label)->switch();
- }
- }
|