| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace App\Module\UrsPromotion\AdminControllers\Actions;
- use App\Module\UrsPromotion\Models\UrsUserReferral;
- use App\Module\UrsPromotion\Services\UrsReferralService;
- use Dcat\Admin\Widgets\Modal;
- use UCore\DcatAdmin\RowAction;
- /**
- * 查看推荐下级树操作
- *
- * 显示用户的下级推荐关系树结构
- */
- class ViewReferralTreeAction extends RowAction
- {
- /**
- * 操作按钮标题
- *
- * @var string
- */
- protected $title = '下级树';
- /**
- * 检查是否允许显示此操作
- *
- * @return bool
- */
- public function allowed()
- {
- // 所有记录都允许查看关系树
- return true;
- }
- /**
- * 渲染操作按钮
- *
- * @return string
- */
- public function render2()
- {
- $row = $this->getRow();
- $ursUserId = $row->urs_user_id;
-
- // 获取推荐关系树数据
- $treeData = $this->getReferralTreeData($ursUserId);
-
- // 构建树形结构HTML
- $treeHtml = $this->buildTreeHtml($treeData);
-
- return Modal::make()
- ->xl()
- ->title("URS用户 {$ursUserId} 的下级推荐树")
- ->body($treeHtml)
- ->button("<i class=\"fa fa-sitemap\"></i> {$this->title}");
- }
- /**
- * 获取推荐关系树数据
- *
- * @param int $ursUserId
- * @return array
- */
- private function getReferralTreeData(int $ursUserId): array
- {
- // 获取直推用户
- $directReferrals = UrsUserReferral::where('urs_referrer_id', $ursUserId)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->with(['userMapping.user.info'])
- ->get();
- $tree = [
- 'user_id' => $ursUserId,
- 'direct' => [],
- 'indirect' => [],
- 'third' => []
- ];
- foreach ($directReferrals as $direct) {
- $directData = [
- 'urs_user_id' => $direct->urs_user_id,
- 'farm_user_id' => $direct->userMapping->user_id ?? null,
- 'username' => $direct->userMapping->user->username ?? '未进入农场',
- 'nickname' => $direct->userMapping->user->info->nickname ?? '无昵称',
- 'referral_time' => $direct->referral_time,
- 'children' => []
- ];
- // 获取间推用户(直推用户的下级)
- $indirectReferrals = UrsUserReferral::where('urs_referrer_id', $direct->urs_user_id)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->with(['userMapping.user.info'])
- ->get();
- foreach ($indirectReferrals as $indirect) {
- $indirectData = [
- 'urs_user_id' => $indirect->urs_user_id,
- 'farm_user_id' => $indirect->userMapping->user_id ?? null,
- 'username' => $indirect->userMapping->user->username ?? '未进入农场',
- 'nickname' => $indirect->userMapping->user->info->nickname ?? '无昵称',
- 'referral_time' => $indirect->referral_time,
- 'children' => []
- ];
- // 获取三推用户(间推用户的下级)
- $thirdReferrals = UrsUserReferral::where('urs_referrer_id', $indirect->urs_user_id)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->with(['userMapping.user.info'])
- ->get();
- foreach ($thirdReferrals as $third) {
- $indirectData['children'][] = [
- 'urs_user_id' => $third->urs_user_id,
- 'farm_user_id' => $third->userMapping->user_id ?? null,
- 'username' => $third->userMapping->user->username ?? '未进入农场',
- 'nickname' => $third->userMapping->user->info->nickname ?? '无昵称',
- 'referral_time' => $third->referral_time,
- ];
- }
- $directData['children'][] = $indirectData;
- }
- $tree['direct'][] = $directData;
- }
- return $tree;
- }
- /**
- * 构建树形结构HTML
- *
- * @param array $treeData
- * @return string
- */
- private function buildTreeHtml(array $treeData): string
- {
- $html = '<div class="referral-tree-container">';
- $html .= '<style>
- .referral-tree-container { font-family: Arial, sans-serif; }
- .tree-node { margin: 10px 0; padding: 10px; border: 1px solid #ddd; border-radius: 5px; }
- .tree-level-1 { background-color: #e8f5e8; margin-left: 0; }
- .tree-level-2 { background-color: #e8f0ff; margin-left: 30px; }
- .tree-level-3 { background-color: #fff8e8; margin-left: 60px; }
- .user-info { font-weight: bold; color: #333; }
- .user-meta { font-size: 12px; color: #666; margin-top: 5px; }
- .level-badge { display: inline-block; padding: 2px 6px; border-radius: 3px; font-size: 11px; color: white; }
- .level-1 { background-color: #28a745; }
- .level-2 { background-color: #007bff; }
- .level-3 { background-color: #ffc107; color: #333; }
- </style>';
- $html .= '<h5><i class="fa fa-user"></i> 根用户: URS-' . $treeData['user_id'] . '</h5>';
- if (empty($treeData['direct'])) {
- $html .= '<p class="text-muted">暂无下级推荐用户</p>';
- } else {
- $html .= '<div class="tree-structure">';
-
- foreach ($treeData['direct'] as $direct) {
- $html .= $this->renderUserNode($direct, 1);
- }
-
- $html .= '</div>';
- }
- $html .= '</div>';
- return $html;
- }
- /**
- * 渲染用户节点
- *
- * @param array $user
- * @param int $level
- * @return string
- */
- private function renderUserNode(array $user, int $level): string
- {
- $levelNames = [1 => '直推', 2 => '间推', 3 => '三推'];
- $levelClass = "tree-level-{$level}";
- $badgeClass = "level-{$level}";
-
- $html = "<div class='tree-node {$levelClass}'>";
- $html .= "<span class='level-badge {$badgeClass}'>{$levelNames[$level]}</span> ";
- $html .= "<span class='user-info'>URS-{$user['urs_user_id']}</span>";
-
- if ($user['farm_user_id']) {
- $html .= " → 农场用户ID: {$user['farm_user_id']} | {$user['username']} ({$user['nickname']})";
- } else {
- $html .= " → <span class='text-warning'>未进入农场</span>";
- }
-
- $html .= "<div class='user-meta'>";
- $html .= "推荐时间: {$user['referral_time']}";
- $html .= "</div>";
-
- // 渲染子节点
- if (!empty($user['children']) && $level < 3) {
- foreach ($user['children'] as $child) {
- $html .= $this->renderUserNode($child, $level + 1);
- }
- }
-
- $html .= "</div>";
- return $html;
- }
- }
|