UrsUserTalentController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace App\Module\UrsPromotion\AdminControllers;
  3. use App\Module\UrsPromotion\Models\UrsUserMapping;
  4. use UCore\DcatAdmin\AdminController;
  5. use Spatie\RouteAttributes\Attributes\Resource;
  6. use Spatie\RouteAttributes\Attributes\Post;
  7. use App\Module\UrsPromotion\Models\UrsUserTalent;
  8. use App\Module\UrsPromotion\Repositorys\UrsUserTalentRepository;
  9. use App\Module\UrsPromotion\AdminControllers\Helper\UrsUserTalentGridHelper;
  10. use App\Module\UrsPromotion\AdminControllers\Helper\UrsUserTalentShowHelper;
  11. use App\Module\UrsPromotion\AdminControllers\Helper\UrsUserTalentFormHelper;
  12. use App\Module\UrsPromotion\AdminControllers\Helper\UrsUserTalentFilterHelper;
  13. use App\Module\UrsPromotion\Services\UrsTalentService;
  14. use App\Module\UrsPromotion\Enums\UrsTalentLevel;
  15. use Dcat\Admin\Grid;
  16. use Dcat\Admin\Show;
  17. use Dcat\Admin\Form;
  18. use UCore\DcatAdmin\GridHelper;
  19. /**
  20. * URS用户达人等级管理控制器
  21. *
  22. * @route /admin/urs-promotion/user-talents
  23. */
  24. #[Resource('urs-promotion/user-talents', names: 'dcat.admin.urs-promotion.user-talents')]
  25. class UrsUserTalentController extends AdminController
  26. {
  27. /**
  28. * 页面标题
  29. */
  30. protected $title = 'URS用户达人等级';
  31. /**
  32. * 模型类
  33. */
  34. protected $model = UrsUserTalent::class;
  35. /**
  36. * 仓库类
  37. */
  38. protected $repository = UrsUserTalentRepository::class;
  39. /**
  40. * 列表页面
  41. */
  42. protected function grid(): Grid
  43. {
  44. return Grid::make(new UrsUserTalentRepository(['userMapping']), function (Grid $grid) {
  45. $helper = new GridHelper($grid, $this);
  46. $grid->column('id', 'ID')->sortable();
  47. $grid->column('user_id', '用户ID')->sortable()->display(function ($value) {
  48. // 添加到用户绑定和推荐关系的链接
  49. $mappingUrl = admin_url('urs-promotion/user-mappings?user_id=' . $value);
  50. $referralUrl = admin_url('urs-promotion/user-referrals?user_id=' . $value);
  51. return $value . '<br><small>
  52. <a href="' . $mappingUrl . '" class="text-primary">查看绑定关系</a> |
  53. <a href="' . $referralUrl . '" class="text-info">查看推荐关系</a>
  54. </small>';
  55. });
  56. $grid->column('userMapping.user_id', '用户ID')->sortable()->display(function ($value) {
  57. if (!$value) return '<span class="text-muted">未绑定</span>';
  58. // 添加到收益记录的链接
  59. $profitUrl = admin_url('urs-promotion/profits?farm_user_id=' . $value);
  60. return $value . '<br><small>
  61. <a href="' . $profitUrl . '" class="text-success">查看收益记录</a>
  62. </small>';
  63. });
  64. $grid->column('talent_level', '达人等级')->display(function ($value) {
  65. return UrsTalentLevel::getLevelName($value);
  66. })->label([
  67. 0 => 'default',
  68. 1 => 'primary',
  69. 2 => 'info',
  70. 3 => 'success',
  71. 4 => 'warning',
  72. 5 => 'danger',
  73. ])->sortable();
  74. $grid->column('direct_count', '直推人数')->sortable();
  75. $grid->column('indirect_count', '间推人数')->sortable();
  76. $grid->column('third_count', '三推人数')->sortable();
  77. $grid->column('promotion_count', '团队总人数')->sortable();
  78. $grid->column('last_level_update_time', '最后升级时间')->sortable();
  79. // $grid->column('created_at', '创建时间')->sortable();
  80. $helper->columnCreatedAt();
  81. // 添加批量更新达人等级功能
  82. $grid->tools(function (Grid\Tools $tools) {
  83. $tools->append(new \App\Module\UrsPromotion\AdminControllers\Actions\BatchUpdateTalentAction());
  84. });
  85. // 添加单个更新功能
  86. $grid->actions(function (Grid\Displayers\Actions $actions) {
  87. $actions->append(new \App\Module\UrsPromotion\AdminControllers\Actions\UpdateTalentAction());
  88. });
  89. $grid->filter(function (Grid\Filter $filter) {
  90. $filter->equal('user_id', '用户ID');
  91. $filter->where('urs_user_id', function (\Illuminate\Database\Eloquent\Builder $query) {
  92. // $this->input;
  93. $user_id = UrsUserMapping::query()->where('urs_user_id',$this->input)->value('user_id');
  94. // dd($this->input,$user_id);
  95. if($user_id){
  96. $query->where('user_id',$user_id);
  97. }
  98. },'URS用户ID');
  99. $filter->equal('talent_level', '达人等级')->select(UrsTalentLevel::getAllLevels());
  100. $filter->between('direct_count', '直推人数');
  101. $filter->between('indirect_count', '间推人数');
  102. $filter->between('third_count', '三推人数');
  103. $filter->between('promotion_count', '团队总人数');
  104. $filter->between('last_level_update_time', '最后升级时间')->datetime();
  105. $filter->between('created_at', '创建时间')->datetime();
  106. });
  107. });
  108. }
  109. /**
  110. * 详情页面
  111. */
  112. protected function detail($id): Show
  113. {
  114. return Show::make($id, new UrsUserTalentRepository(), function (Show $show) {
  115. $show->field('id', 'ID');
  116. $show->field('user_id', '用户ID');
  117. $show->field('talent_level', '达人等级')->using(UrsTalentLevel::getAllLevels());
  118. $show->field('direct_count', '直推人数');
  119. $show->field('indirect_count', '间推人数');
  120. $show->field('third_count', '三推人数');
  121. $show->field('promotion_count', '团队总人数');
  122. $show->field('last_level_update_time', '最后升级时间');
  123. $show->field('created_at', '创建时间');
  124. $show->field('updated_at', '更新时间');
  125. // 显示推荐关系树
  126. $show->divider();
  127. $show->field('referral_tree', '推荐关系树')->unescape()->as(function () {
  128. $logic = new \App\Module\UrsPromotion\Logics\UrsTalentLogic();
  129. $tree = $logic->getUserReferralTree($this->user_id);
  130. $html = '<div class="referral-tree">';
  131. if (!empty($tree['direct'])) {
  132. $html .= '<h5>直推用户 (' . count($tree['direct']) . '人)</h5>';
  133. $html .= '<ul>';
  134. foreach ($tree['direct'] as $user) {
  135. $html .= '<li>用户' . $user['user_id'] . ' (推荐时间: ' . $user['referral_time'] . ')</li>';
  136. }
  137. $html .= '</ul>';
  138. }
  139. if (!empty($tree['indirect'])) {
  140. $html .= '<h5>间推用户 (' . count($tree['indirect']) . '人)</h5>';
  141. $html .= '<ul>';
  142. foreach ($tree['indirect'] as $user) {
  143. $html .= '<li>用户' . $user['user_id'] . ' (通过用户' . $user['referrer_id'] . ')</li>';
  144. }
  145. $html .= '</ul>';
  146. }
  147. if (!empty($tree['third'])) {
  148. $html .= '<h5>三推用户 (' . count($tree['third']) . '人)</h5>';
  149. $html .= '<ul>';
  150. foreach ($tree['third'] as $user) {
  151. $html .= '<li>用户' . $user['user_id'] . ' (通过用户' . $user['referrer_id'] . ')</li>';
  152. }
  153. $html .= '</ul>';
  154. }
  155. if (empty($tree['direct']) && empty($tree['indirect']) && empty($tree['third'])) {
  156. $html .= '<p>暂无团队成员</p>';
  157. }
  158. $html .= '</div>';
  159. return $html;
  160. });
  161. // 添加相关链接区域
  162. $show->divider('相关信息');
  163. });
  164. }
  165. /**
  166. * 表单页面
  167. */
  168. protected function form(): Form
  169. {
  170. return Form::make(new UrsUserTalentRepository(), function (Form $form) {
  171. $form->display('id', 'ID');
  172. $form->number('user_id', '用户ID')->required();
  173. $form->select('talent_level', '达人等级')->options(UrsTalentLevel::getAllLevels())->required();
  174. $form->number('direct_count', '直推人数')->default(0);
  175. $form->number('indirect_count', '间推人数')->default(0);
  176. $form->number('third_count', '三推人数')->default(0);
  177. $form->number('promotion_count', '团队总人数')->default(0);
  178. $form->datetime('last_level_update_time', '最后升级时间');
  179. $form->display('created_at', '创建时间');
  180. $form->display('updated_at', '更新时间');
  181. });
  182. }
  183. /**
  184. * 更新单个用户达人等级
  185. */
  186. public function updateTalent($id)
  187. {
  188. $talent = UrsUserTalent::find($id);
  189. if (!$talent) {
  190. return response()->json(['status' => false, 'message' => '用户不存在']);
  191. }
  192. try {
  193. $result = UrsTalentService::updateTalentLevel($talent->user_id);
  194. return response()->json(['status' => true, 'message' => '达人等级更新成功']);
  195. } catch (\Exception $e) {
  196. return response()->json(['status' => false, 'message' => '达人等级更新失败:' . $e->getMessage()]);
  197. }
  198. }
  199. }