PointCurrencyController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Module\Point\AdminControllers;
  3. use App\Module\Point\AdminControllers\Helper\FilterHelper;
  4. use App\Module\Point\AdminControllers\Helper\FormHelper;
  5. use App\Module\Point\AdminControllers\Helper\GridHelper;
  6. use App\Module\Point\AdminControllers\Helper\ShowHelper;
  7. use App\Module\Point\Enums\POINT_CURRENCY_TYPE;
  8. use App\Module\Point\Models\PointCurrencyModel;
  9. use App\Module\Point\Repositorys\PointCurrencyRepository;
  10. use Dcat\Admin\Form;
  11. use Dcat\Admin\Grid;
  12. use Dcat\Admin\Show;
  13. use Spatie\RouteAttributes\Attributes\Resource;
  14. use UCore\DcatAdmin\AdminController;
  15. /**
  16. * 积分币种控制器
  17. *
  18. * 路由: /admin/point/point-currency
  19. * 菜单: 积分管理 -> 积分币种
  20. * 功能: 管理积分币种类型,当前只支持经验积分
  21. */
  22. #[Resource('point-point-currency', names: 'dcat.admin.point-currency')]
  23. class PointCurrencyController extends AdminController
  24. {
  25. /**
  26. * 数据仓库
  27. */
  28. protected string $repository = PointCurrencyRepository::class;
  29. /**
  30. * 页面标题
  31. */
  32. protected $title = '积分币种';
  33. /**
  34. * 列表页面
  35. */
  36. protected function grid(): Grid
  37. {
  38. return Grid::make(new PointCurrencyRepository(), function (Grid $grid) {
  39. $gridHelper = new GridHelper($grid,$this);
  40. $grid->column('id', 'ID')->sortable();
  41. $grid->column('identification', '标识');
  42. $grid->column('type', '类型')->display(function ($type) {
  43. if ($type instanceof POINT_CURRENCY_TYPE) {
  44. return $type->getCurrencyName();
  45. }
  46. return POINT_CURRENCY_TYPE::tryFrom($type)?->getCurrencyName() ?? '未知类型';
  47. })->label([
  48. POINT_CURRENCY_TYPE::EXP->value => 'primary',
  49. ]);
  50. $grid->column('icon', '图标')->display(function ($icon) {
  51. return "<span style='font-size: 20px;'>{$icon}</span>";
  52. });
  53. $grid->column('name', '名称');
  54. $gridHelper->columnTimestamp('create_time', '创建时间');
  55. $gridHelper->columnTimestamp('update_time', '更新时间');
  56. $grid->filter(function (Grid\Filter $filter) {
  57. $filterHelper = new FilterHelper($filter,$this);
  58. $filter->like('identification', '标识');
  59. $filter->equal('type', '类型')->select(POINT_CURRENCY_TYPE::getAllTypes());
  60. $filter->like('name', '名称');
  61. $filterHelper->betweenTimestamp('create_time', '创建时间');
  62. });
  63. $grid->tools(function (Grid\Tools $tools) {
  64. $tools->append('<a href="/admin/point/point-config" class="btn btn-sm btn-primary">积分配置</a>');
  65. });
  66. });
  67. }
  68. /**
  69. * 详情页面
  70. */
  71. protected function detail($id): Show
  72. {
  73. return Show::make($id, new PointCurrencyRepository(), function (Show $show) {
  74. $showHelper = new ShowHelper($show,$this);
  75. $show->field('id', 'ID');
  76. $show->field('identification', '标识');
  77. $show->field('type', '类型')->as(function ($type) {
  78. if ($type instanceof POINT_CURRENCY_TYPE) {
  79. return $type->getCurrencyName();
  80. }
  81. return POINT_CURRENCY_TYPE::tryFrom($type)?->getCurrencyName() ?? '未知类型';
  82. });
  83. $show->field('icon', '图标')->as(function ($icon) {
  84. return "<span style='font-size: 30px;'>{$icon}</span>";
  85. });
  86. $show->field('name', '名称');
  87. $showHelper->fieldJson('display_attributes', '显示属性');
  88. $showHelper->fieldTimestamp('create_time', '创建时间');
  89. $showHelper->fieldTimestamp('update_time', '更新时间');
  90. });
  91. }
  92. /**
  93. * 表单页面
  94. */
  95. protected function form(): Form
  96. {
  97. return Form::make(new PointCurrencyRepository(), function (Form $form) {
  98. $formHelper = new FormHelper($form,$this);
  99. $form->display('id', 'ID');
  100. $form->text('identification', '标识')->required()->help('英文标识,如:EXP、ACHIEVEMENT等');
  101. $form->select('type', '类型')->options(POINT_CURRENCY_TYPE::getAllTypes())->required();
  102. $form->text('icon', '图标')->required()->help('Emoji图标,如:⭐、🏆等');
  103. $form->text('name', '名称')->required();
  104. $formHelper->textareaJson('display_attributes', '显示属性');
  105. $formHelper->hiddenTimestamps();
  106. $form->saving(function (Form $form) {
  107. // 检查类型和标识是否已存在
  108. $excludeId = $form->isEditing() ? $form->model()->id : null;
  109. if ($excludeId) {
  110. // 编辑时检查是否有其他记录使用相同类型
  111. $typeExists = PointCurrencyModel::where('type', $form->type)
  112. ->where('id', '!=', $excludeId)
  113. ->exists();
  114. $identificationExists = PointCurrencyModel::where('identification', $form->identification)
  115. ->where('id', '!=', $excludeId)
  116. ->exists();
  117. } else {
  118. // 新建时检查类型和标识是否已存在
  119. $typeExists = PointCurrencyModel::typeExists($form->type);
  120. $identificationExists = PointCurrencyModel::identificationExists($form->identification);
  121. }
  122. if ($typeExists) {
  123. return $form->response()->error('该积分类型已存在');
  124. }
  125. if ($identificationExists) {
  126. return $form->response()->error('该标识已存在');
  127. }
  128. if ($form->isCreating()) {
  129. $form->create_time = time();
  130. }
  131. $form->update_time = time();
  132. });
  133. });
  134. }
  135. }