|
|
@@ -5,24 +5,45 @@ namespace App\Module\UrsPromotion\AdminControllers\Metrics;
|
|
|
use App\Module\UrsPromotion\Models\UrsUserTalent;
|
|
|
use App\Module\UrsPromotion\Models\UrsTalentConfig;
|
|
|
use App\Module\UrsPromotion\Enums\UrsTalentLevel;
|
|
|
-use Dcat\Admin\Widgets\Card;
|
|
|
+use UCore\DcatAdmin\Metrics\Examples\NumberS2;
|
|
|
+use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
/**
|
|
|
* URS达人等级统计卡片
|
|
|
- * 显示各个达人等级的人数统计
|
|
|
+ * 使用单行多数字卡片显示各个达人等级的人数统计
|
|
|
*/
|
|
|
-class UrsTalentLevelStatsCard extends Card
|
|
|
+class UrsTalentLevelStatsCard extends NumberS2
|
|
|
{
|
|
|
/**
|
|
|
- * 初始化卡片
|
|
|
+ * 初始化卡片内容
|
|
|
*/
|
|
|
- public function __construct()
|
|
|
+ protected function init()
|
|
|
+ {
|
|
|
+ parent::init();
|
|
|
+
|
|
|
+ $this->title('URS达人等级统计');
|
|
|
+ // 移除下拉选项,因为达人等级统计不需要时间范围选择
|
|
|
+ $this->dropdown([]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理请求
|
|
|
+ *
|
|
|
+ * @param Request $request
|
|
|
+ * @return mixed|void
|
|
|
+ */
|
|
|
+ public function handle(Request $request)
|
|
|
{
|
|
|
$stats = $this->getTalentLevelStats();
|
|
|
- $content = $this->renderStatsContent($stats);
|
|
|
|
|
|
- parent::__construct('URS达人等级统计', $content);
|
|
|
+ // 转换为NumberS2需要的数据格式
|
|
|
+ $dataList = [];
|
|
|
+ foreach ($stats as $stat) {
|
|
|
+ $dataList[$stat['name']] = $stat['count'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->withContent($dataList);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -32,12 +53,6 @@ class UrsTalentLevelStatsCard extends Card
|
|
|
*/
|
|
|
protected function getTalentLevelStats(): array
|
|
|
{
|
|
|
- // 获取所有启用的达人等级配置
|
|
|
- $configs = UrsTalentConfig::where('status', UrsTalentConfig::STATUS_ENABLED)
|
|
|
- ->orderBy('level')
|
|
|
- ->get()
|
|
|
- ->keyBy('level');
|
|
|
-
|
|
|
// 统计各等级的用户数量
|
|
|
$levelCounts = UrsUserTalent::select('talent_level', DB::raw('COUNT(*) as count'))
|
|
|
->groupBy('talent_level')
|
|
|
@@ -45,128 +60,21 @@ class UrsTalentLevelStatsCard extends Card
|
|
|
->keyBy('talent_level');
|
|
|
|
|
|
$stats = [];
|
|
|
-
|
|
|
+
|
|
|
// 遍历所有等级配置,包括非达人等级
|
|
|
foreach (UrsTalentLevel::cases() as $levelEnum) {
|
|
|
$level = $levelEnum->value;
|
|
|
- $config = $configs->get($level);
|
|
|
$count = $levelCounts->get($level)->count ?? 0;
|
|
|
-
|
|
|
+
|
|
|
$stats[] = [
|
|
|
'level' => $level,
|
|
|
'name' => $levelEnum->getName(),
|
|
|
'count' => $count,
|
|
|
- 'config' => $config,
|
|
|
- 'color' => $this->getLevelColor($level),
|
|
|
- 'icon' => $this->getLevelIcon($level),
|
|
|
];
|
|
|
}
|
|
|
|
|
|
return $stats;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 渲染统计内容
|
|
|
- *
|
|
|
- * @param array $stats
|
|
|
- * @return string
|
|
|
- */
|
|
|
- protected function renderStatsContent(array $stats): string
|
|
|
- {
|
|
|
- $html = '<div class="row">';
|
|
|
-
|
|
|
- foreach ($stats as $stat) {
|
|
|
- $html .= $this->renderLevelCard($stat);
|
|
|
- }
|
|
|
-
|
|
|
- $html .= '</div>';
|
|
|
-
|
|
|
- return $html;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 渲染单个等级卡片
|
|
|
- *
|
|
|
- * @param array $stat
|
|
|
- * @return string
|
|
|
- */
|
|
|
- protected function renderLevelCard(array $stat): string
|
|
|
- {
|
|
|
- $icon = $stat['icon'];
|
|
|
- $name = $stat['name'];
|
|
|
- $count = $stat['count'];
|
|
|
- $color = $stat['color'];
|
|
|
-
|
|
|
- return <<<HTML
|
|
|
- <div class="col-md-4 col-sm-6 mb-3">
|
|
|
- <div class="card border-0 shadow-sm h-100">
|
|
|
- <div class="card-body text-center p-3">
|
|
|
- <div class="mb-2">
|
|
|
- <i class="{$icon}" style="font-size: 24px; color: {$color};" aria-hidden="true"></i>
|
|
|
- </div>
|
|
|
- <h6 class="card-title mb-1">{$name}</h6>
|
|
|
- <h4 class="text-primary mb-0">{$count}</h4>
|
|
|
- <small class="text-muted">人</small>
|
|
|
- </div>
|
|
|
- </div>
|
|
|
- </div>
|
|
|
-HTML;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取等级颜色
|
|
|
- *
|
|
|
- * @param int $level
|
|
|
- * @return string
|
|
|
- */
|
|
|
- protected function getLevelColor(int $level): string
|
|
|
- {
|
|
|
- return match($level) {
|
|
|
- 0 => '#6c757d', // 非达人 - 灰色
|
|
|
- 1 => '#28a745', // 初级达人 - 绿色
|
|
|
- 2 => '#17a2b8', // 中级达人 - 青色
|
|
|
- 3 => '#ffc107', // 高级达人 - 黄色
|
|
|
- 4 => '#fd7e14', // 资深达人 - 橙色
|
|
|
- 5 => '#dc3545', // 顶级达人 - 红色
|
|
|
- default => '#6c757d',
|
|
|
- };
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取等级图标
|
|
|
- *
|
|
|
- * @param int $level
|
|
|
- * @return string
|
|
|
- */
|
|
|
- protected function getLevelIcon(int $level): string
|
|
|
- {
|
|
|
- return match($level) {
|
|
|
- 0 => 'fa fa-user', // 非达人
|
|
|
- 1 => 'fa fa-star', // 初级达人
|
|
|
- 2 => 'fa fa-star-half-alt', // 中级达人
|
|
|
- 3 => 'fa fa-crown', // 高级达人
|
|
|
- 4 => 'fa fa-gem', // 资深达人
|
|
|
- 5 => 'fa fa-trophy', // 顶级达人
|
|
|
- default => 'fa fa-user',
|
|
|
- };
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 获取徽章样式类
|
|
|
- *
|
|
|
- * @param int $level
|
|
|
- * @return string
|
|
|
- */
|
|
|
- protected function getBadgeClass(int $level): string
|
|
|
- {
|
|
|
- return match($level) {
|
|
|
- 0 => 'badge-secondary',
|
|
|
- 1 => 'badge-success',
|
|
|
- 2 => 'badge-info',
|
|
|
- 3 => 'badge-warning',
|
|
|
- 4 => 'badge-primary',
|
|
|
- 5 => 'badge-danger',
|
|
|
- default => 'badge-secondary',
|
|
|
- };
|
|
|
- }
|
|
|
}
|