| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- <?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 Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- /**
- * 详情页辅助特性
- *
- * 提供任务模块后台控制器的详情页构建功能
- */
- trait ShowHelperTrait
- {
- /**
- * 显示ID字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldId(string $field = 'id', string $label = 'ID')
- {
- return $this->show->field($field, $label);
- }
- /**
- * 显示名称字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldName(string $field = 'name', string $label = '名称')
- {
- return $this->show->field($field, $label);
- }
- /**
- * 显示描述字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldDescription(string $field = 'description', string $label = '描述')
- {
- return $this->show->field($field, $label);
- }
- /**
- * 显示父级分类字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldParentCategory(string $field = 'parent_id', string $label = '父级分类')
- {
- return $this->show->field($field, $label)->as(function ($parentId) {
- if ($parentId == 0) {
- return '无';
- }
- $parent = TaskCategory::find($parentId);
- return $parent ? $parent->name : '未知';
- });
- }
- /**
- * 显示排序权重字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldSortOrder(string $field = 'sort_order', string $label = '排序权重')
- {
- return $this->show->field($field, $label);
- }
- /**
- * 显示是否激活字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldIsActive(string $field = 'is_active', string $label = '是否激活')
- {
- return $this->show->field($field, $label)->as(function ($isActive) {
- return $isActive ? '是' : '否';
- });
- }
- /**
- * 显示创建时间字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldCreatedAt(string $field = 'created_at', string $label = '创建时间')
- {
- return $this->show->field($field, $label);
- }
- /**
- * 显示更新时间字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldUpdatedAt(string $field = 'updated_at', string $label = '更新时间')
- {
- return $this->show->field($field, $label);
- }
- /**
- * 显示任务类型字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldTaskType(string $field = 'type', string $label = '任务类型')
- {
- return $this->show->field($field, $label)->as(function ($type) {
- return TASK_TYPE::getDescription(TASK_TYPE::tryFrom($type) ?? TASK_TYPE::DAILY);
- });
- }
- /**
- * 显示重置类型字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldResetType(string $field = 'reset_type', string $label = '重置类型')
- {
- return $this->show->field($field, $label)->as(function ($resetType) {
- return RESET_TYPE::getDescription(RESET_TYPE::tryFrom($resetType) ?? RESET_TYPE::NONE);
- });
- }
- /**
- * 显示任务状态字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldTaskStatus(string $field = 'status', string $label = '任务状态')
- {
- return $this->show->field($field, $label)->as(function ($status) {
- return TASK_STATUS::getDescription(TASK_STATUS::from($status));
- });
- }
- /**
- * 显示奖励类型字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldRewardType(string $field = 'reward_type', string $label = '奖励类型')
- {
- return $this->show->field($field, $label)->as(function ($rewardType) {
- return REWARD_TYPE::getDescription(REWARD_TYPE::tryFrom($rewardType) ?? REWARD_TYPE::ITEM);
- });
- }
- /**
- * 显示条件类型字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldConditionType(string $field = 'condition_type', string $label = '条件类型')
- {
- return $this->show->field($field, $label)->as(function ($type) {
- return $type == 'prerequisite' ? '前置条件' : '进度条件';
- });
- }
- /**
- * 显示是否必要条件字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldIsRequired(string $field = 'is_required', string $label = '是否必要条件')
- {
- return $this->show->field($field, $label)->as(function ($isRequired) {
- return $isRequired ? '是' : '否';
- });
- }
- /**
- * 显示JSON字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldJson(string $field, string $label)
- {
- return $this->show->field($field, $label)->json();
- }
- /**
- * 显示前置任务字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldPrerequisiteTasks(string $field = 'prerequisite_tasks', string $label = '前置任务')
- {
- return $this->show->field($field, $label)->as(function ($prerequisiteTasks) {
- if (empty($prerequisiteTasks)) {
- return '无';
- }
- $tasks = Task::whereIn('id', $prerequisiteTasks)->pluck('name', 'id')->toArray();
- $result = [];
- foreach ($prerequisiteTasks as $taskId) {
- $result[] = $tasks[$taskId] ?? "任务#{$taskId}";
- }
- return implode('<br>', $result);
- });
- }
- /**
- * 显示时间限制字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldTimeLimit(string $field = 'time_limit', string $label = '时间限制(秒)')
- {
- return $this->show->field($field, $label)->as(function ($timeLimit) {
- return $timeLimit ? $timeLimit . '秒' : '无限制';
- });
- }
- /**
- * 显示最大完成次数字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldMaxCompletions(string $field = 'max_completions', string $label = '最大完成次数')
- {
- return $this->show->field($field, $label)->as(function ($maxCompletions) {
- return $maxCompletions ? $maxCompletions : '无限制';
- });
- }
- /**
- * 显示进度字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldProgress(string $field = 'progress', string $label = '进度')
- {
- return $this->show->field($field, $label)->as(function ($progress) {
- return $progress . '%';
- });
- }
- /**
- * 显示计算的进度字段
- *
- * @param string $currentField 当前值字段名
- * @param string $targetField 目标值字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldCalculatedProgress(string $currentField = 'current_value', string $targetField = 'target_value', string $label = '进度')
- {
- return $this->show->field('progress', $label)->as(function () use ($currentField, $targetField) {
- $current = $this->{$currentField};
- $target = $this->{$targetField};
- $progress = $target > 0 ? min(100, round(($current / $target) * 100)) : 0;
- return $progress . '%';
- });
- }
- /**
- * 显示奖励内容字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldRewards(string $field = 'rewards', string $label = '奖励内容')
- {
- return $this->show->field($field, $label)->as(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 Show\Field
- */
- public function fieldCosts(string $field = 'costs', string $label = '消耗内容')
- {
- return $this->show->field($field, $label)->as(function ($costs) {
- $result = [];
- foreach ($costs as $cost) {
- $result[] = "{$cost['cost_type']}: {$cost['quantity']}";
- }
- return implode('<br>', $result);
- });
- }
- /**
- * 显示任务奖励关联
- *
- * @param string $relation 关联名称
- * @param string $label 标签
- * @param callable $callback 回调函数
- * @return void
- */
- public function showRewards(string $relation = 'rewards', string $label = '任务奖励', callable $callback = null)
- {
- $this->show->relation($relation, $label, function ($model) use ($callback) {
- $grid = new Grid(new \App\Module\Task\Models\TaskReward);
- $grid->model()->where('task_id', $model->id);
- $grid->column('id', 'ID');
- $grid->column('reward_type', '奖励类型')->display(function ($rewardType) {
- return REWARD_TYPE::getDescription(REWARD_TYPE::tryFrom($rewardType) ?? REWARD_TYPE::ITEM);
- });
- $grid->column('reward_param1', '奖励参数1');
- $grid->column('reward_param2', '奖励参数2');
- $grid->column('quantity', '奖励数量');
- $grid->column('extra_data', '额外数据')->json();
- $grid->column('sort_order', '排序权重');
- $grid->disableCreateButton();
- $grid->disableActions();
- $grid->disableBatchDelete();
- $grid->disableRowSelector();
- if ($callback) {
- $callback($grid);
- }
- return $grid;
- });
- }
- /**
- * 显示任务接取消耗关联
- *
- * @param string $relation 关联名称
- * @param string $label 标签
- * @param callable $callback 回调函数
- * @return void
- */
- public function showCosts(string $relation = 'costs', string $label = '任务接取消耗', callable $callback = null)
- {
- $this->show->relation($relation, $label, function ($model) use ($callback) {
- $grid = new Grid(new \App\Module\Task\Models\TaskCost);
- $grid->model()->where('task_id', $model->id);
- $grid->column('id', 'ID');
- $grid->column('cost_type', '消耗类型');
- $grid->column('cost_param1', '消耗参数1');
- $grid->column('cost_param2', '消耗参数2');
- $grid->column('quantity', '消耗数量');
- $grid->column('extra_data', '额外数据')->json();
- $grid->disableCreateButton();
- $grid->disableActions();
- $grid->disableBatchDelete();
- $grid->disableRowSelector();
- if ($callback) {
- $callback($grid);
- }
- return $grid;
- });
- }
- /**
- * 显示任务达成条件关联
- *
- * @param string $relation 关联名称
- * @param string $label 标签
- * @param callable $callback 回调函数
- * @return void
- */
- public function showAchievementConditions(string $relation = 'achievementConditions', string $label = '任务达成条件', callable $callback = null)
- {
- $this->show->relation($relation, $label, function ($model) use ($callback) {
- $grid = new Grid(new \App\Module\Task\Models\TaskAchievementCondition);
- $grid->model()->where('task_id', $model->id);
- $grid->column('id', 'ID');
- $grid->column('condition.name', '条件名称');
- $grid->column('condition_type', '条件类型')->display(function ($type) {
- return $type == 'prerequisite' ? '前置条件' : '进度条件';
- });
- $grid->column('condition_params', '条件参数')->json();
- $grid->column('target_value', '目标值');
- $grid->column('is_required', '是否必要条件')->display(function ($isRequired) {
- return $isRequired ? '是' : '否';
- });
- $grid->column('sort_order', '排序权重');
- $grid->disableCreateButton();
- $grid->disableActions();
- $grid->disableBatchDelete();
- $grid->disableRowSelector();
- if ($callback) {
- $callback($grid);
- }
- return $grid;
- });
- }
- /**
- * 显示用户任务进度关联
- *
- * @param string $relation 关联名称
- * @param string $label 标签
- * @param callable $callback 回调函数
- * @return void
- */
- public function showUserProgress(string $relation = 'userProgress', string $label = '任务进度', callable $callback = null)
- {
- $this->show->relation($relation, $label, function ($model) use ($callback) {
- $grid = new Grid(new \App\Module\Task\Models\TaskUserProgress);
- $grid->model()->where('user_id', $model->user_id)->where('task_id', $model->task_id);
- $grid->column('id', 'ID');
- $grid->column('achievementCondition.condition.name', '条件名称');
- $grid->column('current_value', '当前值');
- $grid->column('target_value', '目标值');
- $grid->column('progress', '进度')->display(function () {
- $progress = $this->target_value > 0 ? min(100, round(($this->current_value / $this->target_value) * 100)) : 0;
- $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>";
- });
- $grid->disableCreateButton();
- $grid->disableActions();
- $grid->disableBatchDelete();
- $grid->disableRowSelector();
- if ($callback) {
- $callback($grid);
- }
- return $grid;
- });
- }
- /**
- * 显示自动接取字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldAutoAccept(string $field = 'auto_accept', string $label = '自动接取')
- {
- return $this->show->field($field, $label)->as(function ($value) {
- return $value ? '<span class="badge badge-primary">是</span>' : '<span class="badge badge-secondary">否</span>';
- });
- }
- /**
- * 显示自动完成字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldAutoComplete(string $field = 'auto_complete', string $label = '自动完成')
- {
- return $this->show->field($field, $label)->as(function ($value) {
- return $value ? '<span class="badge badge-success">是</span>' : '<span class="badge badge-secondary">否</span>';
- });
- }
- /**
- * 显示自动发放奖励字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldAutoReward(string $field = 'auto_reward', string $label = '自动奖励')
- {
- return $this->show->field($field, $label)->as(function ($value) {
- return $value ? '<span class="badge badge-warning">是</span>' : '<span class="badge badge-secondary">否</span>';
- });
- }
- /**
- * 显示自动重置字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldAutoReset(string $field = 'auto_reset', string $label = '自动重置')
- {
- return $this->show->field($field, $label)->as(function ($value) {
- return $value ? '<span class="badge badge-info">是</span>' : '<span class="badge badge-secondary">否</span>';
- });
- }
- /**
- * 显示自动化配置汇总
- *
- * @param string $label 标签
- * @return Show\Field
- */
- public function fieldAutoConfig(string $label = '自动化配置')
- {
- return $this->show->field('auto_config', $label)->as(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);
- });
- }
- }
|