| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace App\Module\GameItems\AdminControllers;
- use App\Module\GameItems\Repositorys\ItemUserRecipeRepository;
- use App\Module\GameItems\Repositorys\ItemRecipeRepository;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Layout\Content;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- use UCore\DcatAdmin\FilterHelper;
- use UCore\DcatAdmin\GridHelper;
- use UCore\DcatAdmin\ShowHelper;
- /**
- * 用户配方解锁状态管理控制器
- *
- * @package App\Module\GameItems\AdminControllers
- */
- #[Resource('game-items-user-recipes', names: 'dcat.admin.game-items-user-recipes')]
- class UserRecipeController extends AdminController
- {
- /**
- * 标题
- *
- * @var string
- */
- protected $title = '用户配方解锁状态';
- /**
- * 禁用创建按钮
- *
- * @var bool
- */
- protected $showCreateButton = false;
- /**
- * 列表页
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new ItemUserRecipeRepository(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
-
- // 禁用创建、编辑和删除按钮
- $grid->disableCreateButton();
- $grid->disableActions();
- $grid->disableBatchDelete();
- $grid->disableDeleteButton();
- $grid->disableEditButton();
- // 只保留详情按钮
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->disableDelete();
- $actions->disableEdit();
- $actions->disableQuickEdit();
- });
- $helper->columnId();
- $grid->column('user_id', '用户ID');
- $grid->column('recipe.name', '配方名称');
- $grid->column('is_unlocked', '是否解锁')->switch();
- $grid->column('unlock_time', '解锁时间');
- $grid->column('craft_count', '合成次数');
- $grid->column('last_craft_time', '上次合成时间');
- $grid->column('created_at', '创建时间');
- $grid->column('updated_at', '更新时间');
- // 筛选
- $grid->filter(function ($filter) {
- $helper = new FilterHelper($filter, $this);
- $helper->equal('id', 'ID');
- $helper->equal('user_id', '用户ID');
- $filter->equal('recipe_id', '配方')->select(
- (new ItemRecipeRepository())->pluck('name', 'id')
- );
- $filter->equal('is_unlocked', '是否解锁')->radio([
- 1 => '是',
- 0 => '否',
- ]);
- $helper->between('unlock_time', '解锁时间')->datetime();
- $helper->between('craft_count', '合成次数');
- $helper->between('last_craft_time', '上次合成时间')->datetime();
- });
- });
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @param Content $content
- * @return Content
- */
- public function show($id, Content $content)
- {
- return $content
- ->header($this->title)
- ->description('详情')
- ->body($this->detail($id));
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new ItemUserRecipeRepository(), function (Show $show) {
- $helper = new ShowHelper($show, $this);
-
- // 禁用编辑和删除按钮
- $show->panel()->tools(function ($tools) {
- $tools->disableEdit();
- $tools->disableDelete();
- });
- $helper->field('id', 'ID');
- $helper->field('user_id', '用户ID');
- // 显示配方信息
- $show->field('recipe.name', '配方名称');
- $show->field('recipe.resultItem.name', '产出物品');
- $helper->field('recipe.result_quantity', '产出数量');
- $show->field('recipe.success_rate', '成功率')->as(function ($value) {
- return ($value * 100) . '%';
- });
- $show->field('recipe.cooldown_seconds', '冷却时间(秒)');
- $show->field('is_unlocked', '是否解锁')->as(function ($value) {
- return $value ? '是' : '否';
- });
- $helper->field('unlock_time', '解锁时间');
- $helper->field('craft_count', '合成次数');
- $helper->field('last_craft_time', '上次合成时间');
- // 计算冷却状态
- $show->field('cooldown_status', '冷却状态')->as(function () {
- if (!$this->recipe || $this->recipe->cooldown_seconds <= 0) {
- return '无冷却';
- }
- if (!$this->last_craft_time) {
- return '未合成过';
- }
- $cooldownEnd = $this->last_craft_time->addSeconds($this->recipe->cooldown_seconds);
- if ($cooldownEnd->isPast()) {
- return '已冷却完成';
- } else {
- $remainingSeconds = now()->diffInSeconds($cooldownEnd, false);
- return '冷却中,剩余 ' . $remainingSeconds . ' 秒';
- }
- });
- $helper->field('created_at', '创建时间');
- $helper->field('updated_at', '更新时间');
- });
- }
- }
|