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('
', $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) {
$type = REWARD_TYPE::getDescription(REWARD_TYPE::tryFrom($reward['reward_type']) ?? REWARD_TYPE::ITEM);
$result[] = "{$type}: {$reward['quantity']}";
}
return implode('
', $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('
', $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 "