UrsUserRelationCacheController.php 6.0 KB

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