UrsUserTalentController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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('urs_user_id', 'URS用户ID')->sortable()->display(function ($value) {
  45. // 添加到用户绑定和推荐关系的链接
  46. $mappingUrl = admin_url('urs-promotion/user-mappings?urs_user_id=' . $value);
  47. $referralUrl = admin_url('urs-promotion/user-referrals?urs_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. $tree = UrsTalentService::getUserReferralTree($this->user_id);
  110. $html = '<div class="referral-tree">';
  111. if (!empty($tree['direct'])) {
  112. $html .= '<h5>直推用户 (' . count($tree['direct']) . '人)</h5>';
  113. $html .= '<ul>';
  114. foreach ($tree['direct'] as $user) {
  115. $html .= '<li>用户' . $user['user_id'] . ' (推荐时间: ' . $user['referral_time'] . ')</li>';
  116. }
  117. $html .= '</ul>';
  118. }
  119. if (!empty($tree['indirect'])) {
  120. $html .= '<h5>间推用户 (' . count($tree['indirect']) . '人)</h5>';
  121. $html .= '<ul>';
  122. foreach ($tree['indirect'] as $user) {
  123. $html .= '<li>用户' . $user['user_id'] . ' (通过用户' . $user['referrer_id'] . ')</li>';
  124. }
  125. $html .= '</ul>';
  126. }
  127. if (!empty($tree['third'])) {
  128. $html .= '<h5>三推用户 (' . count($tree['third']) . '人)</h5>';
  129. $html .= '<ul>';
  130. foreach ($tree['third'] as $user) {
  131. $html .= '<li>用户' . $user['user_id'] . ' (通过用户' . $user['referrer_id'] . ')</li>';
  132. }
  133. $html .= '</ul>';
  134. }
  135. if (empty($tree['direct']) && empty($tree['indirect']) && empty($tree['third'])) {
  136. $html .= '<p>暂无团队成员</p>';
  137. }
  138. $html .= '</div>';
  139. return $html;
  140. });
  141. // 添加相关链接区域
  142. $show->divider('相关信息');
  143. $show->field('related_links', '相关链接')->unescape()->as(function ($value, $model) {
  144. $links = [];
  145. // 用户绑定关系链接
  146. $mappingUrl = admin_url('urs-promotion/user-mappings?urs_user_id=' . $model->urs_user_id);
  147. $links[] = '<a href="' . $mappingUrl . '" class="btn btn-primary btn-sm" target="_blank">
  148. <i class="fa fa-link"></i> 查看绑定关系
  149. </a>';
  150. // 推荐关系链接
  151. $referralUrl = admin_url('urs-promotion/user-referrals?urs_user_id=' . $model->urs_user_id);
  152. $links[] = '<a href="' . $referralUrl . '" class="btn btn-info btn-sm" target="_blank">
  153. <i class="fa fa-users"></i> 查看推荐关系
  154. </a>';
  155. // 收益记录链接(如果有农场用户ID)
  156. if ($model->userMapping && $model->userMapping->user_id) {
  157. $profitUrl = admin_url('urs-promotion/profits?farm_user_id=' . $model->userMapping->user_id);
  158. $links[] = '<a href="' . $profitUrl . '" class="btn btn-success btn-sm" target="_blank">
  159. <i class="fa fa-money"></i> 查看收益记录
  160. </a>';
  161. }
  162. return implode(' ', $links);
  163. });
  164. });
  165. }
  166. /**
  167. * 表单页面
  168. */
  169. protected function form(): Form
  170. {
  171. return Form::make(new UrsUserTalentRepository(), function (Form $form) {
  172. $form->display('id', 'ID');
  173. $form->number('user_id', '用户ID')->required();
  174. $form->select('talent_level', '达人等级')->options(UrsTalentLevel::getAllLevels())->required();
  175. $form->number('direct_count', '直推人数')->default(0);
  176. $form->number('indirect_count', '间推人数')->default(0);
  177. $form->number('third_count', '三推人数')->default(0);
  178. $form->number('promotion_count', '团队总人数')->default(0);
  179. $form->datetime('last_level_update_time', '最后升级时间');
  180. $form->display('created_at', '创建时间');
  181. $form->display('updated_at', '更新时间');
  182. });
  183. }
  184. /**
  185. * 更新单个用户达人等级
  186. */
  187. public function updateTalent($id)
  188. {
  189. $talent = UrsUserTalent::find($id);
  190. if (!$talent) {
  191. return response()->json(['status' => false, 'message' => '用户不存在']);
  192. }
  193. try {
  194. $result = UrsTalentService::updateTalentLevel($talent->urs_user_id);
  195. return response()->json(['status' => true, 'message' => '达人等级更新成功']);
  196. } catch (\Exception $e) {
  197. return response()->json(['status' => false, 'message' => '达人等级更新失败:' . $e->getMessage()]);
  198. }
  199. }
  200. /**
  201. * 批量更新所有用户达人等级
  202. */
  203. #[Post('/admin/urs-promotion/user-talents/batch-update', name: 'dcat.admin.urs-promotion.user-talents.batch-update')]
  204. public function batchUpdate()
  205. {
  206. try {
  207. $userIds = UrsUserTalent::pluck('urs_user_id')->toArray();
  208. $results = UrsTalentService::batchUpdateTalentLevels($userIds);
  209. // 统计结果
  210. $successCount = 0;
  211. $failedCount = 0;
  212. $upgradedCount = 0;
  213. foreach ($results as $ursUserId => $result) {
  214. if ($result['success']) {
  215. $successCount++;
  216. // 这里可以添加升级检测逻辑
  217. } else {
  218. $failedCount++;
  219. }
  220. }
  221. return response()->json([
  222. 'status' => true,
  223. 'message' => "批量更新完成,成功: {$successCount},失败: {$failedCount}"
  224. ]);
  225. } catch (\Exception $e) {
  226. return response()->json([
  227. 'status' => false,
  228. 'message' => '批量更新失败:' . $e->getMessage()
  229. ]);
  230. }
  231. }
  232. }