UrsUserTalentController.php 9.4 KB

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