| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace App\Module\UrsPromotion\AdminControllers;
- use UCore\DcatAdmin\AdminController;
- use Spatie\RouteAttributes\Attributes\Resource;
- use Spatie\RouteAttributes\Attributes\Post;
- use App\Module\UrsPromotion\Models\UrsUserTalent;
- use App\Module\UrsPromotion\Repositorys\UrsUserTalentRepository;
- use App\Module\UrsPromotion\AdminControllers\Helper\UrsUserTalentGridHelper;
- use App\Module\UrsPromotion\AdminControllers\Helper\UrsUserTalentShowHelper;
- use App\Module\UrsPromotion\AdminControllers\Helper\UrsUserTalentFormHelper;
- use App\Module\UrsPromotion\AdminControllers\Helper\UrsUserTalentFilterHelper;
- use App\Module\UrsPromotion\Services\UrsTalentService;
- use App\Module\UrsPromotion\Enums\UrsTalentLevel;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Form;
- /**
- * URS用户达人等级管理控制器
- *
- * @route /admin/urs-promotion/user-talents
- */
- #[Resource('urs-promotion/user-talents', names: 'dcat.admin.urs-promotion.user-talents')]
- class UrsUserTalentController extends AdminController
- {
- /**
- * 页面标题
- */
- protected $title = 'URS用户达人等级';
- /**
- * 模型类
- */
- protected $model = UrsUserTalent::class;
- /**
- * 仓库类
- */
- protected $repository = UrsUserTalentRepository::class;
- /**
- * 列表页面
- */
- protected function grid(): Grid
- {
- return Grid::make(new UrsUserTalentRepository(['userMapping']), function (Grid $grid) {
- $grid->column('id', 'ID')->sortable();
- $grid->column('urs_user_id', 'URS用户ID')->sortable()->display(function ($value) {
- // 添加到用户绑定和推荐关系的链接
- $mappingUrl = admin_url('urs-promotion/user-mappings?urs_user_id=' . $value);
- $referralUrl = admin_url('urs-promotion/user-referrals?urs_user_id=' . $value);
- return $value . '<br><small>
- <a href="' . $mappingUrl . '" class="text-primary">查看绑定关系</a> |
- <a href="' . $referralUrl . '" class="text-info">查看推荐关系</a>
- </small>';
- });
- $grid->column('userMapping.user_id', '用户ID')->sortable()->display(function ($value) {
- if (!$value) return '<span class="text-muted">未绑定</span>';
- // 添加到收益记录的链接
- $profitUrl = admin_url('urs-promotion/profits?farm_user_id=' . $value);
- return $value . '<br><small>
- <a href="' . $profitUrl . '" class="text-success">查看收益记录</a>
- </small>';
- });
- $grid->column('talent_level', '达人等级')->display(function ($value) {
- return UrsTalentLevel::getLevelName($value);
- })->label([
- 0 => 'default',
- 1 => 'primary',
- 2 => 'info',
- 3 => 'success',
- 4 => 'warning',
- 5 => 'danger',
- ])->sortable();
- $grid->column('direct_count', '直推人数')->sortable();
- $grid->column('indirect_count', '间推人数')->sortable();
- $grid->column('third_count', '三推人数')->sortable();
- $grid->column('promotion_count', '团队总人数')->sortable();
- $grid->column('last_level_update_time', '最后升级时间')->sortable();
- $grid->column('created_at', '创建时间')->sortable();
- // 添加批量更新达人等级功能
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append(new \App\Module\UrsPromotion\AdminControllers\Actions\BatchUpdateTalentAction());
- });
- // 添加单个更新功能
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->append(new \App\Module\UrsPromotion\AdminControllers\Actions\UpdateTalentAction());
- });
- $grid->filter(function (Grid\Filter $filter) {
- UrsUserTalentFilterHelper::make($filter);
- });
- });
- }
- /**
- * 详情页面
- */
- protected function detail($id): Show
- {
- return Show::make($id, new UrsUserTalentRepository(), function (Show $show) {
- $show->field('id', 'ID');
- $show->field('user_id', '用户ID');
- $show->field('talent_level', '达人等级')->using(UrsTalentLevel::getAllLevels());
- $show->field('direct_count', '直推人数');
- $show->field('indirect_count', '间推人数');
- $show->field('third_count', '三推人数');
- $show->field('promotion_count', '团队总人数');
- $show->field('last_level_update_time', '最后升级时间');
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- // 显示推荐关系树
- $show->divider();
- $show->field('referral_tree', '推荐关系树')->unescape()->as(function () {
- $tree = UrsTalentService::getUserReferralTree($this->user_id);
- $html = '<div class="referral-tree">';
- if (!empty($tree['direct'])) {
- $html .= '<h5>直推用户 (' . count($tree['direct']) . '人)</h5>';
- $html .= '<ul>';
- foreach ($tree['direct'] as $user) {
- $html .= '<li>用户' . $user['user_id'] . ' (推荐时间: ' . $user['referral_time'] . ')</li>';
- }
- $html .= '</ul>';
- }
- if (!empty($tree['indirect'])) {
- $html .= '<h5>间推用户 (' . count($tree['indirect']) . '人)</h5>';
- $html .= '<ul>';
- foreach ($tree['indirect'] as $user) {
- $html .= '<li>用户' . $user['user_id'] . ' (通过用户' . $user['referrer_id'] . ')</li>';
- }
- $html .= '</ul>';
- }
- if (!empty($tree['third'])) {
- $html .= '<h5>三推用户 (' . count($tree['third']) . '人)</h5>';
- $html .= '<ul>';
- foreach ($tree['third'] as $user) {
- $html .= '<li>用户' . $user['user_id'] . ' (通过用户' . $user['referrer_id'] . ')</li>';
- }
- $html .= '</ul>';
- }
- if (empty($tree['direct']) && empty($tree['indirect']) && empty($tree['third'])) {
- $html .= '<p>暂无团队成员</p>';
- }
- $html .= '</div>';
- return $html;
- });
- // 添加相关链接区域
- $show->divider('相关信息');
- $show->field('related_links', '相关链接')->unescape()->as(function ($value, $model) {
- $links = [];
- // 用户绑定关系链接
- $mappingUrl = admin_url('urs-promotion/user-mappings?urs_user_id=' . $model->urs_user_id);
- $links[] = '<a href="' . $mappingUrl . '" class="btn btn-primary btn-sm" target="_blank">
- <i class="fa fa-link"></i> 查看绑定关系
- </a>';
- // 推荐关系链接
- $referralUrl = admin_url('urs-promotion/user-referrals?urs_user_id=' . $model->urs_user_id);
- $links[] = '<a href="' . $referralUrl . '" class="btn btn-info btn-sm" target="_blank">
- <i class="fa fa-users"></i> 查看推荐关系
- </a>';
- // 收益记录链接(如果有农场用户ID)
- if ($model->userMapping && $model->userMapping->user_id) {
- $profitUrl = admin_url('urs-promotion/profits?farm_user_id=' . $model->userMapping->user_id);
- $links[] = '<a href="' . $profitUrl . '" class="btn btn-success btn-sm" target="_blank">
- <i class="fa fa-money"></i> 查看收益记录
- </a>';
- }
- return implode(' ', $links);
- });
- });
- }
- /**
- * 表单页面
- */
- protected function form(): Form
- {
- return Form::make(new UrsUserTalentRepository(), function (Form $form) {
- $form->display('id', 'ID');
- $form->number('user_id', '用户ID')->required();
- $form->select('talent_level', '达人等级')->options(UrsTalentLevel::getAllLevels())->required();
- $form->number('direct_count', '直推人数')->default(0);
- $form->number('indirect_count', '间推人数')->default(0);
- $form->number('third_count', '三推人数')->default(0);
- $form->number('promotion_count', '团队总人数')->default(0);
- $form->datetime('last_level_update_time', '最后升级时间');
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- });
- }
- /**
- * 更新单个用户达人等级
- */
- public function updateTalent($id)
- {
- $talent = UrsUserTalent::find($id);
- if (!$talent) {
- return response()->json(['status' => false, 'message' => '用户不存在']);
- }
- try {
- $result = UrsTalentService::updateTalentLevel($talent->urs_user_id);
- return response()->json(['status' => true, 'message' => '达人等级更新成功']);
- } catch (\Exception $e) {
- return response()->json(['status' => false, 'message' => '达人等级更新失败:' . $e->getMessage()]);
- }
- }
- /**
- * 批量更新所有用户达人等级
- */
- #[Post('/admin/urs-promotion/user-talents/batch-update', name: 'dcat.admin.urs-promotion.user-talents.batch-update')]
- public function batchUpdate()
- {
- try {
- $userIds = UrsUserTalent::pluck('urs_user_id')->toArray();
- $results = UrsTalentService::batchUpdateTalentLevels($userIds);
- // 统计结果
- $successCount = 0;
- $failedCount = 0;
- $upgradedCount = 0;
- foreach ($results as $ursUserId => $result) {
- if ($result['success']) {
- $successCount++;
- // 这里可以添加升级检测逻辑
- } else {
- $failedCount++;
- }
- }
- return response()->json([
- 'status' => true,
- 'message' => "批量更新完成,成功: {$successCount},失败: {$failedCount}"
- ]);
- } catch (\Exception $e) {
- return response()->json([
- 'status' => false,
- 'message' => '批量更新失败:' . $e->getMessage()
- ]);
- }
- }
- }
|