UrsUserRelationCacheController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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', '层级深度')->display(function ($depth) {
  40. if ($depth === 1) {
  41. return '<span class="badge badge-success">第1代(直推)</span>';
  42. } else {
  43. return '<span class="badge badge-info">第' . $depth . '代</span>';
  44. }
  45. })->sortable();
  46. $grid->column('path', '关系路径(农场用户)')->limit(50);
  47. $grid->column('urs_path', 'URS关系路径')->limit(50);
  48. $grid->column('created_at', '创建时间')->sortable();
  49. $grid->column('updated_at', '更新时间')->sortable();
  50. // 筛选器
  51. $grid->filter(function (Grid\Filter $filter) {
  52. FilterHelper::addIdFilter($filter);
  53. $filter->equal('user_id', '农场用户ID');
  54. $filter->equal('related_user_id', '关联农场用户ID');
  55. $filter->equal('urs_user_id', 'URS用户ID');
  56. $filter->equal('urs_related_user_id', '关联URS用户ID');
  57. $filter->equal('level', '关系层级')->select([
  58. 1 => '直接',
  59. 2 => '间接'
  60. ]);
  61. $filter->equal('depth', '层级深度');
  62. FilterHelper::addDateRangeFilter($filter, 'created_at', '创建时间');
  63. });
  64. // 工具栏
  65. $grid->tools(function (Grid\Tools $tools) {
  66. // 添加重建缓存按钮
  67. $tools->append('<a href="javascript:void(0)" class="btn btn-primary btn-sm" onclick="rebuildCache()">
  68. <i class="fa fa-refresh"></i> 重建所有缓存
  69. </a>');
  70. // 添加检查完整性按钮
  71. $tools->append('<a href="javascript:void(0)" class="btn btn-info btn-sm" onclick="checkIntegrity()">
  72. <i class="fa fa-check"></i> 检查完整性
  73. </a>');
  74. });
  75. // 禁用新增和编辑
  76. $grid->disableCreateButton();
  77. $grid->disableEditButton();
  78. // 批量操作
  79. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  80. $batch->disableDelete();
  81. });
  82. GridHelper::defaultConfig($grid);
  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. ShowHelper::defaultConfig($show);
  107. });
  108. }
  109. /**
  110. * 表单页面(禁用)
  111. */
  112. protected function form(): Form
  113. {
  114. return Form::make(new UrsUserRelationCacheRepository(), function (Form $form) {
  115. $form->display('id', 'ID');
  116. // 关系缓存数据不允许手动编辑
  117. $form->html('<div class="alert alert-warning">关系缓存数据由系统自动生成,不允许手动编辑</div>');
  118. });
  119. }
  120. /**
  121. * 重建所有缓存
  122. */
  123. public function rebuildCache()
  124. {
  125. try {
  126. $logic = new UrsRelationCacheLogic();
  127. $result = $logic->rebuildAllRelationCache();
  128. return response()->json([
  129. 'status' => true,
  130. 'message' => '缓存重建完成',
  131. 'data' => $result
  132. ]);
  133. } catch (\Exception $e) {
  134. return response()->json([
  135. 'status' => false,
  136. 'message' => '缓存重建失败: ' . $e->getMessage()
  137. ]);
  138. }
  139. }
  140. /**
  141. * 检查缓存完整性
  142. */
  143. public function checkIntegrity()
  144. {
  145. try {
  146. $logic = new UrsRelationCacheLogic();
  147. $result = $logic->checkRelationCacheIntegrity();
  148. return response()->json([
  149. 'status' => true,
  150. 'message' => '完整性检查完成',
  151. 'data' => $result
  152. ]);
  153. } catch (\Exception $e) {
  154. return response()->json([
  155. 'status' => false,
  156. 'message' => '完整性检查失败: ' . $e->getMessage()
  157. ]);
  158. }
  159. }
  160. }