UrsUserRelationCacheController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace App\Module\UrsPromotion\AdminControllers;
  3. use App\Module\UrsPromotion\Logics\UrsRelationCacheLogic;
  4. use App\Module\UrsPromotion\Repositorys\UrsUserRelationCacheRepository;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use UCore\DcatAdmin\AdminController;
  9. use Spatie\RouteAttributes\Attributes\Resource;
  10. /**
  11. * URS用户关系缓存后台控制器
  12. *
  13. * 路由: /admin/urs-promotion/user-relation-cache
  14. * 菜单位置: URS推广 -> 关系缓存管理
  15. */
  16. #[Resource('urs-promotion/user-relation-cache', names: 'dcat.admin.urs-promotion.user-relation-cache')]
  17. class UrsUserRelationCacheController extends AdminController
  18. {
  19. protected $title = 'URS用户关系缓存';
  20. /**
  21. * 列表页面
  22. */
  23. protected function grid(): Grid
  24. {
  25. $grid = Grid::make(new UrsUserRelationCacheRepository(), function (Grid $grid) {
  26. $grid->column('id', 'ID')->sortable();
  27. $grid->column('user_id', '农场用户ID')->sortable();
  28. $grid->column('urs_user_id', 'URS用户ID')->sortable();
  29. $grid->column('related_user_id', '上级农场用户ID')->sortable();
  30. $grid->column('urs_related_user_id', '上级URS用户ID')->sortable();
  31. $grid->column('level', '关系层级')->using([
  32. 1 => '直接',
  33. 2 => '间接'
  34. ])->label([
  35. 1 => 'success',
  36. 2 => 'info'
  37. ])->sortable();
  38. $grid->column('depth', '层级深度')->display(function ($depth) {
  39. if ($depth === 1) {
  40. return '<span class="badge badge-success">第1代(直推)</span>';
  41. } else {
  42. return '<span class="badge badge-info">第' . $depth . '代</span>';
  43. }
  44. })->sortable();
  45. $grid->column('path', '关系路径(农场用户)')->limit(50);
  46. $grid->column('urs_path', 'URS关系路径')->limit(50);
  47. $grid->column('created_at', '创建时间')->sortable();
  48. $grid->column('updated_at', '更新时间')->sortable();
  49. // 筛选器
  50. $grid->filter(function (Grid\Filter $filter) {
  51. $filter->equal('id', 'ID');
  52. $filter->equal('user_id', '农场用户ID');
  53. $filter->equal('urs_user_id', 'URS用户ID');
  54. $filter->equal('related_user_id', '上级农场用户ID');
  55. $filter->equal('urs_related_user_id', '上级URS用户ID');
  56. $filter->equal('level', '关系层级')->select([
  57. 1 => '直接',
  58. 2 => '间接'
  59. ]);
  60. $filter->equal('depth', '层级深度');
  61. $filter->between('created_at', '创建时间')->datetime();
  62. });
  63. // 工具栏
  64. $grid->tools(function (Grid\Tools $tools) {
  65. // 添加重建缓存按钮
  66. $tools->append('<a href="javascript:void(0)" class="btn btn-primary btn-sm" onclick="rebuildCache()">
  67. <i class="fa fa-refresh"></i> 重建所有缓存
  68. </a>');
  69. // 添加检查完整性按钮
  70. $tools->append('<a href="javascript:void(0)" class="btn btn-info btn-sm" onclick="checkIntegrity()">
  71. <i class="fa fa-check"></i> 检查完整性
  72. </a>');
  73. });
  74. // 禁用新增和编辑
  75. $grid->disableCreateButton();
  76. $grid->disableEditButton();
  77. // 批量操作
  78. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  79. $batch->disableDelete();
  80. });
  81. // 设置默认排序
  82. $grid->model()->orderBy('id', 'desc');
  83. });
  84. return $grid;
  85. }
  86. /**
  87. * 详情页面
  88. */
  89. protected function detail($id): Show
  90. {
  91. return Show::make($id, new UrsUserRelationCacheRepository(), function (Show $show) {
  92. $show->field('id', 'ID');
  93. $show->field('user_id', '农场用户ID');
  94. $show->field('related_user_id', '关联农场用户ID(上级)');
  95. $show->field('urs_user_id', 'URS用户ID');
  96. $show->field('urs_related_user_id', '关联URS用户ID(上级)');
  97. $show->field('level', '关系层级')->using([
  98. 1 => '直接',
  99. 2 => '间接'
  100. ]);
  101. $show->field('depth', '层级深度');
  102. $show->field('path', '关系路径(农场用户)');
  103. $show->field('urs_path', 'URS关系路径');
  104. $show->field('created_at', '创建时间');
  105. $show->field('updated_at', '更新时间');
  106. // 禁用编辑和删除按钮
  107. $show->disableEditButton();
  108. $show->disableDeleteButton();
  109. });
  110. }
  111. /**
  112. * 表单页面(禁用)
  113. */
  114. protected function form(): Form
  115. {
  116. return Form::make(new UrsUserRelationCacheRepository(), function (Form $form) {
  117. $form->display('id', 'ID');
  118. // 关系缓存数据不允许手动编辑
  119. $form->html('<div class="alert alert-warning">关系缓存数据由系统自动生成,不允许手动编辑</div>');
  120. });
  121. }
  122. /**
  123. * 重建所有缓存
  124. */
  125. public function rebuildCache()
  126. {
  127. try {
  128. $logic = new UrsRelationCacheLogic();
  129. $result = $logic->rebuildAllRelationCache();
  130. return response()->json([
  131. 'status' => true,
  132. 'message' => '缓存重建完成',
  133. 'data' => $result
  134. ]);
  135. } catch (\Exception $e) {
  136. return response()->json([
  137. 'status' => false,
  138. 'message' => '缓存重建失败: ' . $e->getMessage()
  139. ]);
  140. }
  141. }
  142. /**
  143. * 检查缓存完整性
  144. */
  145. public function checkIntegrity()
  146. {
  147. try {
  148. $logic = new UrsRelationCacheLogic();
  149. $result = $logic->checkRelationCacheIntegrity();
  150. return response()->json([
  151. 'status' => true,
  152. 'message' => '完整性检查完成',
  153. 'data' => $result
  154. ]);
  155. } catch (\Exception $e) {
  156. return response()->json([
  157. 'status' => false,
  158. 'message' => '完整性检查失败: ' . $e->getMessage()
  159. ]);
  160. }
  161. }
  162. }